org.el 957 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279
  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. ;; `org-outline-regexp' ought to be a defconst but is let-bound in
  85. ;; some places -- e.g. see the macro `org-with-limited-levels'.
  86. ;;
  87. ;; In Org buffers, the value of `outline-regexp' is that of
  88. ;; `org-outline-regexp'. The only function still directly relying on
  89. ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
  90. ;; job when `orgstruct-mode' is active.
  91. (defvar org-outline-regexp "\\*+ "
  92. "Regexp to match Org headlines.")
  93. (defvar org-outline-regexp-bol "^\\*+ "
  94. "Regexp to match Org headlines.
  95. This is similar to `org-outline-regexp' but additionally makes
  96. sure that we are at the beginning of the line.")
  97. (defvar org-heading-regexp "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  98. "Matches a headline, putting stars and text into groups.
  99. Stars are put in group 1 and the trimmed body in group 2.")
  100. ;; Emacs 22 calendar compatibility: Make sure the new variables are available
  101. (unless (boundp 'calendar-view-holidays-initially-flag)
  102. (org-defvaralias 'calendar-view-holidays-initially-flag
  103. 'view-calendar-holidays-initially))
  104. (unless (boundp 'calendar-view-diary-initially-flag)
  105. (org-defvaralias 'calendar-view-diary-initially-flag
  106. 'view-diary-entries-initially))
  107. (unless (boundp 'diary-fancy-buffer)
  108. (org-defvaralias 'diary-fancy-buffer 'fancy-diary-buffer))
  109. (declare-function cdlatex-environment "ext:cdlatex" (environment item))
  110. (declare-function org-add-archive-files "org-archive" (files))
  111. (declare-function org-agenda-entry-get-agenda-timestamp "org-agenda" (pom))
  112. (declare-function org-agenda-list "org-agenda" (&optional arg start-day span))
  113. (declare-function org-agenda-redo "org-agenda" (&optional all))
  114. (declare-function org-babel-do-in-edit-buffer "ob-core" (&rest body))
  115. (declare-function org-babel-tangle-file "ob-tangle" (file &optional target-file lang))
  116. (declare-function org-beamer-mode "ox-beamer" ())
  117. (declare-function org-clock-get-last-clock-out-time "org-clock" ())
  118. (declare-function org-clock-out "org-clock" (&optional switch-to-state fail-quietly at-time))
  119. (declare-function org-clock-remove-overlays "org-clock" (&optional beg end noremove))
  120. (declare-function org-clock-sum "org-clock" (&optional tstart tend headline-filter propname))
  121. (declare-function org-clock-sum-current-item "org-clock" (&optional tstart))
  122. (declare-function org-clock-timestamps-down "org-clock" (&optional n))
  123. (declare-function org-clock-timestamps-up "org-clock" (&optional n))
  124. (declare-function org-clock-update-time-maybe "org-clock" ())
  125. (declare-function org-clocktable-shift "org-clock" (dir n))
  126. (declare-function org-element-at-point "org-element" ())
  127. (declare-function org-element-cache-refresh "org-element" (pos))
  128. (declare-function org-element-cache-reset "org-element" (&optional all))
  129. (declare-function org-element-contents "org-element" (element))
  130. (declare-function org-element-context "org-element" (&optional element))
  131. (declare-function org-element-copy "org-element" (datum))
  132. (declare-function org-element-interpret-data "org-element" (data &optional parent))
  133. (declare-function org-element-lineage "org-element" (blob &optional types with-self))
  134. (declare-function org-element-nested-p "org-element" (elem-a elem-b))
  135. (declare-function org-element-parse-buffer "org-element" (&optional granularity visible-only))
  136. (declare-function org-element-property "org-element" (property element))
  137. (declare-function org-element-put-property "org-element" (element property value))
  138. (declare-function org-element-swap-A-B "org-element" (elem-a elem-b))
  139. (declare-function org-element-type "org-element" (element))
  140. (declare-function org-element-update-syntax "org-element" ())
  141. (declare-function org-id-find-id-file "org-id" (id))
  142. (declare-function org-id-get-create "org-id" (&optional force))
  143. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  144. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  145. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  146. (declare-function org-plot/gnuplot "org-plot" (&optional params))
  147. (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label))
  148. (declare-function org-table-align "org-table" ())
  149. (declare-function org-table-begin "org-table" (&optional table-type))
  150. (declare-function org-table-beginning-of-field "org-table" (&optional n))
  151. (declare-function org-table-blank-field "org-table" ())
  152. (declare-function org-table-calc-current-TBLFM "org-table" (&optional arg))
  153. (declare-function org-table-edit-field "org-table" (arg))
  154. (declare-function org-table-end "org-table" (&optional table-type))
  155. (declare-function org-table-end-of-field "org-table" (&optional n))
  156. (declare-function org-table-insert-row "org-table" (&optional arg))
  157. (declare-function org-table-justify-field-maybe "org-table" (&optional new))
  158. (declare-function org-table-maybe-eval-formula "org-table" ())
  159. (declare-function org-table-maybe-recalculate-line "org-table" ())
  160. (declare-function org-table-next-row "org-table" ())
  161. (declare-function org-table-paste-rectangle "org-table" ())
  162. (declare-function org-table-wrap-region "org-table" (arg))
  163. (declare-function org-tags-view "org-agenda" (&optional todo-only match))
  164. (declare-function orgtbl-ascii-plot "org-table" (&optional ask))
  165. (declare-function orgtbl-mode "org-table" (&optional arg))
  166. (defsubst org-uniquify (list)
  167. "Non-destructively remove duplicate elements from LIST."
  168. (let ((res (copy-sequence list))) (delete-dups res)))
  169. (defsubst org-get-at-bol (property)
  170. "Get text property PROPERTY at the beginning of line."
  171. (get-text-property (point-at-bol) property))
  172. (defsubst org-trim (s)
  173. "Remove whitespace at the beginning and the end of string S."
  174. (replace-regexp-in-string
  175. "\\`[ \t\n\r]+" ""
  176. (replace-regexp-in-string "[ \t\n\r]+\\'" "" s)))
  177. ;; load languages based on value of `org-babel-load-languages'
  178. (defvar org-babel-load-languages)
  179. ;;;###autoload
  180. (defun org-babel-do-load-languages (sym value)
  181. "Load the languages defined in `org-babel-load-languages'."
  182. (set-default sym value)
  183. (mapc (lambda (pair)
  184. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  185. (if active
  186. (progn
  187. (require (intern (concat "ob-" lang))))
  188. (progn
  189. (funcall 'fmakunbound
  190. (intern (concat "org-babel-execute:" lang)))
  191. (funcall 'fmakunbound
  192. (intern (concat "org-babel-expand-body:" lang)))))))
  193. org-babel-load-languages))
  194. (declare-function org-babel-tangle-file "ob-tangle" (file &optional target-file lang))
  195. ;;;###autoload
  196. (defun org-babel-load-file (file &optional compile)
  197. "Load Emacs Lisp source code blocks in the Org-mode FILE.
  198. This function exports the source code using `org-babel-tangle'
  199. and then loads the resulting file using `load-file'. With prefix
  200. arg (noninteractively: 2nd arg) COMPILE the tangled Emacs Lisp
  201. file to byte-code before it is loaded."
  202. (interactive "fFile to load: \nP")
  203. (let* ((age (lambda (file)
  204. (float-time
  205. (time-subtract (current-time)
  206. (nth 5 (or (file-attributes (file-truename file))
  207. (file-attributes file)))))))
  208. (base-name (file-name-sans-extension file))
  209. (exported-file (concat base-name ".el")))
  210. ;; tangle if the org-mode file is newer than the elisp file
  211. (unless (and (file-exists-p exported-file)
  212. (> (funcall age file) (funcall age exported-file)))
  213. ;; Tangle-file traversal returns reversed list of tangled files
  214. ;; and we want to evaluate the first target.
  215. (setq exported-file
  216. (car (last (org-babel-tangle-file file exported-file "emacs-lisp")))))
  217. (message "%s %s"
  218. (if compile
  219. (progn (byte-compile-file exported-file 'load)
  220. "Compiled and loaded")
  221. (progn (load-file exported-file) "Loaded"))
  222. exported-file)))
  223. (defcustom org-babel-load-languages '((emacs-lisp . t))
  224. "Languages which can be evaluated in Org-mode buffers.
  225. This list can be used to load support for any of the languages
  226. below, note that each language will depend on a different set of
  227. system executables and/or Emacs modes. When a language is
  228. \"loaded\", then code blocks in that language can be evaluated
  229. with `org-babel-execute-src-block' bound by default to C-c
  230. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  231. be set to remove code block evaluation from the C-c C-c
  232. keybinding. By default only Emacs Lisp (which has no
  233. requirements) is loaded."
  234. :group 'org-babel
  235. :set 'org-babel-do-load-languages
  236. :version "24.1"
  237. :type '(alist :tag "Babel Languages"
  238. :key-type
  239. (choice
  240. (const :tag "Awk" awk)
  241. (const :tag "C" C)
  242. (const :tag "R" R)
  243. (const :tag "Asymptote" asymptote)
  244. (const :tag "Calc" calc)
  245. (const :tag "Clojure" clojure)
  246. (const :tag "CSS" css)
  247. (const :tag "Ditaa" ditaa)
  248. (const :tag "Dot" dot)
  249. (const :tag "Emacs Lisp" emacs-lisp)
  250. (const :tag "Forth" forth)
  251. (const :tag "Fortran" fortran)
  252. (const :tag "Gnuplot" gnuplot)
  253. (const :tag "Haskell" haskell)
  254. (const :tag "IO" io)
  255. (const :tag "J" J)
  256. (const :tag "Java" java)
  257. (const :tag "Javascript" js)
  258. (const :tag "LaTeX" latex)
  259. (const :tag "Ledger" ledger)
  260. (const :tag "Lilypond" lilypond)
  261. (const :tag "Lisp" lisp)
  262. (const :tag "Makefile" makefile)
  263. (const :tag "Maxima" maxima)
  264. (const :tag "Matlab" matlab)
  265. (const :tag "Mscgen" mscgen)
  266. (const :tag "Ocaml" ocaml)
  267. (const :tag "Octave" octave)
  268. (const :tag "Org" org)
  269. (const :tag "Perl" perl)
  270. (const :tag "Pico Lisp" picolisp)
  271. (const :tag "PlantUML" plantuml)
  272. (const :tag "Python" python)
  273. (const :tag "Ruby" ruby)
  274. (const :tag "Sass" sass)
  275. (const :tag "Scala" scala)
  276. (const :tag "Scheme" scheme)
  277. (const :tag "Screen" screen)
  278. (const :tag "Shell Script" shell)
  279. (const :tag "Shen" shen)
  280. (const :tag "Sql" sql)
  281. (const :tag "Sqlite" sqlite)
  282. (const :tag "Stan" stan)
  283. (const :tag "ebnf2ps" ebnf2ps))
  284. :value-type (boolean :tag "Activate" :value t)))
  285. ;;;; Customization variables
  286. (defcustom org-clone-delete-id nil
  287. "Remove ID property of clones of a subtree.
  288. When non-nil, clones of a subtree don't inherit the ID property.
  289. Otherwise they inherit the ID property with a new unique
  290. identifier."
  291. :type 'boolean
  292. :version "24.1"
  293. :group 'org-id)
  294. ;;; Version
  295. (org-check-version)
  296. ;;;###autoload
  297. (defun org-version (&optional here full message)
  298. "Show the org-mode version.
  299. Interactively, or when MESSAGE is non-nil, show it in echo area.
  300. With prefix argument, or when HERE is non-nil, insert it at point.
  301. In non-interactive uses, a reduced version string is output unless
  302. FULL is given."
  303. (interactive (list current-prefix-arg t (not current-prefix-arg)))
  304. (let* ((org-dir (ignore-errors (org-find-library-dir "org")))
  305. (save-load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  306. (load-suffixes (list ".el"))
  307. (org-install-dir (ignore-errors (org-find-library-dir "org-loaddefs")))
  308. (org-trash (or
  309. (and (fboundp 'org-release) (fboundp 'org-git-version))
  310. (org-load-noerror-mustsuffix (concat org-dir "org-version"))))
  311. (load-suffixes save-load-suffixes)
  312. (org-version (org-release))
  313. (git-version (org-git-version))
  314. (version (format "Org-mode version %s (%s @ %s)"
  315. org-version
  316. git-version
  317. (if org-install-dir
  318. (if (string= org-dir org-install-dir)
  319. org-install-dir
  320. (concat "mixed installation! " org-install-dir " and " org-dir))
  321. "org-loaddefs.el can not be found!")))
  322. (version1 (if full version org-version)))
  323. (when here (insert version1))
  324. (when message (message "%s" version1))
  325. version1))
  326. (defconst org-version (org-version))
  327. ;;; Syntax Constants
  328. ;;;; Block
  329. (defconst org-block-regexp
  330. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  331. "Regular expression for hiding blocks.")
  332. (defconst org-dblock-start-re
  333. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  334. "Matches the start line of a dynamic block, with parameters.")
  335. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  336. "Matches the end of a dynamic block.")
  337. ;;;; Clock and Planning
  338. (defconst org-clock-string "CLOCK:"
  339. "String used as prefix for timestamps clocking work hours on an item.")
  340. (defvar org-closed-string "CLOSED:"
  341. "String used as the prefix for timestamps logging closing a TODO entry.")
  342. (defvar org-deadline-string "DEADLINE:"
  343. "String to mark deadline entries.
  344. A deadline is this string, followed by a time stamp. Should be a word,
  345. terminated by a colon. You can insert a schedule keyword and
  346. a timestamp with \\[org-deadline].")
  347. (defvar org-scheduled-string "SCHEDULED:"
  348. "String to mark scheduled TODO entries.
  349. A schedule is this string, followed by a time stamp. Should be a word,
  350. terminated by a colon. You can insert a schedule keyword and
  351. a timestamp with \\[org-schedule].")
  352. (defconst org-ds-keyword-length
  353. (+ 2
  354. (apply #'max
  355. (mapcar #'length
  356. (list org-deadline-string org-scheduled-string
  357. org-clock-string org-closed-string))))
  358. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  359. (defconst org-planning-line-re
  360. (concat "^[ \t]*"
  361. (regexp-opt
  362. (list org-closed-string org-deadline-string org-scheduled-string)
  363. t))
  364. "Matches a line with planning info.
  365. Matched keyword is in group 1.")
  366. (defconst org-clock-line-re
  367. (concat "^[ \t]*" org-clock-string)
  368. "Matches a line with clock info.")
  369. (defconst org-deadline-regexp (concat "\\<" org-deadline-string)
  370. "Matches the DEADLINE keyword.")
  371. (defconst org-deadline-time-regexp
  372. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  373. "Matches the DEADLINE keyword together with a time stamp.")
  374. (defconst org-deadline-time-hour-regexp
  375. (concat "\\<" org-deadline-string
  376. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  377. "Matches the DEADLINE keyword together with a time-and-hour stamp.")
  378. (defconst org-deadline-line-regexp
  379. (concat "\\<\\(" org-deadline-string "\\).*")
  380. "Matches the DEADLINE keyword and the rest of the line.")
  381. (defconst org-scheduled-regexp (concat "\\<" org-scheduled-string)
  382. "Matches the SCHEDULED keyword.")
  383. (defconst org-scheduled-time-regexp
  384. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  385. "Matches the SCHEDULED keyword together with a time stamp.")
  386. (defconst org-scheduled-time-hour-regexp
  387. (concat "\\<" org-scheduled-string
  388. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  389. "Matches the SCHEDULED keyword together with a time-and-hour stamp.")
  390. (defconst org-closed-time-regexp
  391. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  392. "Matches the CLOSED keyword together with a time stamp.")
  393. (defconst org-keyword-time-regexp
  394. (concat "\\<"
  395. (regexp-opt
  396. (list org-scheduled-string org-deadline-string org-closed-string
  397. org-clock-string)
  398. t)
  399. " *[[<]\\([^]>]+\\)[]>]")
  400. "Matches any of the 4 keywords, together with the time stamp.")
  401. (defconst org-keyword-time-not-clock-regexp
  402. (concat
  403. "\\<"
  404. (regexp-opt
  405. (list org-scheduled-string org-deadline-string org-closed-string) t)
  406. " *[[<]\\([^]>]+\\)[]>]")
  407. "Matches any of the 3 keywords, together with the time stamp.")
  408. (defconst org-maybe-keyword-time-regexp
  409. (concat "\\(\\<"
  410. (regexp-opt
  411. (list org-scheduled-string org-deadline-string org-closed-string
  412. org-clock-string)
  413. t)
  414. "\\)?"
  415. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]"
  416. "\\|"
  417. "<%%([^\r\n>]*>\\)")
  418. "Matches a timestamp, possibly preceded by a keyword.")
  419. (defconst org-all-time-keywords
  420. (mapcar (lambda (w) (substring w 0 -1))
  421. (list org-scheduled-string org-deadline-string
  422. org-clock-string org-closed-string))
  423. "List of time keywords.")
  424. ;;;; Drawer
  425. (defconst org-drawer-regexp "^[ \t]*:\\(\\(?:\\w\\|[-_]\\)+\\):[ \t]*$"
  426. "Matches first or last line of a hidden block.
  427. Group 1 contains drawer's name or \"END\".")
  428. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  429. "Regular expression matching the first line of a property drawer.")
  430. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  431. "Regular expression matching the last line of a property drawer.")
  432. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  433. "Regular expression matching the first line of a clock drawer.")
  434. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  435. "Regular expression matching the last line of a clock drawer.")
  436. (defconst org-property-drawer-re
  437. (concat "^[ \t]*:PROPERTIES:[ \t]*\n"
  438. "\\(?:[ \t]*:\\S-+:\\(?: .*\\)?[ \t]*\n\\)*?"
  439. "[ \t]*:END:[ \t]*$")
  440. "Matches an entire property drawer.")
  441. (defconst org-clock-drawer-re
  442. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*?\\("
  443. org-clock-drawer-end-re "\\)\n?")
  444. "Matches an entire clock drawer.")
  445. ;;;; Headline
  446. (defconst org-heading-keyword-regexp-format
  447. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  448. "Printf format for a regexp matching a headline with some keyword.
  449. This regexp will match the headline of any node which has the
  450. exact keyword that is put into the format. The keyword isn't in
  451. any group by default, but the stars and the body are.")
  452. (defconst org-heading-keyword-maybe-regexp-format
  453. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  454. "Printf format for a regexp matching a headline, possibly with some keyword.
  455. This regexp can match any headline with the specified keyword, or
  456. without a keyword. The keyword isn't in any group by default,
  457. but the stars and the body are.")
  458. (defconst org-archive-tag "ARCHIVE"
  459. "The tag that marks a subtree as archived.
  460. An archived subtree does not open during visibility cycling, and does
  461. not contribute to the agenda listings.")
  462. (defconst org-comment-string "COMMENT"
  463. "Entries starting with this keyword will never be exported.
  464. An entry can be toggled between COMMENT and normal with
  465. \\[org-toggle-comment].")
  466. ;;;; LaTeX Environments and Fragments
  467. (defconst org-latex-regexps
  468. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  469. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  470. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  471. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\(\\s.\\|\\s-\\|\\s(\\|\\s)\\|\\s\"\\|\000\\|$\\)" 2 nil)
  472. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\(\\s.\\|\\s-\\|\\s(\\|\\s)\\|\\s\"\\|\000\\|$\\)" 2 nil)
  473. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  474. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  475. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  476. "Regular expressions for matching embedded LaTeX.")
  477. ;;;; Node Property
  478. (defconst org-effort-property "Effort"
  479. "The property that is being used to keep track of effort estimates.
  480. Effort estimates given in this property need to have the format H:MM.")
  481. ;;;; Table
  482. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  483. "Detect an org-type or table-type table.")
  484. (defconst org-table-line-regexp "^[ \t]*|"
  485. "Detect an org-type table line.")
  486. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  487. "Detect an org-type table line.")
  488. (defconst org-table-hline-regexp "^[ \t]*|-"
  489. "Detect an org-type table hline.")
  490. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  491. "Detect a table-type table hline.")
  492. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  493. "Detect the first line outside a table when searching from within it.
  494. This works for both table types.")
  495. (defconst org-TBLFM-regexp "^[ \t]*#\\+TBLFM: "
  496. "Detect a #+TBLFM line.")
  497. ;;;; Timestamp
  498. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  499. "Regular expression for fast time stamp matching.")
  500. (defconst org-ts-regexp-inactive
  501. "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)\\]"
  502. "Regular expression for fast inactive time stamp matching.")
  503. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  504. "Regular expression for fast time stamp matching.")
  505. (defconst org-ts-regexp0
  506. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  507. "Regular expression matching time strings for analysis.
  508. This one does not require the space after the date, so it can be used
  509. on a string that terminates immediately after the date.")
  510. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  511. "Regular expression matching time strings for analysis.")
  512. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  513. "Regular expression matching time stamps, with groups.")
  514. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  515. "Regular expression matching time stamps (also [..]), with groups.")
  516. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  517. "Regular expression matching a time stamp range.")
  518. (defconst org-tr-regexp-both
  519. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  520. "Regular expression matching a time stamp range.")
  521. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  522. org-ts-regexp "\\)?")
  523. "Regular expression matching a time stamp or time stamp range.")
  524. (defconst org-tsr-regexp-both
  525. (concat org-ts-regexp-both "\\(--?-?"
  526. org-ts-regexp-both "\\)?")
  527. "Regular expression matching a time stamp or time stamp range.
  528. The time stamps may be either active or inactive.")
  529. (defconst org-repeat-re
  530. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  531. "Regular expression for specifying repeated events.
  532. After a match, group 1 contains the repeat expression.")
  533. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  534. "Formats for `format-time-string' which are used for time stamps.")
  535. ;;; The custom variables
  536. (defgroup org nil
  537. "Outline-based notes management and organizer."
  538. :tag "Org"
  539. :group 'outlines
  540. :group 'calendar)
  541. (defcustom org-mode-hook nil
  542. "Mode hook for Org-mode, run after the mode was turned on."
  543. :group 'org
  544. :type 'hook)
  545. (defcustom org-load-hook nil
  546. "Hook that is run after org.el has been loaded."
  547. :group 'org
  548. :type 'hook)
  549. (defcustom org-log-buffer-setup-hook nil
  550. "Hook that is run after an Org log buffer is created."
  551. :group 'org
  552. :version "24.1"
  553. :type 'hook)
  554. (defvar org-modules) ; defined below
  555. (defvar org-modules-loaded nil
  556. "Have the modules been loaded already?")
  557. (defun org-load-modules-maybe (&optional force)
  558. "Load all extensions listed in `org-modules'."
  559. (when (or force (not org-modules-loaded))
  560. (mapc (lambda (ext)
  561. (condition-case nil (require ext)
  562. (error (message "Problems while trying to load feature `%s'" ext))))
  563. org-modules)
  564. (setq org-modules-loaded t)))
  565. (defun org-set-modules (var value)
  566. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  567. (set var value)
  568. (when (featurep 'org)
  569. (org-load-modules-maybe 'force)
  570. (org-element-cache-reset 'all)))
  571. (defcustom org-modules '(org-w3m org-bbdb org-bibtex org-docview org-gnus org-info org-irc org-mhe org-rmail)
  572. "Modules that should always be loaded together with org.el.
  573. If a description starts with <C>, the file is not part of Emacs
  574. and loading it will require that you have downloaded and properly
  575. installed the Org mode distribution.
  576. You can also use this system to load external packages (i.e. neither Org
  577. core modules, nor modules from the CONTRIB directory). Just add symbols
  578. to the end of the list. If the package is called org-xyz.el, then you need
  579. to add the symbol `xyz', and the package must have a call to:
  580. \(provide 'org-xyz)
  581. For export specific modules, see also `org-export-backends'."
  582. :group 'org
  583. :set 'org-set-modules
  584. :version "24.4"
  585. :package-version '(Org . "8.0")
  586. :type
  587. '(set :greedy t
  588. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  589. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  590. (const :tag " crypt: Encryption of subtrees" org-crypt)
  591. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  592. (const :tag " docview: Links to doc-view buffers" org-docview)
  593. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  594. (const :tag " habit: Track your consistency with habits" org-habit)
  595. (const :tag " id: Global IDs for identifying entries" org-id)
  596. (const :tag " info: Links to Info nodes" org-info)
  597. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  598. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  599. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  600. (const :tag " mouse: Additional mouse support" org-mouse)
  601. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  602. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  603. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  604. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  605. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  606. (const :tag "C bullets: Add overlays to headlines stars" org-bullets)
  607. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  608. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  609. (const :tag "C collector: Collect properties into tables" org-collector)
  610. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  611. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  612. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  613. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  614. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  615. (const :tag "C eval: Include command output as text" org-eval)
  616. (const :tag "C eww: Store link to url of eww" org-eww)
  617. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  618. (const :tag "C favtable: Lookup table of favorite references and links" org-favtable)
  619. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  620. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  621. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  622. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  623. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  624. (const :tag "C mac-link: Grab links and url from various mac Applications" org-mac-link)
  625. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  626. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  627. (const :tag "C mew: Links to Mew folders/messages" org-mew)
  628. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  629. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  630. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  631. (const :tag "C registry: A registry for Org-mode links" org-registry)
  632. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  633. (const :tag "C secretary: Team management with org-mode" org-secretary)
  634. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  635. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  636. (const :tag "C track: Keep up with Org-mode development" org-track)
  637. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  638. (const :tag "C vm: Links to VM folders/messages" org-vm)
  639. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  640. (const :tag "C wl: Links to Wanderlust folders/messages" org-wl)
  641. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  642. (defvar org-export-registered-backends) ; From ox.el.
  643. (declare-function org-export-derived-backend-p "ox" (backend &rest backends))
  644. (declare-function org-export-backend-name "ox" (backend))
  645. (defcustom org-export-backends '(ascii html icalendar latex)
  646. "List of export back-ends that should be always available.
  647. If a description starts with <C>, the file is not part of Emacs
  648. and loading it will require that you have downloaded and properly
  649. installed the Org mode distribution.
  650. Unlike to `org-modules', libraries in this list will not be
  651. loaded along with Org, but only once the export framework is
  652. needed.
  653. This variable needs to be set before org.el is loaded. If you
  654. need to make a change while Emacs is running, use the customize
  655. interface or run the following code, where VAL stands for the new
  656. value of the variable, after updating it:
  657. \(progn
  658. \(setq org-export-registered-backends
  659. \(org-remove-if-not
  660. \(lambda (backend)
  661. \(let ((name (org-export-backend-name backend)))
  662. \(or (memq name val)
  663. \(catch 'parentp
  664. \(dolist (b val)
  665. \(and (org-export-derived-backend-p b name)
  666. \(throw 'parentp t)))))))
  667. org-export-registered-backends))
  668. \(let ((new-list (mapcar #'org-export-backend-name
  669. org-export-registered-backends)))
  670. \(dolist (backend val)
  671. \(cond
  672. \((not (load (format \"ox-%s\" backend) t t))
  673. \(message \"Problems while trying to load export back-end `%s'\"
  674. backend))
  675. \((not (memq backend new-list)) (push backend new-list))))
  676. \(set-default 'org-export-backends new-list)))
  677. Adding a back-end to this list will also pull the back-end it
  678. depends on, if any."
  679. :group 'org
  680. :group 'org-export
  681. :version "24.4"
  682. :package-version '(Org . "8.0")
  683. :initialize 'custom-initialize-set
  684. :set (lambda (var val)
  685. (if (not (featurep 'ox)) (set-default var val)
  686. ;; Any back-end not required anymore (not present in VAL and not
  687. ;; a parent of any back-end in the new value) is removed from the
  688. ;; list of registered back-ends.
  689. (setq org-export-registered-backends
  690. (org-remove-if-not
  691. (lambda (backend)
  692. (let ((name (org-export-backend-name backend)))
  693. (or (memq name val)
  694. (catch 'parentp
  695. (dolist (b val)
  696. (and (org-export-derived-backend-p b name)
  697. (throw 'parentp t)))))))
  698. org-export-registered-backends))
  699. ;; Now build NEW-LIST of both new back-ends and required
  700. ;; parents.
  701. (let ((new-list (mapcar #'org-export-backend-name
  702. org-export-registered-backends)))
  703. (dolist (backend val)
  704. (cond
  705. ((not (load (format "ox-%s" backend) t t))
  706. (message "Problems while trying to load export back-end `%s'"
  707. backend))
  708. ((not (memq backend new-list)) (push backend new-list))))
  709. ;; Set VAR to that list with fixed dependencies.
  710. (set-default var new-list))))
  711. :type '(set :greedy t
  712. (const :tag " ascii Export buffer to ASCII format" ascii)
  713. (const :tag " beamer Export buffer to Beamer presentation" beamer)
  714. (const :tag " html Export buffer to HTML format" html)
  715. (const :tag " icalendar Export buffer to iCalendar format" icalendar)
  716. (const :tag " latex Export buffer to LaTeX format" latex)
  717. (const :tag " man Export buffer to MAN format" man)
  718. (const :tag " md Export buffer to Markdown format" md)
  719. (const :tag " odt Export buffer to ODT format" odt)
  720. (const :tag " org Export buffer to Org format" org)
  721. (const :tag " texinfo Export buffer to Texinfo format" texinfo)
  722. (const :tag "C confluence Export buffer to Confluence Wiki format" confluence)
  723. (const :tag "C deck Export buffer to deck.js presentations" deck)
  724. (const :tag "C freemind Export buffer to Freemind mindmap format" freemind)
  725. (const :tag "C groff Export buffer to Groff format" groff)
  726. (const :tag "C koma-letter Export buffer to KOMA Scrlttrl2 format" koma-letter)
  727. (const :tag "C RSS 2.0 Export buffer to RSS 2.0 format" rss)
  728. (const :tag "C s5 Export buffer to s5 presentations" s5)
  729. (const :tag "C taskjuggler Export buffer to TaskJuggler format" taskjuggler)))
  730. (eval-after-load 'ox
  731. '(mapc
  732. (lambda (backend)
  733. (condition-case nil (require (intern (format "ox-%s" backend)))
  734. (error (message "Problems while trying to load export back-end `%s'"
  735. backend))))
  736. org-export-backends))
  737. (defcustom org-support-shift-select nil
  738. "Non-nil means make shift-cursor commands select text when possible.
  739. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  740. start selecting a region, or enlarge regions started in this way.
  741. In Org-mode, in special contexts, these same keys are used for
  742. other purposes, important enough to compete with shift selection.
  743. Org tries to balance these needs by supporting `shift-select-mode'
  744. outside these special contexts, under control of this variable.
  745. The default of this variable is nil, to avoid confusing behavior. Shifted
  746. cursor keys will then execute Org commands in the following contexts:
  747. - on a headline, changing TODO state (left/right) and priority (up/down)
  748. - on a time stamp, changing the time
  749. - in a plain list item, changing the bullet type
  750. - in a property definition line, switching between allowed values
  751. - in the BEGIN line of a clock table (changing the time block).
  752. Outside these contexts, the commands will throw an error.
  753. When this variable is t and the cursor is not in a special
  754. context, Org-mode will support shift-selection for making and
  755. enlarging regions. To make this more effective, the bullet
  756. cycling will no longer happen anywhere in an item line, but only
  757. if the cursor is exactly on the bullet.
  758. If you set this variable to the symbol `always', then the keys
  759. will not be special in headlines, property lines, and item lines,
  760. to make shift selection work there as well. If this is what you
  761. want, you can use the following alternative commands: `C-c C-t'
  762. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  763. can be used to switch TODO sets, `C-c -' to cycle item bullet
  764. types, and properties can be edited by hand or in column view.
  765. However, when the cursor is on a timestamp, shift-cursor commands
  766. will still edit the time stamp - this is just too good to give up.
  767. XEmacs user should have this variable set to nil, because
  768. `shift-select-mode' is in Emacs 23 or later only."
  769. :group 'org
  770. :type '(choice
  771. (const :tag "Never" nil)
  772. (const :tag "When outside special context" t)
  773. (const :tag "Everywhere except timestamps" always)))
  774. (defcustom org-loop-over-headlines-in-active-region nil
  775. "Shall some commands act upon headlines in the active region?
  776. When set to t, some commands will be performed in all headlines
  777. within the active region.
  778. When set to `start-level', some commands will be performed in all
  779. headlines within the active region, provided that these headlines
  780. are of the same level than the first one.
  781. When set to a string, those commands will be performed on the
  782. matching headlines within the active region. Such string must be
  783. a tags/property/todo match as it is used in the agenda tags view.
  784. The list of commands is: `org-schedule', `org-deadline',
  785. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  786. `org-archive-to-archive-sibling'. The archiving commands skip
  787. already archived entries."
  788. :type '(choice (const :tag "Don't loop" nil)
  789. (const :tag "All headlines in active region" t)
  790. (const :tag "In active region, headlines at the same level than the first one" start-level)
  791. (string :tag "Tags/Property/Todo matcher"))
  792. :version "24.1"
  793. :group 'org-todo
  794. :group 'org-archive)
  795. (defgroup org-startup nil
  796. "Options concerning startup of Org-mode."
  797. :tag "Org Startup"
  798. :group 'org)
  799. (defcustom org-startup-folded t
  800. "Non-nil means entering Org-mode will switch to OVERVIEW.
  801. This can also be configured on a per-file basis by adding one of
  802. the following lines anywhere in the buffer:
  803. #+STARTUP: fold (or `overview', this is equivalent)
  804. #+STARTUP: nofold (or `showall', this is equivalent)
  805. #+STARTUP: content
  806. #+STARTUP: showeverything
  807. By default, this option is ignored when Org opens agenda files
  808. for the first time. If you want the agenda to honor the startup
  809. option, set `org-agenda-inhibit-startup' to nil."
  810. :group 'org-startup
  811. :type '(choice
  812. (const :tag "nofold: show all" nil)
  813. (const :tag "fold: overview" t)
  814. (const :tag "content: all headlines" content)
  815. (const :tag "show everything, even drawers" showeverything)))
  816. (defcustom org-startup-truncated t
  817. "Non-nil means entering Org-mode will set `truncate-lines'.
  818. This is useful since some lines containing links can be very long and
  819. uninteresting. Also tables look terrible when wrapped."
  820. :group 'org-startup
  821. :type 'boolean)
  822. (defcustom org-startup-indented nil
  823. "Non-nil means turn on `org-indent-mode' on startup.
  824. This can also be configured on a per-file basis by adding one of
  825. the following lines anywhere in the buffer:
  826. #+STARTUP: indent
  827. #+STARTUP: noindent"
  828. :group 'org-structure
  829. :type '(choice
  830. (const :tag "Not" nil)
  831. (const :tag "Globally (slow on startup in large files)" t)))
  832. (defcustom org-use-sub-superscripts t
  833. "Non-nil means interpret \"_\" and \"^\" for display.
  834. If you want to control how Org exports those characters, see
  835. `org-export-with-sub-superscripts'. `org-use-sub-superscripts'
  836. used to be an alias for `org-export-with-sub-superscripts' in
  837. Org <8.0, it is not anymore.
  838. When this option is turned on, you can use TeX-like syntax for
  839. sub- and superscripts within the buffer. Several characters after
  840. \"_\" or \"^\" will be considered as a single item - so grouping
  841. with {} is normally not needed. For example, the following things
  842. will be parsed as single sub- or superscripts:
  843. 10^24 or 10^tau several digits will be considered 1 item.
  844. 10^-12 or 10^-tau a leading sign with digits or a word
  845. x^2-y^3 will be read as x^2 - y^3, because items are
  846. terminated by almost any nonword/nondigit char.
  847. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  848. Still, ambiguity is possible. So when in doubt, use {} to enclose
  849. the sub/superscript. If you set this variable to the symbol `{}',
  850. the braces are *required* in order to trigger interpretations as
  851. sub/superscript. This can be helpful in documents that need \"_\"
  852. frequently in plain text."
  853. :group 'org-startup
  854. :version "24.4"
  855. :package-version '(Org . "8.0")
  856. :type '(choice
  857. (const :tag "Always interpret" t)
  858. (const :tag "Only with braces" {})
  859. (const :tag "Never interpret" nil)))
  860. (defcustom org-startup-with-beamer-mode nil
  861. "Non-nil means turn on `org-beamer-mode' on startup.
  862. This can also be configured on a per-file basis by adding one of
  863. the following lines anywhere in the buffer:
  864. #+STARTUP: beamer"
  865. :group 'org-startup
  866. :version "24.1"
  867. :type 'boolean)
  868. (defcustom org-startup-align-all-tables nil
  869. "Non-nil means align all tables when visiting a file.
  870. This is useful when the column width in tables is forced with <N> cookies
  871. in table fields. Such tables will look correct only after the first re-align.
  872. This can also be configured on a per-file basis by adding one of
  873. the following lines anywhere in the buffer:
  874. #+STARTUP: align
  875. #+STARTUP: noalign"
  876. :group 'org-startup
  877. :type 'boolean)
  878. (defcustom org-startup-with-inline-images nil
  879. "Non-nil means show inline images when loading a new Org file.
  880. This can also be configured on a per-file basis by adding one of
  881. the following lines anywhere in the buffer:
  882. #+STARTUP: inlineimages
  883. #+STARTUP: noinlineimages"
  884. :group 'org-startup
  885. :version "24.1"
  886. :type 'boolean)
  887. (defcustom org-startup-with-latex-preview nil
  888. "Non-nil means preview LaTeX fragments when loading a new Org file.
  889. This can also be configured on a per-file basis by adding one of
  890. the following lines anywhere in the buffer:
  891. #+STARTUP: latexpreview
  892. #+STARTUP: nolatexpreview"
  893. :group 'org-startup
  894. :version "24.4"
  895. :package-version '(Org . "8.0")
  896. :type 'boolean)
  897. (defcustom org-insert-mode-line-in-empty-file nil
  898. "Non-nil means insert the first line setting Org-mode in empty files.
  899. When the function `org-mode' is called interactively in an empty file, this
  900. normally means that the file name does not automatically trigger Org-mode.
  901. To ensure that the file will always be in Org-mode in the future, a
  902. line enforcing Org-mode will be inserted into the buffer, if this option
  903. has been set."
  904. :group 'org-startup
  905. :type 'boolean)
  906. (defcustom org-replace-disputed-keys nil
  907. "Non-nil means use alternative key bindings for some keys.
  908. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  909. These keys are also used by other packages like shift-selection-mode'
  910. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  911. If you want to use Org-mode together with one of these other modes,
  912. or more generally if you would like to move some Org-mode commands to
  913. other keys, set this variable and configure the keys with the variable
  914. `org-disputed-keys'.
  915. This option is only relevant at load-time of Org-mode, and must be set
  916. *before* org.el is loaded. Changing it requires a restart of Emacs to
  917. become effective."
  918. :group 'org-startup
  919. :type 'boolean)
  920. (defcustom org-use-extra-keys nil
  921. "Non-nil means use extra key sequence definitions for certain commands.
  922. This happens automatically if you run XEmacs or if `window-system'
  923. is nil. This variable lets you do the same manually. You must
  924. set it before loading org.
  925. Example: on Carbon Emacs 22 running graphically, with an external
  926. keyboard on a Powerbook, the default way of setting M-left might
  927. not work for either Alt or ESC. Setting this variable will make
  928. it work for ESC."
  929. :group 'org-startup
  930. :type 'boolean)
  931. (org-defvaralias 'org-CUA-compatible 'org-replace-disputed-keys)
  932. (defcustom org-disputed-keys
  933. '(([(shift up)] . [(meta p)])
  934. ([(shift down)] . [(meta n)])
  935. ([(shift left)] . [(meta -)])
  936. ([(shift right)] . [(meta +)])
  937. ([(control shift right)] . [(meta shift +)])
  938. ([(control shift left)] . [(meta shift -)]))
  939. "Keys for which Org-mode and other modes compete.
  940. This is an alist, cars are the default keys, second element specifies
  941. the alternative to use when `org-replace-disputed-keys' is t.
  942. Keys can be specified in any syntax supported by `define-key'.
  943. The value of this option takes effect only at Org-mode's startup,
  944. therefore you'll have to restart Emacs to apply it after changing."
  945. :group 'org-startup
  946. :type 'alist)
  947. (defun org-key (key)
  948. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  949. Or return the original if not disputed.
  950. Also apply the translations defined in `org-xemacs-key-equivalents'."
  951. (when org-replace-disputed-keys
  952. (let* ((nkey (key-description key))
  953. (x (org-find-if (lambda (x)
  954. (equal (key-description (car x)) nkey))
  955. org-disputed-keys)))
  956. (setq key (if x (cdr x) key))))
  957. (when (featurep 'xemacs)
  958. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  959. key)
  960. (defun org-find-if (predicate seq)
  961. (catch 'exit
  962. (while seq
  963. (if (funcall predicate (car seq))
  964. (throw 'exit (car seq))
  965. (pop seq)))))
  966. (defun org-defkey (keymap key def)
  967. "Define a key, possibly translated, as returned by `org-key'."
  968. (define-key keymap (org-key key) def))
  969. (defcustom org-ellipsis nil
  970. "The ellipsis to use in the Org-mode outline.
  971. When nil, just use the standard three dots.
  972. When a string, use that string instead.
  973. When a face, use the standard 3 dots, but with the specified face.
  974. The change affects only Org-mode (which will then use its own display table).
  975. Changing this requires executing \\[org-mode] in a buffer to become
  976. effective."
  977. :group 'org-startup
  978. :type '(choice (const :tag "Default" nil)
  979. (face :tag "Face" :value org-warning)
  980. (string :tag "String" :value "...#")))
  981. (defvar org-display-table nil
  982. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  983. (defgroup org-keywords nil
  984. "Keywords in Org-mode."
  985. :tag "Org Keywords"
  986. :group 'org)
  987. (defcustom org-closed-keep-when-no-todo nil
  988. "Remove CLOSED: time-stamp when switching back to a non-todo state?"
  989. :group 'org-todo
  990. :group 'org-keywords
  991. :version "24.4"
  992. :package-version '(Org . "8.0")
  993. :type 'boolean)
  994. (defgroup org-structure nil
  995. "Options concerning the general structure of Org-mode files."
  996. :tag "Org Structure"
  997. :group 'org)
  998. (defgroup org-reveal-location nil
  999. "Options about how to make context of a location visible."
  1000. :tag "Org Reveal Location"
  1001. :group 'org-structure)
  1002. (defcustom org-show-context-detail '((isearch . lineage)
  1003. (bookmark-jump . lineage)
  1004. (default . ancestors))
  1005. "Alist between context and visibility span when revealing a location.
  1006. \\<org-mode-map>Some actions may move point into invisible
  1007. locations. As a consequence, Org always expose a neighborhood
  1008. around point. How much is shown depends on the initial action,
  1009. or context. Valid contexts are
  1010. agenda when exposing an entry from the agenda
  1011. org-goto when using the command `org-goto' (\\[org-goto])
  1012. occur-tree when using the command `org-occur' (\\[org-sparse-tree] /)
  1013. tags-tree when constructing a sparse tree based on tags matches
  1014. link-search when exposing search matches associated with a link
  1015. mark-goto when exposing the jump goal of a mark
  1016. bookmark-jump when exposing a bookmark location
  1017. isearch when exiting from an incremental search
  1018. default default for all contexts not set explicitly
  1019. Allowed visibility spans are
  1020. minimal show current headline; if point is not on headline,
  1021. also show entry
  1022. local show current headline, entry and next headline
  1023. ancestors show current headline and its direct ancestors; if
  1024. point is not on headline, also show entry
  1025. lineage show current headline, its direct ancestors and all
  1026. their children; if point is not on headline, also show
  1027. entry and first child
  1028. tree show current headline, its direct ancestors and all
  1029. their children; if point is not on headline, also show
  1030. entry and all children
  1031. canonical show current headline, its direct ancestors along with
  1032. their entries and children; if point is not located on
  1033. the headline, also show current entry and all children
  1034. As special cases, a nil or t value means show all contexts in
  1035. `minimal' or `canonical' view, respectively.
  1036. Some views can make displayed information very compact, but also
  1037. make it harder to edit the location of the match. In such
  1038. a case, use the command `org-reveal' (\\[org-reveal]) to show
  1039. more context."
  1040. :group 'org-reveal-location
  1041. :version "25.1"
  1042. :package-version '(Org . "8.3")
  1043. :type '(choice
  1044. (const :tag "Canonical" t)
  1045. (const :tag "Minimal" nil)
  1046. (repeat :greedy t :tag "Individual contexts"
  1047. (cons
  1048. (choice :tag "Context"
  1049. (const agenda)
  1050. (const org-goto)
  1051. (const occur-tree)
  1052. (const tags-tree)
  1053. (const link-search)
  1054. (const mark-goto)
  1055. (const bookmark-jump)
  1056. (const isearch)
  1057. (const default))
  1058. (choice :tag "Detail level"
  1059. (const minimal)
  1060. (const local)
  1061. (const ancestors)
  1062. (const lineage)
  1063. (const tree)
  1064. (const canonical))))))
  1065. (defcustom org-indirect-buffer-display 'other-window
  1066. "How should indirect tree buffers be displayed?
  1067. This applies to indirect buffers created with the commands
  1068. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  1069. Valid values are:
  1070. current-window Display in the current window
  1071. other-window Just display in another window.
  1072. dedicated-frame Create one new frame, and re-use it each time.
  1073. new-frame Make a new frame each time. Note that in this case
  1074. previously-made indirect buffers are kept, and you need to
  1075. kill these buffers yourself."
  1076. :group 'org-structure
  1077. :group 'org-agenda-windows
  1078. :type '(choice
  1079. (const :tag "In current window" current-window)
  1080. (const :tag "In current frame, other window" other-window)
  1081. (const :tag "Each time a new frame" new-frame)
  1082. (const :tag "One dedicated frame" dedicated-frame)))
  1083. (defcustom org-use-speed-commands nil
  1084. "Non-nil means activate single letter commands at beginning of a headline.
  1085. This may also be a function to test for appropriate locations where speed
  1086. commands should be active.
  1087. For example, to activate speed commands when the point is on any
  1088. star at the beginning of the headline, you can do this:
  1089. (setq org-use-speed-commands
  1090. (lambda () (and (looking-at org-outline-regexp) (looking-back \"^\\**\"))))"
  1091. :group 'org-structure
  1092. :type '(choice
  1093. (const :tag "Never" nil)
  1094. (const :tag "At beginning of headline stars" t)
  1095. (function)))
  1096. (defcustom org-speed-commands-user nil
  1097. "Alist of additional speed commands.
  1098. This list will be checked before `org-speed-commands-default'
  1099. when the variable `org-use-speed-commands' is non-nil
  1100. and when the cursor is at the beginning of a headline.
  1101. The car if each entry is a string with a single letter, which must
  1102. be assigned to `self-insert-command' in the global map.
  1103. The cdr is either a command to be called interactively, a function
  1104. to be called, or a form to be evaluated.
  1105. An entry that is just a list with a single string will be interpreted
  1106. as a descriptive headline that will be added when listing the speed
  1107. commands in the Help buffer using the `?' speed command."
  1108. :group 'org-structure
  1109. :type '(repeat :value ("k" . ignore)
  1110. (choice :value ("k" . ignore)
  1111. (list :tag "Descriptive Headline" (string :tag "Headline"))
  1112. (cons :tag "Letter and Command"
  1113. (string :tag "Command letter")
  1114. (choice
  1115. (function)
  1116. (sexp))))))
  1117. (defcustom org-bookmark-names-plist
  1118. '(:last-capture "org-capture-last-stored"
  1119. :last-refile "org-refile-last-stored"
  1120. :last-capture-marker "org-capture-last-stored-marker")
  1121. "Names for bookmarks automatically set by some Org commands.
  1122. This can provide strings as names for a number of bookmarks Org sets
  1123. automatically. The following keys are currently implemented:
  1124. :last-capture
  1125. :last-capture-marker
  1126. :last-refile
  1127. When a key does not show up in the property list, the corresponding bookmark
  1128. is not set."
  1129. :group 'org-structure
  1130. :type 'plist)
  1131. (defgroup org-cycle nil
  1132. "Options concerning visibility cycling in Org-mode."
  1133. :tag "Org Cycle"
  1134. :group 'org-structure)
  1135. (defcustom org-cycle-skip-children-state-if-no-children t
  1136. "Non-nil means skip CHILDREN state in entries that don't have any."
  1137. :group 'org-cycle
  1138. :type 'boolean)
  1139. (defcustom org-cycle-max-level nil
  1140. "Maximum level which should still be subject to visibility cycling.
  1141. Levels higher than this will, for cycling, be treated as text, not a headline.
  1142. When `org-odd-levels-only' is set, a value of N in this variable actually
  1143. means 2N-1 stars as the limiting headline.
  1144. When nil, cycle all levels.
  1145. Note that the limiting level of cycling is also influenced by
  1146. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  1147. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  1148. than its value."
  1149. :group 'org-cycle
  1150. :type '(choice
  1151. (const :tag "No limit" nil)
  1152. (integer :tag "Maximum level")))
  1153. (defcustom org-hide-block-startup nil
  1154. "Non-nil means entering Org-mode will fold all blocks.
  1155. This can also be set in on a per-file basis with
  1156. #+STARTUP: hideblocks
  1157. #+STARTUP: showblocks"
  1158. :group 'org-startup
  1159. :group 'org-cycle
  1160. :type 'boolean)
  1161. (defcustom org-cycle-global-at-bob nil
  1162. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  1163. This makes it possible to do global cycling without having to use S-TAB or
  1164. \\[universal-argument] TAB. For this special case to work, the first line
  1165. of the buffer must not be a headline -- it may be empty or some other text.
  1166. When used in this way, `org-cycle-hook' is disabled temporarily to make
  1167. sure the cursor stays at the beginning of the buffer. When this option is
  1168. nil, don't do anything special at the beginning of the buffer."
  1169. :group 'org-cycle
  1170. :type 'boolean)
  1171. (defcustom org-cycle-level-after-item/entry-creation t
  1172. "Non-nil means cycle entry level or item indentation in new empty entries.
  1173. When the cursor is at the end of an empty headline, i.e., with only stars
  1174. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  1175. and then all possible ancestor states, before returning to the original state.
  1176. This makes data entry extremely fast: M-RET to create a new headline,
  1177. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  1178. When the cursor is at the end of an empty plain list item, one TAB will
  1179. make it a subitem, two or more tabs will back up to make this an item
  1180. higher up in the item hierarchy."
  1181. :group 'org-cycle
  1182. :type 'boolean)
  1183. (defcustom org-cycle-emulate-tab t
  1184. "Where should `org-cycle' emulate TAB.
  1185. nil Never
  1186. white Only in completely white lines
  1187. whitestart Only at the beginning of lines, before the first non-white char
  1188. t Everywhere except in headlines
  1189. exc-hl-bol Everywhere except at the start of a headline
  1190. If TAB is used in a place where it does not emulate TAB, the current subtree
  1191. visibility is cycled."
  1192. :group 'org-cycle
  1193. :type '(choice (const :tag "Never" nil)
  1194. (const :tag "Only in completely white lines" white)
  1195. (const :tag "Before first char in a line" whitestart)
  1196. (const :tag "Everywhere except in headlines" t)
  1197. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)))
  1198. (defcustom org-cycle-separator-lines 2
  1199. "Number of empty lines needed to keep an empty line between collapsed trees.
  1200. If you leave an empty line between the end of a subtree and the following
  1201. headline, this empty line is hidden when the subtree is folded.
  1202. Org-mode will leave (exactly) one empty line visible if the number of
  1203. empty lines is equal or larger to the number given in this variable.
  1204. So the default 2 means at least 2 empty lines after the end of a subtree
  1205. are needed to produce free space between a collapsed subtree and the
  1206. following headline.
  1207. If the number is negative, and the number of empty lines is at least -N,
  1208. all empty lines are shown.
  1209. Special case: when 0, never leave empty lines in collapsed view."
  1210. :group 'org-cycle
  1211. :type 'integer)
  1212. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  1213. (defcustom org-pre-cycle-hook nil
  1214. "Hook that is run before visibility cycling is happening.
  1215. The function(s) in this hook must accept a single argument which indicates
  1216. the new state that will be set right after running this hook. The
  1217. argument is a symbol. Before a global state change, it can have the values
  1218. `overview', `content', or `all'. Before a local state change, it can have
  1219. the values `folded', `children', or `subtree'."
  1220. :group 'org-cycle
  1221. :type 'hook)
  1222. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  1223. org-cycle-hide-drawers
  1224. org-cycle-show-empty-lines
  1225. org-optimize-window-after-visibility-change)
  1226. "Hook that is run after `org-cycle' has changed the buffer visibility.
  1227. The function(s) in this hook must accept a single argument which indicates
  1228. the new state that was set by the most recent `org-cycle' command. The
  1229. argument is a symbol. After a global state change, it can have the values
  1230. `overview', `contents', or `all'. After a local state change, it can have
  1231. the values `folded', `children', or `subtree'."
  1232. :group 'org-cycle
  1233. :type 'hook
  1234. :version "25.1"
  1235. :package-version '(Org . "8.3"))
  1236. (defgroup org-edit-structure nil
  1237. "Options concerning structure editing in Org-mode."
  1238. :tag "Org Edit Structure"
  1239. :group 'org-structure)
  1240. (defcustom org-odd-levels-only nil
  1241. "Non-nil means skip even levels and only use odd levels for the outline.
  1242. This has the effect that two stars are being added/taken away in
  1243. promotion/demotion commands. It also influences how levels are
  1244. handled by the exporters.
  1245. Changing it requires restart of `font-lock-mode' to become effective
  1246. for fontification also in regions already fontified.
  1247. You may also set this on a per-file basis by adding one of the following
  1248. lines to the buffer:
  1249. #+STARTUP: odd
  1250. #+STARTUP: oddeven"
  1251. :group 'org-edit-structure
  1252. :group 'org-appearance
  1253. :type 'boolean)
  1254. (defcustom org-adapt-indentation t
  1255. "Non-nil means adapt indentation to outline node level.
  1256. When this variable is set, Org assumes that you write outlines by
  1257. indenting text in each node to align with the headline (after the
  1258. stars). The following issues are influenced by this variable:
  1259. - The indentation is increased by one space in a demotion
  1260. command, and decreased by one in a promotion command. However,
  1261. in the latter case, if shifting some line in the entry body
  1262. would alter document structure (e.g., insert a new headline),
  1263. indentation is not changed at all.
  1264. - Property drawers and planning information is inserted indented
  1265. when this variable is set. When nil, they will not be indented.
  1266. - TAB indents a line relative to current level. The lines below
  1267. a headline will be indented when this variable is set.
  1268. Note that this is all about true indentation, by adding and
  1269. removing space characters. See also `org-indent.el' which does
  1270. level-dependent indentation in a virtual way, i.e. at display
  1271. time in Emacs."
  1272. :group 'org-edit-structure
  1273. :type 'boolean)
  1274. (defcustom org-special-ctrl-a/e nil
  1275. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  1276. When t, `C-a' will bring back the cursor to the beginning of the
  1277. headline text, i.e. after the stars and after a possible TODO
  1278. keyword. In an item, this will be the position after bullet and
  1279. check-box, if any. When the cursor is already at that position,
  1280. another `C-a' will bring it to the beginning of the line.
  1281. `C-e' will jump to the end of the headline, ignoring the presence
  1282. of tags in the headline. A second `C-e' will then jump to the
  1283. true end of the line, after any tags. This also means that, when
  1284. this variable is non-nil, `C-e' also will never jump beyond the
  1285. end of the heading of a folded section, i.e. not after the
  1286. ellipses.
  1287. When set to the symbol `reversed', the first `C-a' or `C-e' works
  1288. normally, going to the true line boundary first. Only a directly
  1289. following, identical keypress will bring the cursor to the
  1290. special positions.
  1291. This may also be a cons cell where the behavior for `C-a' and
  1292. `C-e' is set separately."
  1293. :group 'org-edit-structure
  1294. :type '(choice
  1295. (const :tag "off" nil)
  1296. (const :tag "on: after stars/bullet and before tags first" t)
  1297. (const :tag "reversed: true line boundary first" reversed)
  1298. (cons :tag "Set C-a and C-e separately"
  1299. (choice :tag "Special C-a"
  1300. (const :tag "off" nil)
  1301. (const :tag "on: after stars/bullet first" t)
  1302. (const :tag "reversed: before stars/bullet first" reversed))
  1303. (choice :tag "Special C-e"
  1304. (const :tag "off" nil)
  1305. (const :tag "on: before tags first" t)
  1306. (const :tag "reversed: after tags first" reversed)))))
  1307. (org-defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e)
  1308. (defcustom org-special-ctrl-k nil
  1309. "Non-nil means `C-k' will behave specially in headlines.
  1310. When nil, `C-k' will call the default `kill-line' command.
  1311. When t, the following will happen while the cursor is in the headline:
  1312. - When the cursor is at the beginning of a headline, kill the entire
  1313. line and possible the folded subtree below the line.
  1314. - When in the middle of the headline text, kill the headline up to the tags.
  1315. - When after the headline text, kill the tags."
  1316. :group 'org-edit-structure
  1317. :type 'boolean)
  1318. (defcustom org-ctrl-k-protect-subtree nil
  1319. "Non-nil means, do not delete a hidden subtree with C-k.
  1320. When set to the symbol `error', simply throw an error when C-k is
  1321. used to kill (part-of) a headline that has hidden text behind it.
  1322. Any other non-nil value will result in a query to the user, if it is
  1323. OK to kill that hidden subtree. When nil, kill without remorse."
  1324. :group 'org-edit-structure
  1325. :version "24.1"
  1326. :type '(choice
  1327. (const :tag "Do not protect hidden subtrees" nil)
  1328. (const :tag "Protect hidden subtrees with a security query" t)
  1329. (const :tag "Never kill a hidden subtree with C-k" error)))
  1330. (defcustom org-special-ctrl-o t
  1331. "Non-nil means, make `C-o' insert a row in tables."
  1332. :group 'org-edit-structure
  1333. :type 'boolean)
  1334. (defcustom org-catch-invisible-edits nil
  1335. "Check if in invisible region before inserting or deleting a character.
  1336. Valid values are:
  1337. nil Do not check, so just do invisible edits.
  1338. error Throw an error and do nothing.
  1339. show Make point visible, and do the requested edit.
  1340. show-and-error Make point visible, then throw an error and abort the edit.
  1341. smart Make point visible, and do insertion/deletion if it is
  1342. adjacent to visible text and the change feels predictable.
  1343. Never delete a previously invisible character or add in the
  1344. middle or right after an invisible region. Basically, this
  1345. allows insertion and backward-delete right before ellipses.
  1346. FIXME: maybe in this case we should not even show?"
  1347. :group 'org-edit-structure
  1348. :version "24.1"
  1349. :type '(choice
  1350. (const :tag "Do not check" nil)
  1351. (const :tag "Throw error when trying to edit" error)
  1352. (const :tag "Unhide, but do not do the edit" show-and-error)
  1353. (const :tag "Show invisible part and do the edit" show)
  1354. (const :tag "Be smart and do the right thing" smart)))
  1355. (defcustom org-yank-folded-subtrees t
  1356. "Non-nil means when yanking subtrees, fold them.
  1357. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  1358. it starts with a heading and all other headings in it are either children
  1359. or siblings, then fold all the subtrees. However, do this only if no
  1360. text after the yank would be swallowed into a folded tree by this action."
  1361. :group 'org-edit-structure
  1362. :type 'boolean)
  1363. (defcustom org-yank-adjusted-subtrees nil
  1364. "Non-nil means when yanking subtrees, adjust the level.
  1365. With this setting, `org-paste-subtree' is used to insert the subtree, see
  1366. this function for details."
  1367. :group 'org-edit-structure
  1368. :type 'boolean)
  1369. (defcustom org-M-RET-may-split-line '((default . t))
  1370. "Non-nil means M-RET will split the line at the cursor position.
  1371. When nil, it will go to the end of the line before making a
  1372. new line.
  1373. You may also set this option in a different way for different
  1374. contexts. Valid contexts are:
  1375. headline when creating a new headline
  1376. item when creating a new item
  1377. table in a table field
  1378. default the value to be used for all contexts not explicitly
  1379. customized"
  1380. :group 'org-structure
  1381. :group 'org-table
  1382. :type '(choice
  1383. (const :tag "Always" t)
  1384. (const :tag "Never" nil)
  1385. (repeat :greedy t :tag "Individual contexts"
  1386. (cons
  1387. (choice :tag "Context"
  1388. (const headline)
  1389. (const item)
  1390. (const table)
  1391. (const default))
  1392. (boolean)))))
  1393. (defcustom org-insert-heading-respect-content nil
  1394. "Non-nil means insert new headings after the current subtree.
  1395. When nil, the new heading is created directly after the current line.
  1396. The commands \\[org-insert-heading-respect-content] and \\[org-insert-todo-heading-respect-content] turn
  1397. this variable on for the duration of the command."
  1398. :group 'org-structure
  1399. :type 'boolean)
  1400. (defcustom org-blank-before-new-entry '((heading . auto)
  1401. (plain-list-item . auto))
  1402. "Should `org-insert-heading' leave a blank line before new heading/item?
  1403. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1404. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1405. which case Org will look at the surrounding headings/items and try to
  1406. make an intelligent decision whether to insert a blank line or not."
  1407. :group 'org-edit-structure
  1408. :type '(list
  1409. (cons (const heading)
  1410. (choice (const :tag "Never" nil)
  1411. (const :tag "Always" t)
  1412. (const :tag "Auto" auto)))
  1413. (cons (const plain-list-item)
  1414. (choice (const :tag "Never" nil)
  1415. (const :tag "Always" t)
  1416. (const :tag "Auto" auto)))))
  1417. (defcustom org-insert-heading-hook nil
  1418. "Hook being run after inserting a new heading."
  1419. :group 'org-edit-structure
  1420. :type 'hook)
  1421. (defcustom org-enable-fixed-width-editor t
  1422. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1423. This currently only means they are never auto-wrapped.
  1424. When nil, such lines will be treated like ordinary lines."
  1425. :group 'org-edit-structure
  1426. :type 'boolean)
  1427. (defcustom org-goto-auto-isearch t
  1428. "Non-nil means typing characters in `org-goto' starts incremental search.
  1429. When nil, you can use these keybindings to navigate the buffer:
  1430. q Quit the org-goto interface
  1431. n Go to the next visible heading
  1432. p Go to the previous visible heading
  1433. f Go one heading forward on same level
  1434. b Go one heading backward on same level
  1435. u Go one heading up"
  1436. :group 'org-edit-structure
  1437. :type 'boolean)
  1438. (defgroup org-sparse-trees nil
  1439. "Options concerning sparse trees in Org-mode."
  1440. :tag "Org Sparse Trees"
  1441. :group 'org-structure)
  1442. (defcustom org-highlight-sparse-tree-matches t
  1443. "Non-nil means highlight all matches that define a sparse tree.
  1444. The highlights will automatically disappear the next time the buffer is
  1445. changed by an edit command."
  1446. :group 'org-sparse-trees
  1447. :type 'boolean)
  1448. (defcustom org-remove-highlights-with-change t
  1449. "Non-nil means any change to the buffer will remove temporary highlights.
  1450. Such highlights are created by `org-occur' and `org-clock-display'.
  1451. When nil, `C-c C-c' needs to be used to get rid of the highlights.
  1452. The highlights created by `org-toggle-latex-fragment' always need
  1453. `C-c C-x C-l' to be removed."
  1454. :group 'org-sparse-trees
  1455. :group 'org-time
  1456. :type 'boolean)
  1457. (defcustom org-occur-hook '(org-first-headline-recenter)
  1458. "Hook that is run after `org-occur' has constructed a sparse tree.
  1459. This can be used to recenter the window to show as much of the structure
  1460. as possible."
  1461. :group 'org-sparse-trees
  1462. :type 'hook)
  1463. (defgroup org-imenu-and-speedbar nil
  1464. "Options concerning imenu and speedbar in Org-mode."
  1465. :tag "Org Imenu and Speedbar"
  1466. :group 'org-structure)
  1467. (defcustom org-imenu-depth 2
  1468. "The maximum level for Imenu access to Org-mode headlines.
  1469. This also applied for speedbar access."
  1470. :group 'org-imenu-and-speedbar
  1471. :type 'integer)
  1472. (defgroup org-table nil
  1473. "Options concerning tables in Org-mode."
  1474. :tag "Org Table"
  1475. :group 'org)
  1476. (defcustom org-enable-table-editor 'optimized
  1477. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1478. When nil, such lines will be treated like ordinary lines.
  1479. When equal to the symbol `optimized', the table editor will be optimized to
  1480. do the following:
  1481. - Automatic overwrite mode in front of whitespace in table fields.
  1482. This makes the structure of the table stay in tact as long as the edited
  1483. field does not exceed the column width.
  1484. - Minimize the number of realigns. Normally, the table is aligned each time
  1485. TAB or RET are pressed to move to another field. With optimization this
  1486. happens only if changes to a field might have changed the column width.
  1487. Optimization requires replacing the functions `self-insert-command',
  1488. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1489. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1490. very good at guessing when a re-align will be necessary, but you can always
  1491. force one with \\[org-ctrl-c-ctrl-c].
  1492. If you would like to use the optimized version in Org-mode, but the
  1493. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1494. This variable can be used to turn on and off the table editor during a session,
  1495. but in order to toggle optimization, a restart is required.
  1496. See also the variable `org-table-auto-blank-field'."
  1497. :group 'org-table
  1498. :type '(choice
  1499. (const :tag "off" nil)
  1500. (const :tag "on" t)
  1501. (const :tag "on, optimized" optimized)))
  1502. (defcustom org-self-insert-cluster-for-undo (or (featurep 'xemacs)
  1503. (version<= emacs-version "24.1"))
  1504. "Non-nil means cluster self-insert commands for undo when possible.
  1505. If this is set, then, like in the Emacs command loop, 20 consecutive
  1506. characters will be undone together.
  1507. This is configurable, because there is some impact on typing performance."
  1508. :group 'org-table
  1509. :type 'boolean)
  1510. (defcustom org-table-tab-recognizes-table.el t
  1511. "Non-nil means TAB will automatically notice a table.el table.
  1512. When it sees such a table, it moves point into it and - if necessary -
  1513. calls `table-recognize-table'."
  1514. :group 'org-table-editing
  1515. :type 'boolean)
  1516. (defgroup org-link nil
  1517. "Options concerning links in Org-mode."
  1518. :tag "Org Link"
  1519. :group 'org)
  1520. (defvar org-link-abbrev-alist-local nil
  1521. "Buffer-local version of `org-link-abbrev-alist', which see.
  1522. The value of this is taken from the #+LINK lines.")
  1523. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1524. (defcustom org-link-abbrev-alist nil
  1525. "Alist of link abbreviations.
  1526. The car of each element is a string, to be replaced at the start of a link.
  1527. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1528. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1529. [[linkkey:tag][description]]
  1530. The 'linkkey' must be a word word, starting with a letter, followed
  1531. by letters, numbers, '-' or '_'.
  1532. If REPLACE is a string, the tag will simply be appended to create the link.
  1533. If the string contains \"%s\", the tag will be inserted there. If the string
  1534. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1535. at that point (see the function `url-hexify-string'). If the string contains
  1536. the specifier \"%(my-function)\", then the custom function `my-function' will
  1537. be invoked: this function takes the tag as its only argument and must return
  1538. a string.
  1539. REPLACE may also be a function that will be called with the tag as the
  1540. only argument to create the link, which should be returned as a string.
  1541. See the manual for examples."
  1542. :group 'org-link
  1543. :type '(repeat
  1544. (cons
  1545. (string :tag "Protocol")
  1546. (choice
  1547. (string :tag "Format")
  1548. (function)))))
  1549. (defcustom org-descriptive-links t
  1550. "Non-nil means Org will display descriptive links.
  1551. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1552. \"Org Website\", hiding the link itself and just displaying its
  1553. description. When set to nil, Org will display the full links
  1554. literally.
  1555. You can interactively set the value of this variable by calling
  1556. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1557. :group 'org-link
  1558. :type 'boolean)
  1559. (defcustom org-link-file-path-type 'adaptive
  1560. "How the path name in file links should be stored.
  1561. Valid values are:
  1562. relative Relative to the current directory, i.e. the directory of the file
  1563. into which the link is being inserted.
  1564. absolute Absolute path, if possible with ~ for home directory.
  1565. noabbrev Absolute path, no abbreviation of home directory.
  1566. adaptive Use relative path for files in the current directory and sub-
  1567. directories of it. For other files, use an absolute path."
  1568. :group 'org-link
  1569. :type '(choice
  1570. (const relative)
  1571. (const absolute)
  1572. (const noabbrev)
  1573. (const adaptive)))
  1574. (defvaralias 'org-activate-links 'org-highlight-links)
  1575. (defcustom org-highlight-links '(bracket angle plain radio tag date footnote)
  1576. "Types of links that should be highlighted in Org-mode files.
  1577. This is a list of symbols, each one of them leading to the
  1578. highlighting of a certain link type.
  1579. You can still open links that are not highlighted.
  1580. In principle, it does not hurt to turn on highlighting for all
  1581. link types. There may be a small gain when turning off unused
  1582. link types. The types are:
  1583. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1584. angle Links in angular brackets that may contain whitespace like
  1585. <bbdb:Carsten Dominik>.
  1586. plain Plain links in normal text, no whitespace, like http://google.com.
  1587. radio Text that is matched by a radio target, see manual for details.
  1588. tag Tag settings in a headline (link to tag search).
  1589. date Time stamps (link to calendar).
  1590. footnote Footnote labels.
  1591. If you set this variable during an Emacs session, use `org-mode-restart'
  1592. in the Org buffer so that the change takes effect."
  1593. :group 'org-link
  1594. :group 'org-appearance
  1595. :type '(set :greedy t
  1596. (const :tag "Double bracket links" bracket)
  1597. (const :tag "Angular bracket links" angle)
  1598. (const :tag "Plain text links" plain)
  1599. (const :tag "Radio target matches" radio)
  1600. (const :tag "Tags" tag)
  1601. (const :tag "Timestamps" date)
  1602. (const :tag "Footnotes" footnote)))
  1603. (defcustom org-make-link-description-function nil
  1604. "Function to use for generating link descriptions from links.
  1605. When nil, the link location will be used. This function must take
  1606. two parameters: the first one is the link, the second one is the
  1607. description generated by `org-insert-link'. The function should
  1608. return the description to use."
  1609. :group 'org-link
  1610. :type '(choice (const nil) (function)))
  1611. (defgroup org-link-store nil
  1612. "Options concerning storing links in Org-mode."
  1613. :tag "Org Store Link"
  1614. :group 'org-link)
  1615. (defcustom org-url-hexify-p t
  1616. "When non-nil, hexify URL when creating a link."
  1617. :type 'boolean
  1618. :version "24.3"
  1619. :group 'org-link-store)
  1620. (defcustom org-email-link-description-format "Email %c: %.30s"
  1621. "Format of the description part of a link to an email or usenet message.
  1622. The following %-escapes will be replaced by corresponding information:
  1623. %F full \"From\" field
  1624. %f name, taken from \"From\" field, address if no name
  1625. %T full \"To\" field
  1626. %t first name in \"To\" field, address if no name
  1627. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1628. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1629. %s subject
  1630. %d date
  1631. %m message-id.
  1632. You may use normal field width specification between the % and the letter.
  1633. This is for example useful to limit the length of the subject.
  1634. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1635. :group 'org-link-store
  1636. :type 'string)
  1637. (defcustom org-from-is-user-regexp
  1638. (let (r1 r2)
  1639. (when (and user-mail-address (not (string= user-mail-address "")))
  1640. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1641. (when (and user-full-name (not (string= user-full-name "")))
  1642. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1643. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1644. "Regexp matched against the \"From:\" header of an email or usenet message.
  1645. It should match if the message is from the user him/herself."
  1646. :group 'org-link-store
  1647. :type 'regexp)
  1648. (defcustom org-context-in-file-links t
  1649. "Non-nil means file links from `org-store-link' contain context.
  1650. A search string will be added to the file name with :: as separator and
  1651. used to find the context when the link is activated by the command
  1652. `org-open-at-point'. When this option is t, the entire active region
  1653. will be placed in the search string of the file link. If set to a
  1654. positive integer, only the first n lines of context will be stored.
  1655. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1656. negates this setting for the duration of the command."
  1657. :group 'org-link-store
  1658. :type '(choice boolean integer))
  1659. (defcustom org-keep-stored-link-after-insertion nil
  1660. "Non-nil means keep link in list for entire session.
  1661. The command `org-store-link' adds a link pointing to the current
  1662. location to an internal list. These links accumulate during a session.
  1663. The command `org-insert-link' can be used to insert links into any
  1664. Org-mode file (offering completion for all stored links). When this
  1665. option is nil, every link which has been inserted once using \\[org-insert-link]
  1666. will be removed from the list, to make completing the unused links
  1667. more efficient."
  1668. :group 'org-link-store
  1669. :type 'boolean)
  1670. (defgroup org-link-follow nil
  1671. "Options concerning following links in Org-mode."
  1672. :tag "Org Follow Link"
  1673. :group 'org-link)
  1674. (defcustom org-link-translation-function nil
  1675. "Function to translate links with different syntax to Org syntax.
  1676. This can be used to translate links created for example by the Planner
  1677. or emacs-wiki packages to Org syntax.
  1678. The function must accept two parameters, a TYPE containing the link
  1679. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1680. which is everything after the link protocol. It should return a cons
  1681. with possibly modified values of type and path.
  1682. Org contains a function for this, so if you set this variable to
  1683. `org-translate-link-from-planner', you should be able follow many
  1684. links created by planner."
  1685. :group 'org-link-follow
  1686. :type '(choice (const nil) (function)))
  1687. (defcustom org-follow-link-hook nil
  1688. "Hook that is run after a link has been followed."
  1689. :group 'org-link-follow
  1690. :type 'hook)
  1691. (defcustom org-tab-follows-link nil
  1692. "Non-nil means on links TAB will follow the link.
  1693. Needs to be set before org.el is loaded.
  1694. This really should not be used, it does not make sense, and the
  1695. implementation is bad."
  1696. :group 'org-link-follow
  1697. :type 'boolean)
  1698. (defcustom org-return-follows-link nil
  1699. "Non-nil means on links RET will follow the link.
  1700. In tables, the special behavior of RET has precedence."
  1701. :group 'org-link-follow
  1702. :type 'boolean)
  1703. (defcustom org-mouse-1-follows-link
  1704. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1705. "Non-nil means mouse-1 on a link will follow the link.
  1706. A longer mouse click will still set point. Does not work on XEmacs.
  1707. Needs to be set before org.el is loaded."
  1708. :group 'org-link-follow
  1709. :version "24.4"
  1710. :package-version '(Org . "8.3")
  1711. :type '(choice
  1712. (const :tag "A double click follows the link" double)
  1713. (const :tag "Unconditionally follow the link with mouse-1" t)
  1714. (integer :tag "mouse-1 click does not follow the link if longer than N ms" 450)))
  1715. (defcustom org-mark-ring-length 4
  1716. "Number of different positions to be recorded in the ring.
  1717. Changing this requires a restart of Emacs to work correctly."
  1718. :group 'org-link-follow
  1719. :type 'integer)
  1720. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1721. "Non-nil means internal links in Org files must exactly match a headline.
  1722. When nil, the link search tries to match a phrase with all words
  1723. in the search text."
  1724. :group 'org-link-follow
  1725. :version "24.1"
  1726. :type '(choice
  1727. (const :tag "Use fuzzy text search" nil)
  1728. (const :tag "Match only exact headline" t)
  1729. (const :tag "Match exact headline or query to create it"
  1730. query-to-create)))
  1731. (defcustom org-link-frame-setup
  1732. '((vm . vm-visit-folder-other-frame)
  1733. (vm-imap . vm-visit-imap-folder-other-frame)
  1734. (gnus . org-gnus-no-new-news)
  1735. (file . find-file-other-window)
  1736. (wl . wl-other-frame))
  1737. "Setup the frame configuration for following links.
  1738. When following a link with Emacs, it may often be useful to display
  1739. this link in another window or frame. This variable can be used to
  1740. set this up for the different types of links.
  1741. For VM, use any of
  1742. `vm-visit-folder'
  1743. `vm-visit-folder-other-window'
  1744. `vm-visit-folder-other-frame'
  1745. For Gnus, use any of
  1746. `gnus'
  1747. `gnus-other-frame'
  1748. `org-gnus-no-new-news'
  1749. For FILE, use any of
  1750. `find-file'
  1751. `find-file-other-window'
  1752. `find-file-other-frame'
  1753. For Wanderlust use any of
  1754. `wl'
  1755. `wl-other-frame'
  1756. For the calendar, use the variable `calendar-setup'.
  1757. For BBDB, it is currently only possible to display the matches in
  1758. another window."
  1759. :group 'org-link-follow
  1760. :type '(list
  1761. (cons (const vm)
  1762. (choice
  1763. (const vm-visit-folder)
  1764. (const vm-visit-folder-other-window)
  1765. (const vm-visit-folder-other-frame)))
  1766. (cons (const vm-imap)
  1767. (choice
  1768. (const vm-visit-imap-folder)
  1769. (const vm-visit-imap-folder-other-window)
  1770. (const vm-visit-imap-folder-other-frame)))
  1771. (cons (const gnus)
  1772. (choice
  1773. (const gnus)
  1774. (const gnus-other-frame)
  1775. (const org-gnus-no-new-news)))
  1776. (cons (const file)
  1777. (choice
  1778. (const find-file)
  1779. (const find-file-other-window)
  1780. (const find-file-other-frame)))
  1781. (cons (const wl)
  1782. (choice
  1783. (const wl)
  1784. (const wl-other-frame)))))
  1785. (defcustom org-display-internal-link-with-indirect-buffer nil
  1786. "Non-nil means use indirect buffer to display infile links.
  1787. Activating internal links (from one location in a file to another location
  1788. in the same file) normally just jumps to the location. When the link is
  1789. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1790. is displayed in
  1791. another window. When this option is set, the other window actually displays
  1792. an indirect buffer clone of the current buffer, to avoid any visibility
  1793. changes to the current buffer."
  1794. :group 'org-link-follow
  1795. :type 'boolean)
  1796. (defcustom org-open-non-existing-files nil
  1797. "Non-nil means `org-open-file' will open non-existing files.
  1798. When nil, an error will be generated.
  1799. This variable applies only to external applications because they
  1800. might choke on non-existing files. If the link is to a file that
  1801. will be opened in Emacs, the variable is ignored."
  1802. :group 'org-link-follow
  1803. :type 'boolean)
  1804. (defcustom org-open-directory-means-index-dot-org nil
  1805. "Non-nil means a link to a directory really means to index.org.
  1806. When nil, following a directory link will run dired or open a finder/explorer
  1807. window on that directory."
  1808. :group 'org-link-follow
  1809. :type 'boolean)
  1810. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1811. "Non-nil means ask for confirmation before executing shell links.
  1812. Shell links can be dangerous: just think about a link
  1813. [[shell:rm -rf ~/*][Google Search]]
  1814. This link would show up in your Org-mode document as \"Google Search\",
  1815. but really it would remove your entire home directory.
  1816. Therefore we advise against setting this variable to nil.
  1817. Just change it to `y-or-n-p' if you want to confirm with a
  1818. single keystroke rather than having to type \"yes\"."
  1819. :group 'org-link-follow
  1820. :type '(choice
  1821. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1822. (const :tag "with y-or-n (faster)" y-or-n-p)
  1823. (const :tag "no confirmation (dangerous)" nil)))
  1824. (put 'org-confirm-shell-link-function
  1825. 'safe-local-variable
  1826. (lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1827. (defcustom org-confirm-shell-link-not-regexp ""
  1828. "A regexp to skip confirmation for shell links."
  1829. :group 'org-link-follow
  1830. :version "24.1"
  1831. :type 'regexp)
  1832. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1833. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1834. Elisp links can be dangerous: just think about a link
  1835. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1836. This link would show up in your Org-mode document as \"Google Search\",
  1837. but really it would remove your entire home directory.
  1838. Therefore we advise against setting this variable to nil.
  1839. Just change it to `y-or-n-p' if you want to confirm with a
  1840. single keystroke rather than having to type \"yes\"."
  1841. :group 'org-link-follow
  1842. :type '(choice
  1843. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1844. (const :tag "with y-or-n (faster)" y-or-n-p)
  1845. (const :tag "no confirmation (dangerous)" nil)))
  1846. (put 'org-confirm-shell-link-function
  1847. 'safe-local-variable
  1848. (lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1849. (defcustom org-confirm-elisp-link-not-regexp ""
  1850. "A regexp to skip confirmation for Elisp links."
  1851. :group 'org-link-follow
  1852. :version "24.1"
  1853. :type 'regexp)
  1854. (defconst org-file-apps-defaults-gnu
  1855. '((remote . emacs)
  1856. (system . mailcap)
  1857. (t . mailcap))
  1858. "Default file applications on a UNIX or GNU/Linux system.
  1859. See `org-file-apps'.")
  1860. (defconst org-file-apps-defaults-macosx
  1861. '((remote . emacs)
  1862. (t . "open %s")
  1863. (system . "open %s")
  1864. ("ps.gz" . "gv %s")
  1865. ("eps.gz" . "gv %s")
  1866. ("dvi" . "xdvi %s")
  1867. ("fig" . "xfig %s"))
  1868. "Default file applications on a MacOS X system.
  1869. The system \"open\" is known as a default, but we use X11 applications
  1870. for some files for which the OS does not have a good default.
  1871. See `org-file-apps'.")
  1872. (defconst org-file-apps-defaults-windowsnt
  1873. (list
  1874. '(remote . emacs)
  1875. (cons t
  1876. (list (if (featurep 'xemacs)
  1877. 'mswindows-shell-execute
  1878. 'w32-shell-execute)
  1879. "open" 'file))
  1880. (cons 'system
  1881. (list (if (featurep 'xemacs)
  1882. 'mswindows-shell-execute
  1883. 'w32-shell-execute)
  1884. "open" 'file)))
  1885. "Default file applications on a Windows NT system.
  1886. The system \"open\" is used for most files.
  1887. See `org-file-apps'.")
  1888. (defcustom org-file-apps
  1889. '((auto-mode . emacs)
  1890. ("\\.mm\\'" . default)
  1891. ("\\.x?html?\\'" . default)
  1892. ("\\.pdf\\'" . default))
  1893. "External applications for opening `file:path' items in a document.
  1894. Org-mode uses system defaults for different file types, but
  1895. you can use this variable to set the application for a given file
  1896. extension. The entries in this list are cons cells where the car identifies
  1897. files and the cdr the corresponding command. Possible values for the
  1898. file identifier are
  1899. \"string\" A string as a file identifier can be interpreted in different
  1900. ways, depending on its contents:
  1901. - Alphanumeric characters only:
  1902. Match links with this file extension.
  1903. Example: (\"pdf\" . \"evince %s\")
  1904. to open PDFs with evince.
  1905. - Regular expression: Match links where the
  1906. filename matches the regexp. If you want to
  1907. use groups here, use shy groups.
  1908. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1909. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1910. to open *.html and *.xhtml with firefox.
  1911. - Regular expression which contains (non-shy) groups:
  1912. Match links where the whole link, including \"::\", and
  1913. anything after that, matches the regexp.
  1914. In a custom command string, %1, %2, etc. are replaced with
  1915. the parts of the link that were matched by the groups.
  1916. For backwards compatibility, if a command string is given
  1917. that does not use any of the group matches, this case is
  1918. handled identically to the second one (i.e. match against
  1919. file name only).
  1920. In a custom lisp form, you can access the group matches with
  1921. (match-string n link).
  1922. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1923. to open [[file:document.pdf::5]] with evince at page 5.
  1924. `directory' Matches a directory
  1925. `remote' Matches a remote file, accessible through tramp or efs.
  1926. Remote files most likely should be visited through Emacs
  1927. because external applications cannot handle such paths.
  1928. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1929. so all files Emacs knows how to handle. Using this with
  1930. command `emacs' will open most files in Emacs. Beware that this
  1931. will also open html files inside Emacs, unless you add
  1932. (\"html\" . default) to the list as well.
  1933. t Default for files not matched by any of the other options.
  1934. `system' The system command to open files, like `open' on Windows
  1935. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1936. that will be selected if you call `C-c C-o' with a double
  1937. \\[universal-argument] \\[universal-argument] prefix.
  1938. Possible values for the command are:
  1939. `emacs' The file will be visited by the current Emacs process.
  1940. `default' Use the default application for this file type, which is the
  1941. association for t in the list, most likely in the system-specific
  1942. part.
  1943. This can be used to overrule an unwanted setting in the
  1944. system-specific variable.
  1945. `system' Use the system command for opening files, like \"open\".
  1946. This command is specified by the entry whose car is `system'.
  1947. Most likely, the system-specific version of this variable
  1948. does define this command, but you can overrule/replace it
  1949. here.
  1950. string A command to be executed by a shell; %s will be replaced
  1951. by the path to the file.
  1952. sexp A Lisp form which will be evaluated. The file path will
  1953. be available in the Lisp variable `file'.
  1954. For more examples, see the system specific constants
  1955. `org-file-apps-defaults-macosx'
  1956. `org-file-apps-defaults-windowsnt'
  1957. `org-file-apps-defaults-gnu'."
  1958. :group 'org-link-follow
  1959. :type '(repeat
  1960. (cons (choice :value ""
  1961. (string :tag "Extension")
  1962. (const :tag "System command to open files" system)
  1963. (const :tag "Default for unrecognized files" t)
  1964. (const :tag "Remote file" remote)
  1965. (const :tag "Links to a directory" directory)
  1966. (const :tag "Any files that have Emacs modes"
  1967. auto-mode))
  1968. (choice :value ""
  1969. (const :tag "Visit with Emacs" emacs)
  1970. (const :tag "Use default" default)
  1971. (const :tag "Use the system command" system)
  1972. (string :tag "Command")
  1973. (sexp :tag "Lisp form")))))
  1974. (defcustom org-doi-server-url "http://dx.doi.org/"
  1975. "The URL of the DOI server."
  1976. :type 'string
  1977. :version "24.3"
  1978. :group 'org-link-follow)
  1979. (defgroup org-refile nil
  1980. "Options concerning refiling entries in Org-mode."
  1981. :tag "Org Refile"
  1982. :group 'org)
  1983. (defcustom org-directory "~/org"
  1984. "Directory with org files.
  1985. This is just a default location to look for Org files. There is no need
  1986. at all to put your files into this directory. It is only used in the
  1987. following situations:
  1988. 1. When a capture template specifies a target file that is not an
  1989. absolute path. The path will then be interpreted relative to
  1990. `org-directory'
  1991. 2. When a capture note is filed away in an interactive way (when exiting the
  1992. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1993. with `org-directory' as the default path."
  1994. :group 'org-refile
  1995. :group 'org-capture
  1996. :type 'directory)
  1997. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1998. "Default target for storing notes.
  1999. Used as a fall back file for org-capture.el, for templates that
  2000. do not specify a target file."
  2001. :group 'org-refile
  2002. :group 'org-capture
  2003. :type 'file)
  2004. (defcustom org-goto-interface 'outline
  2005. "The default interface to be used for `org-goto'.
  2006. Allowed values are:
  2007. outline The interface shows an outline of the relevant file
  2008. and the correct heading is found by moving through
  2009. the outline or by searching with incremental search.
  2010. outline-path-completion Headlines in the current buffer are offered via
  2011. completion. This is the interface also used by
  2012. the refile command."
  2013. :group 'org-refile
  2014. :type '(choice
  2015. (const :tag "Outline" outline)
  2016. (const :tag "Outline-path-completion" outline-path-completion)))
  2017. (defcustom org-goto-max-level 5
  2018. "Maximum target level when running `org-goto' with refile interface."
  2019. :group 'org-refile
  2020. :type 'integer)
  2021. (defcustom org-reverse-note-order nil
  2022. "Non-nil means store new notes at the beginning of a file or entry.
  2023. When nil, new notes will be filed to the end of a file or entry.
  2024. This can also be a list with cons cells of regular expressions that
  2025. are matched against file names, and values."
  2026. :group 'org-capture
  2027. :group 'org-refile
  2028. :type '(choice
  2029. (const :tag "Reverse always" t)
  2030. (const :tag "Reverse never" nil)
  2031. (repeat :tag "By file name regexp"
  2032. (cons regexp boolean))))
  2033. (defcustom org-log-refile nil
  2034. "Information to record when a task is refiled.
  2035. Possible values are:
  2036. nil Don't add anything
  2037. time Add a time stamp to the task
  2038. note Prompt for a note and add it with template `org-log-note-headings'
  2039. This option can also be set with on a per-file-basis with
  2040. #+STARTUP: nologrefile
  2041. #+STARTUP: logrefile
  2042. #+STARTUP: lognoterefile
  2043. You can have local logging settings for a subtree by setting the LOGGING
  2044. property to one or more of these keywords.
  2045. When bulk-refiling from the agenda, the value `note' is forbidden and
  2046. will temporarily be changed to `time'."
  2047. :group 'org-refile
  2048. :group 'org-progress
  2049. :version "24.1"
  2050. :type '(choice
  2051. (const :tag "No logging" nil)
  2052. (const :tag "Record timestamp" time)
  2053. (const :tag "Record timestamp with note." note)))
  2054. (defcustom org-refile-targets nil
  2055. "Targets for refiling entries with \\[org-refile].
  2056. This is a list of cons cells. Each cell contains:
  2057. - a specification of the files to be considered, either a list of files,
  2058. or a symbol whose function or variable value will be used to retrieve
  2059. a file name or a list of file names. If you use `org-agenda-files' for
  2060. that, all agenda files will be scanned for targets. Nil means consider
  2061. headings in the current buffer.
  2062. - A specification of how to find candidate refile targets. This may be
  2063. any of:
  2064. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  2065. This tag has to be present in all target headlines, inheritance will
  2066. not be considered.
  2067. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  2068. todo keyword.
  2069. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  2070. headlines that are refiling targets.
  2071. - a cons cell (:level . N). Any headline of level N is considered a target.
  2072. Note that, when `org-odd-levels-only' is set, level corresponds to
  2073. order in hierarchy, not to the number of stars.
  2074. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  2075. Note that, when `org-odd-levels-only' is set, level corresponds to
  2076. order in hierarchy, not to the number of stars.
  2077. Each element of this list generates a set of possible targets.
  2078. The union of these sets is presented (with completion) to
  2079. the user by `org-refile'.
  2080. You can set the variable `org-refile-target-verify-function' to a function
  2081. to verify each headline found by the simple criteria above.
  2082. When this variable is nil, all top-level headlines in the current buffer
  2083. are used, equivalent to the value `((nil . (:level . 1))'."
  2084. :group 'org-refile
  2085. :type '(repeat
  2086. (cons
  2087. (choice :value org-agenda-files
  2088. (const :tag "All agenda files" org-agenda-files)
  2089. (const :tag "Current buffer" nil)
  2090. (function) (variable) (file))
  2091. (choice :tag "Identify target headline by"
  2092. (cons :tag "Specific tag" (const :value :tag) (string))
  2093. (cons :tag "TODO keyword" (const :value :todo) (string))
  2094. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  2095. (cons :tag "Level number" (const :value :level) (integer))
  2096. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  2097. (defcustom org-refile-target-verify-function nil
  2098. "Function to verify if the headline at point should be a refile target.
  2099. The function will be called without arguments, with point at the
  2100. beginning of the headline. It should return t and leave point
  2101. where it is if the headline is a valid target for refiling.
  2102. If the target should not be selected, the function must return nil.
  2103. In addition to this, it may move point to a place from where the search
  2104. should be continued. For example, the function may decide that the entire
  2105. subtree of the current entry should be excluded and move point to the end
  2106. of the subtree."
  2107. :group 'org-refile
  2108. :type '(choice
  2109. (const nil)
  2110. (function)))
  2111. (defcustom org-refile-use-cache nil
  2112. "Non-nil means cache refile targets to speed up the process.
  2113. The cache for a particular file will be updated automatically when
  2114. the buffer has been killed, or when any of the marker used for flagging
  2115. refile targets no longer points at a live buffer.
  2116. If you have added new entries to a buffer that might themselves be targets,
  2117. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  2118. find that easier, `C-u C-u C-u C-c C-w'."
  2119. :group 'org-refile
  2120. :version "24.1"
  2121. :type 'boolean)
  2122. (defcustom org-refile-use-outline-path nil
  2123. "Non-nil means provide refile targets as paths.
  2124. So a level 3 headline will be available as level1/level2/level3.
  2125. When the value is `file', also include the file name (without directory)
  2126. into the path. In this case, you can also stop the completion after
  2127. the file name, to get entries inserted as top level in the file.
  2128. When `full-file-path', include the full file path."
  2129. :group 'org-refile
  2130. :type '(choice
  2131. (const :tag "Not" nil)
  2132. (const :tag "Yes" t)
  2133. (const :tag "Start with file name" file)
  2134. (const :tag "Start with full file path" full-file-path)))
  2135. (defcustom org-outline-path-complete-in-steps t
  2136. "Non-nil means complete the outline path in hierarchical steps.
  2137. When Org-mode uses the refile interface to select an outline path
  2138. \(see variable `org-refile-use-outline-path'), the completion of
  2139. the path can be done in a single go, or it can be done in steps down
  2140. the headline hierarchy. Going in steps is probably the best if you
  2141. do not use a special completion package like `ido' or `icicles'.
  2142. However, when using these packages, going in one step can be very
  2143. fast, while still showing the whole path to the entry."
  2144. :group 'org-refile
  2145. :type 'boolean)
  2146. (defcustom org-refile-allow-creating-parent-nodes nil
  2147. "Non-nil means allow to create new nodes as refile targets.
  2148. New nodes are then created by adding \"/new node name\" to the completion
  2149. of an existing node. When the value of this variable is `confirm',
  2150. new node creation must be confirmed by the user (recommended).
  2151. When nil, the completion must match an existing entry.
  2152. Note that, if the new heading is not seen by the criteria
  2153. listed in `org-refile-targets', multiple instances of the same
  2154. heading would be created by trying again to file under the new
  2155. heading."
  2156. :group 'org-refile
  2157. :type '(choice
  2158. (const :tag "Never" nil)
  2159. (const :tag "Always" t)
  2160. (const :tag "Prompt for confirmation" confirm)))
  2161. (defcustom org-refile-active-region-within-subtree nil
  2162. "Non-nil means also refile active region within a subtree.
  2163. By default `org-refile' doesn't allow refiling regions if they
  2164. don't contain a set of subtrees, but it might be convenient to
  2165. do so sometimes: in that case, the first line of the region is
  2166. converted to a headline before refiling."
  2167. :group 'org-refile
  2168. :version "24.1"
  2169. :type 'boolean)
  2170. (defgroup org-todo nil
  2171. "Options concerning TODO items in Org-mode."
  2172. :tag "Org TODO"
  2173. :group 'org)
  2174. (defgroup org-progress nil
  2175. "Options concerning Progress logging in Org-mode."
  2176. :tag "Org Progress"
  2177. :group 'org-time)
  2178. (defvar org-todo-interpretation-widgets
  2179. '((:tag "Sequence (cycling hits every state)" sequence)
  2180. (:tag "Type (cycling directly to DONE)" type))
  2181. "The available interpretation symbols for customizing `org-todo-keywords'.
  2182. Interested libraries should add to this list.")
  2183. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  2184. "List of TODO entry keyword sequences and their interpretation.
  2185. \\<org-mode-map>This is a list of sequences.
  2186. Each sequence starts with a symbol, either `sequence' or `type',
  2187. indicating if the keywords should be interpreted as a sequence of
  2188. action steps, or as different types of TODO items. The first
  2189. keywords are states requiring action - these states will select a headline
  2190. for inclusion into the global TODO list Org-mode produces. If one of
  2191. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  2192. signify that no further action is necessary. If \"|\" is not found,
  2193. the last keyword is treated as the only DONE state of the sequence.
  2194. The command \\[org-todo] cycles an entry through these states, and one
  2195. additional state where no keyword is present. For details about this
  2196. cycling, see the manual.
  2197. TODO keywords and interpretation can also be set on a per-file basis with
  2198. the special #+SEQ_TODO and #+TYP_TODO lines.
  2199. Each keyword can optionally specify a character for fast state selection
  2200. \(in combination with the variable `org-use-fast-todo-selection')
  2201. and specifiers for state change logging, using the same syntax that
  2202. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  2203. the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  2204. indicates to record a time stamp each time this state is selected.
  2205. Each keyword may also specify if a timestamp or a note should be
  2206. recorded when entering or leaving the state, by adding additional
  2207. characters in the parenthesis after the keyword. This looks like this:
  2208. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  2209. record only the time of the state change. With X and Y being either
  2210. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  2211. Y when leaving the state if and only if the *target* state does not
  2212. define X. You may omit any of the fast-selection key or X or /Y,
  2213. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  2214. For backward compatibility, this variable may also be just a list
  2215. of keywords. In this case the interpretation (sequence or type) will be
  2216. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  2217. :group 'org-todo
  2218. :group 'org-keywords
  2219. :type '(choice
  2220. (repeat :tag "Old syntax, just keywords"
  2221. (string :tag "Keyword"))
  2222. (repeat :tag "New syntax"
  2223. (cons
  2224. (choice
  2225. :tag "Interpretation"
  2226. ;;Quick and dirty way to see
  2227. ;;`org-todo-interpretations'. This takes the
  2228. ;;place of item arguments
  2229. :convert-widget
  2230. (lambda (widget)
  2231. (widget-put widget
  2232. :args (mapcar
  2233. (lambda (x)
  2234. (widget-convert
  2235. (cons 'const x)))
  2236. org-todo-interpretation-widgets))
  2237. widget))
  2238. (repeat
  2239. (string :tag "Keyword"))))))
  2240. (defvar org-todo-keywords-1 nil
  2241. "All TODO and DONE keywords active in a buffer.")
  2242. (make-variable-buffer-local 'org-todo-keywords-1)
  2243. (defvar org-todo-keywords-for-agenda nil)
  2244. (defvar org-done-keywords-for-agenda nil)
  2245. (defvar org-todo-keyword-alist-for-agenda nil)
  2246. (defvar org-tag-alist-for-agenda nil
  2247. "Alist of all tags from all agenda files.")
  2248. (defvar org-tag-groups-alist-for-agenda nil
  2249. "Alist of all groups tags from all current agenda files.")
  2250. (defvar org-tag-groups-alist nil)
  2251. (make-variable-buffer-local 'org-tag-groups-alist)
  2252. (defvar org-agenda-contributing-files nil)
  2253. (defvar org-not-done-keywords nil)
  2254. (make-variable-buffer-local 'org-not-done-keywords)
  2255. (defvar org-done-keywords nil)
  2256. (make-variable-buffer-local 'org-done-keywords)
  2257. (defvar org-todo-heads nil)
  2258. (make-variable-buffer-local 'org-todo-heads)
  2259. (defvar org-todo-sets nil)
  2260. (make-variable-buffer-local 'org-todo-sets)
  2261. (defvar org-todo-log-states nil)
  2262. (make-variable-buffer-local 'org-todo-log-states)
  2263. (defvar org-todo-kwd-alist nil)
  2264. (make-variable-buffer-local 'org-todo-kwd-alist)
  2265. (defvar org-todo-key-alist nil)
  2266. (make-variable-buffer-local 'org-todo-key-alist)
  2267. (defvar org-todo-key-trigger nil)
  2268. (make-variable-buffer-local 'org-todo-key-trigger)
  2269. (defcustom org-todo-interpretation 'sequence
  2270. "Controls how TODO keywords are interpreted.
  2271. This variable is in principle obsolete and is only used for
  2272. backward compatibility, if the interpretation of todo keywords is
  2273. not given already in `org-todo-keywords'. See that variable for
  2274. more information."
  2275. :group 'org-todo
  2276. :group 'org-keywords
  2277. :type '(choice (const sequence)
  2278. (const type)))
  2279. (defcustom org-use-fast-todo-selection t
  2280. "Non-nil means use the fast todo selection scheme with C-c C-t.
  2281. This variable describes if and under what circumstances the cycling
  2282. mechanism for TODO keywords will be replaced by a single-key, direct
  2283. selection scheme.
  2284. When nil, fast selection is never used.
  2285. When the symbol `prefix', it will be used when `org-todo' is called
  2286. with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
  2287. `C-u t' in an agenda buffer.
  2288. When t, fast selection is used by default. In this case, the prefix
  2289. argument forces cycling instead.
  2290. In all cases, the special interface is only used if access keys have
  2291. actually been assigned by the user, i.e. if keywords in the configuration
  2292. are followed by a letter in parenthesis, like TODO(t)."
  2293. :group 'org-todo
  2294. :type '(choice
  2295. (const :tag "Never" nil)
  2296. (const :tag "By default" t)
  2297. (const :tag "Only with C-u C-c C-t" prefix)))
  2298. (defcustom org-provide-todo-statistics t
  2299. "Non-nil means update todo statistics after insert and toggle.
  2300. ALL-HEADLINES means update todo statistics by including headlines
  2301. with no TODO keyword as well, counting them as not done.
  2302. A list of TODO keywords means the same, but skip keywords that are
  2303. not in this list.
  2304. When set to a list of two lists, the first list contains keywords
  2305. to consider as TODO keywords, the second list contains keywords
  2306. to consider as DONE keywords.
  2307. When this is set, todo statistics is updated in the parent of the
  2308. current entry each time a todo state is changed."
  2309. :group 'org-todo
  2310. :type '(choice
  2311. (const :tag "Yes, only for TODO entries" t)
  2312. (const :tag "Yes, including all entries" all-headlines)
  2313. (repeat :tag "Yes, for TODOs in this list"
  2314. (string :tag "TODO keyword"))
  2315. (list :tag "Yes, for TODOs and DONEs in these lists"
  2316. (repeat (string :tag "TODO keyword"))
  2317. (repeat (string :tag "DONE keyword")))
  2318. (other :tag "No TODO statistics" nil)))
  2319. (defcustom org-hierarchical-todo-statistics t
  2320. "Non-nil means TODO statistics covers just direct children.
  2321. When nil, all entries in the subtree are considered.
  2322. This has only an effect if `org-provide-todo-statistics' is set.
  2323. To set this to nil for only a single subtree, use a COOKIE_DATA
  2324. property and include the word \"recursive\" into the value."
  2325. :group 'org-todo
  2326. :type 'boolean)
  2327. (defcustom org-after-todo-state-change-hook nil
  2328. "Hook which is run after the state of a TODO item was changed.
  2329. The new state (a string with a TODO keyword, or nil) is available in the
  2330. Lisp variable `org-state'."
  2331. :group 'org-todo
  2332. :type 'hook)
  2333. (defvar org-blocker-hook nil
  2334. "Hook for functions that are allowed to block a state change.
  2335. Functions in this hook should not modify the buffer.
  2336. Each function gets as its single argument a property list,
  2337. see `org-trigger-hook' for more information about this list.
  2338. If any of the functions in this hook returns nil, the state change
  2339. is blocked.")
  2340. (defvar org-trigger-hook nil
  2341. "Hook for functions that are triggered by a state change.
  2342. Each function gets as its single argument a property list with at
  2343. least the following elements:
  2344. (:type type-of-change :position pos-at-entry-start
  2345. :from old-state :to new-state)
  2346. Depending on the type, more properties may be present.
  2347. This mechanism is currently implemented for:
  2348. TODO state changes
  2349. ------------------
  2350. :type todo-state-change
  2351. :from previous state (keyword as a string), or nil, or a symbol
  2352. 'todo' or 'done', to indicate the general type of state.
  2353. :to new state, like in :from")
  2354. (defcustom org-enforce-todo-dependencies nil
  2355. "Non-nil means undone TODO entries will block switching the parent to DONE.
  2356. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  2357. be blocked if any prior sibling is not yet done.
  2358. Finally, if the parent is blocked because of ordered siblings of its own,
  2359. the child will also be blocked."
  2360. :set (lambda (var val)
  2361. (set var val)
  2362. (if val
  2363. (add-hook 'org-blocker-hook
  2364. 'org-block-todo-from-children-or-siblings-or-parent)
  2365. (remove-hook 'org-blocker-hook
  2366. 'org-block-todo-from-children-or-siblings-or-parent)))
  2367. :group 'org-todo
  2368. :type 'boolean)
  2369. (defcustom org-enforce-todo-checkbox-dependencies nil
  2370. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2371. When this is nil, checkboxes have no influence on switching TODO states.
  2372. When non-nil, you first need to check off all check boxes before the TODO
  2373. entry can be switched to DONE.
  2374. This variable needs to be set before org.el is loaded, and you need to
  2375. restart Emacs after a change to make the change effective. The only way
  2376. to change is while Emacs is running is through the customize interface."
  2377. :set (lambda (var val)
  2378. (set var val)
  2379. (if val
  2380. (add-hook 'org-blocker-hook
  2381. 'org-block-todo-from-checkboxes)
  2382. (remove-hook 'org-blocker-hook
  2383. 'org-block-todo-from-checkboxes)))
  2384. :group 'org-todo
  2385. :type 'boolean)
  2386. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2387. "Non-nil means inserting a TODO heading is treated as state change.
  2388. So when the command \\[org-insert-todo-heading] is used, state change
  2389. logging will apply if appropriate. When nil, the new TODO item will
  2390. be inserted directly, and no logging will take place."
  2391. :group 'org-todo
  2392. :type 'boolean)
  2393. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2394. "Non-nil means switching TODO states with S-cursor counts as state change.
  2395. This is the default behavior. However, setting this to nil allows a
  2396. convenient way to select a TODO state and bypass any logging associated
  2397. with that."
  2398. :group 'org-todo
  2399. :type 'boolean)
  2400. (defcustom org-todo-state-tags-triggers nil
  2401. "Tag changes that should be triggered by TODO state changes.
  2402. This is a list. Each entry is
  2403. (state-change (tag . flag) .......)
  2404. State-change can be a string with a state, and empty string to indicate the
  2405. state that has no TODO keyword, or it can be one of the symbols `todo'
  2406. or `done', meaning any not-done or done state, respectively."
  2407. :group 'org-todo
  2408. :group 'org-tags
  2409. :type '(repeat
  2410. (cons (choice :tag "When changing to"
  2411. (const :tag "Not-done state" todo)
  2412. (const :tag "Done state" done)
  2413. (string :tag "State"))
  2414. (repeat
  2415. (cons :tag "Tag action"
  2416. (string :tag "Tag")
  2417. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2418. (defcustom org-log-done nil
  2419. "Information to record when a task moves to the DONE state.
  2420. Possible values are:
  2421. nil Don't add anything, just change the keyword
  2422. time Add a time stamp to the task
  2423. note Prompt for a note and add it with template `org-log-note-headings'
  2424. This option can also be set with on a per-file-basis with
  2425. #+STARTUP: nologdone
  2426. #+STARTUP: logdone
  2427. #+STARTUP: lognotedone
  2428. You can have local logging settings for a subtree by setting the LOGGING
  2429. property to one or more of these keywords."
  2430. :group 'org-todo
  2431. :group 'org-progress
  2432. :type '(choice
  2433. (const :tag "No logging" nil)
  2434. (const :tag "Record CLOSED timestamp" time)
  2435. (const :tag "Record CLOSED timestamp with note." note)))
  2436. ;; Normalize old uses of org-log-done.
  2437. (cond
  2438. ((eq org-log-done t) (setq org-log-done 'time))
  2439. ((and (listp org-log-done) (memq 'done org-log-done))
  2440. (setq org-log-done 'note)))
  2441. (defcustom org-log-reschedule nil
  2442. "Information to record when the scheduling date of a tasks is modified.
  2443. Possible values are:
  2444. nil Don't add anything, just change the date
  2445. time Add a time stamp to the task
  2446. note Prompt for a note and add it with template `org-log-note-headings'
  2447. This option can also be set with on a per-file-basis with
  2448. #+STARTUP: nologreschedule
  2449. #+STARTUP: logreschedule
  2450. #+STARTUP: lognotereschedule"
  2451. :group 'org-todo
  2452. :group 'org-progress
  2453. :type '(choice
  2454. (const :tag "No logging" nil)
  2455. (const :tag "Record timestamp" time)
  2456. (const :tag "Record timestamp with note." note)))
  2457. (defcustom org-log-redeadline nil
  2458. "Information to record when the deadline date of a tasks is modified.
  2459. Possible values are:
  2460. nil Don't add anything, just change the date
  2461. time Add a time stamp to the task
  2462. note Prompt for a note and add it with template `org-log-note-headings'
  2463. This option can also be set with on a per-file-basis with
  2464. #+STARTUP: nologredeadline
  2465. #+STARTUP: logredeadline
  2466. #+STARTUP: lognoteredeadline
  2467. You can have local logging settings for a subtree by setting the LOGGING
  2468. property to one or more of these keywords."
  2469. :group 'org-todo
  2470. :group 'org-progress
  2471. :type '(choice
  2472. (const :tag "No logging" nil)
  2473. (const :tag "Record timestamp" time)
  2474. (const :tag "Record timestamp with note." note)))
  2475. (defcustom org-log-note-clock-out nil
  2476. "Non-nil means record a note when clocking out of an item.
  2477. This can also be configured on a per-file basis by adding one of
  2478. the following lines anywhere in the buffer:
  2479. #+STARTUP: lognoteclock-out
  2480. #+STARTUP: nolognoteclock-out"
  2481. :group 'org-todo
  2482. :group 'org-progress
  2483. :type 'boolean)
  2484. (defcustom org-log-done-with-time t
  2485. "Non-nil means the CLOSED time stamp will contain date and time.
  2486. When nil, only the date will be recorded."
  2487. :group 'org-progress
  2488. :type 'boolean)
  2489. (defcustom org-log-note-headings
  2490. '((done . "CLOSING NOTE %t")
  2491. (state . "State %-12s from %-12S %t")
  2492. (note . "Note taken on %t")
  2493. (reschedule . "Rescheduled from %S on %t")
  2494. (delschedule . "Not scheduled, was %S on %t")
  2495. (redeadline . "New deadline from %S on %t")
  2496. (deldeadline . "Removed deadline, was %S on %t")
  2497. (refile . "Refiled on %t")
  2498. (clock-out . ""))
  2499. "Headings for notes added to entries.
  2500. The value is an alist, with the car being a symbol indicating the
  2501. note context, and the cdr is the heading to be used. The heading
  2502. may also be the empty string. The following placeholders can be
  2503. used:
  2504. %t a time stamp.
  2505. %T an active time stamp instead the default inactive one
  2506. %d a short-format time stamp.
  2507. %D an active short-format time stamp.
  2508. %s the new TODO state or time stamp (inactive), in double quotes.
  2509. %S the old TODO state or time stamp (inactive), in double quotes.
  2510. %u the user name.
  2511. %U full user name.
  2512. In fact, it is not a good idea to change the `state' entry,
  2513. because Agenda Log mode depends on the format of these entries."
  2514. :group 'org-todo
  2515. :group 'org-progress
  2516. :type '(list :greedy t
  2517. (cons (const :tag "Heading when closing an item" done) string)
  2518. (cons (const :tag
  2519. "Heading when changing todo state (todo sequence only)"
  2520. state) string)
  2521. (cons (const :tag "Heading when just taking a note" note) string)
  2522. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2523. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2524. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2525. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2526. (cons (const :tag "Heading when refiling" refile) string)
  2527. (cons (const :tag "Heading when clocking out" clock-out) string)))
  2528. (unless (assq 'note org-log-note-headings)
  2529. (push '(note . "%t") org-log-note-headings))
  2530. (defcustom org-log-into-drawer nil
  2531. "Non-nil means insert state change notes and time stamps into a drawer.
  2532. When nil, state changes notes will be inserted after the headline and
  2533. any scheduling and clock lines, but not inside a drawer.
  2534. The value of this variable should be the name of the drawer to use.
  2535. LOGBOOK is proposed as the default drawer for this purpose, you can
  2536. also set this to a string to define the drawer of your choice.
  2537. A value of t is also allowed, representing \"LOGBOOK\".
  2538. A value of t or nil can also be set with on a per-file-basis with
  2539. #+STARTUP: logdrawer
  2540. #+STARTUP: nologdrawer
  2541. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2542. will be ignored.
  2543. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2544. a subtree.
  2545. Do not check directly this variable in a Lisp program. Call
  2546. function `org-log-into-drawer' instead."
  2547. :group 'org-todo
  2548. :group 'org-progress
  2549. :type '(choice
  2550. (const :tag "Not into a drawer" nil)
  2551. (const :tag "LOGBOOK" t)
  2552. (string :tag "Other")))
  2553. (org-defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer)
  2554. (defun org-log-into-drawer ()
  2555. "Name of the log drawer, as a string, or nil.
  2556. This is the value of `org-log-into-drawer'. However, if the
  2557. current entry has or inherits a LOG_INTO_DRAWER property, it will
  2558. be used instead of the default value."
  2559. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit t)))
  2560. (cond ((equal p "nil") nil)
  2561. ((equal p "t") "LOGBOOK")
  2562. ((stringp p) p)
  2563. (p "LOGBOOK")
  2564. ((stringp org-log-into-drawer) org-log-into-drawer)
  2565. (org-log-into-drawer "LOGBOOK"))))
  2566. (defcustom org-log-state-notes-insert-after-drawers nil
  2567. "Non-nil means insert state change notes after any drawers in entry.
  2568. Only the drawers that *immediately* follow the headline and the
  2569. deadline/scheduled line are skipped.
  2570. When nil, insert notes right after the heading and perhaps the line
  2571. with deadline/scheduling if present.
  2572. This variable will have no effect if `org-log-into-drawer' is
  2573. set."
  2574. :group 'org-todo
  2575. :group 'org-progress
  2576. :type 'boolean)
  2577. (defcustom org-log-states-order-reversed t
  2578. "Non-nil means the latest state note will be directly after heading.
  2579. When nil, the state change notes will be ordered according to time.
  2580. This option can also be set with on a per-file-basis with
  2581. #+STARTUP: logstatesreversed
  2582. #+STARTUP: nologstatesreversed"
  2583. :group 'org-todo
  2584. :group 'org-progress
  2585. :type 'boolean)
  2586. (defcustom org-todo-repeat-to-state nil
  2587. "The TODO state to which a repeater should return the repeating task.
  2588. By default this is the first task in a TODO sequence, or the previous state
  2589. in a TODO_TYP set. But you can specify another task here.
  2590. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2591. :group 'org-todo
  2592. :version "24.1"
  2593. :type '(choice (const :tag "Head of sequence" nil)
  2594. (string :tag "Specific state")))
  2595. (defcustom org-log-repeat 'time
  2596. "Non-nil means record moving through the DONE state when triggering repeat.
  2597. An auto-repeating task is immediately switched back to TODO when
  2598. marked DONE. If you are not logging state changes (by adding \"@\"
  2599. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2600. record a closing note, there will be no record of the task moving
  2601. through DONE. This variable forces taking a note anyway.
  2602. nil Don't force a record
  2603. time Record a time stamp
  2604. note Prompt for a note and add it with template `org-log-note-headings'
  2605. This option can also be set with on a per-file-basis with
  2606. #+STARTUP: nologrepeat
  2607. #+STARTUP: logrepeat
  2608. #+STARTUP: lognoterepeat
  2609. You can have local logging settings for a subtree by setting the LOGGING
  2610. property to one or more of these keywords."
  2611. :group 'org-todo
  2612. :group 'org-progress
  2613. :type '(choice
  2614. (const :tag "Don't force a record" nil)
  2615. (const :tag "Force recording the DONE state" time)
  2616. (const :tag "Force recording a note with the DONE state" note)))
  2617. (defgroup org-priorities nil
  2618. "Priorities in Org-mode."
  2619. :tag "Org Priorities"
  2620. :group 'org-todo)
  2621. (defcustom org-enable-priority-commands t
  2622. "Non-nil means priority commands are active.
  2623. When nil, these commands will be disabled, so that you never accidentally
  2624. set a priority."
  2625. :group 'org-priorities
  2626. :type 'boolean)
  2627. (defcustom org-highest-priority ?A
  2628. "The highest priority of TODO items. A character like ?A, ?B etc.
  2629. Must have a smaller ASCII number than `org-lowest-priority'."
  2630. :group 'org-priorities
  2631. :type 'character)
  2632. (defcustom org-lowest-priority ?C
  2633. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2634. Must have a larger ASCII number than `org-highest-priority'."
  2635. :group 'org-priorities
  2636. :type 'character)
  2637. (defcustom org-default-priority ?B
  2638. "The default priority of TODO items.
  2639. This is the priority an item gets if no explicit priority is given.
  2640. When starting to cycle on an empty priority the first step in the cycle
  2641. depends on `org-priority-start-cycle-with-default'. The resulting first
  2642. step priority must not exceed the range from `org-highest-priority' to
  2643. `org-lowest-priority' which means that `org-default-priority' has to be
  2644. in this range exclusive or inclusive the range boundaries. Else the
  2645. first step refuses to set the default and the second will fall back
  2646. to (depending on the command used) the highest or lowest priority."
  2647. :group 'org-priorities
  2648. :type 'character)
  2649. (defcustom org-priority-start-cycle-with-default t
  2650. "Non-nil means start with default priority when starting to cycle.
  2651. When this is nil, the first step in the cycle will be (depending on the
  2652. command used) one higher or lower than the default priority.
  2653. See also `org-default-priority'."
  2654. :group 'org-priorities
  2655. :type 'boolean)
  2656. (defcustom org-get-priority-function nil
  2657. "Function to extract the priority from a string.
  2658. The string is normally the headline. If this is nil Org computes the
  2659. priority from the priority cookie like [#A] in the headline. It returns
  2660. an integer, increasing by 1000 for each priority level.
  2661. The user can set a different function here, which should take a string
  2662. as an argument and return the numeric priority."
  2663. :group 'org-priorities
  2664. :version "24.1"
  2665. :type '(choice
  2666. (const nil)
  2667. (function)))
  2668. (defgroup org-time nil
  2669. "Options concerning time stamps and deadlines in Org-mode."
  2670. :tag "Org Time"
  2671. :group 'org)
  2672. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2673. "Number of minutes to round time stamps to.
  2674. These are two values, the first applies when first creating a time stamp.
  2675. The second applies when changing it with the commands `S-up' and `S-down'.
  2676. When changing the time stamp, this means that it will change in steps
  2677. of N minutes, as given by the second value.
  2678. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2679. numbers should be factors of 60, so for example 5, 10, 15.
  2680. When this is larger than 1, you can still force an exact time stamp by using
  2681. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2682. and by using a prefix arg to `S-up/down' to specify the exact number
  2683. of minutes to shift."
  2684. :group 'org-time
  2685. :get (lambda (var) ; Make sure both elements are there
  2686. (if (integerp (default-value var))
  2687. (list (default-value var) 5)
  2688. (default-value var)))
  2689. :type '(list
  2690. (integer :tag "when inserting times")
  2691. (integer :tag "when modifying times")))
  2692. ;; Normalize old customizations of this variable.
  2693. (when (integerp org-time-stamp-rounding-minutes)
  2694. (setq org-time-stamp-rounding-minutes
  2695. (list org-time-stamp-rounding-minutes
  2696. org-time-stamp-rounding-minutes)))
  2697. (defcustom org-display-custom-times nil
  2698. "Non-nil means overlay custom formats over all time stamps.
  2699. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2700. To turn this on on a per-file basis, insert anywhere in the file:
  2701. #+STARTUP: customtime"
  2702. :group 'org-time
  2703. :set 'set-default
  2704. :type 'sexp)
  2705. (make-variable-buffer-local 'org-display-custom-times)
  2706. (defcustom org-time-stamp-custom-formats
  2707. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2708. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2709. These are overlaid over the default ISO format if the variable
  2710. `org-display-custom-times' is set. Time like %H:%M should be at the
  2711. end of the second format. The custom formats are also honored by export
  2712. commands, if custom time display is turned on at the time of export."
  2713. :group 'org-time
  2714. :type 'sexp)
  2715. (defun org-time-stamp-format (&optional long inactive)
  2716. "Get the right format for a time string."
  2717. (let ((f (if long (cdr org-time-stamp-formats)
  2718. (car org-time-stamp-formats))))
  2719. (if inactive
  2720. (concat "[" (substring f 1 -1) "]")
  2721. f)))
  2722. (defcustom org-time-clocksum-format
  2723. '(:days "%dd " :hours "%d" :require-hours t :minutes ":%02d" :require-minutes t)
  2724. "The format string used when creating CLOCKSUM lines.
  2725. This is also used when Org mode generates a time duration.
  2726. The value can be a single format string containing two
  2727. %-sequences, which will be filled with the number of hours and
  2728. minutes in that order.
  2729. Alternatively, the value can be a plist associating any of the
  2730. keys :years, :months, :weeks, :days, :hours or :minutes with
  2731. format strings. The time duration is formatted using only the
  2732. time components that are needed and concatenating the results.
  2733. If a time unit in absent, it falls back to the next smallest
  2734. unit.
  2735. The keys :require-years, :require-months, :require-days,
  2736. :require-weeks, :require-hours, :require-minutes are also
  2737. meaningful. A non-nil value for these keys indicates that the
  2738. corresponding time component should always be included, even if
  2739. its value is 0.
  2740. For example,
  2741. \(:days \"%dd\" :hours \"%d\" :require-hours t :minutes \":%02d\"
  2742. :require-minutes t)
  2743. means durations longer than a day will be expressed in days,
  2744. hours and minutes, and durations less than a day will always be
  2745. expressed in hours and minutes (even for durations less than an
  2746. hour).
  2747. The value
  2748. \(:days \"%dd\" :minutes \"%dm\")
  2749. means durations longer than a day will be expressed in days and
  2750. minutes, and durations less than a day will be expressed entirely
  2751. in minutes (even for durations longer than an hour)."
  2752. :group 'org-time
  2753. :group 'org-clock
  2754. :version "24.4"
  2755. :package-version '(Org . "8.0")
  2756. :type '(choice (string :tag "Format string")
  2757. (set :tag "Plist"
  2758. (group :inline t (const :tag "Years" :years)
  2759. (string :tag "Format string"))
  2760. (group :inline t
  2761. (const :tag "Always show years" :require-years)
  2762. (const t))
  2763. (group :inline t (const :tag "Months" :months)
  2764. (string :tag "Format string"))
  2765. (group :inline t
  2766. (const :tag "Always show months" :require-months)
  2767. (const t))
  2768. (group :inline t (const :tag "Weeks" :weeks)
  2769. (string :tag "Format string"))
  2770. (group :inline t
  2771. (const :tag "Always show weeks" :require-weeks)
  2772. (const t))
  2773. (group :inline t (const :tag "Days" :days)
  2774. (string :tag "Format string"))
  2775. (group :inline t
  2776. (const :tag "Always show days" :require-days)
  2777. (const t))
  2778. (group :inline t (const :tag "Hours" :hours)
  2779. (string :tag "Format string"))
  2780. (group :inline t
  2781. (const :tag "Always show hours" :require-hours)
  2782. (const t))
  2783. (group :inline t (const :tag "Minutes" :minutes)
  2784. (string :tag "Format string"))
  2785. (group :inline t
  2786. (const :tag "Always show minutes" :require-minutes)
  2787. (const t)))))
  2788. (defcustom org-time-clocksum-use-fractional nil
  2789. "When non-nil, \\[org-clock-display] uses fractional times.
  2790. See `org-time-clocksum-format' for more on time clock formats."
  2791. :group 'org-time
  2792. :group 'org-clock
  2793. :version "24.3"
  2794. :type 'boolean)
  2795. (defcustom org-time-clocksum-use-effort-durations nil
  2796. "When non-nil, \\[org-clock-display] uses effort durations.
  2797. E.g. by default, one day is considered to be a 8 hours effort,
  2798. so a task that has been clocked for 16 hours will be displayed
  2799. as during 2 days in the clock display or in the clocktable.
  2800. See `org-effort-durations' on how to set effort durations
  2801. and `org-time-clocksum-format' for more on time clock formats."
  2802. :group 'org-time
  2803. :group 'org-clock
  2804. :version "24.4"
  2805. :package-version '(Org . "8.0")
  2806. :type 'boolean)
  2807. (defcustom org-time-clocksum-fractional-format "%.2f"
  2808. "The format string used when creating CLOCKSUM lines,
  2809. or when Org mode generates a time duration, if
  2810. `org-time-clocksum-use-fractional' is enabled.
  2811. The value can be a single format string containing one
  2812. %-sequence, which will be filled with the number of hours as
  2813. a float.
  2814. Alternatively, the value can be a plist associating any of the
  2815. keys :years, :months, :weeks, :days, :hours or :minutes with
  2816. a format string. The time duration is formatted using the
  2817. largest time unit which gives a non-zero integer part. If all
  2818. specified formats have zero integer part, the smallest time unit
  2819. is used."
  2820. :group 'org-time
  2821. :type '(choice (string :tag "Format string")
  2822. (set (group :inline t (const :tag "Years" :years)
  2823. (string :tag "Format string"))
  2824. (group :inline t (const :tag "Months" :months)
  2825. (string :tag "Format string"))
  2826. (group :inline t (const :tag "Weeks" :weeks)
  2827. (string :tag "Format string"))
  2828. (group :inline t (const :tag "Days" :days)
  2829. (string :tag "Format string"))
  2830. (group :inline t (const :tag "Hours" :hours)
  2831. (string :tag "Format string"))
  2832. (group :inline t (const :tag "Minutes" :minutes)
  2833. (string :tag "Format string")))))
  2834. (defcustom org-deadline-warning-days 14
  2835. "Number of days before expiration during which a deadline becomes active.
  2836. This variable governs the display in sparse trees and in the agenda.
  2837. When 0 or negative, it means use this number (the absolute value of it)
  2838. even if a deadline has a different individual lead time specified.
  2839. Custom commands can set this variable in the options section."
  2840. :group 'org-time
  2841. :group 'org-agenda-daily/weekly
  2842. :type 'integer)
  2843. (defcustom org-scheduled-delay-days 0
  2844. "Number of days before a scheduled item becomes active.
  2845. This variable governs the display in sparse trees and in the agenda.
  2846. The default value (i.e. 0) means: don't delay scheduled item.
  2847. When negative, it means use this number (the absolute value of it)
  2848. even if a scheduled item has a different individual delay time
  2849. specified.
  2850. Custom commands can set this variable in the options section."
  2851. :group 'org-time
  2852. :group 'org-agenda-daily/weekly
  2853. :version "24.4"
  2854. :package-version '(Org . "8.0")
  2855. :type 'integer)
  2856. (defcustom org-read-date-prefer-future t
  2857. "Non-nil means assume future for incomplete date input from user.
  2858. This affects the following situations:
  2859. 1. The user gives a month but not a year.
  2860. For example, if it is April and you enter \"feb 2\", this will be read
  2861. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2862. 2. The user gives a day, but no month.
  2863. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2864. read this as the third of *next* month. However, if you enter \"17\",
  2865. it will be considered as *this* month.
  2866. If you set this variable to the symbol `time', then also the following
  2867. will work:
  2868. 3. If the user gives a time.
  2869. If the time is before now, it will be interpreted as tomorrow.
  2870. Currently none of this works for ISO week specifications.
  2871. When this option is nil, the current day, month and year will always be
  2872. used as defaults.
  2873. See also `org-agenda-jump-prefer-future'."
  2874. :group 'org-time
  2875. :type '(choice
  2876. (const :tag "Never" nil)
  2877. (const :tag "Check month and day" t)
  2878. (const :tag "Check month, day, and time" time)))
  2879. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2880. "Should the agenda jump command prefer the future for incomplete dates?
  2881. The default is to do the same as configured in `org-read-date-prefer-future'.
  2882. But you can also set a deviating value here.
  2883. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2884. :group 'org-agenda
  2885. :group 'org-time
  2886. :version "24.1"
  2887. :type '(choice
  2888. (const :tag "Use org-read-date-prefer-future"
  2889. org-read-date-prefer-future)
  2890. (const :tag "Never" nil)
  2891. (const :tag "Always" t)))
  2892. (defcustom org-read-date-force-compatible-dates t
  2893. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2894. Depending on the system Emacs is running on, certain dates cannot
  2895. be represented with the type used internally to represent time.
  2896. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2897. correctly. Some systems allow for earlier dates, some for later,
  2898. some for both. One way to find out it to insert any date into an
  2899. Org buffer, putting the cursor on the year and hitting S-up and
  2900. S-down to test the range.
  2901. When this variable is set to t, the date/time prompt will not let
  2902. you specify dates outside the 1970-2037 range, so it is certain that
  2903. these dates will work in whatever version of Emacs you are
  2904. running, and also that you can move a file from one Emacs implementation
  2905. to another. WHenever Org is forcing the year for you, it will display
  2906. a message and beep.
  2907. When this variable is nil, Org will check if the date is
  2908. representable in the specific Emacs implementation you are using.
  2909. If not, it will force a year, usually the current year, and beep
  2910. to remind you. Currently this setting is not recommended because
  2911. the likelihood that you will open your Org files in an Emacs that
  2912. has limited date range is not negligible.
  2913. A workaround for this problem is to use diary sexp dates for time
  2914. stamps outside of this range."
  2915. :group 'org-time
  2916. :version "24.1"
  2917. :type 'boolean)
  2918. (defcustom org-read-date-display-live t
  2919. "Non-nil means display current interpretation of date prompt live.
  2920. This display will be in an overlay, in the minibuffer."
  2921. :group 'org-time
  2922. :type 'boolean)
  2923. (defcustom org-read-date-popup-calendar t
  2924. "Non-nil means pop up a calendar when prompting for a date.
  2925. In the calendar, the date can be selected with mouse-1. However, the
  2926. minibuffer will also be active, and you can simply enter the date as well.
  2927. When nil, only the minibuffer will be available."
  2928. :group 'org-time
  2929. :type 'boolean)
  2930. (org-defvaralias 'org-popup-calendar-for-date-prompt
  2931. 'org-read-date-popup-calendar)
  2932. (make-obsolete-variable
  2933. 'org-read-date-minibuffer-setup-hook
  2934. "Set `org-read-date-minibuffer-local-map' instead." "24.4")
  2935. (defcustom org-read-date-minibuffer-setup-hook nil
  2936. "Hook to be used to set up keys for the date/time interface.
  2937. Add key definitions to `minibuffer-local-map', which will be a
  2938. temporary copy.
  2939. WARNING: This option is obsolete, you should use
  2940. `org-read-date-minibuffer-local-map' to set up keys."
  2941. :group 'org-time
  2942. :type 'hook)
  2943. (defcustom org-extend-today-until 0
  2944. "The hour when your day really ends. Must be an integer.
  2945. This has influence for the following applications:
  2946. - When switching the agenda to \"today\". It it is still earlier than
  2947. the time given here, the day recognized as TODAY is actually yesterday.
  2948. - When a date is read from the user and it is still before the time given
  2949. here, the current date and time will be assumed to be yesterday, 23:59.
  2950. Also, timestamps inserted in capture templates follow this rule.
  2951. IMPORTANT: This is a feature whose implementation is and likely will
  2952. remain incomplete. Really, it is only here because past midnight seems to
  2953. be the favorite working time of John Wiegley :-)"
  2954. :group 'org-time
  2955. :type 'integer)
  2956. (defcustom org-use-effective-time nil
  2957. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2958. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2959. \"effective time\" of any timestamps between midnight and 8am will be
  2960. 23:59 of the previous day."
  2961. :group 'org-time
  2962. :version "24.1"
  2963. :type 'boolean)
  2964. (defcustom org-use-last-clock-out-time-as-effective-time nil
  2965. "When non-nil, use the last clock out time for `org-todo'.
  2966. Note that this option has precedence over the combined use of
  2967. `org-use-effective-time' and `org-extend-today-until'."
  2968. :group 'org-time
  2969. :version "24.4"
  2970. :package-version '(Org . "8.0")
  2971. :type 'boolean)
  2972. (defcustom org-edit-timestamp-down-means-later nil
  2973. "Non-nil means S-down will increase the time in a time stamp.
  2974. When nil, S-up will increase."
  2975. :group 'org-time
  2976. :type 'boolean)
  2977. (defcustom org-calendar-follow-timestamp-change t
  2978. "Non-nil means make the calendar window follow timestamp changes.
  2979. When a timestamp is modified and the calendar window is visible, it will be
  2980. moved to the new date."
  2981. :group 'org-time
  2982. :type 'boolean)
  2983. (defgroup org-tags nil
  2984. "Options concerning tags in Org-mode."
  2985. :tag "Org Tags"
  2986. :group 'org)
  2987. (defcustom org-tag-alist nil
  2988. "List of tags allowed in Org-mode files.
  2989. When this list is nil, Org-mode will base TAG input on what is already in the
  2990. buffer.
  2991. The value of this variable is an alist, the car of each entry must be a
  2992. keyword as a string, the cdr may be a character that is used to select
  2993. that tag through the fast-tag-selection interface.
  2994. See the manual for details."
  2995. :group 'org-tags
  2996. :type '(repeat
  2997. (choice
  2998. (cons (string :tag "Tag name")
  2999. (character :tag "Access char"))
  3000. (list :tag "Start radio group"
  3001. (const :startgroup)
  3002. (option (string :tag "Group description")))
  3003. (list :tag "Start tag group, non distinct"
  3004. (const :startgrouptag)
  3005. (option (string :tag "Group description")))
  3006. (list :tag "Group tags delimiter"
  3007. (const :grouptags))
  3008. (list :tag "End radio group"
  3009. (const :endgroup)
  3010. (option (string :tag "Group description")))
  3011. (list :tag "End tag group, non distinct"
  3012. (const :endgrouptag)
  3013. (option (string :tag "Group description")))
  3014. (const :tag "New line" (:newline)))))
  3015. (defcustom org-tag-persistent-alist nil
  3016. "List of tags that will always appear in all Org-mode files.
  3017. This is in addition to any in buffer settings or customizations
  3018. of `org-tag-alist'.
  3019. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  3020. The value of this variable is an alist, the car of each entry must be a
  3021. keyword as a string, the cdr may be a character that is used to select
  3022. that tag through the fast-tag-selection interface.
  3023. See the manual for details.
  3024. To disable these tags on a per-file basis, insert anywhere in the file:
  3025. #+STARTUP: noptag"
  3026. :group 'org-tags
  3027. :type '(repeat
  3028. (choice
  3029. (cons (string :tag "Tag name")
  3030. (character :tag "Access char"))
  3031. (const :tag "Start radio group" (:startgroup))
  3032. (const :tag "Group tags delimiter" (:grouptags))
  3033. (const :tag "End radio group" (:endgroup))
  3034. (const :tag "New line" (:newline)))))
  3035. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  3036. "If non-nil, always offer completion for all tags of all agenda files.
  3037. Instead of customizing this variable directly, you might want to
  3038. set it locally for capture buffers, because there no list of
  3039. tags in that file can be created dynamically (there are none).
  3040. (add-hook 'org-capture-mode-hook
  3041. (lambda ()
  3042. (set (make-local-variable
  3043. 'org-complete-tags-always-offer-all-agenda-tags)
  3044. t)))"
  3045. :group 'org-tags
  3046. :version "24.1"
  3047. :type 'boolean)
  3048. (defvar org-file-tags nil
  3049. "List of tags that can be inherited by all entries in the file.
  3050. The tags will be inherited if the variable `org-use-tag-inheritance'
  3051. says they should be.
  3052. This variable is populated from #+FILETAGS lines.")
  3053. (defcustom org-use-fast-tag-selection 'auto
  3054. "Non-nil means use fast tag selection scheme.
  3055. This is a special interface to select and deselect tags with single keys.
  3056. When nil, fast selection is never used.
  3057. When the symbol `auto', fast selection is used if and only if selection
  3058. characters for tags have been configured, either through the variable
  3059. `org-tag-alist' or through a #+TAGS line in the buffer.
  3060. When t, fast selection is always used and selection keys are assigned
  3061. automatically if necessary."
  3062. :group 'org-tags
  3063. :type '(choice
  3064. (const :tag "Always" t)
  3065. (const :tag "Never" nil)
  3066. (const :tag "When selection characters are configured" auto)))
  3067. (defcustom org-fast-tag-selection-single-key nil
  3068. "Non-nil means fast tag selection exits after first change.
  3069. When nil, you have to press RET to exit it.
  3070. During fast tag selection, you can toggle this flag with `C-c'.
  3071. This variable can also have the value `expert'. In this case, the window
  3072. displaying the tags menu is not even shown, until you press C-c again."
  3073. :group 'org-tags
  3074. :type '(choice
  3075. (const :tag "No" nil)
  3076. (const :tag "Yes" t)
  3077. (const :tag "Expert" expert)))
  3078. (defvar org-fast-tag-selection-include-todo nil
  3079. "Non-nil means fast tags selection interface will also offer TODO states.
  3080. This is an undocumented feature, you should not rely on it.")
  3081. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  3082. "The column to which tags should be indented in a headline.
  3083. If this number is positive, it specifies the column. If it is negative,
  3084. it means that the tags should be flushright to that column. For example,
  3085. -80 works well for a normal 80 character screen.
  3086. When 0, place tags directly after headline text, with only one space in
  3087. between."
  3088. :group 'org-tags
  3089. :type 'integer)
  3090. (defcustom org-auto-align-tags t
  3091. "Non-nil keeps tags aligned when modifying headlines.
  3092. Some operations (i.e. demoting) change the length of a headline and
  3093. therefore shift the tags around. With this option turned on, after
  3094. each such operation the tags are again aligned to `org-tags-column'."
  3095. :group 'org-tags
  3096. :type 'boolean)
  3097. (defcustom org-use-tag-inheritance t
  3098. "Non-nil means tags in levels apply also for sublevels.
  3099. When nil, only the tags directly given in a specific line apply there.
  3100. This may also be a list of tags that should be inherited, or a regexp that
  3101. matches tags that should be inherited. Additional control is possible
  3102. with the variable `org-tags-exclude-from-inheritance' which gives an
  3103. explicit list of tags to be excluded from inheritance, even if the value of
  3104. `org-use-tag-inheritance' would select it for inheritance.
  3105. If this option is t, a match early-on in a tree can lead to a large
  3106. number of matches in the subtree when constructing the agenda or creating
  3107. a sparse tree. If you only want to see the first match in a tree during
  3108. a search, check out the variable `org-tags-match-list-sublevels'."
  3109. :group 'org-tags
  3110. :type '(choice
  3111. (const :tag "Not" nil)
  3112. (const :tag "Always" t)
  3113. (repeat :tag "Specific tags" (string :tag "Tag"))
  3114. (regexp :tag "Tags matched by regexp")))
  3115. (defcustom org-tags-exclude-from-inheritance nil
  3116. "List of tags that should never be inherited.
  3117. This is a way to exclude a few tags from inheritance. For way to do
  3118. the opposite, to actively allow inheritance for selected tags,
  3119. see the variable `org-use-tag-inheritance'."
  3120. :group 'org-tags
  3121. :type '(repeat (string :tag "Tag")))
  3122. (defun org-tag-inherit-p (tag)
  3123. "Check if TAG is one that should be inherited."
  3124. (cond
  3125. ((member tag org-tags-exclude-from-inheritance) nil)
  3126. ((eq org-use-tag-inheritance t) t)
  3127. ((not org-use-tag-inheritance) nil)
  3128. ((stringp org-use-tag-inheritance)
  3129. (string-match org-use-tag-inheritance tag))
  3130. ((listp org-use-tag-inheritance)
  3131. (member tag org-use-tag-inheritance))
  3132. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  3133. (defcustom org-tags-match-list-sublevels t
  3134. "Non-nil means list also sublevels of headlines matching a search.
  3135. This variable applies to tags/property searches, and also to stuck
  3136. projects because this search is based on a tags match as well.
  3137. When set to the symbol `indented', sublevels are indented with
  3138. leading dots.
  3139. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  3140. the sublevels of a headline matching a tag search often also match
  3141. the same search. Listing all of them can create very long lists.
  3142. Setting this variable to nil causes subtrees of a match to be skipped.
  3143. This variable is semi-obsolete and probably should always be true. It
  3144. is better to limit inheritance to certain tags using the variables
  3145. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  3146. :group 'org-tags
  3147. :type '(choice
  3148. (const :tag "No, don't list them" nil)
  3149. (const :tag "Yes, do list them" t)
  3150. (const :tag "List them, indented with leading dots" indented)))
  3151. (defcustom org-tags-sort-function nil
  3152. "When set, tags are sorted using this function as a comparator."
  3153. :group 'org-tags
  3154. :type '(choice
  3155. (const :tag "No sorting" nil)
  3156. (const :tag "Alphabetical" string<)
  3157. (const :tag "Reverse alphabetical" string>)
  3158. (function :tag "Custom function" nil)))
  3159. (defvar org-tags-history nil
  3160. "History of minibuffer reads for tags.")
  3161. (defvar org-last-tags-completion-table nil
  3162. "The last used completion table for tags.")
  3163. (defvar org-after-tags-change-hook nil
  3164. "Hook that is run after the tags in a line have changed.")
  3165. (defgroup org-properties nil
  3166. "Options concerning properties in Org-mode."
  3167. :tag "Org Properties"
  3168. :group 'org)
  3169. (defcustom org-property-format "%-10s %s"
  3170. "How property key/value pairs should be formatted by `indent-line'.
  3171. When `indent-line' hits a property definition, it will format the line
  3172. according to this format, mainly to make sure that the values are
  3173. lined-up with respect to each other."
  3174. :group 'org-properties
  3175. :type 'string)
  3176. (defcustom org-properties-postprocess-alist nil
  3177. "Alist of properties and functions to adjust inserted values.
  3178. Elements of this alist must be of the form
  3179. ([string] [function])
  3180. where [string] must be a property name and [function] must be a
  3181. lambda expression: this lambda expression must take one argument,
  3182. the value to adjust, and return the new value as a string.
  3183. For example, this element will allow the property \"Remaining\"
  3184. to be updated wrt the relation between the \"Effort\" property
  3185. and the clock summary:
  3186. ((\"Remaining\" (lambda(value)
  3187. (let ((clocksum (org-clock-sum-current-item))
  3188. (effort (org-duration-string-to-minutes
  3189. (org-entry-get (point) \"Effort\"))))
  3190. (org-minutes-to-clocksum-string (- effort clocksum))))))"
  3191. :group 'org-properties
  3192. :version "24.1"
  3193. :type '(alist :key-type (string :tag "Property")
  3194. :value-type (function :tag "Function")))
  3195. (defcustom org-use-property-inheritance nil
  3196. "Non-nil means properties apply also for sublevels.
  3197. This setting is chiefly used during property searches. Turning it on can
  3198. cause significant overhead when doing a search, which is why it is not
  3199. on by default.
  3200. When nil, only the properties directly given in the current entry count.
  3201. When t, every property is inherited. The value may also be a list of
  3202. properties that should have inheritance, or a regular expression matching
  3203. properties that should be inherited.
  3204. However, note that some special properties use inheritance under special
  3205. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  3206. and the properties ending in \"_ALL\" when they are used as descriptor
  3207. for valid values of a property.
  3208. Note for programmers:
  3209. When querying an entry with `org-entry-get', you can control if inheritance
  3210. should be used. By default, `org-entry-get' looks only at the local
  3211. properties. You can request inheritance by setting the inherit argument
  3212. to t (to force inheritance) or to `selective' (to respect the setting
  3213. in this variable)."
  3214. :group 'org-properties
  3215. :type '(choice
  3216. (const :tag "Not" nil)
  3217. (const :tag "Always" t)
  3218. (repeat :tag "Specific properties" (string :tag "Property"))
  3219. (regexp :tag "Properties matched by regexp")))
  3220. (defun org-property-inherit-p (property)
  3221. "Check if PROPERTY is one that should be inherited."
  3222. (cond
  3223. ((eq org-use-property-inheritance t) t)
  3224. ((not org-use-property-inheritance) nil)
  3225. ((stringp org-use-property-inheritance)
  3226. (string-match org-use-property-inheritance property))
  3227. ((listp org-use-property-inheritance)
  3228. (member property org-use-property-inheritance))
  3229. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  3230. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  3231. "The default column format, if no other format has been defined.
  3232. This variable can be set on the per-file basis by inserting a line
  3233. #+COLUMNS: %25ITEM ....."
  3234. :group 'org-properties
  3235. :type 'string)
  3236. (defcustom org-columns-ellipses ".."
  3237. "The ellipses to be used when a field in column view is truncated.
  3238. When this is the empty string, as many characters as possible are shown,
  3239. but then there will be no visual indication that the field has been truncated.
  3240. When this is a string of length N, the last N characters of a truncated
  3241. field are replaced by this string. If the column is narrower than the
  3242. ellipses string, only part of the ellipses string will be shown."
  3243. :group 'org-properties
  3244. :type 'string)
  3245. (defcustom org-columns-modify-value-for-display-function nil
  3246. "Function that modifies values for display in column view.
  3247. For example, it can be used to cut out a certain part from a time stamp.
  3248. The function must take 2 arguments:
  3249. column-title The title of the column (*not* the property name)
  3250. value The value that should be modified.
  3251. The function should return the value that should be displayed,
  3252. or nil if the normal value should be used."
  3253. :group 'org-properties
  3254. :type '(choice (const nil) (function)))
  3255. (defconst org-global-properties-fixed
  3256. '(("VISIBILITY_ALL" . "folded children content all")
  3257. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  3258. "List of property/value pairs that can be inherited by any entry.
  3259. These are fixed values, for the preset properties. The user variable
  3260. that can be used to add to this list is `org-global-properties'.
  3261. The entries in this list are cons cells where the car is a property
  3262. name and cdr is a string with the value. If the value represents
  3263. multiple items like an \"_ALL\" property, separate the items by
  3264. spaces.")
  3265. (defcustom org-global-properties nil
  3266. "List of property/value pairs that can be inherited by any entry.
  3267. This list will be combined with the constant `org-global-properties-fixed'.
  3268. The entries in this list are cons cells where the car is a property
  3269. name and cdr is a string with the value.
  3270. You can set buffer-local values for the same purpose in the variable
  3271. `org-file-properties' this by adding lines like
  3272. #+PROPERTY: NAME VALUE"
  3273. :group 'org-properties
  3274. :type '(repeat
  3275. (cons (string :tag "Property")
  3276. (string :tag "Value"))))
  3277. (defvar org-file-properties nil
  3278. "List of property/value pairs that can be inherited by any entry.
  3279. Valid for the current buffer.
  3280. This variable is populated from #+PROPERTY lines.")
  3281. (make-variable-buffer-local 'org-file-properties)
  3282. (defgroup org-agenda nil
  3283. "Options concerning agenda views in Org-mode."
  3284. :tag "Org Agenda"
  3285. :group 'org)
  3286. (defvar org-category nil
  3287. "Variable used by org files to set a category for agenda display.
  3288. Such files should use a file variable to set it, for example
  3289. # -*- mode: org; org-category: \"ELisp\"
  3290. or contain a special line
  3291. #+CATEGORY: ELisp
  3292. If the file does not specify a category, then file's base name
  3293. is used instead.")
  3294. (make-variable-buffer-local 'org-category)
  3295. (put 'org-category 'safe-local-variable (lambda (x) (or (symbolp x) (stringp x))))
  3296. (defcustom org-agenda-files nil
  3297. "The files to be used for agenda display.
  3298. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  3299. \\[org-remove-file]. You can also use customize to edit the list.
  3300. If an entry is a directory, all files in that directory that are matched by
  3301. `org-agenda-file-regexp' will be part of the file list.
  3302. If the value of the variable is not a list but a single file name, then
  3303. the list of agenda files is actually stored and maintained in that file, one
  3304. agenda file per line. In this file paths can be given relative to
  3305. `org-directory'. Tilde expansion and environment variable substitution
  3306. are also made."
  3307. :group 'org-agenda
  3308. :type '(choice
  3309. (repeat :tag "List of files and directories" file)
  3310. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  3311. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  3312. "Regular expression to match files for `org-agenda-files'.
  3313. If any element in the list in that variable contains a directory instead
  3314. of a normal file, all files in that directory that are matched by this
  3315. regular expression will be included."
  3316. :group 'org-agenda
  3317. :type 'regexp)
  3318. (defcustom org-agenda-text-search-extra-files nil
  3319. "List of extra files to be searched by text search commands.
  3320. These files will be searched in addition to the agenda files by the
  3321. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  3322. Note that these files will only be searched for text search commands,
  3323. not for the other agenda views like todo lists, tag searches or the weekly
  3324. agenda. This variable is intended to list notes and possibly archive files
  3325. that should also be searched by these two commands.
  3326. In fact, if the first element in the list is the symbol `agenda-archives',
  3327. then all archive files of all agenda files will be added to the search
  3328. scope."
  3329. :group 'org-agenda
  3330. :type '(set :greedy t
  3331. (const :tag "Agenda Archives" agenda-archives)
  3332. (repeat :inline t (file))))
  3333. (org-defvaralias 'org-agenda-multi-occur-extra-files
  3334. 'org-agenda-text-search-extra-files)
  3335. (defcustom org-agenda-skip-unavailable-files nil
  3336. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  3337. A nil value means to remove them, after a query, from the list."
  3338. :group 'org-agenda
  3339. :type 'boolean)
  3340. (defcustom org-calendar-to-agenda-key [?c]
  3341. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  3342. The command `org-calendar-goto-agenda' will be bound to this key. The
  3343. default is the character `c' because then `c' can be used to switch back and
  3344. forth between agenda and calendar."
  3345. :group 'org-agenda
  3346. :type 'sexp)
  3347. (defcustom org-calendar-insert-diary-entry-key [?i]
  3348. "The key to be installed in `calendar-mode-map' for adding diary entries.
  3349. This option is irrelevant until `org-agenda-diary-file' has been configured
  3350. to point to an Org-mode file. When that is the case, the command
  3351. `org-agenda-diary-entry' will be bound to the key given here, by default
  3352. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  3353. if you want to continue doing this, you need to change this to a different
  3354. key."
  3355. :group 'org-agenda
  3356. :type 'sexp)
  3357. (defcustom org-agenda-diary-file 'diary-file
  3358. "File to which to add new entries with the `i' key in agenda and calendar.
  3359. When this is the symbol `diary-file', the functionality in the Emacs
  3360. calendar will be used to add entries to the `diary-file'. But when this
  3361. points to a file, `org-agenda-diary-entry' will be used instead."
  3362. :group 'org-agenda
  3363. :type '(choice
  3364. (const :tag "The standard Emacs diary file" diary-file)
  3365. (file :tag "Special Org file diary entries")))
  3366. (eval-after-load "calendar"
  3367. '(progn
  3368. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  3369. 'org-calendar-goto-agenda)
  3370. (add-hook 'calendar-mode-hook
  3371. (lambda ()
  3372. (unless (eq org-agenda-diary-file 'diary-file)
  3373. (define-key calendar-mode-map
  3374. org-calendar-insert-diary-entry-key
  3375. 'org-agenda-diary-entry))))))
  3376. (defgroup org-latex nil
  3377. "Options for embedding LaTeX code into Org-mode."
  3378. :tag "Org LaTeX"
  3379. :group 'org)
  3380. (defcustom org-format-latex-options
  3381. '(:foreground default :background default :scale 1.0
  3382. :html-foreground "Black" :html-background "Transparent"
  3383. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  3384. "Options for creating images from LaTeX fragments.
  3385. This is a property list with the following properties:
  3386. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  3387. `default' means use the foreground of the default face.
  3388. `auto' means use the foreground from the text face.
  3389. :background the background color, or \"Transparent\".
  3390. `default' means use the background of the default face.
  3391. `auto' means use the background from the text face.
  3392. :scale a scaling factor for the size of the images, to get more pixels
  3393. :html-foreground, :html-background, :html-scale
  3394. the same numbers for HTML export.
  3395. :matchers a list indicating which matchers should be used to
  3396. find LaTeX fragments. Valid members of this list are:
  3397. \"begin\" find environments
  3398. \"$1\" find single characters surrounded by $.$
  3399. \"$\" find math expressions surrounded by $...$
  3400. \"$$\" find math expressions surrounded by $$....$$
  3401. \"\\(\" find math expressions surrounded by \\(...\\)
  3402. \"\\=\\[\" find math expressions surrounded by \\=\\[...\\]"
  3403. :group 'org-latex
  3404. :type 'plist)
  3405. (defcustom org-format-latex-signal-error t
  3406. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  3407. When nil, just push out a message."
  3408. :group 'org-latex
  3409. :version "24.1"
  3410. :type 'boolean)
  3411. (defcustom org-latex-to-mathml-jar-file nil
  3412. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  3413. Use this to specify additional executable file say a jar file.
  3414. When using MathToWeb as the converter, specify the full-path to
  3415. your mathtoweb.jar file."
  3416. :group 'org-latex
  3417. :version "24.1"
  3418. :type '(choice
  3419. (const :tag "None" nil)
  3420. (file :tag "JAR file" :must-match t)))
  3421. (defcustom org-latex-to-mathml-convert-command nil
  3422. "Command to convert LaTeX fragments to MathML.
  3423. Replace format-specifiers in the command as noted below and use
  3424. `shell-command' to convert LaTeX to MathML.
  3425. %j: Executable file in fully expanded form as specified by
  3426. `org-latex-to-mathml-jar-file'.
  3427. %I: Input LaTeX file in fully expanded form.
  3428. %i: The latex fragment to be converted.
  3429. %o: Output MathML file.
  3430. This command is used by `org-create-math-formula'.
  3431. When using MathToWeb as the converter, set this option to
  3432. \"java -jar %j -unicode -force -df %o %I\".
  3433. When using LaTeXML set this option to
  3434. \"latexmlmath \"%i\" --presentationmathml=%o\"."
  3435. :group 'org-latex
  3436. :version "24.1"
  3437. :type '(choice
  3438. (const :tag "None" nil)
  3439. (string :tag "\nShell command")))
  3440. (defcustom org-latex-create-formula-image-program 'dvipng
  3441. "Program to convert LaTeX fragments with.
  3442. dvipng Process the LaTeX fragments to dvi file, then convert
  3443. dvi files to png files using dvipng.
  3444. This will also include processing of non-math environments.
  3445. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  3446. to convert pdf files to png files"
  3447. :group 'org-latex
  3448. :version "24.1"
  3449. :type '(choice
  3450. (const :tag "dvipng" dvipng)
  3451. (const :tag "imagemagick" imagemagick)))
  3452. (defcustom org-latex-preview-ltxpng-directory "ltxpng/"
  3453. "Path to store latex preview images.
  3454. A relative path here creates many directories relative to the
  3455. processed org files paths. An absolute path puts all preview
  3456. images at the same place."
  3457. :group 'org-latex
  3458. :version "24.3"
  3459. :type 'string)
  3460. (defun org-format-latex-mathml-available-p ()
  3461. "Return t if `org-latex-to-mathml-convert-command' is usable."
  3462. (save-match-data
  3463. (when (and (boundp 'org-latex-to-mathml-convert-command)
  3464. org-latex-to-mathml-convert-command)
  3465. (let ((executable (car (split-string
  3466. org-latex-to-mathml-convert-command))))
  3467. (when (executable-find executable)
  3468. (if (string-match
  3469. "%j" org-latex-to-mathml-convert-command)
  3470. (file-readable-p org-latex-to-mathml-jar-file)
  3471. t))))))
  3472. (defcustom org-format-latex-header "\\documentclass{article}
  3473. \\usepackage[usenames]{color}
  3474. \[PACKAGES]
  3475. \[DEFAULT-PACKAGES]
  3476. \\pagestyle{empty} % do not remove
  3477. % The settings below are copied from fullpage.sty
  3478. \\setlength{\\textwidth}{\\paperwidth}
  3479. \\addtolength{\\textwidth}{-3cm}
  3480. \\setlength{\\oddsidemargin}{1.5cm}
  3481. \\addtolength{\\oddsidemargin}{-2.54cm}
  3482. \\setlength{\\evensidemargin}{\\oddsidemargin}
  3483. \\setlength{\\textheight}{\\paperheight}
  3484. \\addtolength{\\textheight}{-\\headheight}
  3485. \\addtolength{\\textheight}{-\\headsep}
  3486. \\addtolength{\\textheight}{-\\footskip}
  3487. \\addtolength{\\textheight}{-3cm}
  3488. \\setlength{\\topmargin}{1.5cm}
  3489. \\addtolength{\\topmargin}{-2.54cm}"
  3490. "The document header used for processing LaTeX fragments.
  3491. It is imperative that this header make sure that no page number
  3492. appears on the page. The package defined in the variables
  3493. `org-latex-default-packages-alist' and `org-latex-packages-alist'
  3494. will either replace the placeholder \"[PACKAGES]\" in this
  3495. header, or they will be appended."
  3496. :group 'org-latex
  3497. :type 'string)
  3498. (defun org-set-packages-alist (var val)
  3499. "Set the packages alist and make sure it has 3 elements per entry."
  3500. (set var (mapcar (lambda (x)
  3501. (if (and (consp x) (= (length x) 2))
  3502. (list (car x) (nth 1 x) t)
  3503. x))
  3504. val)))
  3505. (defun org-get-packages-alist (var)
  3506. "Get the packages alist and make sure it has 3 elements per entry."
  3507. (mapcar (lambda (x)
  3508. (if (and (consp x) (= (length x) 2))
  3509. (list (car x) (nth 1 x) t)
  3510. x))
  3511. (default-value var)))
  3512. (defcustom org-latex-default-packages-alist
  3513. '(("AUTO" "inputenc" t)
  3514. ("T1" "fontenc" t)
  3515. ("" "graphicx" t)
  3516. ("" "grffile" t)
  3517. ("" "longtable" nil)
  3518. ("" "wrapfig" nil)
  3519. ("" "rotating" nil)
  3520. ("normalem" "ulem" t)
  3521. ("" "amsmath" t)
  3522. ("" "textcomp" t)
  3523. ("" "amssymb" t)
  3524. ("" "capt-of" nil)
  3525. ("" "hyperref" nil))
  3526. "Alist of default packages to be inserted in the header.
  3527. Change this only if one of the packages here causes an
  3528. incompatibility with another package you are using.
  3529. The packages in this list are needed by one part or another of
  3530. Org mode to function properly:
  3531. - inputenc, fontenc: for basic font and character selection
  3532. - graphicx: for including images
  3533. - grffile: allow periods and spaces in graphics file names
  3534. - longtable: For multipage tables
  3535. - wrapfig: for figure placement
  3536. - rotating: for sideways figures and tables
  3537. - ulem: for underline and strike-through
  3538. - amsmath: for subscript and superscript and math environments
  3539. - textcomp, amssymb: for various symbols used
  3540. for interpreting the entities in `org-entities'. You can skip
  3541. some of these packages if you don't use any of their symbols.
  3542. - capt-of: for captions outside of floats
  3543. - hyperref: for cross references
  3544. Therefore you should not modify this variable unless you know
  3545. what you are doing. The one reason to change it anyway is that
  3546. you might be loading some other package that conflicts with one
  3547. of the default packages. Each element is either a cell or
  3548. a string.
  3549. A cell is of the format
  3550. \(\"options\" \"package\" SNIPPET-FLAG)
  3551. If SNIPPET-FLAG is non-nil, the package also needs to be included
  3552. when compiling LaTeX snippets into images for inclusion into
  3553. non-LaTeX output.
  3554. A string will be inserted as-is in the header of the document."
  3555. :group 'org-latex
  3556. :group 'org-export-latex
  3557. :set 'org-set-packages-alist
  3558. :get 'org-get-packages-alist
  3559. :version "25.1"
  3560. :package-version '(Org . "8.3")
  3561. :type '(repeat
  3562. (choice
  3563. (list :tag "options/package pair"
  3564. (string :tag "options")
  3565. (string :tag "package")
  3566. (boolean :tag "Snippet"))
  3567. (string :tag "A line of LaTeX"))))
  3568. (defcustom org-latex-packages-alist nil
  3569. "Alist of packages to be inserted in every LaTeX header.
  3570. These will be inserted after `org-latex-default-packages-alist'.
  3571. Each element is either a cell or a string.
  3572. A cell is of the format:
  3573. \(\"options\" \"package\" SNIPPET-FLAG)
  3574. SNIPPET-FLAG, when non-nil, indicates that this package is also
  3575. needed when turning LaTeX snippets into images for inclusion into
  3576. non-LaTeX output.
  3577. A string will be inserted as-is in the header of the document.
  3578. Make sure that you only list packages here which:
  3579. - you want in every file;
  3580. - do not conflict with the setup in `org-format-latex-header';
  3581. - do not conflict with the default packages in
  3582. `org-latex-default-packages-alist'."
  3583. :group 'org-latex
  3584. :group 'org-export-latex
  3585. :set 'org-set-packages-alist
  3586. :get 'org-get-packages-alist
  3587. :type '(repeat
  3588. (choice
  3589. (list :tag "options/package pair"
  3590. (string :tag "options")
  3591. (string :tag "package")
  3592. (boolean :tag "Snippet"))
  3593. (string :tag "A line of LaTeX"))))
  3594. (defgroup org-appearance nil
  3595. "Settings for Org-mode appearance."
  3596. :tag "Org Appearance"
  3597. :group 'org)
  3598. (defcustom org-level-color-stars-only nil
  3599. "Non-nil means fontify only the stars in each headline.
  3600. When nil, the entire headline is fontified.
  3601. Changing it requires restart of `font-lock-mode' to become effective
  3602. also in regions already fontified."
  3603. :group 'org-appearance
  3604. :type 'boolean)
  3605. (defcustom org-hide-leading-stars nil
  3606. "Non-nil means hide the first N-1 stars in a headline.
  3607. This works by using the face `org-hide' for these stars. This
  3608. face is white for a light background, and black for a dark
  3609. background. You may have to customize the face `org-hide' to
  3610. make this work.
  3611. Changing it requires restart of `font-lock-mode' to become effective
  3612. also in regions already fontified.
  3613. You may also set this on a per-file basis by adding one of the following
  3614. lines to the buffer:
  3615. #+STARTUP: hidestars
  3616. #+STARTUP: showstars"
  3617. :group 'org-appearance
  3618. :type 'boolean)
  3619. (defcustom org-hidden-keywords nil
  3620. "List of symbols corresponding to keywords to be hidden the org buffer.
  3621. For example, a value '(title) for this list will make the document's title
  3622. appear in the buffer without the initial #+TITLE: keyword."
  3623. :group 'org-appearance
  3624. :version "24.1"
  3625. :type '(set (const :tag "#+AUTHOR" author)
  3626. (const :tag "#+DATE" date)
  3627. (const :tag "#+EMAIL" email)
  3628. (const :tag "#+TITLE" title)))
  3629. (defcustom org-custom-properties nil
  3630. "List of properties (as strings) with a special meaning.
  3631. The default use of these custom properties is to let the user
  3632. hide them with `org-toggle-custom-properties-visibility'."
  3633. :group 'org-properties
  3634. :group 'org-appearance
  3635. :version "24.3"
  3636. :type '(repeat (string :tag "Property Name")))
  3637. (defcustom org-fontify-done-headline nil
  3638. "Non-nil means change the face of a headline if it is marked DONE.
  3639. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3640. When this is non-nil, the headline after the keyword is set to the
  3641. `org-headline-done' as an additional indication."
  3642. :group 'org-appearance
  3643. :type 'boolean)
  3644. (defcustom org-fontify-emphasized-text t
  3645. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3646. Changing this variable requires a restart of Emacs to take effect."
  3647. :group 'org-appearance
  3648. :type 'boolean)
  3649. (defcustom org-fontify-whole-heading-line nil
  3650. "Non-nil means fontify the whole line for headings.
  3651. This is useful when setting a background color for the
  3652. org-level-* faces."
  3653. :group 'org-appearance
  3654. :type 'boolean)
  3655. (defcustom org-highlight-latex-and-related nil
  3656. "Non-nil means highlight LaTeX related syntax in the buffer.
  3657. When non nil, the value should be a list containing any of the
  3658. following symbols:
  3659. `latex' Highlight LaTeX snippets and environments.
  3660. `script' Highlight subscript and superscript.
  3661. `entities' Highlight entities."
  3662. :group 'org-appearance
  3663. :version "24.4"
  3664. :package-version '(Org . "8.0")
  3665. :type '(choice
  3666. (const :tag "No highlighting" nil)
  3667. (set :greedy t :tag "Highlight"
  3668. (const :tag "LaTeX snippets and environments" latex)
  3669. (const :tag "Subscript and superscript" script)
  3670. (const :tag "Entities" entities))))
  3671. (defcustom org-hide-emphasis-markers nil
  3672. "Non-nil mean font-lock should hide the emphasis marker characters."
  3673. :group 'org-appearance
  3674. :type 'boolean)
  3675. (defcustom org-hide-macro-markers nil
  3676. "Non-nil mean font-lock should hide the brackets marking macro calls."
  3677. :group 'org-appearance
  3678. :type 'boolean)
  3679. (defcustom org-pretty-entities nil
  3680. "Non-nil means show entities as UTF8 characters.
  3681. When nil, the \\name form remains in the buffer."
  3682. :group 'org-appearance
  3683. :version "24.1"
  3684. :type 'boolean)
  3685. (defcustom org-pretty-entities-include-sub-superscripts t
  3686. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3687. :group 'org-appearance
  3688. :version "24.1"
  3689. :type 'boolean)
  3690. (defvar org-emph-re nil
  3691. "Regular expression for matching emphasis.
  3692. After a match, the match groups contain these elements:
  3693. 0 The match of the full regular expression, including the characters
  3694. before and after the proper match
  3695. 1 The character before the proper match, or empty at beginning of line
  3696. 2 The proper match, including the leading and trailing markers
  3697. 3 The leading marker like * or /, indicating the type of highlighting
  3698. 4 The text between the emphasis markers, not including the markers
  3699. 5 The character after the match, empty at the end of a line")
  3700. (defvar org-verbatim-re nil
  3701. "Regular expression for matching verbatim text.")
  3702. (defvar org-emphasis-regexp-components) ; defined just below
  3703. (defvar org-emphasis-alist) ; defined just below
  3704. (defun org-set-emph-re (var val)
  3705. "Set variable and compute the emphasis regular expression."
  3706. (set var val)
  3707. (when (and (boundp 'org-emphasis-alist)
  3708. (boundp 'org-emphasis-regexp-components)
  3709. org-emphasis-alist org-emphasis-regexp-components)
  3710. (let* ((e org-emphasis-regexp-components)
  3711. (pre (car e))
  3712. (post (nth 1 e))
  3713. (border (nth 2 e))
  3714. (body (nth 3 e))
  3715. (nl (nth 4 e))
  3716. (body1 (concat body "*?"))
  3717. (markers (mapconcat 'car org-emphasis-alist ""))
  3718. (vmarkers (mapconcat
  3719. (lambda (x) (if (eq (nth 2 x) 'verbatim) (car x) ""))
  3720. org-emphasis-alist "")))
  3721. ;; make sure special characters appear at the right position in the class
  3722. (if (string-match "\\^" markers)
  3723. (setq markers (concat (replace-match "" t t markers) "^")))
  3724. (if (string-match "-" markers)
  3725. (setq markers (concat (replace-match "" t t markers) "-")))
  3726. (if (string-match "\\^" vmarkers)
  3727. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3728. (if (string-match "-" vmarkers)
  3729. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3730. (if (> nl 0)
  3731. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3732. (int-to-string nl) "\\}")))
  3733. ;; Make the regexp
  3734. (setq org-emph-re
  3735. (concat "\\([" pre "]\\|^\\)"
  3736. "\\("
  3737. "\\([" markers "]\\)"
  3738. "\\("
  3739. "[^" border "]\\|"
  3740. "[^" border "]"
  3741. body1
  3742. "[^" border "]"
  3743. "\\)"
  3744. "\\3\\)"
  3745. "\\([" post "]\\|$\\)"))
  3746. (setq org-verbatim-re
  3747. (concat "\\([" pre "]\\|^\\)"
  3748. "\\("
  3749. "\\([" vmarkers "]\\)"
  3750. "\\("
  3751. "[^" border "]\\|"
  3752. "[^" border "]"
  3753. body1
  3754. "[^" border "]"
  3755. "\\)"
  3756. "\\3\\)"
  3757. "\\([" post "]\\|$\\)")))))
  3758. ;; This used to be a defcustom (Org <8.0) but allowing the users to
  3759. ;; set this option proved cumbersome. See this message/thread:
  3760. ;; http://article.gmane.org/gmane.emacs.orgmode/68681
  3761. (defvar org-emphasis-regexp-components
  3762. '(" \t('\"{" "- \t.,:!?;'\")}\\[" " \t\r\n,\"'" "." 1)
  3763. "Components used to build the regular expression for emphasis.
  3764. This is a list with five entries. Terminology: In an emphasis string
  3765. like \" *strong word* \", we call the initial space PREMATCH, the final
  3766. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3767. and \"trong wor\" is the body. The different components in this variable
  3768. specify what is allowed/forbidden in each part:
  3769. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3770. post Chars allowed as postmatch. End of line will be allowed too.
  3771. border The chars *forbidden* as border characters.
  3772. body-regexp A regexp like \".\" to match a body character. Don't use
  3773. non-shy groups here, and don't allow newline here.
  3774. newline The maximum number of newlines allowed in an emphasis exp.
  3775. You need to reload Org or to restart Emacs after customizing this.")
  3776. (defcustom org-emphasis-alist
  3777. `(("*" bold)
  3778. ("/" italic)
  3779. ("_" underline)
  3780. ("=" org-verbatim verbatim)
  3781. ("~" org-code verbatim)
  3782. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))))
  3783. "Alist of characters and faces to emphasize text.
  3784. Text starting and ending with a special character will be emphasized,
  3785. for example *bold*, _underlined_ and /italic/. This variable sets the
  3786. marker characters and the face to be used by font-lock for highlighting
  3787. in Org-mode Emacs buffers.
  3788. You need to reload Org or to restart Emacs after customizing this."
  3789. :group 'org-appearance
  3790. :set 'org-set-emph-re
  3791. :version "24.4"
  3792. :package-version '(Org . "8.0")
  3793. :type '(repeat
  3794. (list
  3795. (string :tag "Marker character")
  3796. (choice
  3797. (face :tag "Font-lock-face")
  3798. (plist :tag "Face property list"))
  3799. (option (const verbatim)))))
  3800. (defvar org-protecting-blocks
  3801. '("src" "example" "latex" "ascii" "html" "ditaa" "dot" "r" "R")
  3802. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3803. This is needed for font-lock setup.")
  3804. ;;; Miscellaneous options
  3805. (defgroup org-completion nil
  3806. "Completion in Org-mode."
  3807. :tag "Org Completion"
  3808. :group 'org)
  3809. (defcustom org-completion-use-ido nil
  3810. "Non-nil means use ido completion wherever possible.
  3811. Note that `ido-mode' must be active for this variable to be relevant.
  3812. If you decide to turn this variable on, you might well want to turn off
  3813. `org-outline-path-complete-in-steps'.
  3814. See also `org-completion-use-iswitchb'."
  3815. :group 'org-completion
  3816. :type 'boolean)
  3817. (defcustom org-completion-use-iswitchb nil
  3818. "Non-nil means use iswitchb completion wherever possible.
  3819. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3820. If you decide to turn this variable on, you might well want to turn off
  3821. `org-outline-path-complete-in-steps'.
  3822. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3823. :group 'org-completion
  3824. :type 'boolean)
  3825. (defcustom org-completion-fallback-command 'hippie-expand
  3826. "The expansion command called by \\[pcomplete] in normal context.
  3827. Normal means, no org-mode-specific context."
  3828. :group 'org-completion
  3829. :type 'function)
  3830. ;;; Functions and variables from their packages
  3831. ;; Declared here to avoid compiler warnings
  3832. ;; XEmacs only
  3833. (defvar outline-mode-menu-heading)
  3834. (defvar outline-mode-menu-show)
  3835. (defvar outline-mode-menu-hide)
  3836. (defvar zmacs-regions) ; XEmacs regions
  3837. ;; Emacs only
  3838. (defvar mark-active)
  3839. ;; Various packages
  3840. (declare-function calendar-iso-to-absolute "cal-iso" (date))
  3841. (declare-function calendar-forward-day "cal-move" (arg))
  3842. (declare-function calendar-goto-date "cal-move" (date))
  3843. (declare-function calendar-goto-today "cal-move" ())
  3844. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3845. (defvar calc-embedded-close-formula)
  3846. (defvar calc-embedded-open-formula)
  3847. (declare-function cdlatex-tab "ext:cdlatex" ())
  3848. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3849. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3850. (defvar font-lock-unfontify-region-function)
  3851. (declare-function iswitchb-read-buffer "iswitchb"
  3852. (prompt &optional default require-match start matches-set))
  3853. (defvar iswitchb-temp-buflist)
  3854. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3855. (defvar org-agenda-tags-todo-honor-ignore-options)
  3856. (declare-function org-agenda-skip "org-agenda" ())
  3857. (declare-function
  3858. org-agenda-format-item "org-agenda"
  3859. (extra txt &optional level category tags dotime noprefix remove-re habitp))
  3860. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3861. (declare-function org-agenda-change-all-lines "org-agenda"
  3862. (newhead hdmarker &optional fixface just-this))
  3863. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3864. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3865. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3866. (beg end))
  3867. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3868. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3869. "org-agenda" (&optional end))
  3870. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3871. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3872. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3873. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3874. (declare-function org-indent-mode "org-indent" (&optional arg))
  3875. (declare-function parse-time-string "parse-time" (string))
  3876. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3877. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3878. (defvar remember-data-file)
  3879. (defvar texmathp-why)
  3880. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3881. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3882. (declare-function calc-eval "calc" (str &optional separator &rest args))
  3883. ;;;###autoload
  3884. (defun turn-on-orgtbl ()
  3885. "Unconditionally turn on `orgtbl-mode'."
  3886. (require 'org-table)
  3887. (orgtbl-mode 1))
  3888. (defun org-at-table-p (&optional table-type)
  3889. "Non-nil if the cursor is inside an Org table.
  3890. If TABLE-TYPE is non-nil, also check for table.el-type tables.
  3891. If `org-enable-table-editor' is nil, return nil unconditionally."
  3892. (and
  3893. org-enable-table-editor
  3894. (save-excursion
  3895. (beginning-of-line)
  3896. (org-looking-at-p (if table-type "[ \t]*[|+]" "[ \t]*|")))
  3897. (or (not (derived-mode-p 'org-mode))
  3898. (let ((e (org-element-lineage (org-element-at-point) '(table) t)))
  3899. (and e (or table-type (eq (org-element-property :type e) 'org)))))))
  3900. (defsubst org-table-p () (org-at-table-p))
  3901. (defun org-at-table.el-p ()
  3902. "Non-nil when point is at a table.el table."
  3903. (and (save-excursion (beginning-of-line) (looking-at "[ \t]*[|+]"))
  3904. (let ((element (org-element-at-point)))
  3905. (and (eq (org-element-type element) 'table)
  3906. (eq (org-element-property :type element) 'table.el)))))
  3907. (defun org-table-recognize-table.el ()
  3908. "If there is a table.el table nearby, recognize it and move into it."
  3909. (when (and org-table-tab-recognizes-table.el (org-at-table.el-p))
  3910. (beginning-of-line)
  3911. (unless (or (looking-at org-table-dataline-regexp)
  3912. (not (looking-at org-table1-hline-regexp)))
  3913. (forward-line)
  3914. (when (looking-at org-table-any-border-regexp)
  3915. (forward-line -2)))
  3916. (if (re-search-forward "|" (org-table-end t) t)
  3917. (progn
  3918. (require 'table)
  3919. (if (table--at-cell-p (point)) t
  3920. (message "recognizing table.el table...")
  3921. (table-recognize-table)
  3922. (message "recognizing table.el table...done")))
  3923. (error "This should not happen"))))
  3924. (defun org-at-table-hline-p ()
  3925. "Non-nil when point is inside a hline in a table.
  3926. Assume point is already in a table. If `org-enable-table-editor'
  3927. is nil, return nil unconditionally."
  3928. (and org-enable-table-editor
  3929. (save-excursion
  3930. (beginning-of-line)
  3931. (looking-at org-table-hline-regexp))))
  3932. (defun org-table-map-tables (function &optional quietly)
  3933. "Apply FUNCTION to the start of all tables in the buffer."
  3934. (save-excursion
  3935. (save-restriction
  3936. (widen)
  3937. (goto-char (point-min))
  3938. (while (re-search-forward org-table-any-line-regexp nil t)
  3939. (unless quietly
  3940. (message "Mapping tables: %d%%"
  3941. (floor (* 100.0 (point)) (buffer-size))))
  3942. (beginning-of-line 1)
  3943. (when (and (looking-at org-table-line-regexp)
  3944. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3945. (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
  3946. (save-excursion (funcall function))
  3947. (or (looking-at org-table-line-regexp)
  3948. (forward-char 1)))
  3949. (re-search-forward org-table-any-border-regexp nil 1))))
  3950. (unless quietly (message "Mapping tables: done")))
  3951. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
  3952. (declare-function org-clock-update-mode-line "org-clock" ())
  3953. (declare-function org-resolve-clocks "org-clock"
  3954. (&optional also-non-dangling-p prompt last-valid))
  3955. (defun org-at-TBLFM-p (&optional pos)
  3956. "Non-nil when point (or POS) is in #+TBLFM line."
  3957. (save-excursion
  3958. (goto-char (or pos (point)))
  3959. (beginning-of-line)
  3960. (and (let ((case-fold-search t)) (looking-at org-TBLFM-regexp))
  3961. (eq (org-element-type (org-element-at-point)) 'table))))
  3962. (defvar org-clock-start-time)
  3963. (defvar org-clock-marker (make-marker)
  3964. "Marker recording the last clock-in.")
  3965. (defvar org-clock-hd-marker (make-marker)
  3966. "Marker recording the last clock-in, but the headline position.")
  3967. (defvar org-clock-heading ""
  3968. "The heading of the current clock entry.")
  3969. (defun org-clock-is-active ()
  3970. "Return the buffer where the clock is currently running.
  3971. Return nil if no clock is running."
  3972. (marker-buffer org-clock-marker))
  3973. (defun org-check-running-clock ()
  3974. "Check if the current buffer contains the running clock.
  3975. If yes, offer to stop it and to save the buffer with the changes."
  3976. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3977. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3978. (buffer-name))))
  3979. (org-clock-out)
  3980. (when (y-or-n-p "Save changed buffer?")
  3981. (save-buffer))))
  3982. (defun org-clocktable-try-shift (dir n)
  3983. "Check if this line starts a clock table, if yes, shift the time block."
  3984. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3985. (org-clocktable-shift dir n)))
  3986. ;;;###autoload
  3987. (defun org-clock-persistence-insinuate ()
  3988. "Set up hooks for clock persistence."
  3989. (require 'org-clock)
  3990. (add-hook 'org-mode-hook 'org-clock-load)
  3991. (add-hook 'kill-emacs-hook 'org-clock-save))
  3992. (defgroup org-archive nil
  3993. "Options concerning archiving in Org-mode."
  3994. :tag "Org Archive"
  3995. :group 'org-structure)
  3996. (defcustom org-archive-location "%s_archive::"
  3997. "The location where subtrees should be archived.
  3998. The value of this variable is a string, consisting of two parts,
  3999. separated by a double-colon. The first part is a filename and
  4000. the second part is a headline.
  4001. When the filename is omitted, archiving happens in the same file.
  4002. %s in the filename will be replaced by the current file
  4003. name (without the directory part). Archiving to a different file
  4004. is useful to keep archived entries from contributing to the
  4005. Org-mode Agenda.
  4006. The archived entries will be filed as subtrees of the specified
  4007. headline. When the headline is omitted, the subtrees are simply
  4008. filed away at the end of the file, as top-level entries. Also in
  4009. the heading you can use %s to represent the file name, this can be
  4010. useful when using the same archive for a number of different files.
  4011. Here are a few examples:
  4012. \"%s_archive::\"
  4013. If the current file is Projects.org, archive in file
  4014. Projects.org_archive, as top-level trees. This is the default.
  4015. \"::* Archived Tasks\"
  4016. Archive in the current file, under the top-level headline
  4017. \"* Archived Tasks\".
  4018. \"~/org/archive.org::\"
  4019. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  4020. \"~/org/archive.org::* From %s\"
  4021. Archive in file ~/org/archive.org (absolute path), under headlines
  4022. \"From FILENAME\" where file name is the current file name.
  4023. \"~/org/datetree.org::datetree/* Finished Tasks\"
  4024. The \"datetree/\" string is special, signifying to archive
  4025. items to the datetree. Items are placed in either the CLOSED
  4026. date of the item, or the current date if there is no CLOSED date.
  4027. The heading will be a subentry to the current date. There doesn't
  4028. need to be a heading, but there always needs to be a slash after
  4029. datetree. For example, to store archived items directly in the
  4030. datetree, use \"~/org/datetree.org::datetree/\".
  4031. \"basement::** Finished Tasks\"
  4032. Archive in file ./basement (relative path), as level 3 trees
  4033. below the level 2 heading \"** Finished Tasks\".
  4034. You may set this option on a per-file basis by adding to the buffer a
  4035. line like
  4036. #+ARCHIVE: basement::** Finished Tasks
  4037. You may also define it locally for a subtree by setting an ARCHIVE property
  4038. in the entry. If such a property is found in an entry, or anywhere up
  4039. the hierarchy, it will be used."
  4040. :group 'org-archive
  4041. :type 'string)
  4042. (defcustom org-agenda-skip-archived-trees t
  4043. "Non-nil means the agenda will skip any items located in archived trees.
  4044. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  4045. variable is no longer recommended, you should leave it at the value t.
  4046. Instead, use the key `v' to cycle the archives-mode in the agenda."
  4047. :group 'org-archive
  4048. :group 'org-agenda-skip
  4049. :type 'boolean)
  4050. (defcustom org-columns-skip-archived-trees t
  4051. "Non-nil means ignore archived trees when creating column view."
  4052. :group 'org-archive
  4053. :group 'org-properties
  4054. :type 'boolean)
  4055. (defcustom org-cycle-open-archived-trees nil
  4056. "Non-nil means `org-cycle' will open archived trees.
  4057. An archived tree is a tree marked with the tag ARCHIVE.
  4058. When nil, archived trees will stay folded. You can still open them with
  4059. normal outline commands like `show-all', but not with the cycling commands."
  4060. :group 'org-archive
  4061. :group 'org-cycle
  4062. :type 'boolean)
  4063. (defcustom org-sparse-tree-open-archived-trees nil
  4064. "Non-nil means sparse tree construction shows matches in archived trees.
  4065. When nil, matches in these trees are highlighted, but the trees are kept in
  4066. collapsed state."
  4067. :group 'org-archive
  4068. :group 'org-sparse-trees
  4069. :type 'boolean)
  4070. (defcustom org-sparse-tree-default-date-type nil
  4071. "The default date type when building a sparse tree.
  4072. When this is nil, a date is a scheduled or a deadline timestamp.
  4073. Otherwise, these types are allowed:
  4074. all: all timestamps
  4075. active: only active timestamps (<...>)
  4076. inactive: only inactive timestamps ([...])
  4077. scheduled: only scheduled timestamps
  4078. deadline: only deadline timestamps"
  4079. :type '(choice (const :tag "Scheduled or deadline" nil)
  4080. (const :tag "All timestamps" all)
  4081. (const :tag "Only active timestamps" active)
  4082. (const :tag "Only inactive timestamps" inactive)
  4083. (const :tag "Only scheduled timestamps" scheduled)
  4084. (const :tag "Only deadline timestamps" deadline)
  4085. (const :tag "Only closed timestamps" closed))
  4086. :version "25.1"
  4087. :package-version '(Org . "8.3")
  4088. :group 'org-sparse-trees)
  4089. (defun org-cycle-hide-archived-subtrees (state)
  4090. "Re-hide all archived subtrees after a visibility state change."
  4091. (when (and (not org-cycle-open-archived-trees)
  4092. (not (memq state '(overview folded))))
  4093. (save-excursion
  4094. (let* ((globalp (memq state '(contents all)))
  4095. (beg (if globalp (point-min) (point)))
  4096. (end (if globalp (point-max) (org-end-of-subtree t))))
  4097. (org-hide-archived-subtrees beg end)
  4098. (goto-char beg)
  4099. (if (looking-at (concat ".*:" org-archive-tag ":"))
  4100. (message "%s" (substitute-command-keys
  4101. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  4102. (defun org-force-cycle-archived ()
  4103. "Cycle subtree even if it is archived."
  4104. (interactive)
  4105. (setq this-command 'org-cycle)
  4106. (let ((org-cycle-open-archived-trees t))
  4107. (call-interactively 'org-cycle)))
  4108. (defun org-hide-archived-subtrees (beg end)
  4109. "Re-hide all archived subtrees after a visibility state change."
  4110. (org-with-wide-buffer
  4111. (let ((case-fold-search nil)
  4112. (re (concat org-outline-regexp-bol ".*:" org-archive-tag ":")))
  4113. (goto-char beg)
  4114. ;; Include headline point is currently on.
  4115. (beginning-of-line)
  4116. (while (and (< (point) end) (re-search-forward re end t))
  4117. (when (member org-archive-tag (org-get-tags))
  4118. (org-flag-subtree t)
  4119. (org-end-of-subtree t))))))
  4120. (declare-function outline-end-of-heading "outline" ())
  4121. (declare-function outline-flag-region "outline" (from to flag))
  4122. (defun org-flag-subtree (flag)
  4123. (save-excursion
  4124. (org-back-to-heading t)
  4125. (outline-end-of-heading)
  4126. (outline-flag-region (point)
  4127. (progn (org-end-of-subtree t) (point))
  4128. flag)))
  4129. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  4130. ;; Declare Column View Code
  4131. (declare-function org-columns-number-to-string "org-colview" (n fmt &optional printf))
  4132. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  4133. (declare-function org-columns-compute "org-colview" (property))
  4134. ;; Declare ID code
  4135. (declare-function org-id-store-link "org-id")
  4136. (declare-function org-id-locations-load "org-id")
  4137. (declare-function org-id-locations-save "org-id")
  4138. (defvar org-id-track-globally)
  4139. ;;; Variables for pre-computed regular expressions, all buffer local
  4140. (defvar org-todo-regexp nil
  4141. "Matches any of the TODO state keywords.")
  4142. (make-variable-buffer-local 'org-todo-regexp)
  4143. (defvar org-not-done-regexp nil
  4144. "Matches any of the TODO state keywords except the last one.")
  4145. (make-variable-buffer-local 'org-not-done-regexp)
  4146. (defvar org-not-done-heading-regexp nil
  4147. "Matches a TODO headline that is not done.")
  4148. (make-variable-buffer-local 'org-not-done-heading-regexp)
  4149. (defvar org-todo-line-regexp nil
  4150. "Matches a headline and puts TODO state into group 2 if present.")
  4151. (make-variable-buffer-local 'org-todo-line-regexp)
  4152. (defvar org-complex-heading-regexp nil
  4153. "Matches a headline and puts everything into groups:
  4154. group 1: the stars
  4155. group 2: The todo keyword, maybe
  4156. group 3: Priority cookie
  4157. group 4: True headline
  4158. group 5: Tags")
  4159. (make-variable-buffer-local 'org-complex-heading-regexp)
  4160. (defvar org-complex-heading-regexp-format nil
  4161. "Printf format to make regexp to match an exact headline.
  4162. This regexp will match the headline of any node which has the
  4163. exact headline text that is put into the format, but may have any
  4164. TODO state, priority and tags.")
  4165. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  4166. (defvar org-todo-line-tags-regexp nil
  4167. "Matches a headline and puts TODO state into group 2 if present.
  4168. Also put tags into group 4 if tags are present.")
  4169. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  4170. (defconst org-plain-time-of-day-regexp
  4171. (concat
  4172. "\\(\\<[012]?[0-9]"
  4173. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4174. "\\(--?"
  4175. "\\(\\<[012]?[0-9]"
  4176. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4177. "\\)?")
  4178. "Regular expression to match a plain time or time range.
  4179. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4180. groups carry important information:
  4181. 0 the full match
  4182. 1 the first time, range or not
  4183. 8 the second time, if it is a range.")
  4184. (defconst org-plain-time-extension-regexp
  4185. (concat
  4186. "\\(\\<[012]?[0-9]"
  4187. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4188. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  4189. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  4190. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4191. groups carry important information:
  4192. 0 the full match
  4193. 7 hours of duration
  4194. 9 minutes of duration")
  4195. (defconst org-stamp-time-of-day-regexp
  4196. (concat
  4197. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  4198. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  4199. "\\(--?"
  4200. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  4201. "Regular expression to match a timestamp time or time range.
  4202. After a match, the following groups carry important information:
  4203. 0 the full match
  4204. 1 date plus weekday, for back referencing to make sure both times are on the same day
  4205. 2 the first time, range or not
  4206. 4 the second time, if it is a range.")
  4207. (defconst org-startup-options
  4208. '(("fold" org-startup-folded t)
  4209. ("overview" org-startup-folded t)
  4210. ("nofold" org-startup-folded nil)
  4211. ("showall" org-startup-folded nil)
  4212. ("showeverything" org-startup-folded showeverything)
  4213. ("content" org-startup-folded content)
  4214. ("indent" org-startup-indented t)
  4215. ("noindent" org-startup-indented nil)
  4216. ("hidestars" org-hide-leading-stars t)
  4217. ("showstars" org-hide-leading-stars nil)
  4218. ("odd" org-odd-levels-only t)
  4219. ("oddeven" org-odd-levels-only nil)
  4220. ("align" org-startup-align-all-tables t)
  4221. ("noalign" org-startup-align-all-tables nil)
  4222. ("inlineimages" org-startup-with-inline-images t)
  4223. ("noinlineimages" org-startup-with-inline-images nil)
  4224. ("latexpreview" org-startup-with-latex-preview t)
  4225. ("nolatexpreview" org-startup-with-latex-preview nil)
  4226. ("customtime" org-display-custom-times t)
  4227. ("logdone" org-log-done time)
  4228. ("lognotedone" org-log-done note)
  4229. ("nologdone" org-log-done nil)
  4230. ("lognoteclock-out" org-log-note-clock-out t)
  4231. ("nolognoteclock-out" org-log-note-clock-out nil)
  4232. ("logrepeat" org-log-repeat state)
  4233. ("lognoterepeat" org-log-repeat note)
  4234. ("logdrawer" org-log-into-drawer t)
  4235. ("nologdrawer" org-log-into-drawer nil)
  4236. ("logstatesreversed" org-log-states-order-reversed t)
  4237. ("nologstatesreversed" org-log-states-order-reversed nil)
  4238. ("nologrepeat" org-log-repeat nil)
  4239. ("logreschedule" org-log-reschedule time)
  4240. ("lognotereschedule" org-log-reschedule note)
  4241. ("nologreschedule" org-log-reschedule nil)
  4242. ("logredeadline" org-log-redeadline time)
  4243. ("lognoteredeadline" org-log-redeadline note)
  4244. ("nologredeadline" org-log-redeadline nil)
  4245. ("logrefile" org-log-refile time)
  4246. ("lognoterefile" org-log-refile note)
  4247. ("nologrefile" org-log-refile nil)
  4248. ("fninline" org-footnote-define-inline t)
  4249. ("nofninline" org-footnote-define-inline nil)
  4250. ("fnlocal" org-footnote-section nil)
  4251. ("fnauto" org-footnote-auto-label t)
  4252. ("fnprompt" org-footnote-auto-label nil)
  4253. ("fnconfirm" org-footnote-auto-label confirm)
  4254. ("fnplain" org-footnote-auto-label plain)
  4255. ("fnadjust" org-footnote-auto-adjust t)
  4256. ("nofnadjust" org-footnote-auto-adjust nil)
  4257. ("constcgs" constants-unit-system cgs)
  4258. ("constSI" constants-unit-system SI)
  4259. ("noptag" org-tag-persistent-alist nil)
  4260. ("hideblocks" org-hide-block-startup t)
  4261. ("nohideblocks" org-hide-block-startup nil)
  4262. ("beamer" org-startup-with-beamer-mode t)
  4263. ("entitiespretty" org-pretty-entities t)
  4264. ("entitiesplain" org-pretty-entities nil))
  4265. "Variable associated with STARTUP options for org-mode.
  4266. Each element is a list of three items: the startup options (as written
  4267. in the #+STARTUP line), the corresponding variable, and the value to set
  4268. this variable to if the option is found. An optional forth element PUSH
  4269. means to push this value onto the list in the variable.")
  4270. (defcustom org-group-tags t
  4271. "When non-nil (the default), use group tags.
  4272. This can be turned on/off through `org-toggle-tags-groups'."
  4273. :group 'org-tags
  4274. :group 'org-startup
  4275. :type 'boolean)
  4276. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4277. (defun org-toggle-tags-groups ()
  4278. "Toggle support for group tags.
  4279. Support for group tags is controlled by the option
  4280. `org-group-tags', which is non-nil by default."
  4281. (interactive)
  4282. (setq org-group-tags (not org-group-tags))
  4283. (cond ((and (derived-mode-p 'org-agenda-mode)
  4284. org-group-tags)
  4285. (org-agenda-redo))
  4286. ((derived-mode-p 'org-mode)
  4287. (let ((org-inhibit-startup t)) (org-mode))))
  4288. (message "Groups tags support has been turned %s"
  4289. (if org-group-tags "on" "off")))
  4290. (defun org-set-regexps-and-options (&optional tags-only)
  4291. "Precompute regular expressions used in the current buffer.
  4292. When optional argument TAGS-ONLY is non-nil, only compute tags
  4293. related expressions."
  4294. (when (derived-mode-p 'org-mode)
  4295. (let ((alist (org--setup-collect-keywords
  4296. (org-make-options-regexp
  4297. (append '("FILETAGS" "TAGS" "SETUPFILE")
  4298. (and (not tags-only)
  4299. '("ARCHIVE" "CATEGORY" "COLUMNS" "CONSTANTS"
  4300. "LINK" "OPTIONS" "PRIORITIES" "PROPERTY"
  4301. "SEQ_TODO" "STARTUP" "TODO" "TYP_TODO")))))))
  4302. (org--setup-process-tags
  4303. (cdr (assq 'tags alist)) (cdr (assq 'filetags alist)))
  4304. (unless tags-only
  4305. ;; File properties.
  4306. (org-set-local 'org-file-properties (cdr (assq 'property alist)))
  4307. ;; Archive location.
  4308. (let ((archive (cdr (assq 'archive alist))))
  4309. (when archive (org-set-local 'org-archive-location archive)))
  4310. ;; Category.
  4311. (let ((cat (org-string-nw-p (cdr (assq 'category alist)))))
  4312. (when cat
  4313. (org-set-local 'org-category (intern cat))
  4314. (org-set-local 'org-file-properties
  4315. (org--update-property-plist
  4316. "CATEGORY" cat org-file-properties))))
  4317. ;; Columns.
  4318. (let ((column (cdr (assq 'columns alist))))
  4319. (when column (org-set-local 'org-columns-default-format column)))
  4320. ;; Constants.
  4321. (setq org-table-formula-constants-local (cdr (assq 'constants alist)))
  4322. ;; Link abbreviations.
  4323. (let ((links (cdr (assq 'link alist))))
  4324. (when links (setq org-link-abbrev-alist-local (nreverse links))))
  4325. ;; Priorities.
  4326. (let ((priorities (cdr (assq 'priorities alist))))
  4327. (when priorities
  4328. (org-set-local 'org-highest-priority (nth 0 priorities))
  4329. (org-set-local 'org-lowest-priority (nth 1 priorities))
  4330. (org-set-local 'org-default-priority (nth 2 priorities))))
  4331. ;; Scripts.
  4332. (let ((scripts (assq 'scripts alist)))
  4333. (when scripts
  4334. (org-set-local 'org-use-sub-superscripts (cdr scripts))))
  4335. ;; Startup options.
  4336. (let ((startup (cdr (assq 'startup alist))))
  4337. (dolist (option startup)
  4338. (let ((entry (assoc-string option org-startup-options t)))
  4339. (when entry
  4340. (let ((var (nth 1 entry))
  4341. (val (nth 2 entry)))
  4342. (if (not (nth 3 entry)) (org-set-local var val)
  4343. (unless (listp (symbol-value var))
  4344. (org-set-local var nil))
  4345. (add-to-list var val)))))))
  4346. ;; TODO keywords.
  4347. (org-set-local 'org-todo-kwd-alist nil)
  4348. (org-set-local 'org-todo-key-alist nil)
  4349. (org-set-local 'org-todo-key-trigger nil)
  4350. (org-set-local 'org-todo-keywords-1 nil)
  4351. (org-set-local 'org-done-keywords nil)
  4352. (org-set-local 'org-todo-heads nil)
  4353. (org-set-local 'org-todo-sets nil)
  4354. (org-set-local 'org-todo-log-states nil)
  4355. (let ((todo-sequences
  4356. (or (nreverse (cdr (assq 'todo alist)))
  4357. (let ((d (default-value 'org-todo-keywords)))
  4358. (if (not (stringp (car d))) d
  4359. ;; XXX: Backward compatibility code.
  4360. (list (cons org-todo-interpretation d)))))))
  4361. (dolist (sequence todo-sequences)
  4362. (let* ((sequence (or (run-hook-with-args-until-success
  4363. 'org-todo-setup-filter-hook sequence)
  4364. sequence))
  4365. (sequence-type (car sequence))
  4366. (keywords (cdr sequence))
  4367. (sep (member "|" keywords))
  4368. names alist)
  4369. (dolist (k (remove "|" keywords))
  4370. (unless (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$"
  4371. k)
  4372. (error "Invalid TODO keyword %s" k))
  4373. (let ((name (match-string 1 k))
  4374. (key (match-string 2 k))
  4375. (log (org-extract-log-state-settings k)))
  4376. (push name names)
  4377. (push (cons name (and key (string-to-char key))) alist)
  4378. (when log (push log org-todo-log-states))))
  4379. (let* ((names (nreverse names))
  4380. (done (if sep (org-remove-keyword-keys (cdr sep))
  4381. (last names)))
  4382. (head (car names))
  4383. (tail (list sequence-type head (car done) (org-last done))))
  4384. (add-to-list 'org-todo-heads head 'append)
  4385. (push names org-todo-sets)
  4386. (setq org-done-keywords (append org-done-keywords done nil))
  4387. (setq org-todo-keywords-1 (append org-todo-keywords-1 names nil))
  4388. (setq org-todo-key-alist
  4389. (append org-todo-key-alist
  4390. (and alist
  4391. (append '((:startgroup))
  4392. (nreverse alist)
  4393. '((:endgroup))))))
  4394. (dolist (k names) (push (cons k tail) org-todo-kwd-alist))))))
  4395. (setq org-todo-sets (nreverse org-todo-sets)
  4396. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4397. org-todo-key-trigger (delq nil (mapcar #'cdr org-todo-key-alist))
  4398. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist))
  4399. ;; Compute the regular expressions and other local variables.
  4400. ;; Using `org-outline-regexp-bol' would complicate them much,
  4401. ;; because of the fixed white space at the end of that string.
  4402. (if (not org-done-keywords)
  4403. (setq org-done-keywords
  4404. (and org-todo-keywords-1 (last org-todo-keywords-1))))
  4405. (setq org-not-done-keywords
  4406. (org-delete-all org-done-keywords
  4407. (copy-sequence org-todo-keywords-1))
  4408. org-todo-regexp (regexp-opt org-todo-keywords-1 t)
  4409. org-not-done-regexp (regexp-opt org-not-done-keywords t)
  4410. org-not-done-heading-regexp
  4411. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4412. org-todo-line-regexp
  4413. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4414. org-complex-heading-regexp
  4415. (concat "^\\(\\*+\\)"
  4416. "\\(?: +" org-todo-regexp "\\)?"
  4417. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4418. "\\(?: +\\(.*?\\)\\)??"
  4419. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4420. "[ \t]*$")
  4421. org-complex-heading-regexp-format
  4422. (concat "^\\(\\*+\\)"
  4423. "\\(?: +" org-todo-regexp "\\)?"
  4424. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4425. "\\(?: +"
  4426. ;; Stats cookies can be stuck to body.
  4427. "\\(?:\\[[0-9%%/]+\\] *\\)*"
  4428. "\\(%s\\)"
  4429. "\\(?: *\\[[0-9%%/]+\\]\\)*"
  4430. "\\)"
  4431. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4432. "[ \t]*$")
  4433. org-todo-line-tags-regexp
  4434. (concat "^\\(\\*+\\)"
  4435. "\\(?: +" org-todo-regexp "\\)?"
  4436. "\\(?: +\\(.*?\\)\\)??"
  4437. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4438. "[ \t]*$"))
  4439. (org-compute-latex-and-related-regexp)))))
  4440. (defun org--setup-collect-keywords (regexp &optional files alist)
  4441. "Return setup keywords values as an alist.
  4442. REGEXP matches a subset of setup keywords. FILES is a list of
  4443. file names already visited. It is used to avoid circular setup
  4444. files. ALIST, when non-nil, is the alist computed so far.
  4445. Return value contains the following keys: `archive', `category',
  4446. `columns', `constants', `filetags', `link', `priorities',
  4447. `property', `scripts', `startup', `tags' and `todo'."
  4448. (org-with-wide-buffer
  4449. (goto-char (point-min))
  4450. (let ((case-fold-search t))
  4451. (while (re-search-forward regexp nil t)
  4452. (let ((element (org-element-at-point)))
  4453. (when (eq (org-element-type element) 'keyword)
  4454. (let ((key (org-element-property :key element))
  4455. (value (org-element-property :value element)))
  4456. (cond
  4457. ((equal key "ARCHIVE")
  4458. (when (org-string-nw-p value)
  4459. (push (cons 'archive value) alist)))
  4460. ((equal key "CATEGORY") (push (cons 'category value) alist))
  4461. ((equal key "COLUMNS") (push (cons 'columns value) alist))
  4462. ((equal key "CONSTANTS")
  4463. (let* ((constants (assq 'constants alist))
  4464. (store (cdr constants)))
  4465. (dolist (pair (org-split-string value))
  4466. (when (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)"
  4467. pair)
  4468. (let* ((name (match-string 1 pair))
  4469. (value (match-string 2 pair))
  4470. (old (assoc name store)))
  4471. (if old (setcdr old value)
  4472. (push (cons name value) store)))))
  4473. (if constants (setcdr constants store)
  4474. (push (cons 'constants store) alist))))
  4475. ((equal key "FILETAGS")
  4476. (when (org-string-nw-p value)
  4477. (let ((old (assq 'filetags alist))
  4478. (new (apply #'nconc
  4479. (mapcar (lambda (x) (org-split-string x ":"))
  4480. (org-split-string value)))))
  4481. (if old (setcdr old (append new (cdr old)))
  4482. (push (cons 'filetags new) alist)))))
  4483. ((equal key "LINK")
  4484. (when (string-match "\\`\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4485. (let ((links (assq 'link alist))
  4486. (pair (cons (org-match-string-no-properties 1 value)
  4487. (org-match-string-no-properties 2 value))))
  4488. (if links (push pair (cdr links))
  4489. (push (list 'link pair) alist)))))
  4490. ((equal key "OPTIONS")
  4491. (when (and (org-string-nw-p value)
  4492. (string-match "\\^:\\(t\\|nil\\|{}\\)" value))
  4493. (push (cons 'scripts (read (match-string 1 value))) alist)))
  4494. ((equal key "PRIORITIES")
  4495. (push (cons 'priorities
  4496. (let ((prio (org-split-string value)))
  4497. (if (< (length prio) 3) '(?A ?C ?B)
  4498. (mapcar #'string-to-char prio))))
  4499. alist))
  4500. ((equal key "PROPERTY")
  4501. (when (string-match "\\(\\S-+\\)[ \t]+\\(.*\\)" value)
  4502. (let* ((property (assq 'property alist))
  4503. (value (org--update-property-plist
  4504. (org-match-string-no-properties 1 value)
  4505. (org-match-string-no-properties 2 value)
  4506. (cdr property))))
  4507. (if property (setcdr property value)
  4508. (push (cons 'property value) alist)))))
  4509. ((equal key "STARTUP")
  4510. (let ((startup (assq 'startup alist)))
  4511. (if startup
  4512. (setcdr startup
  4513. (append (cdr startup) (org-split-string value)))
  4514. (push (cons 'startup (org-split-string value)) alist))))
  4515. ((equal key "TAGS")
  4516. (let ((tag-cell (assq 'tags alist)))
  4517. (if tag-cell
  4518. (setcdr tag-cell
  4519. (append (cdr tag-cell)
  4520. '("\\n")
  4521. (org-split-string value)))
  4522. (push (cons 'tags (org-split-string value)) alist))))
  4523. ((member key '("TODO" "SEQ_TODO" "TYP_TODO"))
  4524. (let ((todo (assq 'todo alist))
  4525. (value (cons (if (equal key "TYP_TODO") 'type 'sequence)
  4526. (org-split-string value))))
  4527. (if todo (push value (cdr todo))
  4528. (push (list 'todo value) alist))))
  4529. ((equal key "SETUPFILE")
  4530. (unless buffer-read-only ; Do not check in Gnus messages.
  4531. (let ((f (and (org-string-nw-p value)
  4532. (expand-file-name
  4533. (org-remove-double-quotes value)))))
  4534. (when (and f (file-readable-p f) (not (member f files)))
  4535. (with-temp-buffer
  4536. (insert-file-contents f)
  4537. (setq alist
  4538. ;; Fake Org mode to benefit from cache
  4539. ;; without recurring needlessly.
  4540. (let ((major-mode 'org-mode))
  4541. (org--setup-collect-keywords
  4542. regexp (cons f files) alist)))))))))))))))
  4543. alist)
  4544. (defun org--setup-process-tags (tags filetags)
  4545. "Precompute variables used for tags.
  4546. TAGS is a list of tags and tag group symbols, as strings.
  4547. FILETAGS is a list of tags, as strings."
  4548. ;; Process the file tags.
  4549. (org-set-local 'org-file-tags
  4550. (mapcar #'org-add-prop-inherited filetags))
  4551. ;; Provide default tags if no local tags are found.
  4552. (when (and (not tags) org-tag-alist)
  4553. (setq tags
  4554. (mapcar (lambda (tag)
  4555. (case (car tag)
  4556. (:startgroup "{")
  4557. (:endgroup "}")
  4558. (:startgrouptag "[")
  4559. (:endgrouptag "]")
  4560. (:grouptags ":")
  4561. (:newline "\\n")
  4562. (otherwise (concat (car tag)
  4563. (and (characterp (cdr tag))
  4564. (format "(%c)" (cdr tag)))))))
  4565. org-tag-alist)))
  4566. ;; Process the tags.
  4567. (org-set-local 'org-tag-groups-alist nil)
  4568. (org-set-local 'org-tag-alist nil)
  4569. (let (group-flag)
  4570. (while tags
  4571. (let ((e (car tags)))
  4572. (setq tags (cdr tags))
  4573. (cond
  4574. ((equal e "{")
  4575. (push '(:startgroup) org-tag-alist)
  4576. (when (equal (nth 1 tags) ":") (setq group-flag t)))
  4577. ((equal e "}")
  4578. (push '(:endgroup) org-tag-alist)
  4579. (setq group-flag nil))
  4580. ((equal e "[")
  4581. (push '(:startgrouptag) org-tag-alist)
  4582. (when (equal (nth 1 tags) ":") (setq group-flag t)))
  4583. ((equal e "]")
  4584. (push '(:endgrouptag) org-tag-alist)
  4585. (setq group-flag nil))
  4586. ((equal e ":")
  4587. (push '(:grouptags) org-tag-alist)
  4588. (setq group-flag 'append))
  4589. ((equal e "\\n") (push '(:newline) org-tag-alist))
  4590. ((string-match
  4591. (org-re (concat "\\`\\([[:alnum:]_@#%]+"
  4592. "\\|{.+?}\\)" ; regular expression
  4593. "\\(?:(\\(.\\))\\)?\\'")) e)
  4594. (let ((tag (match-string 1 e))
  4595. (key (and (match-beginning 2)
  4596. (string-to-char (match-string 2 e)))))
  4597. (cond ((eq group-flag 'append)
  4598. (setcar org-tag-groups-alist
  4599. (append (car org-tag-groups-alist) (list tag))))
  4600. (group-flag (push (list tag) org-tag-groups-alist)))
  4601. ;; Push all tags in groups, no matter if they already exist.
  4602. (unless (and (not group-flag) (assoc tag org-tag-alist))
  4603. (push (cons tag key) org-tag-alist))))))))
  4604. (setq org-tag-alist (nreverse org-tag-alist)))
  4605. (defun org-file-contents (file &optional noerror)
  4606. "Return the contents of FILE, as a string."
  4607. (if (and file (file-readable-p file))
  4608. (with-temp-buffer
  4609. (insert-file-contents file)
  4610. (buffer-string))
  4611. (funcall (if noerror 'message 'error)
  4612. "Cannot read file \"%s\"%s"
  4613. file
  4614. (let ((from (buffer-file-name (buffer-base-buffer))))
  4615. (if from (concat " (referenced in file \"" from "\")") "")))))
  4616. (defun org-extract-log-state-settings (x)
  4617. "Extract the log state setting from a TODO keyword string.
  4618. This will extract info from a string like \"WAIT(w@/!)\"."
  4619. (let (kw key log1 log2)
  4620. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4621. (setq kw (match-string 1 x)
  4622. key (and (match-end 2) (match-string 2 x))
  4623. log1 (and (match-end 3) (match-string 3 x))
  4624. log2 (and (match-end 4) (match-string 4 x)))
  4625. (and (or log1 log2)
  4626. (list kw
  4627. (and log1 (if (equal log1 "!") 'time 'note))
  4628. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4629. (defun org-remove-keyword-keys (list)
  4630. "Remove a pair of parenthesis at the end of each string in LIST."
  4631. (mapcar (lambda (x)
  4632. (if (string-match "(.*)$" x)
  4633. (substring x 0 (match-beginning 0))
  4634. x))
  4635. list))
  4636. (defun org-assign-fast-keys (alist)
  4637. "Assign fast keys to a keyword-key alist.
  4638. Respect keys that are already there."
  4639. (let (new e (alt ?0))
  4640. (while (setq e (pop alist))
  4641. (if (or (memq (car e) '(:newline :grouptags :endgroup :startgroup))
  4642. (cdr e)) ;; Key already assigned.
  4643. (push e new)
  4644. (let ((clist (string-to-list (downcase (car e))))
  4645. (used (append new alist)))
  4646. (when (= (car clist) ?@)
  4647. (pop clist))
  4648. (while (and clist (rassoc (car clist) used))
  4649. (pop clist))
  4650. (unless clist
  4651. (while (rassoc alt used)
  4652. (incf alt)))
  4653. (push (cons (car e) (or (car clist) alt)) new))))
  4654. (nreverse new)))
  4655. ;;; Some variables used in various places
  4656. (defvar org-window-configuration nil
  4657. "Used in various places to store a window configuration.")
  4658. (defvar org-selected-window nil
  4659. "Used in various places to store a window configuration.")
  4660. (defvar org-finish-function nil
  4661. "Function to be called when `C-c C-c' is used.
  4662. This is for getting out of special buffers like capture.")
  4663. ;; FIXME: Occasionally check by commenting these, to make sure
  4664. ;; no other functions uses these, forgetting to let-bind them.
  4665. (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
  4666. (defvar org-last-state)
  4667. (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  4668. ;; Defined somewhere in this file, but used before definition.
  4669. (defvar org-entities) ;; defined in org-entities.el
  4670. (defvar org-struct-menu)
  4671. (defvar org-org-menu)
  4672. (defvar org-tbl-menu)
  4673. ;;;; Define the Org-mode
  4674. ;; We use a before-change function to check if a table might need
  4675. ;; an update.
  4676. (defvar org-table-may-need-update t
  4677. "Indicates that a table might need an update.
  4678. This variable is set by `org-before-change-function'.
  4679. `org-table-align' sets it back to nil.")
  4680. (defun org-before-change-function (beg end)
  4681. "Every change indicates that a table might need an update."
  4682. (setq org-table-may-need-update t))
  4683. (defvar org-mode-map)
  4684. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4685. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4686. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4687. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4688. (defvar org-table-buffer-is-an nil)
  4689. (defvar bidi-paragraph-direction)
  4690. (defvar buffer-face-mode-face)
  4691. (require 'outline)
  4692. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4693. (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"))
  4694. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4695. ;; Other stuff we need.
  4696. (require 'time-date)
  4697. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4698. (require 'easymenu)
  4699. (autoload 'easy-menu-add "easymenu")
  4700. (require 'overlay)
  4701. ;; (require 'org-macs) moved higher up in the file before it is first used
  4702. (require 'org-entities)
  4703. ;; (require 'org-compat) moved higher up in the file before it is first used
  4704. (require 'org-faces)
  4705. (require 'org-list)
  4706. (require 'org-pcomplete)
  4707. (require 'org-src)
  4708. (require 'org-footnote)
  4709. (require 'org-macro)
  4710. ;; babel
  4711. (require 'ob)
  4712. ;;;###autoload
  4713. (define-derived-mode org-mode outline-mode "Org"
  4714. "Outline-based notes management and organizer, alias
  4715. \"Carsten's outline-mode for keeping track of everything.\"
  4716. Org-mode develops organizational tasks around a NOTES file which
  4717. contains information about projects as plain text. Org-mode is
  4718. implemented on top of outline-mode, which is ideal to keep the content
  4719. of large files well structured. It supports ToDo items, deadlines and
  4720. time stamps, which magically appear in the diary listing of the Emacs
  4721. calendar. Tables are easily created with a built-in table editor.
  4722. Plain text URL-like links connect to websites, emails (VM), Usenet
  4723. messages (Gnus), BBDB entries, and any files related to the project.
  4724. For printing and sharing of notes, an Org-mode file (or a part of it)
  4725. can be exported as a structured ASCII or HTML file.
  4726. The following commands are available:
  4727. \\{org-mode-map}"
  4728. ;; Get rid of Outline menus, they are not needed
  4729. ;; Need to do this here because define-derived-mode sets up
  4730. ;; the keymap so late. Still, it is a waste to call this each time
  4731. ;; we switch another buffer into org-mode.
  4732. (if (featurep 'xemacs)
  4733. (when (boundp 'outline-mode-menu-heading)
  4734. ;; Assume this is Greg's port, it uses easymenu
  4735. (easy-menu-remove outline-mode-menu-heading)
  4736. (easy-menu-remove outline-mode-menu-show)
  4737. (easy-menu-remove outline-mode-menu-hide))
  4738. (define-key org-mode-map [menu-bar headings] 'undefined)
  4739. (define-key org-mode-map [menu-bar hide] 'undefined)
  4740. (define-key org-mode-map [menu-bar show] 'undefined))
  4741. (org-load-modules-maybe)
  4742. (when (featurep 'xemacs)
  4743. (easy-menu-add org-org-menu)
  4744. (easy-menu-add org-tbl-menu))
  4745. (org-install-agenda-files-menu)
  4746. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4747. (add-to-invisibility-spec '(org-cwidth))
  4748. (add-to-invisibility-spec '(org-hide-block . t))
  4749. (when (featurep 'xemacs)
  4750. (org-set-local 'line-move-ignore-invisible t))
  4751. (org-set-local 'outline-regexp org-outline-regexp)
  4752. (org-set-local 'outline-level 'org-outline-level)
  4753. (setq bidi-paragraph-direction 'left-to-right)
  4754. (when (and org-ellipsis
  4755. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4756. (fboundp 'make-glyph-code))
  4757. (unless org-display-table
  4758. (setq org-display-table (make-display-table)))
  4759. (set-display-table-slot
  4760. org-display-table 4
  4761. (vconcat (mapcar
  4762. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4763. org-ellipsis)))
  4764. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4765. (setq buffer-display-table org-display-table))
  4766. (org-set-regexps-and-options)
  4767. (org-set-font-lock-defaults)
  4768. (when (and org-tag-faces (not org-tags-special-faces-re))
  4769. ;; tag faces set outside customize.... force initialization.
  4770. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4771. ;; Calc embedded
  4772. (org-set-local 'calc-embedded-open-mode "# ")
  4773. ;; Modify a few syntax entries
  4774. (modify-syntax-entry ?@ "w")
  4775. (modify-syntax-entry ?\" "\"")
  4776. (modify-syntax-entry ?\\ "_")
  4777. (modify-syntax-entry ?~ "_")
  4778. (if org-startup-truncated (setq truncate-lines t))
  4779. (when org-startup-indented (require 'org-indent) (org-indent-mode 1))
  4780. (org-set-local 'font-lock-unfontify-region-function
  4781. 'org-unfontify-region)
  4782. ;; Activate before-change-function
  4783. (org-set-local 'org-table-may-need-update t)
  4784. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4785. 'local)
  4786. ;; Check for running clock before killing a buffer
  4787. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4788. ;; Initialize macros templates.
  4789. (org-macro-initialize-templates)
  4790. ;; Initialize radio targets.
  4791. (org-update-radio-target-regexp)
  4792. ;; Indentation.
  4793. (org-set-local 'indent-line-function 'org-indent-line)
  4794. (org-set-local 'indent-region-function 'org-indent-region)
  4795. ;; Filling and auto-filling.
  4796. (org-setup-filling)
  4797. ;; Comments.
  4798. (org-setup-comments-handling)
  4799. ;; Initialize cache.
  4800. (org-element-cache-reset)
  4801. ;; Beginning/end of defun
  4802. (org-set-local 'beginning-of-defun-function 'org-backward-element)
  4803. (org-set-local 'end-of-defun-function
  4804. (lambda ()
  4805. (if (not (org-at-heading-p))
  4806. (org-forward-element)
  4807. (org-forward-element)
  4808. (forward-char -1))))
  4809. ;; Next error for sparse trees
  4810. (org-set-local 'next-error-function 'org-occur-next-match)
  4811. ;; Make sure dependence stuff works reliably, even for users who set it
  4812. ;; too late :-(
  4813. (if org-enforce-todo-dependencies
  4814. (add-hook 'org-blocker-hook
  4815. 'org-block-todo-from-children-or-siblings-or-parent)
  4816. (remove-hook 'org-blocker-hook
  4817. 'org-block-todo-from-children-or-siblings-or-parent))
  4818. (if org-enforce-todo-checkbox-dependencies
  4819. (add-hook 'org-blocker-hook
  4820. 'org-block-todo-from-checkboxes)
  4821. (remove-hook 'org-blocker-hook
  4822. 'org-block-todo-from-checkboxes))
  4823. ;; Align options lines
  4824. (org-set-local
  4825. 'align-mode-rules-list
  4826. '((org-in-buffer-settings
  4827. (regexp . "^[ \t]*#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4828. (modes . '(org-mode)))))
  4829. ;; Imenu
  4830. (org-set-local 'imenu-create-index-function
  4831. 'org-imenu-get-tree)
  4832. ;; Make isearch reveal context
  4833. (if (or (featurep 'xemacs)
  4834. (not (boundp 'outline-isearch-open-invisible-function)))
  4835. ;; Emacs 21 and XEmacs make use of the hook
  4836. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4837. ;; Emacs 22 deals with this through a special variable
  4838. (org-set-local 'outline-isearch-open-invisible-function
  4839. (lambda (&rest ignore) (org-show-context 'isearch))))
  4840. ;; Setup the pcomplete hooks
  4841. (set (make-local-variable 'pcomplete-command-completion-function)
  4842. 'org-pcomplete-initial)
  4843. (set (make-local-variable 'pcomplete-command-name-function)
  4844. 'org-command-at-point)
  4845. (set (make-local-variable 'pcomplete-default-completion-function)
  4846. 'ignore)
  4847. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4848. 'org-parse-arguments)
  4849. (set (make-local-variable 'pcomplete-termination-string) "")
  4850. (when (>= emacs-major-version 23)
  4851. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4852. ;; If empty file that did not turn on org-mode automatically, make it to.
  4853. (if (and org-insert-mode-line-in-empty-file
  4854. (org-called-interactively-p 'any)
  4855. (= (point-min) (point-max)))
  4856. (insert "# -*- mode: org -*-\n\n"))
  4857. (unless org-inhibit-startup
  4858. (org-unmodified
  4859. (and org-startup-with-beamer-mode (org-beamer-mode))
  4860. (when org-startup-align-all-tables
  4861. (org-table-map-tables 'org-table-align 'quietly))
  4862. (when org-startup-with-inline-images
  4863. (org-display-inline-images))
  4864. (when org-startup-with-latex-preview
  4865. (org-toggle-latex-fragment))
  4866. (unless org-inhibit-startup-visibility-stuff
  4867. (org-set-startup-visibility))
  4868. (org-refresh-effort-properties)))
  4869. ;; Try to set org-hide correctly
  4870. (let ((foreground (org-find-invisible-foreground)))
  4871. (if foreground
  4872. (set-face-foreground 'org-hide foreground))))
  4873. ;; Update `customize-package-emacs-version-alist'
  4874. (add-to-list 'customize-package-emacs-version-alist
  4875. '(Org ("6.21b" . "23.1") ("6.33x" . "23.2")
  4876. ("7.8.11" . "24.1") ("7.9.4" . "24.3")
  4877. ("8.2.6" . "24.4") ("8.3" . "25.1")))
  4878. (defvar org-mode-transpose-word-syntax-table
  4879. (let ((st (make-syntax-table text-mode-syntax-table)))
  4880. (mapc (lambda(c) (modify-syntax-entry
  4881. (string-to-char (car c)) "w p" st))
  4882. org-emphasis-alist)
  4883. st))
  4884. (when (fboundp 'abbrev-table-put)
  4885. (abbrev-table-put org-mode-abbrev-table
  4886. :parents (list text-mode-abbrev-table)))
  4887. (defun org-find-invisible-foreground ()
  4888. (let ((candidates (remove
  4889. "unspecified-bg"
  4890. (nconc
  4891. (list (face-background 'default)
  4892. (face-background 'org-default))
  4893. (mapcar
  4894. (lambda (alist)
  4895. (when (boundp alist)
  4896. (cdr (assoc 'background-color (symbol-value alist)))))
  4897. '(default-frame-alist initial-frame-alist window-system-default-frame-alist))
  4898. (list (face-foreground 'org-hide))))))
  4899. (car (remove nil candidates))))
  4900. (defun org-current-time (&optional rounding-minutes past)
  4901. "Current time, possibly rounded to ROUNDING-MINUTES.
  4902. When ROUNDING-MINUTES is not an integer, fall back on the car of
  4903. `org-time-stamp-rounding-minutes'. When PAST is non-nil, ensure
  4904. the rounding returns a past time."
  4905. (let ((r (or (and (integerp rounding-minutes) rounding-minutes)
  4906. (car org-time-stamp-rounding-minutes)))
  4907. (time (decode-time)) res)
  4908. (if (< r 1)
  4909. (current-time)
  4910. (setq res
  4911. (apply 'encode-time
  4912. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4913. (nthcdr 2 time))))
  4914. (if (and past (< (org-float-time (time-subtract (current-time) res)) 0))
  4915. (seconds-to-time (- (org-float-time res) (* r 60)))
  4916. res))))
  4917. (defun org-today ()
  4918. "Return today date, considering `org-extend-today-until'."
  4919. (time-to-days
  4920. (time-subtract (current-time)
  4921. (list 0 (* 3600 org-extend-today-until) 0))))
  4922. ;;;; Font-Lock stuff, including the activators
  4923. (defvar org-mouse-map (make-sparse-keymap))
  4924. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4925. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4926. (when org-mouse-1-follows-link
  4927. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4928. (when org-tab-follows-link
  4929. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4930. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4931. (require 'font-lock)
  4932. (defconst org-non-link-chars "]\t\n\r<>")
  4933. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "file+emacs"
  4934. "file+sys" "news" "shell" "elisp" "doi" "message"
  4935. "help"))
  4936. (defvar org-link-types-re nil
  4937. "Matches a link that has a url-like prefix like \"http:\"")
  4938. (defvar org-link-re-with-space nil
  4939. "Matches a link with spaces, optional angular brackets around it.")
  4940. (defvar org-link-re-with-space2 nil
  4941. "Matches a link with spaces, optional angular brackets around it.")
  4942. (defvar org-link-re-with-space3 nil
  4943. "Matches a link with spaces, only for internal part in bracket links.")
  4944. (defvar org-angle-link-re nil
  4945. "Matches link with angular brackets, spaces are allowed.")
  4946. (defvar org-plain-link-re nil
  4947. "Matches plain link, without spaces.")
  4948. (defvar org-bracket-link-regexp nil
  4949. "Matches a link in double brackets.")
  4950. (defvar org-bracket-link-analytic-regexp nil
  4951. "Regular expression used to analyze links.
  4952. Here is what the match groups contain after a match:
  4953. 1: http:
  4954. 2: http
  4955. 3: path
  4956. 4: [desc]
  4957. 5: desc")
  4958. (defvar org-bracket-link-analytic-regexp++ nil
  4959. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4960. (defvar org-any-link-re nil
  4961. "Regular expression matching any link.")
  4962. (defconst org-match-sexp-depth 3
  4963. "Number of stacked braces for sub/superscript matching.")
  4964. (defun org-create-multibrace-regexp (left right n)
  4965. "Create a regular expression which will match a balanced sexp.
  4966. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4967. as single character strings.
  4968. The regexp returned will match the entire expression including the
  4969. delimiters. It will also define a single group which contains the
  4970. match except for the outermost delimiters. The maximum depth of
  4971. stacked delimiters is N. Escaping delimiters is not possible."
  4972. (let* ((nothing (concat "[^" left right "]*?"))
  4973. (or "\\|")
  4974. (re nothing)
  4975. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4976. (while (> n 1)
  4977. (setq n (1- n)
  4978. re (concat re or next)
  4979. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4980. (concat left "\\(" re "\\)" right)))
  4981. (defconst org-match-substring-regexp
  4982. (concat
  4983. "\\(\\S-\\)\\([_^]\\)\\("
  4984. "\\(?:" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4985. "\\|"
  4986. "\\(?:" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4987. "\\|"
  4988. "\\(?:\\*\\|[+-]?[[:alnum:].,\\]*[[:alnum:]]\\)\\)")
  4989. "The regular expression matching a sub- or superscript.")
  4990. (defconst org-match-substring-with-braces-regexp
  4991. (concat
  4992. "\\(\\S-\\)\\([_^]\\)"
  4993. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)")
  4994. "The regular expression matching a sub- or superscript, forcing braces.")
  4995. (defun org-make-link-regexps ()
  4996. "Update the link regular expressions.
  4997. This should be called after the variable `org-link-types' has changed."
  4998. (let ((types-re (regexp-opt org-link-types t)))
  4999. (setq org-link-types-re
  5000. (concat "\\`" types-re ":")
  5001. org-link-re-with-space
  5002. (concat "<?" types-re ":"
  5003. "\\([^" org-non-link-chars " ]"
  5004. "[^" org-non-link-chars "]*"
  5005. "[^" org-non-link-chars " ]\\)>?")
  5006. org-link-re-with-space2
  5007. (concat "<?" types-re ":"
  5008. "\\([^" org-non-link-chars " ]"
  5009. "[^\t\n\r]*"
  5010. "[^" org-non-link-chars " ]\\)>?")
  5011. org-link-re-with-space3
  5012. (concat "<?" types-re ":"
  5013. "\\([^" org-non-link-chars " ]"
  5014. "[^\t\n\r]*\\)")
  5015. org-angle-link-re
  5016. (format "<%s:\\([^>\n]*\\(?:\n[ \t]*[^> \t\n][^>\n]*\\)*\\)>"
  5017. types-re)
  5018. org-plain-link-re
  5019. (concat
  5020. "\\<" types-re ":"
  5021. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  5022. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  5023. org-bracket-link-regexp
  5024. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  5025. org-bracket-link-analytic-regexp
  5026. (concat
  5027. "\\[\\["
  5028. "\\(" types-re ":\\)?"
  5029. "\\([^]]+\\)"
  5030. "\\]"
  5031. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  5032. "\\]")
  5033. org-bracket-link-analytic-regexp++
  5034. (concat
  5035. "\\[\\["
  5036. "\\(" (regexp-opt (cons "coderef" org-link-types) t) ":\\)?"
  5037. "\\([^]]+\\)"
  5038. "\\]"
  5039. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  5040. "\\]")
  5041. org-any-link-re
  5042. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  5043. org-angle-link-re "\\)\\|\\("
  5044. org-plain-link-re "\\)"))))
  5045. (org-make-link-regexps)
  5046. (defvar org-emph-face nil)
  5047. (defun org-do-emphasis-faces (limit)
  5048. "Run through the buffer and emphasize strings."
  5049. (let (rtn a)
  5050. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  5051. (let* ((border (char-after (match-beginning 3)))
  5052. (bre (regexp-quote (char-to-string border))))
  5053. (if (and (not (= border (char-after (match-beginning 4))))
  5054. (not (save-match-data
  5055. (string-match (concat bre ".*" bre)
  5056. (replace-regexp-in-string
  5057. "\n" " "
  5058. (substring (match-string 2) 1 -1))))))
  5059. (progn
  5060. (setq rtn t)
  5061. (setq a (assoc (match-string 3) org-emphasis-alist))
  5062. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  5063. 'face
  5064. (nth 1 a))
  5065. (and (nth 2 a)
  5066. (org-remove-flyspell-overlays-in
  5067. (match-beginning 0) (match-end 0)))
  5068. (add-text-properties (match-beginning 2) (match-end 2)
  5069. '(font-lock-multiline t org-emphasis t))
  5070. (when org-hide-emphasis-markers
  5071. (add-text-properties (match-end 4) (match-beginning 5)
  5072. '(invisible org-link))
  5073. (add-text-properties (match-beginning 3) (match-end 3)
  5074. '(invisible org-link))))))
  5075. (goto-char (1+ (match-beginning 0))))
  5076. rtn))
  5077. (defun org-emphasize (&optional char)
  5078. "Insert or change an emphasis, i.e. a font like bold or italic.
  5079. If there is an active region, change that region to a new emphasis.
  5080. If there is no region, just insert the marker characters and position
  5081. the cursor between them.
  5082. CHAR should be the marker character. If it is a space, it means to
  5083. remove the emphasis of the selected region.
  5084. If CHAR is not given (for example in an interactive call) it will be
  5085. prompted for."
  5086. (interactive)
  5087. (let ((erc org-emphasis-regexp-components)
  5088. (prompt "")
  5089. (string "") beg end move c s)
  5090. (if (org-region-active-p)
  5091. (setq beg (region-beginning) end (region-end)
  5092. string (buffer-substring beg end))
  5093. (setq move t))
  5094. (unless char
  5095. (message "Emphasis marker or tag: [%s]"
  5096. (mapconcat (lambda(e) (car e)) org-emphasis-alist ""))
  5097. (setq char (read-char-exclusive)))
  5098. (if (equal char ?\ )
  5099. (setq s "" move nil)
  5100. (unless (assoc (char-to-string char) org-emphasis-alist)
  5101. (user-error "No such emphasis marker: \"%c\"" char))
  5102. (setq s (char-to-string char)))
  5103. (while (and (> (length string) 1)
  5104. (equal (substring string 0 1) (substring string -1))
  5105. (assoc (substring string 0 1) org-emphasis-alist))
  5106. (setq string (substring string 1 -1)))
  5107. (setq string (concat s string s))
  5108. (if beg (delete-region beg end))
  5109. (unless (or (bolp)
  5110. (string-match (concat "[" (nth 0 erc) "\n]")
  5111. (char-to-string (char-before (point)))))
  5112. (insert " "))
  5113. (unless (or (eobp)
  5114. (string-match (concat "[" (nth 1 erc) "\n]")
  5115. (char-to-string (char-after (point)))))
  5116. (insert " ") (backward-char 1))
  5117. (insert string)
  5118. (and move (backward-char 1))))
  5119. (defconst org-nonsticky-props
  5120. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
  5121. (defsubst org-rear-nonsticky-at (pos)
  5122. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  5123. (defun org-activate-plain-links (limit)
  5124. "Add link properties for plain links."
  5125. (let (f hl)
  5126. (when (and (re-search-forward (concat org-plain-link-re) limit t)
  5127. (not (member 'org-tag
  5128. (get-text-property (1- (match-beginning 0)) 'face)))
  5129. (not (org-in-src-block-p)))
  5130. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5131. (setq f (get-text-property (match-beginning 0) 'face))
  5132. (setq hl (org-match-string-no-properties 0))
  5133. (if (or (eq f 'org-tag)
  5134. (and (listp f) (memq 'org-tag f)))
  5135. nil
  5136. (add-text-properties (match-beginning 0) (match-end 0)
  5137. (list 'mouse-face 'highlight
  5138. 'face 'org-link
  5139. 'htmlize-link `(:uri ,hl)
  5140. 'keymap org-mouse-map))
  5141. (org-rear-nonsticky-at (match-end 0)))
  5142. t)))
  5143. (defun org-activate-code (limit)
  5144. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  5145. (progn
  5146. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5147. (remove-text-properties (match-beginning 0) (match-end 0)
  5148. '(display t invisible t intangible t))
  5149. t)))
  5150. (defcustom org-src-fontify-natively t
  5151. "When non-nil, fontify code in code blocks."
  5152. :type 'boolean
  5153. :version "24.4"
  5154. :package-version '(Org . "8.3")
  5155. :group 'org-appearance
  5156. :group 'org-babel)
  5157. (defcustom org-allow-promoting-top-level-subtree nil
  5158. "When non-nil, allow promoting a top level subtree.
  5159. The leading star of the top level headline will be replaced
  5160. by a #."
  5161. :type 'boolean
  5162. :version "24.1"
  5163. :group 'org-appearance)
  5164. (defun org-fontify-meta-lines-and-blocks (limit)
  5165. (condition-case nil
  5166. (org-fontify-meta-lines-and-blocks-1 limit)
  5167. (error (message "org-mode fontification error"))))
  5168. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  5169. "Fontify #+ lines and blocks."
  5170. (let ((case-fold-search t))
  5171. (if (re-search-forward
  5172. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  5173. limit t)
  5174. (let ((beg (match-beginning 0))
  5175. (block-start (match-end 0))
  5176. (block-end nil)
  5177. (lang (match-string 7))
  5178. (beg1 (line-beginning-position 2))
  5179. (dc1 (downcase (match-string 2)))
  5180. (dc3 (downcase (match-string 3)))
  5181. end end1 quoting block-type ovl)
  5182. (cond
  5183. ((and (match-end 4) (equal dc3 "+begin"))
  5184. ;; Truly a block
  5185. (setq block-type (downcase (match-string 5))
  5186. quoting (member block-type org-protecting-blocks))
  5187. (when (re-search-forward
  5188. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  5189. nil t) ;; on purpose, we look further than LIMIT
  5190. (setq end (min (point-max) (match-end 0))
  5191. end1 (min (point-max) (1- (match-beginning 0))))
  5192. (setq block-end (match-beginning 0))
  5193. (when quoting
  5194. (org-remove-flyspell-overlays-in beg1 end1)
  5195. (remove-text-properties beg end
  5196. '(display t invisible t intangible t)))
  5197. (add-text-properties
  5198. beg end '(font-lock-fontified t font-lock-multiline t))
  5199. (add-text-properties beg beg1 '(face org-meta-line))
  5200. (org-remove-flyspell-overlays-in beg beg1)
  5201. (add-text-properties ; For end_src
  5202. end1 (min (point-max) (1+ end)) '(face org-meta-line))
  5203. (org-remove-flyspell-overlays-in end1 end)
  5204. (cond
  5205. ((and lang (not (string= lang "")) org-src-fontify-natively)
  5206. (org-src-font-lock-fontify-block lang block-start block-end)
  5207. (add-text-properties beg1 block-end '(src-block t)))
  5208. (quoting
  5209. (add-text-properties beg1 (min (point-max) (1+ end1))
  5210. '(face org-block))) ; end of source block
  5211. ((not org-fontify-quote-and-verse-blocks))
  5212. ((string= block-type "quote")
  5213. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  5214. ((string= block-type "verse")
  5215. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  5216. (add-text-properties beg beg1 '(face org-block-begin-line))
  5217. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  5218. '(face org-block-end-line))
  5219. t))
  5220. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  5221. (org-remove-flyspell-overlays-in
  5222. (match-beginning 0)
  5223. (if (equal "+title:" dc1) (match-end 2) (match-end 0)))
  5224. (add-text-properties
  5225. beg (match-end 3)
  5226. (if (member (intern (substring dc1 1 -1)) org-hidden-keywords)
  5227. '(font-lock-fontified t invisible t)
  5228. '(font-lock-fontified t face org-document-info-keyword)))
  5229. (add-text-properties
  5230. (match-beginning 6) (min (point-max) (1+ (match-end 6)))
  5231. (if (string-equal dc1 "+title:")
  5232. '(font-lock-fontified t face org-document-title)
  5233. '(font-lock-fontified t face org-document-info))))
  5234. ((equal dc1 "+caption:")
  5235. (org-remove-flyspell-overlays-in (match-end 2) (match-end 0))
  5236. (remove-text-properties (match-beginning 0) (match-end 0)
  5237. '(display t invisible t intangible t))
  5238. (add-text-properties (match-beginning 1) (match-end 3)
  5239. '(font-lock-fontified t face org-meta-line))
  5240. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  5241. '(font-lock-fontified t face org-block))
  5242. t)
  5243. ((member dc3 '(" " ""))
  5244. (org-remove-flyspell-overlays-in beg (match-end 0))
  5245. (add-text-properties
  5246. beg (match-end 0)
  5247. '(font-lock-fontified t face font-lock-comment-face)))
  5248. (t ;; just any other in-buffer setting, but not indented
  5249. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5250. (remove-text-properties (match-beginning 0) (match-end 0)
  5251. '(display t invisible t intangible t))
  5252. (add-text-properties beg (match-end 0)
  5253. '(font-lock-fontified t face org-meta-line))
  5254. t))))))
  5255. (defun org-fontify-drawers (limit)
  5256. "Fontify drawers."
  5257. (when (re-search-forward org-drawer-regexp limit t)
  5258. (add-text-properties
  5259. (match-beginning 0) (match-end 0)
  5260. '(font-lock-fontified t face org-special-keyword))
  5261. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5262. t))
  5263. (defun org-fontify-macros (limit)
  5264. "Fontify macros."
  5265. (when (re-search-forward "\\({{{\\).+?\\(}}}\\)" limit t)
  5266. (add-text-properties
  5267. (match-beginning 0) (match-end 0)
  5268. '(font-lock-fontified t face org-macro))
  5269. (when org-hide-macro-markers
  5270. (add-text-properties (match-end 2) (match-beginning 2)
  5271. '(invisible t))
  5272. (add-text-properties (match-beginning 1) (match-end 1)
  5273. '(invisible t)))
  5274. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5275. t))
  5276. (defun org-activate-angle-links (limit)
  5277. "Add text properties for angle links."
  5278. (if (and (re-search-forward org-angle-link-re limit t)
  5279. (not (org-in-src-block-p)))
  5280. (progn
  5281. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5282. (add-text-properties (match-beginning 0) (match-end 0)
  5283. (list 'mouse-face 'highlight
  5284. 'keymap org-mouse-map
  5285. 'font-lock-multiline t))
  5286. (org-rear-nonsticky-at (match-end 0))
  5287. t)))
  5288. (defun org-activate-footnote-links (limit)
  5289. "Add text properties for footnotes."
  5290. (let ((fn (org-footnote-next-reference-or-definition limit)))
  5291. (when fn
  5292. (let* ((beg (nth 1 fn))
  5293. (end (nth 2 fn))
  5294. (label (car fn))
  5295. (referencep (/= (line-beginning-position) beg)))
  5296. (when (and referencep (nth 3 fn))
  5297. (save-excursion
  5298. (goto-char beg)
  5299. (search-forward (or label "fn:"))
  5300. (org-remove-flyspell-overlays-in beg (match-end 0))))
  5301. (add-text-properties beg end
  5302. (list 'mouse-face 'highlight
  5303. 'keymap org-mouse-map
  5304. 'help-echo
  5305. (if referencep "Footnote reference"
  5306. "Footnote definition")
  5307. 'font-lock-fontified t
  5308. 'font-lock-multiline t
  5309. 'face 'org-footnote))))))
  5310. (defun org-activate-bracket-links (limit)
  5311. "Add text properties for bracketed links."
  5312. (if (and (re-search-forward org-bracket-link-regexp limit t)
  5313. (not (org-in-src-block-p)))
  5314. (let* ((hl (org-match-string-no-properties 1))
  5315. (help (concat "LINK: " (save-match-data (org-link-unescape hl))))
  5316. (ip (org-maybe-intangible
  5317. (list 'invisible 'org-link
  5318. 'keymap org-mouse-map 'mouse-face 'highlight
  5319. 'font-lock-multiline t 'help-echo help
  5320. 'htmlize-link `(:uri ,hl))))
  5321. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  5322. 'font-lock-multiline t 'help-echo help
  5323. 'htmlize-link `(:uri ,hl))))
  5324. ;; We need to remove the invisible property here. Table narrowing
  5325. ;; may have made some of this invisible.
  5326. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5327. (remove-text-properties (match-beginning 0) (match-end 0)
  5328. '(invisible nil))
  5329. (if (match-end 3)
  5330. (progn
  5331. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  5332. (org-rear-nonsticky-at (match-beginning 3))
  5333. (add-text-properties (match-beginning 3) (match-end 3) vp)
  5334. (org-rear-nonsticky-at (match-end 3))
  5335. (add-text-properties (match-end 3) (match-end 0) ip)
  5336. (org-rear-nonsticky-at (match-end 0)))
  5337. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  5338. (org-rear-nonsticky-at (match-beginning 1))
  5339. (add-text-properties (match-beginning 1) (match-end 1) vp)
  5340. (org-rear-nonsticky-at (match-end 1))
  5341. (add-text-properties (match-end 1) (match-end 0) ip)
  5342. (org-rear-nonsticky-at (match-end 0)))
  5343. t)))
  5344. (defun org-activate-dates (limit)
  5345. "Add text properties for dates."
  5346. (if (and (re-search-forward org-tsr-regexp-both limit t)
  5347. (not (equal (char-before (match-beginning 0)) 91)))
  5348. (progn
  5349. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5350. (add-text-properties (match-beginning 0) (match-end 0)
  5351. (list 'mouse-face 'highlight
  5352. 'keymap org-mouse-map))
  5353. (org-rear-nonsticky-at (match-end 0))
  5354. (when org-display-custom-times
  5355. (if (match-end 3)
  5356. (org-display-custom-time (match-beginning 3) (match-end 3)))
  5357. (org-display-custom-time (match-beginning 1) (match-end 1)))
  5358. t)))
  5359. (defvar org-target-link-regexp nil
  5360. "Regular expression matching radio targets in plain text.")
  5361. (make-variable-buffer-local 'org-target-link-regexp)
  5362. (defconst org-target-regexp (let ((border "[^<>\n\r \t]"))
  5363. (format "<<\\(%s\\|%s[^<>\n\r]*%s\\)>>"
  5364. border border border))
  5365. "Regular expression matching a link target.")
  5366. (defconst org-radio-target-regexp (format "<%s>" org-target-regexp)
  5367. "Regular expression matching a radio target.")
  5368. (defconst org-any-target-regexp
  5369. (format "%s\\|%s" org-radio-target-regexp org-target-regexp)
  5370. "Regular expression matching any target.")
  5371. (defun org-activate-target-links (limit)
  5372. "Add text properties for target matches."
  5373. (when org-target-link-regexp
  5374. (let ((case-fold-search t))
  5375. (if (re-search-forward org-target-link-regexp limit t)
  5376. (progn
  5377. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5378. (add-text-properties (match-beginning 1) (match-end 1)
  5379. (list 'mouse-face 'highlight
  5380. 'keymap org-mouse-map
  5381. 'help-echo "Radio target link"
  5382. 'org-linked-text t))
  5383. (org-rear-nonsticky-at (match-end 1))
  5384. t)))))
  5385. (defun org-update-radio-target-regexp ()
  5386. "Find all radio targets in this file and update the regular expression.
  5387. Also refresh fontification if needed."
  5388. (interactive)
  5389. (let ((old-regexp org-target-link-regexp)
  5390. (before-re "\\(?:^\\|[^[:alnum:]]\\)\\(")
  5391. (after-re "\\)\\(?:$\\|[^[:alnum:]]\\)")
  5392. (targets
  5393. (org-with-wide-buffer
  5394. (goto-char (point-min))
  5395. (let (rtn)
  5396. (while (re-search-forward org-radio-target-regexp nil t)
  5397. ;; Make sure point is really within the object.
  5398. (backward-char)
  5399. (let ((obj (org-element-context)))
  5400. (when (eq (org-element-type obj) 'radio-target)
  5401. (add-to-list 'rtn (org-element-property :value obj)))))
  5402. rtn))))
  5403. (setq org-target-link-regexp
  5404. (and targets
  5405. (concat before-re
  5406. (mapconcat
  5407. (lambda (x)
  5408. (replace-regexp-in-string
  5409. " +" "\\s-+" (regexp-quote x) t t))
  5410. targets
  5411. "\\|")
  5412. after-re)))
  5413. (unless (equal old-regexp org-target-link-regexp)
  5414. ;; Clean-up cache.
  5415. (let ((regexp (cond ((not old-regexp) org-target-link-regexp)
  5416. ((not org-target-link-regexp) old-regexp)
  5417. (t
  5418. (concat before-re
  5419. (mapconcat
  5420. (lambda (re)
  5421. (substring re (length before-re)
  5422. (- (length after-re))))
  5423. (list old-regexp org-target-link-regexp)
  5424. "\\|")
  5425. after-re)))))
  5426. (org-with-wide-buffer
  5427. (goto-char (point-min))
  5428. (while (re-search-forward regexp nil t)
  5429. (org-element-cache-refresh (match-beginning 1)))))
  5430. ;; Re fontify buffer.
  5431. (when (memq 'radio org-highlight-links)
  5432. (org-restart-font-lock)))))
  5433. (defun org-hide-wide-columns (limit)
  5434. (let (s e)
  5435. (setq s (text-property-any (point) (or limit (point-max))
  5436. 'org-cwidth t))
  5437. (when s
  5438. (setq e (next-single-property-change s 'org-cwidth))
  5439. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5440. (goto-char e)
  5441. t)))
  5442. (defvar org-latex-and-related-regexp nil
  5443. "Regular expression for highlighting LaTeX, entities and sub/superscript.")
  5444. (defun org-compute-latex-and-related-regexp ()
  5445. "Compute regular expression for LaTeX, entities and sub/superscript.
  5446. Result depends on variable `org-highlight-latex-and-related'."
  5447. (org-set-local
  5448. 'org-latex-and-related-regexp
  5449. (let* ((re-sub
  5450. (cond ((not (memq 'script org-highlight-latex-and-related)) nil)
  5451. ((eq org-use-sub-superscripts '{})
  5452. (list org-match-substring-with-braces-regexp))
  5453. (org-use-sub-superscripts (list org-match-substring-regexp))))
  5454. (re-latex
  5455. (when (memq 'latex org-highlight-latex-and-related)
  5456. (let ((matchers (plist-get org-format-latex-options :matchers)))
  5457. (delq nil
  5458. (mapcar (lambda (x)
  5459. (and (member (car x) matchers) (nth 1 x)))
  5460. org-latex-regexps)))))
  5461. (re-entities
  5462. (when (memq 'entities org-highlight-latex-and-related)
  5463. (list "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))))
  5464. (mapconcat 'identity (append re-latex re-entities re-sub) "\\|"))))
  5465. (defun org-do-latex-and-related (limit)
  5466. "Highlight LaTeX snippets and environments, entities and sub/superscript.
  5467. LIMIT bounds the search for syntax to highlight. Stop at first
  5468. highlighted object, if any. Return t if some highlighting was
  5469. done, nil otherwise."
  5470. (when (org-string-nw-p org-latex-and-related-regexp)
  5471. (catch 'found
  5472. (while (re-search-forward org-latex-and-related-regexp limit t)
  5473. (unless (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5474. 'face))
  5475. '(org-code org-verbatim underline))
  5476. (let ((offset (if (memq (char-after (1+ (match-beginning 0)))
  5477. '(?_ ?^))
  5478. 1
  5479. 0)))
  5480. (font-lock-prepend-text-property
  5481. (+ offset (match-beginning 0)) (match-end 0)
  5482. 'face 'org-latex-and-related)
  5483. (add-text-properties (+ offset (match-beginning 0)) (match-end 0)
  5484. '(font-lock-multiline t)))
  5485. (throw 'found t)))
  5486. nil)))
  5487. (defun org-restart-font-lock ()
  5488. "Restart `font-lock-mode', to force refontification."
  5489. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5490. (font-lock-mode -1)
  5491. (font-lock-mode 1)))
  5492. (defun org-activate-tags (limit)
  5493. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5494. (progn
  5495. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5496. (add-text-properties (match-beginning 1) (match-end 1)
  5497. (list 'mouse-face 'highlight
  5498. 'keymap org-mouse-map))
  5499. (org-rear-nonsticky-at (match-end 1))
  5500. t)))
  5501. (defun org-outline-level ()
  5502. "Compute the outline level of the heading at point.
  5503. If this is called at a normal headline, the level is the number
  5504. of stars. Use `org-reduced-level' to remove the effect of
  5505. `org-odd-levels'. Unlike to `org-current-level', this function
  5506. takes into consideration inlinetasks."
  5507. (org-with-wide-buffer
  5508. (end-of-line)
  5509. (if (re-search-backward org-outline-regexp-bol nil t)
  5510. (1- (- (match-end 0) (match-beginning 0)))
  5511. 0)))
  5512. (defvar org-font-lock-keywords nil)
  5513. (defsubst org-re-property (property &optional literal allow-null value)
  5514. "Return a regexp matching a PROPERTY line.
  5515. When optional argument LITERAL is non-nil, do not quote PROPERTY.
  5516. This is useful when PROPERTY is a regexp. When ALLOW-NULL is
  5517. non-nil, match properties even without a value.
  5518. Match group 3 is set to the value when it exists. If there is no
  5519. value and ALLOW-NULL is non-nil, it is set to the empty string.
  5520. With optional argument VALUE, match only property lines with
  5521. that value; in this case, ALLOW-NULL is ignored. VALUE is quoted
  5522. unless LITERAL is non-nil."
  5523. (concat
  5524. "^\\(?4:[ \t]*\\)"
  5525. (format "\\(?1::\\(?2:%s\\):\\)"
  5526. (if literal property (regexp-quote property)))
  5527. (cond (value
  5528. (format "[ \t]+\\(?3:%s\\)\\(?5:[ \t]*\\)$"
  5529. (if literal value (regexp-quote value))))
  5530. (allow-null
  5531. "\\(?:\\(?3:$\\)\\|[ \t]+\\(?3:.*?\\)\\)\\(?5:[ \t]*\\)$")
  5532. (t
  5533. "[ \t]+\\(?3:[^ \r\t\n]+.*?\\)\\(?5:[ \t]*\\)$"))))
  5534. (defconst org-property-re
  5535. (org-re-property "\\S-+" 'literal t)
  5536. "Regular expression matching a property line.
  5537. There are four matching groups:
  5538. 1: :PROPKEY: including the leading and trailing colon,
  5539. 2: PROPKEY without the leading and trailing colon,
  5540. 3: PROPVAL without leading or trailing spaces,
  5541. 4: the indentation of the current line,
  5542. 5: trailing whitespace.")
  5543. (defvar org-font-lock-hook nil
  5544. "Functions to be called for special font lock stuff.")
  5545. (defvar org-font-lock-set-keywords-hook nil
  5546. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5547. This is called after `org-font-lock-extra-keywords' is defined, but before
  5548. it is installed to be used by font lock. This can be useful if something
  5549. needs to be inserted at a specific position in the font-lock sequence.")
  5550. (defun org-font-lock-hook (limit)
  5551. "Run `org-font-lock-hook' within LIMIT."
  5552. (run-hook-with-args 'org-font-lock-hook limit))
  5553. (defun org-set-font-lock-defaults ()
  5554. "Set font lock defaults for the current buffer."
  5555. (let* ((em org-fontify-emphasized-text)
  5556. (lk org-highlight-links)
  5557. (org-font-lock-extra-keywords
  5558. (list
  5559. ;; Call the hook
  5560. '(org-font-lock-hook)
  5561. ;; Headlines
  5562. `(,(if org-fontify-whole-heading-line
  5563. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5564. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5565. (1 (org-get-level-face 1))
  5566. (2 (org-get-level-face 2))
  5567. (3 (org-get-level-face 3)))
  5568. ;; Table lines
  5569. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5570. (1 'org-table t))
  5571. ;; Table internals
  5572. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5573. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5574. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5575. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5576. ;; Drawers
  5577. '(org-fontify-drawers)
  5578. ;; Properties
  5579. (list org-property-re
  5580. '(1 'org-special-keyword t)
  5581. '(3 'org-property-value t))
  5582. ;; Links
  5583. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5584. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5585. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  5586. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5587. (if (memq 'radio lk) '(org-activate-target-links (1 'org-link t)))
  5588. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5589. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5590. ;; Targets.
  5591. (list org-any-target-regexp '(0 'org-target t))
  5592. ;; Diary sexps.
  5593. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5594. ;; Macro
  5595. '(org-fontify-macros)
  5596. '(org-hide-wide-columns (0 nil append))
  5597. ;; TODO keyword
  5598. (list (format org-heading-keyword-regexp-format
  5599. org-todo-regexp)
  5600. '(2 (org-get-todo-face 2) t))
  5601. ;; DONE
  5602. (if org-fontify-done-headline
  5603. (list (format org-heading-keyword-regexp-format
  5604. (concat
  5605. "\\(?:"
  5606. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5607. "\\)"))
  5608. '(2 'org-headline-done t))
  5609. nil)
  5610. ;; Priorities
  5611. '(org-font-lock-add-priority-faces)
  5612. ;; Tags
  5613. '(org-font-lock-add-tag-faces)
  5614. ;; Tags groups
  5615. (if (and org-group-tags org-tag-groups-alist)
  5616. (list (concat org-outline-regexp-bol ".+\\(:"
  5617. (regexp-opt (mapcar 'car org-tag-groups-alist))
  5618. ":\\).*$")
  5619. '(1 'org-tag-group prepend)))
  5620. ;; Special keywords
  5621. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5622. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5623. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5624. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5625. ;; Emphasis
  5626. (if em
  5627. (if (featurep 'xemacs)
  5628. '(org-do-emphasis-faces (0 nil append))
  5629. '(org-do-emphasis-faces)))
  5630. ;; Checkboxes
  5631. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5632. 1 'org-checkbox prepend)
  5633. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5634. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5635. (0 (org-get-checkbox-statistics-face) t)))
  5636. ;; Description list items
  5637. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5638. 1 'org-list-dt prepend)
  5639. ;; ARCHIVEd headings
  5640. (list (concat
  5641. org-outline-regexp-bol
  5642. "\\(.*:" org-archive-tag ":.*\\)")
  5643. '(1 'org-archived prepend))
  5644. ;; Specials
  5645. '(org-do-latex-and-related)
  5646. '(org-fontify-entities)
  5647. '(org-raise-scripts)
  5648. ;; Code
  5649. '(org-activate-code (1 'org-code t))
  5650. ;; COMMENT
  5651. (list (format
  5652. "^\\*+\\(?: +%s\\)?\\(?: +\\[#[A-Z0-9]\\]\\)? +\\(?9:%s\\)\\(?: \\|$\\)"
  5653. org-todo-regexp
  5654. org-comment-string)
  5655. '(9 'org-special-keyword t))
  5656. ;; Blocks and meta lines
  5657. '(org-fontify-meta-lines-and-blocks))))
  5658. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5659. (run-hooks 'org-font-lock-set-keywords-hook)
  5660. ;; Now set the full font-lock-keywords
  5661. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5662. (org-set-local 'font-lock-defaults
  5663. '(org-font-lock-keywords t nil nil backward-paragraph))
  5664. (kill-local-variable 'font-lock-keywords) nil))
  5665. (defun org-toggle-pretty-entities ()
  5666. "Toggle the composition display of entities as UTF8 characters."
  5667. (interactive)
  5668. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5669. (org-restart-font-lock)
  5670. (if org-pretty-entities
  5671. (message "Entities are now displayed as UTF8 characters")
  5672. (save-restriction
  5673. (widen)
  5674. (org-decompose-region (point-min) (point-max))
  5675. (message "Entities are now displayed as plain text"))))
  5676. (defvar org-custom-properties-overlays nil
  5677. "List of overlays used for custom properties.")
  5678. (make-variable-buffer-local 'org-custom-properties-overlays)
  5679. (defun org-toggle-custom-properties-visibility ()
  5680. "Display or hide properties in `org-custom-properties'."
  5681. (interactive)
  5682. (if org-custom-properties-overlays
  5683. (progn (mapc #'delete-overlay org-custom-properties-overlays)
  5684. (setq org-custom-properties-overlays nil))
  5685. (when org-custom-properties
  5686. (org-with-wide-buffer
  5687. (goto-char (point-min))
  5688. (let ((regexp (org-re-property (regexp-opt org-custom-properties) t t)))
  5689. (while (re-search-forward regexp nil t)
  5690. (let ((end (cdr (save-match-data (org-get-property-block)))))
  5691. (when (and end (< (point) end))
  5692. ;; Hide first custom property in current drawer.
  5693. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5694. (overlay-put o 'invisible t)
  5695. (overlay-put o 'org-custom-property t)
  5696. (push o org-custom-properties-overlays))
  5697. ;; Hide additional custom properties in the same drawer.
  5698. (while (re-search-forward regexp end t)
  5699. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5700. (overlay-put o 'invisible t)
  5701. (overlay-put o 'org-custom-property t)
  5702. (push o org-custom-properties-overlays)))))
  5703. ;; Each entry is limited to a single property drawer.
  5704. (outline-next-heading)))))))
  5705. (defun org-fontify-entities (limit)
  5706. "Find an entity to fontify."
  5707. (let (ee)
  5708. (when org-pretty-entities
  5709. (catch 'match
  5710. ;; "\_ "-family is left out on purpose. Only the first one,
  5711. ;; i.e., "\_ ", could be fontified anyway, and it would be
  5712. ;; confusing when adding a second white space character.
  5713. (while (re-search-forward
  5714. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5715. limit t)
  5716. (if (and (not (org-at-comment-p))
  5717. (setq ee (org-entity-get (match-string 1)))
  5718. (= (length (nth 6 ee)) 1))
  5719. (let*
  5720. ((end (if (equal (match-string 2) "{}")
  5721. (match-end 2)
  5722. (match-end 1))))
  5723. (add-text-properties
  5724. (match-beginning 0) end
  5725. (list 'font-lock-fontified t))
  5726. (compose-region (match-beginning 0) end
  5727. (nth 6 ee) nil)
  5728. (backward-char 1)
  5729. (throw 'match t))))
  5730. nil))))
  5731. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5732. "Fontify string S like in Org-mode."
  5733. (with-temp-buffer
  5734. (insert s)
  5735. (let ((org-odd-levels-only odd-levels))
  5736. (org-mode)
  5737. (font-lock-ensure)
  5738. (buffer-string))))
  5739. (defvar org-m nil)
  5740. (defvar org-l nil)
  5741. (defvar org-f nil)
  5742. (defun org-get-level-face (n)
  5743. "Get the right face for match N in font-lock matching of headlines."
  5744. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5745. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5746. (if org-cycle-level-faces
  5747. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5748. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5749. (cond
  5750. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5751. ((eq n 2) org-f)
  5752. (t (if org-level-color-stars-only nil org-f))))
  5753. (defun org-face-from-face-or-color (context inherit face-or-color)
  5754. "Create a face list that inherits INHERIT, but sets the foreground color.
  5755. When FACE-OR-COLOR is not a string, just return it."
  5756. (if (stringp face-or-color)
  5757. (list :inherit inherit
  5758. (cdr (assoc context org-faces-easy-properties))
  5759. face-or-color)
  5760. face-or-color))
  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-get-priority-face (priority)
  5770. "Get the right face for PRIORITY.
  5771. PRIORITY is a character."
  5772. (or (org-face-from-face-or-color
  5773. 'priority 'org-priority (cdr (assq priority org-priority-faces)))
  5774. 'org-priority))
  5775. (defun org-get-tag-face (tag)
  5776. "Get the right face for TAG.
  5777. If TAG is a number, get the corresponding match group."
  5778. (let ((tag (if (wholenump tag) (match-string tag) tag)))
  5779. (or (org-face-from-face-or-color
  5780. 'tag 'org-tag (cdr (assoc tag org-tag-faces)))
  5781. 'org-tag)))
  5782. (defun org-font-lock-add-priority-faces (limit)
  5783. "Add the special priority faces."
  5784. (while (re-search-forward "^\\*+ .*?\\(\\[#\\(.\\)\\]\\)" limit t)
  5785. (add-text-properties
  5786. (match-beginning 1) (match-end 1)
  5787. (list 'face (org-get-priority-face (string-to-char (match-string 2)))
  5788. 'font-lock-fontified t))))
  5789. (defun org-font-lock-add-tag-faces (limit)
  5790. "Add the special tag faces."
  5791. (when (and org-tag-faces org-tags-special-faces-re)
  5792. (while (re-search-forward org-tags-special-faces-re limit t)
  5793. (add-text-properties (match-beginning 1) (match-end 1)
  5794. (list 'face (org-get-tag-face 1)
  5795. 'font-lock-fontified t))
  5796. (backward-char 1))))
  5797. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5798. "Remove fontification and activation overlays from links."
  5799. (font-lock-default-unfontify-region beg end)
  5800. (let* ((buffer-undo-list t)
  5801. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5802. (inhibit-modification-hooks t)
  5803. deactivate-mark buffer-file-name buffer-file-truename)
  5804. (org-decompose-region beg end)
  5805. (remove-text-properties beg end
  5806. '(mouse-face t keymap t org-linked-text t
  5807. invisible t intangible t
  5808. org-emphasis t))
  5809. (org-remove-font-lock-display-properties beg end)))
  5810. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5811. ((raise 0.3) (height 0.7))
  5812. ((raise -0.5))
  5813. ((raise 0.5)))
  5814. "Display properties for showing superscripts and subscripts.")
  5815. (defun org-remove-font-lock-display-properties (beg end)
  5816. "Remove specific display properties that have been added by font lock.
  5817. The will remove the raise properties that are used to show superscripts
  5818. and subscripts."
  5819. (let (next prop)
  5820. (while (< beg end)
  5821. (setq next (next-single-property-change beg 'display nil end)
  5822. prop (get-text-property beg 'display))
  5823. (if (member prop org-script-display)
  5824. (put-text-property beg next 'display nil))
  5825. (setq beg next))))
  5826. (defun org-raise-scripts (limit)
  5827. "Add raise properties to sub/superscripts."
  5828. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5829. (if (re-search-forward
  5830. (if (eq org-use-sub-superscripts t)
  5831. org-match-substring-regexp
  5832. org-match-substring-with-braces-regexp)
  5833. limit t)
  5834. (let* ((pos (point)) table-p comment-p
  5835. (mpos (match-beginning 3))
  5836. (emph-p (get-text-property mpos 'org-emphasis))
  5837. (link-p (get-text-property mpos 'mouse-face))
  5838. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5839. (goto-char (point-at-bol))
  5840. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5841. comment-p (org-looking-at-p "^[ \t]*#[ +]"))
  5842. (goto-char pos)
  5843. ;; Handle a_b^c
  5844. (if (member (char-after) '(?_ ?^)) (goto-char (1- pos)))
  5845. (if (or comment-p emph-p link-p keyw-p)
  5846. t
  5847. (put-text-property (match-beginning 3) (match-end 0)
  5848. 'display
  5849. (if (equal (char-after (match-beginning 2)) ?^)
  5850. (nth (if table-p 3 1) org-script-display)
  5851. (nth (if table-p 2 0) org-script-display)))
  5852. (add-text-properties (match-beginning 2) (match-end 2)
  5853. (list 'invisible t
  5854. 'org-dwidth t 'org-dwidth-n 1))
  5855. (if (and (eq (char-after (match-beginning 3)) ?{)
  5856. (eq (char-before (match-end 3)) ?}))
  5857. (progn
  5858. (add-text-properties
  5859. (match-beginning 3) (1+ (match-beginning 3))
  5860. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5861. (add-text-properties
  5862. (1- (match-end 3)) (match-end 3)
  5863. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5864. t)))))
  5865. ;;;; Visibility cycling, including org-goto and indirect buffer
  5866. ;;; Cycling
  5867. (defvar org-cycle-global-status nil)
  5868. (make-variable-buffer-local 'org-cycle-global-status)
  5869. (put 'org-cycle-global-status 'org-state t)
  5870. (defvar org-cycle-subtree-status nil)
  5871. (make-variable-buffer-local 'org-cycle-subtree-status)
  5872. (put 'org-cycle-subtree-status 'org-state t)
  5873. (defvar org-inlinetask-min-level)
  5874. (defun org-unlogged-message (&rest args)
  5875. "Display a message, but avoid logging it in the *Messages* buffer."
  5876. (let ((message-log-max nil))
  5877. (apply 'message args)))
  5878. ;;;###autoload
  5879. (defun org-cycle (&optional arg)
  5880. "TAB-action and visibility cycling for Org-mode.
  5881. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5882. is outline visibility cycling, but it also invokes other actions
  5883. in special contexts.
  5884. - When this function is called with a prefix argument, rotate the entire
  5885. buffer through 3 states (global cycling)
  5886. 1. OVERVIEW: Show only top-level headlines.
  5887. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5888. 3. SHOW ALL: Show everything.
  5889. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5890. determined by the variable `org-startup-folded', and by any VISIBILITY
  5891. properties in the buffer.
  5892. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5893. including any drawers.
  5894. - When inside a table, re-align the table and move to the next field.
  5895. - When point is at the beginning of a headline, rotate the subtree started
  5896. by this line through 3 different states (local cycling)
  5897. 1. FOLDED: Only the main headline is shown.
  5898. 2. CHILDREN: The main headline and the direct children are shown.
  5899. From this state, you can move to one of the children
  5900. and zoom in further.
  5901. 3. SUBTREE: Show the entire subtree, including body text.
  5902. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5903. - When point is at the beginning of an empty headline and the variable
  5904. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5905. of the headline by demoting and promoting it to likely levels. This
  5906. speeds up creation document structure by pressing TAB once or several
  5907. times right after creating a new headline.
  5908. - When there is a numeric prefix, go up to a heading with level ARG, do
  5909. a `show-subtree' and return to the previous cursor position. If ARG
  5910. is negative, go up that many levels.
  5911. - When point is not at the beginning of a headline, execute the global
  5912. binding for TAB, which is re-indenting the line. See the option
  5913. `org-cycle-emulate-tab' for details.
  5914. - Special case: if point is at the beginning of the buffer and there is
  5915. no headline in line 1, this function will act as if called with prefix arg
  5916. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5917. But only if also the variable `org-cycle-global-at-bob' is t."
  5918. (interactive "P")
  5919. (org-load-modules-maybe)
  5920. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5921. (and org-cycle-level-after-item/entry-creation
  5922. (or (org-cycle-level)
  5923. (org-cycle-item-indentation))))
  5924. (let* ((limit-level
  5925. (or org-cycle-max-level
  5926. (and (boundp 'org-inlinetask-min-level)
  5927. org-inlinetask-min-level
  5928. (1- org-inlinetask-min-level))))
  5929. (nstars (and limit-level
  5930. (if org-odd-levels-only
  5931. (and limit-level (1- (* limit-level 2)))
  5932. limit-level)))
  5933. (org-outline-regexp
  5934. (if (not (derived-mode-p 'org-mode))
  5935. outline-regexp
  5936. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5937. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5938. (not (looking-at org-outline-regexp))))
  5939. (org-cycle-hook
  5940. (if bob-special
  5941. (delq 'org-optimize-window-after-visibility-change
  5942. (copy-sequence org-cycle-hook))
  5943. org-cycle-hook))
  5944. (pos (point)))
  5945. (if (or bob-special (equal arg '(4)))
  5946. ;; special case: use global cycling
  5947. (setq arg t))
  5948. (cond
  5949. ((equal arg '(16))
  5950. (setq last-command 'dummy)
  5951. (org-set-startup-visibility)
  5952. (org-unlogged-message "Startup visibility, plus VISIBILITY properties"))
  5953. ((equal arg '(64))
  5954. (show-all)
  5955. (org-unlogged-message "Entire buffer visible, including drawers"))
  5956. ;; Try cdlatex TAB completion
  5957. ((org-try-cdlatex-tab))
  5958. ;; Table: enter it or move to the next field.
  5959. ((org-at-table-p 'any)
  5960. (if (org-at-table.el-p)
  5961. (message "Use C-c ' to edit table.el tables")
  5962. (if arg (org-table-edit-field t)
  5963. (org-table-justify-field-maybe)
  5964. (call-interactively 'org-table-next-field))))
  5965. ((run-hook-with-args-until-success
  5966. 'org-tab-after-check-for-table-hook))
  5967. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5968. ((eq arg t) (org-cycle-internal-global))
  5969. ;; Drawers: delegate to `org-flag-drawer'.
  5970. ((save-excursion
  5971. (beginning-of-line 1)
  5972. (looking-at org-drawer-regexp))
  5973. (org-flag-drawer ; toggle block visibility
  5974. (not (get-char-property (match-end 0) 'invisible))))
  5975. ;; Show-subtree, ARG levels up from here.
  5976. ((integerp arg)
  5977. (save-excursion
  5978. (org-back-to-heading)
  5979. (outline-up-heading (if (< arg 0) (- arg)
  5980. (- (funcall outline-level) arg)))
  5981. (org-show-subtree)))
  5982. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5983. ((and (featurep 'org-inlinetask)
  5984. (org-inlinetask-at-task-p)
  5985. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5986. (org-inlinetask-toggle-visibility))
  5987. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5988. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5989. (save-excursion (move-beginning-of-line 1)
  5990. (looking-at org-outline-regexp)))
  5991. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5992. (org-cycle-internal-local))
  5993. ;; From there: TAB emulation and template completion.
  5994. (buffer-read-only (org-back-to-heading))
  5995. ((run-hook-with-args-until-success
  5996. 'org-tab-after-check-for-cycling-hook))
  5997. ((org-try-structure-completion))
  5998. ((run-hook-with-args-until-success
  5999. 'org-tab-before-tab-emulation-hook))
  6000. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  6001. (or (not (bolp))
  6002. (not (looking-at org-outline-regexp))))
  6003. (call-interactively (global-key-binding "\t")))
  6004. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  6005. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  6006. (or (and (eq org-cycle-emulate-tab 'white)
  6007. (= (match-end 0) (point-at-eol)))
  6008. (and (eq org-cycle-emulate-tab 'whitestart)
  6009. (>= (match-end 0) pos))))
  6010. t
  6011. (eq org-cycle-emulate-tab t))
  6012. (call-interactively (global-key-binding "\t")))
  6013. (t (save-excursion
  6014. (org-back-to-heading)
  6015. (org-cycle)))))))
  6016. (defun org-cycle-internal-global ()
  6017. "Do the global cycling action."
  6018. ;; Hack to avoid display of messages for .org attachments in Gnus
  6019. (let ((ga (string-match "\\*fontification" (buffer-name))))
  6020. (cond
  6021. ((and (eq last-command this-command)
  6022. (eq org-cycle-global-status 'overview))
  6023. ;; We just created the overview - now do table of contents
  6024. ;; This can be slow in very large buffers, so indicate action
  6025. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  6026. (unless ga (org-unlogged-message "CONTENTS..."))
  6027. (org-content)
  6028. (unless ga (org-unlogged-message "CONTENTS...done"))
  6029. (setq org-cycle-global-status 'contents)
  6030. (run-hook-with-args 'org-cycle-hook 'contents))
  6031. ((and (eq last-command this-command)
  6032. (eq org-cycle-global-status 'contents))
  6033. ;; We just showed the table of contents - now show everything
  6034. (run-hook-with-args 'org-pre-cycle-hook 'all)
  6035. (show-all)
  6036. (unless ga (org-unlogged-message "SHOW ALL"))
  6037. (setq org-cycle-global-status 'all)
  6038. (run-hook-with-args 'org-cycle-hook 'all))
  6039. (t
  6040. ;; Default action: go to overview
  6041. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  6042. (org-overview)
  6043. (unless ga (org-unlogged-message "OVERVIEW"))
  6044. (setq org-cycle-global-status 'overview)
  6045. (run-hook-with-args 'org-cycle-hook 'overview)))))
  6046. (defvar org-called-with-limited-levels nil
  6047. "Non-nil when `org-with-limited-levels' is currently active.")
  6048. (defun org-cycle-internal-local ()
  6049. "Do the local cycling action."
  6050. (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
  6051. ;; First, determine end of headline (EOH), end of subtree or item
  6052. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  6053. (save-excursion
  6054. (if (org-at-item-p)
  6055. (progn
  6056. (beginning-of-line)
  6057. (setq struct (org-list-struct))
  6058. (setq eoh (point-at-eol))
  6059. (setq eos (org-list-get-item-end-before-blank (point) struct))
  6060. (setq has-children (org-list-has-child-p (point) struct)))
  6061. (org-back-to-heading)
  6062. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  6063. (setq eos (save-excursion (org-end-of-subtree t t)
  6064. (when (bolp) (backward-char)) (point)))
  6065. (setq has-children
  6066. (or (save-excursion
  6067. (let ((level (funcall outline-level)))
  6068. (outline-next-heading)
  6069. (and (org-at-heading-p t)
  6070. (> (funcall outline-level) level))))
  6071. (save-excursion
  6072. (org-list-search-forward (org-item-beginning-re) eos t)))))
  6073. ;; Determine end invisible part of buffer (EOL)
  6074. (beginning-of-line 2)
  6075. ;; XEmacs doesn't have `next-single-char-property-change'
  6076. (if (featurep 'xemacs)
  6077. (while (and (not (eobp)) ;; this is like `next-line'
  6078. (get-char-property (1- (point)) 'invisible))
  6079. (beginning-of-line 2))
  6080. (while (and (not (eobp)) ;; this is like `next-line'
  6081. (get-char-property (1- (point)) 'invisible))
  6082. (goto-char (next-single-char-property-change (point) 'invisible))
  6083. (and (eolp) (beginning-of-line 2))))
  6084. (setq eol (point)))
  6085. ;; Find out what to do next and set `this-command'
  6086. (cond
  6087. ((= eos eoh)
  6088. ;; Nothing is hidden behind this heading
  6089. (unless (org-before-first-heading-p)
  6090. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  6091. (org-unlogged-message "EMPTY ENTRY")
  6092. (setq org-cycle-subtree-status nil)
  6093. (save-excursion
  6094. (goto-char eos)
  6095. (outline-next-heading)
  6096. (if (outline-invisible-p) (org-flag-heading nil))))
  6097. ((and (or (>= eol eos)
  6098. (not (string-match "\\S-" (buffer-substring eol eos))))
  6099. (or has-children
  6100. (not (setq children-skipped
  6101. org-cycle-skip-children-state-if-no-children))))
  6102. ;; Entire subtree is hidden in one line: children view
  6103. (unless (org-before-first-heading-p)
  6104. (run-hook-with-args 'org-pre-cycle-hook 'children))
  6105. (if (org-at-item-p)
  6106. (org-list-set-item-visibility (point-at-bol) struct 'children)
  6107. (org-show-entry)
  6108. (org-with-limited-levels (org-show-children))
  6109. ;; FIXME: This slows down the func way too much.
  6110. ;; How keep drawers hidden in subtree anyway?
  6111. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  6112. ;; (org-cycle-hide-drawers 'subtree))
  6113. ;; Fold every list in subtree to top-level items.
  6114. (when (eq org-cycle-include-plain-lists 'integrate)
  6115. (save-excursion
  6116. (org-back-to-heading)
  6117. (while (org-list-search-forward (org-item-beginning-re) eos t)
  6118. (beginning-of-line 1)
  6119. (let* ((struct (org-list-struct))
  6120. (prevs (org-list-prevs-alist struct))
  6121. (end (org-list-get-bottom-point struct)))
  6122. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  6123. (org-list-get-all-items (point) struct prevs))
  6124. (goto-char (if (< end eos) end eos)))))))
  6125. (org-unlogged-message "CHILDREN")
  6126. (save-excursion
  6127. (goto-char eos)
  6128. (outline-next-heading)
  6129. (if (outline-invisible-p) (org-flag-heading nil)))
  6130. (setq org-cycle-subtree-status 'children)
  6131. (unless (org-before-first-heading-p)
  6132. (run-hook-with-args 'org-cycle-hook 'children)))
  6133. ((or children-skipped
  6134. (and (eq last-command this-command)
  6135. (eq org-cycle-subtree-status 'children)))
  6136. ;; We just showed the children, or no children are there,
  6137. ;; now show everything.
  6138. (unless (org-before-first-heading-p)
  6139. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  6140. (outline-flag-region eoh eos nil)
  6141. (org-unlogged-message
  6142. (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  6143. (setq org-cycle-subtree-status 'subtree)
  6144. (unless (org-before-first-heading-p)
  6145. (run-hook-with-args 'org-cycle-hook 'subtree)))
  6146. (t
  6147. ;; Default action: hide the subtree.
  6148. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  6149. (outline-flag-region eoh eos t)
  6150. (org-unlogged-message "FOLDED")
  6151. (setq org-cycle-subtree-status 'folded)
  6152. (unless (org-before-first-heading-p)
  6153. (run-hook-with-args 'org-cycle-hook 'folded))))))
  6154. ;;;###autoload
  6155. (defun org-global-cycle (&optional arg)
  6156. "Cycle the global visibility. For details see `org-cycle'.
  6157. With \\[universal-argument] prefix arg, switch to startup visibility.
  6158. With a numeric prefix, show all headlines up to that level."
  6159. (interactive "P")
  6160. (let ((org-cycle-include-plain-lists
  6161. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  6162. (cond
  6163. ((integerp arg)
  6164. (show-all)
  6165. (hide-sublevels arg)
  6166. (setq org-cycle-global-status 'contents))
  6167. ((equal arg '(4))
  6168. (org-set-startup-visibility)
  6169. (org-unlogged-message "Startup visibility, plus VISIBILITY properties."))
  6170. (t
  6171. (org-cycle '(4))))))
  6172. (defun org-set-startup-visibility ()
  6173. "Set the visibility required by startup options and properties."
  6174. (cond
  6175. ((eq org-startup-folded t)
  6176. (org-overview))
  6177. ((eq org-startup-folded 'content)
  6178. (org-content))
  6179. ((or (eq org-startup-folded 'showeverything)
  6180. (eq org-startup-folded nil))
  6181. (show-all)))
  6182. (unless (eq org-startup-folded 'showeverything)
  6183. (if org-hide-block-startup (org-hide-block-all))
  6184. (org-set-visibility-according-to-property 'no-cleanup)
  6185. (org-cycle-hide-archived-subtrees 'all)
  6186. (org-cycle-hide-drawers 'all)
  6187. (org-cycle-show-empty-lines t)))
  6188. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  6189. "Switch subtree visibilities according to :VISIBILITY: property."
  6190. (interactive)
  6191. (let (org-show-entry-below)
  6192. (org-with-wide-buffer
  6193. (goto-char (point-min))
  6194. (while (re-search-forward "^[ \t]*:VISIBILITY:" nil t)
  6195. (if (not (org-at-property-p)) (outline-next-heading)
  6196. (let ((state (match-string 3)))
  6197. (save-excursion
  6198. (org-back-to-heading t)
  6199. (hide-subtree)
  6200. (org-reveal)
  6201. (cond
  6202. ((equal state "folded")
  6203. (hide-subtree))
  6204. ((equal state "children")
  6205. (org-show-hidden-entry)
  6206. (org-show-children))
  6207. ((equal state "content")
  6208. (save-excursion
  6209. (save-restriction
  6210. (org-narrow-to-subtree)
  6211. (org-content))))
  6212. ((member state '("all" "showall"))
  6213. (show-subtree)))))))
  6214. (unless no-cleanup
  6215. (org-cycle-hide-archived-subtrees 'all)
  6216. (org-cycle-hide-drawers 'all)
  6217. (org-cycle-show-empty-lines 'all)))))
  6218. ;; This function uses outline-regexp instead of the more fundamental
  6219. ;; org-outline-regexp so that org-cycle-global works outside of Org
  6220. ;; buffers, where outline-regexp is needed.
  6221. (defun org-overview ()
  6222. "Switch to overview mode, showing only top-level headlines.
  6223. This shows all headlines with a level equal or greater than the level
  6224. of the first headline in the buffer. This is important, because if the
  6225. first headline is not level one, then (hide-sublevels 1) gives confusing
  6226. results."
  6227. (interactive)
  6228. (save-excursion
  6229. (let ((level
  6230. (save-excursion
  6231. (goto-char (point-min))
  6232. (if (re-search-forward (concat "^" outline-regexp) nil t)
  6233. (progn
  6234. (goto-char (match-beginning 0))
  6235. (funcall outline-level))))))
  6236. (and level (hide-sublevels level)))))
  6237. (defun org-content (&optional arg)
  6238. "Show all headlines in the buffer, like a table of contents.
  6239. With numerical argument N, show content up to level N."
  6240. (interactive "P")
  6241. (org-overview)
  6242. (save-excursion
  6243. ;; Visit all headings and show their offspring
  6244. (and (integerp arg) (org-overview))
  6245. (goto-char (point-max))
  6246. (catch 'exit
  6247. (while (and (progn (condition-case nil
  6248. (outline-previous-visible-heading 1)
  6249. (error (goto-char (point-min))))
  6250. t)
  6251. (looking-at org-outline-regexp))
  6252. (if (integerp arg)
  6253. (org-show-children (1- arg))
  6254. (show-branches))
  6255. (if (bobp) (throw 'exit nil))))))
  6256. (defun org-optimize-window-after-visibility-change (state)
  6257. "Adjust the window after a change in outline visibility.
  6258. This function is the default value of the hook `org-cycle-hook'."
  6259. (when (get-buffer-window (current-buffer))
  6260. (cond
  6261. ((eq state 'content) nil)
  6262. ((eq state 'all) nil)
  6263. ((eq state 'folded) nil)
  6264. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  6265. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  6266. (defun org-remove-empty-overlays-at (pos)
  6267. "Remove outline overlays that do not contain non-white stuff."
  6268. (mapc
  6269. (lambda (o)
  6270. (and (eq 'outline (overlay-get o 'invisible))
  6271. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  6272. (overlay-end o))))
  6273. (delete-overlay o)))
  6274. (overlays-at pos)))
  6275. (defun org-clean-visibility-after-subtree-move ()
  6276. "Fix visibility issues after moving a subtree."
  6277. ;; First, find a reasonable region to look at:
  6278. ;; Start two siblings above, end three below
  6279. (let* ((beg (save-excursion
  6280. (and (org-get-last-sibling)
  6281. (org-get-last-sibling))
  6282. (point)))
  6283. (end (save-excursion
  6284. (and (org-get-next-sibling)
  6285. (org-get-next-sibling)
  6286. (org-get-next-sibling))
  6287. (if (org-at-heading-p)
  6288. (point-at-eol)
  6289. (point))))
  6290. (level (looking-at "\\*+"))
  6291. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  6292. (save-excursion
  6293. (save-restriction
  6294. (narrow-to-region beg end)
  6295. (when re
  6296. ;; Properly fold already folded siblings
  6297. (goto-char (point-min))
  6298. (while (re-search-forward re nil t)
  6299. (if (and (not (outline-invisible-p))
  6300. (save-excursion
  6301. (goto-char (point-at-eol)) (outline-invisible-p)))
  6302. (hide-entry))))
  6303. (org-cycle-show-empty-lines 'overview)
  6304. (org-cycle-hide-drawers 'overview)))))
  6305. (defun org-cycle-show-empty-lines (state)
  6306. "Show empty lines above all visible headlines.
  6307. The region to be covered depends on STATE when called through
  6308. `org-cycle-hook'. Lisp program can use t for STATE to get the
  6309. entire buffer covered. Note that an empty line is only shown if there
  6310. are at least `org-cycle-separator-lines' empty lines before the headline."
  6311. (when (/= org-cycle-separator-lines 0)
  6312. (save-excursion
  6313. (let* ((n (abs org-cycle-separator-lines))
  6314. (re (cond
  6315. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  6316. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  6317. (t (let ((ns (number-to-string (- n 2))))
  6318. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  6319. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  6320. beg end)
  6321. (cond
  6322. ((memq state '(overview contents t))
  6323. (setq beg (point-min) end (point-max)))
  6324. ((memq state '(children folded))
  6325. (setq beg (point)
  6326. end (progn (org-end-of-subtree t t)
  6327. (line-beginning-position 2)))))
  6328. (when beg
  6329. (goto-char beg)
  6330. (while (re-search-forward re end t)
  6331. (unless (get-char-property (match-end 1) 'invisible)
  6332. (let ((e (match-end 1))
  6333. (b (if (>= org-cycle-separator-lines 0)
  6334. (match-beginning 1)
  6335. (save-excursion
  6336. (goto-char (match-beginning 0))
  6337. (skip-chars-backward " \t\n")
  6338. (line-end-position)))))
  6339. (outline-flag-region b e nil))))))))
  6340. ;; Never hide empty lines at the end of the file.
  6341. (save-excursion
  6342. (goto-char (point-max))
  6343. (outline-previous-heading)
  6344. (outline-end-of-heading)
  6345. (if (and (looking-at "[ \t\n]+")
  6346. (= (match-end 0) (point-max)))
  6347. (outline-flag-region (point) (match-end 0) nil))))
  6348. (defun org-show-empty-lines-in-parent ()
  6349. "Move to the parent and re-show empty lines before visible headlines."
  6350. (save-excursion
  6351. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  6352. (org-cycle-show-empty-lines context))))
  6353. (defun org-files-list ()
  6354. "Return `org-agenda-files' list, plus all open org-mode files.
  6355. This is useful for operations that need to scan all of a user's
  6356. open and agenda-wise Org files."
  6357. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  6358. (dolist (buf (buffer-list))
  6359. (with-current-buffer buf
  6360. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  6361. (let ((file (expand-file-name (buffer-file-name))))
  6362. (unless (member file files)
  6363. (push file files))))))
  6364. files))
  6365. (defsubst org-entry-beginning-position ()
  6366. "Return the beginning position of the current entry."
  6367. (save-excursion (outline-back-to-heading t) (point)))
  6368. (defsubst org-entry-end-position ()
  6369. "Return the end position of the current entry."
  6370. (save-excursion (outline-next-heading) (point)))
  6371. (defun org-cycle-hide-drawers (state &optional exceptions)
  6372. "Re-hide all drawers after a visibility state change.
  6373. When non-nil, optional argument EXCEPTIONS is a list of strings
  6374. specifying which drawers should not be hidden."
  6375. (when (and (derived-mode-p 'org-mode)
  6376. (not (memq state '(overview folded contents))))
  6377. (save-excursion
  6378. (let* ((globalp (memq state '(contents all)))
  6379. (beg (if globalp (point-min) (point)))
  6380. (end (if globalp (point-max)
  6381. (if (eq state 'children)
  6382. (save-excursion (outline-next-heading) (point))
  6383. (org-end-of-subtree t)))))
  6384. (goto-char beg)
  6385. (while (re-search-forward org-drawer-regexp (max end (point)) t)
  6386. (unless (member-ignore-case (match-string 1) exceptions)
  6387. (let ((drawer (org-element-at-point)))
  6388. (when (memq (org-element-type drawer) '(drawer property-drawer))
  6389. (org-flag-drawer t drawer)
  6390. ;; Make sure to skip drawer entirely or we might flag
  6391. ;; it another time when matching its ending line with
  6392. ;; `org-drawer-regexp'.
  6393. (goto-char (org-element-property :end drawer))))))))))
  6394. (defun org-flag-drawer (flag &optional element)
  6395. "When FLAG is non-nil, hide the drawer we are at.
  6396. Otherwise make it visible. When optional argument ELEMENT is
  6397. a parsed drawer, as returned by `org-element-at-point', hide or
  6398. show that drawer instead."
  6399. (when (save-excursion
  6400. (beginning-of-line)
  6401. (org-looking-at-p org-drawer-regexp))
  6402. (let ((drawer (or element (org-element-at-point))))
  6403. (when (memq (org-element-type drawer) '(drawer property-drawer))
  6404. (let ((post (org-element-property :post-affiliated drawer)))
  6405. (save-excursion
  6406. (outline-flag-region
  6407. (progn (goto-char post) (line-end-position))
  6408. (progn (goto-char (org-element-property :end drawer))
  6409. (skip-chars-backward " \r\t\n")
  6410. (line-end-position))
  6411. flag))
  6412. ;; When the drawer is hidden away, make sure point lies in
  6413. ;; a visible part of the buffer.
  6414. (when (and flag (> (line-beginning-position) post))
  6415. (goto-char post)))))))
  6416. (defun org-subtree-end-visible-p ()
  6417. "Is the end of the current subtree visible?"
  6418. (pos-visible-in-window-p
  6419. (save-excursion (org-end-of-subtree t) (point))))
  6420. (defun org-first-headline-recenter ()
  6421. "Move cursor to the first headline and recenter the headline."
  6422. (goto-char (point-min))
  6423. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6424. (set-window-start (selected-window) (point-at-bol))))
  6425. ;;; Saving and restoring visibility
  6426. (defun org-outline-overlay-data (&optional use-markers)
  6427. "Return a list of the locations of all outline overlays.
  6428. These are overlays with the `invisible' property value `outline'.
  6429. The return value is a list of cons cells, with start and stop
  6430. positions for each overlay.
  6431. If USE-MARKERS is set, return the positions as markers."
  6432. (let (beg end)
  6433. (save-excursion
  6434. (save-restriction
  6435. (widen)
  6436. (delq nil
  6437. (mapcar (lambda (o)
  6438. (when (eq (overlay-get o 'invisible) 'outline)
  6439. (setq beg (overlay-start o)
  6440. end (overlay-end o))
  6441. (and beg end (> end beg)
  6442. (if use-markers
  6443. (cons (copy-marker beg)
  6444. (copy-marker end t))
  6445. (cons beg end)))))
  6446. (overlays-in (point-min) (point-max))))))))
  6447. (defun org-set-outline-overlay-data (data)
  6448. "Create visibility overlays for all positions in DATA.
  6449. DATA should have been made by `org-outline-overlay-data'."
  6450. (let (o)
  6451. (save-excursion
  6452. (save-restriction
  6453. (widen)
  6454. (show-all)
  6455. (mapc (lambda (c)
  6456. (outline-flag-region (car c) (cdr c) t))
  6457. data)))))
  6458. ;;; Folding of blocks
  6459. (defvar org-hide-block-overlays nil
  6460. "Overlays hiding blocks.")
  6461. (make-variable-buffer-local 'org-hide-block-overlays)
  6462. (defun org-block-map (function &optional start end)
  6463. "Call FUNCTION at the head of all source blocks in the current buffer.
  6464. Optional arguments START and END can be used to limit the range."
  6465. (let ((start (or start (point-min)))
  6466. (end (or end (point-max))))
  6467. (save-excursion
  6468. (goto-char start)
  6469. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6470. (save-excursion
  6471. (save-match-data
  6472. (goto-char (match-beginning 0))
  6473. (funcall function)))))))
  6474. (defun org-hide-block-toggle-all ()
  6475. "Toggle the visibility of all blocks in the current buffer."
  6476. (org-block-map 'org-hide-block-toggle))
  6477. (defun org-hide-block-all ()
  6478. "Fold all blocks in the current buffer."
  6479. (interactive)
  6480. (org-show-block-all)
  6481. (org-block-map 'org-hide-block-toggle-maybe))
  6482. (defun org-show-block-all ()
  6483. "Unfold all blocks in the current buffer."
  6484. (interactive)
  6485. (mapc 'delete-overlay org-hide-block-overlays)
  6486. (setq org-hide-block-overlays nil))
  6487. (defun org-hide-block-toggle-maybe ()
  6488. "Toggle visibility of block at point.
  6489. Unlike to `org-hide-block-toggle', this function does not throw
  6490. an error. Return a non-nil value when toggling is successful."
  6491. (interactive)
  6492. (ignore-errors (org-hide-block-toggle)))
  6493. (defun org-hide-block-toggle (&optional force)
  6494. "Toggle the visibility of the current block.
  6495. When optional argument FORCE is `off', make block visible. If it
  6496. is non-nil, hide it unconditionally. Throw an error when not at
  6497. a block. Return a non-nil value when toggling is successful."
  6498. (interactive)
  6499. (let ((element (org-element-at-point)))
  6500. (unless (memq (org-element-type element)
  6501. '(center-block comment-block dynamic-block example-block
  6502. export-block quote-block special-block
  6503. src-block verse-block))
  6504. (user-error "Not at a block"))
  6505. (let* ((start (save-excursion
  6506. (goto-char (org-element-property :post-affiliated element))
  6507. (line-end-position)))
  6508. (end (save-excursion
  6509. (goto-char (org-element-property :end element))
  6510. (skip-chars-backward " \r\t\n")
  6511. (line-end-position)))
  6512. (overlays (overlays-at start)))
  6513. (cond
  6514. ;; Do nothing when not before or at the block opening line or
  6515. ;; at the block closing line.
  6516. ((let ((eol (line-end-position))) (and (> eol start) (/= eol end))) nil)
  6517. ((and (not (eq force 'off))
  6518. (not (memq t (mapcar
  6519. (lambda (o)
  6520. (eq (overlay-get o 'invisible) 'org-hide-block))
  6521. overlays))))
  6522. (let ((ov (make-overlay start end)))
  6523. (overlay-put ov 'invisible 'org-hide-block)
  6524. ;; Make the block accessible to `isearch'.
  6525. (overlay-put
  6526. ov 'isearch-open-invisible
  6527. (lambda (ov)
  6528. (when (memq ov org-hide-block-overlays)
  6529. (setq org-hide-block-overlays (delq ov org-hide-block-overlays)))
  6530. (when (eq (overlay-get ov 'invisible) 'org-hide-block)
  6531. (delete-overlay ov))))
  6532. (push ov org-hide-block-overlays)
  6533. ;; When the block is hidden away, make sure point is left in
  6534. ;; a visible part of the buffer.
  6535. (when (> (line-beginning-position) start)
  6536. (goto-char start)
  6537. (beginning-of-line))
  6538. ;; Signal successful toggling.
  6539. t))
  6540. ((or (not force) (eq force 'off))
  6541. (dolist (ov overlays t)
  6542. (when (memq ov org-hide-block-overlays)
  6543. (setq org-hide-block-overlays (delq ov org-hide-block-overlays)))
  6544. (when (eq (overlay-get ov 'invisible) 'org-hide-block)
  6545. (delete-overlay ov))))))))
  6546. ;; org-tab-after-check-for-cycling-hook
  6547. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6548. ;; Remove overlays when changing major mode
  6549. (add-hook 'org-mode-hook
  6550. (lambda () (org-add-hook 'change-major-mode-hook
  6551. 'org-show-block-all 'append 'local)))
  6552. ;;; Org-goto
  6553. (defvar org-goto-window-configuration nil)
  6554. (defvar org-goto-marker nil)
  6555. (defvar org-goto-map)
  6556. (defun org-goto-map ()
  6557. "Set the keymap `org-goto'."
  6558. (setq org-goto-map
  6559. (let ((map (make-sparse-keymap)))
  6560. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6561. mouse-drag-region universal-argument org-occur)))
  6562. (dolist (cmd cmds)
  6563. (substitute-key-definition cmd cmd map global-map)))
  6564. (suppress-keymap map)
  6565. (org-defkey map "\C-m" 'org-goto-ret)
  6566. (org-defkey map [(return)] 'org-goto-ret)
  6567. (org-defkey map [(left)] 'org-goto-left)
  6568. (org-defkey map [(right)] 'org-goto-right)
  6569. (org-defkey map [(control ?g)] 'org-goto-quit)
  6570. (org-defkey map "\C-i" 'org-cycle)
  6571. (org-defkey map [(tab)] 'org-cycle)
  6572. (org-defkey map [(down)] 'outline-next-visible-heading)
  6573. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6574. (if org-goto-auto-isearch
  6575. (if (fboundp 'define-key-after)
  6576. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6577. nil)
  6578. (org-defkey map "q" 'org-goto-quit)
  6579. (org-defkey map "n" 'outline-next-visible-heading)
  6580. (org-defkey map "p" 'outline-previous-visible-heading)
  6581. (org-defkey map "f" 'outline-forward-same-level)
  6582. (org-defkey map "b" 'outline-backward-same-level)
  6583. (org-defkey map "u" 'outline-up-heading))
  6584. (org-defkey map "/" 'org-occur)
  6585. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6586. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6587. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6588. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6589. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6590. map)))
  6591. (defconst org-goto-help
  6592. "Browse buffer copy, to find location or copy text.%s
  6593. RET=jump to location C-g=quit and return to previous location
  6594. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6595. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6596. (defun org-goto (&optional alternative-interface)
  6597. "Look up a different location in the current file, keeping current visibility.
  6598. When you want look-up or go to a different location in a
  6599. document, the fastest way is often to fold the entire buffer and
  6600. then dive into the tree. This method has the disadvantage, that
  6601. the previous location will be folded, which may not be what you
  6602. want.
  6603. This command works around this by showing a copy of the current
  6604. buffer in an indirect buffer, in overview mode. You can dive
  6605. into the tree in that copy, use org-occur and incremental search
  6606. to find a location. When pressing RET or `Q', the command
  6607. returns to the original buffer in which the visibility is still
  6608. unchanged. After RET it will also jump to the location selected
  6609. in the indirect buffer and expose the headline hierarchy above.
  6610. With a prefix argument, use the alternative interface: e.g. if
  6611. `org-goto-interface' is 'outline use 'outline-path-completion."
  6612. (interactive "P")
  6613. (org-goto-map)
  6614. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6615. (org-refile-use-outline-path t)
  6616. (org-refile-target-verify-function nil)
  6617. (interface
  6618. (if (not alternative-interface)
  6619. org-goto-interface
  6620. (if (eq org-goto-interface 'outline)
  6621. 'outline-path-completion
  6622. 'outline)))
  6623. (org-goto-start-pos (point))
  6624. (selected-point
  6625. (if (eq interface 'outline)
  6626. (car (org-get-location (current-buffer) org-goto-help))
  6627. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6628. (org-refile-check-position pa)
  6629. (nth 3 pa)))))
  6630. (if selected-point
  6631. (progn
  6632. (org-mark-ring-push org-goto-start-pos)
  6633. (goto-char selected-point)
  6634. (if (or (outline-invisible-p) (org-invisible-p2))
  6635. (org-show-context 'org-goto)))
  6636. (message "Quit"))))
  6637. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6638. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6639. (defvar org-goto-local-auto-isearch-map) ; defined below
  6640. (defun org-get-location (buf help)
  6641. "Let the user select a location in the Org-mode buffer BUF.
  6642. This function uses a recursive edit. It returns the selected position
  6643. or nil."
  6644. (org-no-popups
  6645. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6646. (isearch-hide-immediately nil)
  6647. (isearch-search-fun-function
  6648. (lambda () 'org-goto-local-search-headings))
  6649. (org-goto-selected-point org-goto-exit-command))
  6650. (save-excursion
  6651. (save-window-excursion
  6652. (delete-other-windows)
  6653. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6654. (org-pop-to-buffer-same-window
  6655. (condition-case nil
  6656. (make-indirect-buffer (current-buffer) "*org-goto*")
  6657. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6658. (with-output-to-temp-buffer "*Org Help*"
  6659. (princ (format help (if org-goto-auto-isearch
  6660. " Just type for auto-isearch."
  6661. " n/p/f/b/u to navigate, q to quit."))))
  6662. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6663. (setq buffer-read-only nil)
  6664. (let ((org-startup-truncated t)
  6665. (org-startup-folded nil)
  6666. (org-startup-align-all-tables nil))
  6667. (org-mode)
  6668. (org-overview))
  6669. (setq buffer-read-only t)
  6670. (if (and (boundp 'org-goto-start-pos)
  6671. (integer-or-marker-p org-goto-start-pos))
  6672. (progn (goto-char org-goto-start-pos)
  6673. (when (outline-invisible-p)
  6674. (org-show-set-visibility 'lineage)))
  6675. (goto-char (point-min)))
  6676. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6677. (message "Select location and press RET")
  6678. (use-local-map org-goto-map)
  6679. (recursive-edit)))
  6680. (kill-buffer "*org-goto*")
  6681. (cons org-goto-selected-point org-goto-exit-command))))
  6682. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6683. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6684. ;; `isearch-other-control-char' was removed in Emacs 24.4.
  6685. (if (fboundp 'isearch-other-control-char)
  6686. (progn
  6687. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6688. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char))
  6689. (define-key org-goto-local-auto-isearch-map "\C-i" nil)
  6690. (define-key org-goto-local-auto-isearch-map "\C-m" nil)
  6691. (define-key org-goto-local-auto-isearch-map [return] nil))
  6692. (defun org-goto-local-search-headings (string bound noerror)
  6693. "Search and make sure that any matches are in headlines."
  6694. (catch 'return
  6695. (while (if isearch-forward
  6696. (search-forward string bound noerror)
  6697. (search-backward string bound noerror))
  6698. (when (save-match-data
  6699. (and (save-excursion
  6700. (beginning-of-line)
  6701. (looking-at org-complex-heading-regexp))
  6702. (or (not (match-beginning 5))
  6703. (< (point) (match-beginning 5)))))
  6704. (throw 'return (point))))))
  6705. (defun org-goto-local-auto-isearch ()
  6706. "Start isearch."
  6707. (interactive)
  6708. (goto-char (point-min))
  6709. (let ((keys (this-command-keys)))
  6710. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6711. (isearch-mode t)
  6712. (isearch-process-search-char (string-to-char keys)))))
  6713. (defun org-goto-ret (&optional arg)
  6714. "Finish `org-goto' by going to the new location."
  6715. (interactive "P")
  6716. (setq org-goto-selected-point (point)
  6717. org-goto-exit-command 'return)
  6718. (throw 'exit nil))
  6719. (defun org-goto-left ()
  6720. "Finish `org-goto' by going to the new location."
  6721. (interactive)
  6722. (if (org-at-heading-p)
  6723. (progn
  6724. (beginning-of-line 1)
  6725. (setq org-goto-selected-point (point)
  6726. org-goto-exit-command 'left)
  6727. (throw 'exit nil))
  6728. (user-error "Not on a heading")))
  6729. (defun org-goto-right ()
  6730. "Finish `org-goto' by going to the new location."
  6731. (interactive)
  6732. (if (org-at-heading-p)
  6733. (progn
  6734. (setq org-goto-selected-point (point)
  6735. org-goto-exit-command 'right)
  6736. (throw 'exit nil))
  6737. (user-error "Not on a heading")))
  6738. (defun org-goto-quit ()
  6739. "Finish `org-goto' without cursor motion."
  6740. (interactive)
  6741. (setq org-goto-selected-point nil)
  6742. (setq org-goto-exit-command 'quit)
  6743. (throw 'exit nil))
  6744. ;;; Indirect buffer display of subtrees
  6745. (defvar org-indirect-dedicated-frame nil
  6746. "This is the frame being used for indirect tree display.")
  6747. (defvar org-last-indirect-buffer nil)
  6748. (defun org-tree-to-indirect-buffer (&optional arg)
  6749. "Create indirect buffer and narrow it to current subtree.
  6750. With a numerical prefix ARG, go up to this level and then take that tree.
  6751. If ARG is negative, go up that many levels.
  6752. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6753. indirect buffer previously made with this command, to avoid proliferation of
  6754. indirect buffers. However, when you call the command with a \
  6755. \\[universal-argument] prefix, or
  6756. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6757. is kept so that you can work with several indirect buffers at the same time.
  6758. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6759. \\[universal-argument] prefix also
  6760. requests that a new frame be made for the new buffer, so that the dedicated
  6761. frame is not changed."
  6762. (interactive "P")
  6763. (let ((cbuf (current-buffer))
  6764. (cwin (selected-window))
  6765. (pos (point))
  6766. beg end level heading ibuf)
  6767. (save-excursion
  6768. (org-back-to-heading t)
  6769. (when (numberp arg)
  6770. (setq level (org-outline-level))
  6771. (if (< arg 0) (setq arg (+ level arg)))
  6772. (while (> (setq level (org-outline-level)) arg)
  6773. (org-up-heading-safe)))
  6774. (setq beg (point)
  6775. heading (org-get-heading))
  6776. (org-end-of-subtree t t)
  6777. (if (org-at-heading-p) (backward-char 1))
  6778. (setq end (point)))
  6779. (if (and (buffer-live-p org-last-indirect-buffer)
  6780. (not (eq org-indirect-buffer-display 'new-frame))
  6781. (not arg))
  6782. (kill-buffer org-last-indirect-buffer))
  6783. (setq ibuf (org-get-indirect-buffer cbuf heading)
  6784. org-last-indirect-buffer ibuf)
  6785. (cond
  6786. ((or (eq org-indirect-buffer-display 'new-frame)
  6787. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6788. (select-frame (make-frame))
  6789. (delete-other-windows)
  6790. (org-pop-to-buffer-same-window ibuf)
  6791. (org-set-frame-title heading))
  6792. ((eq org-indirect-buffer-display 'dedicated-frame)
  6793. (raise-frame
  6794. (select-frame (or (and org-indirect-dedicated-frame
  6795. (frame-live-p org-indirect-dedicated-frame)
  6796. org-indirect-dedicated-frame)
  6797. (setq org-indirect-dedicated-frame (make-frame)))))
  6798. (delete-other-windows)
  6799. (org-pop-to-buffer-same-window ibuf)
  6800. (org-set-frame-title (concat "Indirect: " heading)))
  6801. ((eq org-indirect-buffer-display 'current-window)
  6802. (org-pop-to-buffer-same-window ibuf))
  6803. ((eq org-indirect-buffer-display 'other-window)
  6804. (pop-to-buffer ibuf))
  6805. (t (error "Invalid value")))
  6806. (if (featurep 'xemacs)
  6807. (save-excursion (org-mode) (turn-on-font-lock)))
  6808. (narrow-to-region beg end)
  6809. (show-all)
  6810. (goto-char pos)
  6811. (run-hook-with-args 'org-cycle-hook 'all)
  6812. (and (window-live-p cwin) (select-window cwin))))
  6813. (defun org-get-indirect-buffer (&optional buffer heading)
  6814. (setq buffer (or buffer (current-buffer)))
  6815. (let ((n 1) (base (buffer-name buffer)) bname)
  6816. (while (buffer-live-p
  6817. (get-buffer
  6818. (setq bname
  6819. (concat base "-"
  6820. (if heading (concat heading "-" (number-to-string n))
  6821. (number-to-string n))))))
  6822. (setq n (1+ n)))
  6823. (condition-case nil
  6824. (make-indirect-buffer buffer bname 'clone)
  6825. (error (make-indirect-buffer buffer bname)))))
  6826. (defun org-set-frame-title (title)
  6827. "Set the title of the current frame to the string TITLE."
  6828. ;; FIXME: how to name a single frame in XEmacs???
  6829. (unless (featurep 'xemacs)
  6830. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6831. ;;;; Structure editing
  6832. ;;; Inserting headlines
  6833. (defun org-previous-line-empty-p (&optional next)
  6834. "Is the previous line a blank line?
  6835. When NEXT is non-nil, check the next line instead."
  6836. (save-excursion
  6837. (and (not (bobp))
  6838. (or (beginning-of-line (if next 2 0)) t)
  6839. (save-match-data
  6840. (looking-at "[ \t]*$")))))
  6841. (defun org-insert-heading (&optional arg invisible-ok top-level)
  6842. "Insert a new heading or an item with the same depth at point.
  6843. If point is at the beginning of a heading or a list item, insert
  6844. a new heading or a new item above the current one. If point is
  6845. at the beginning of a normal line, turn the line into a heading.
  6846. If point is in the middle of a headline or a list item, split the
  6847. headline or the item and create a new headline/item with the text
  6848. in the current line after point \(see `org-M-RET-may-split-line'
  6849. on how to modify this behavior).
  6850. With one universal prefix argument, set the user option
  6851. `org-insert-heading-respect-content' to t for the duration of
  6852. the command. This modifies the behavior described above in this
  6853. ways: on list items and at the beginning of normal lines, force
  6854. the insertion of a heading after the current subtree.
  6855. With two universal prefix arguments, insert the heading at the
  6856. end of the grandparent subtree. For example, if point is within
  6857. a 2nd-level heading, then it will insert a 2nd-level heading at
  6858. the end of the 1st-level parent heading.
  6859. If point is at the beginning of a headline, insert a sibling
  6860. before the current headline. If point is not at the beginning,
  6861. split the line and create a new headline with the text in the
  6862. current line after point \(see `org-M-RET-may-split-line' on how
  6863. to modify this behavior).
  6864. If point is at the beginning of a normal line, turn this line
  6865. into a heading.
  6866. When INVISIBLE-OK is set, stop at invisible headlines when going
  6867. back. This is important for non-interactive uses of the
  6868. command.
  6869. When optional argument TOP-LEVEL is non-nil, insert a level 1
  6870. heading, unconditionally."
  6871. (interactive "P")
  6872. (if (org-called-interactively-p 'any) (org-reveal))
  6873. (let ((itemp (and (not top-level) (org-in-item-p)))
  6874. (may-split (org-get-alist-option org-M-RET-may-split-line 'headline))
  6875. (respect-content (or org-insert-heading-respect-content
  6876. (equal arg '(4))))
  6877. (initial-content ""))
  6878. (cond
  6879. ((or (= (buffer-size) 0)
  6880. (and (not (save-excursion
  6881. (and (ignore-errors (org-back-to-heading invisible-ok))
  6882. (org-at-heading-p))))
  6883. (or arg (not itemp))))
  6884. ;; At beginning of buffer or so high up that only a heading
  6885. ;; makes sense.
  6886. (cond ((and (bolp) (not respect-content)) (insert "* "))
  6887. ((not respect-content)
  6888. (unless may-split (end-of-line))
  6889. (insert "\n* "))
  6890. ((re-search-forward org-outline-regexp-bol nil t)
  6891. (beginning-of-line)
  6892. (insert "* \n")
  6893. (backward-char))
  6894. (t (goto-char (point-max))
  6895. (insert "\n* ")))
  6896. (run-hooks 'org-insert-heading-hook))
  6897. ((and itemp (not (member arg '((4) (16)))) (org-insert-item)))
  6898. (t
  6899. ;; Maybe move at the end of the subtree
  6900. (when (equal arg '(16))
  6901. (org-up-heading-safe)
  6902. (org-end-of-subtree t))
  6903. ;; Insert a heading
  6904. (save-restriction
  6905. (widen)
  6906. (let* ((level nil)
  6907. (on-heading (org-at-heading-p))
  6908. (empty-line-p (if on-heading
  6909. (org-previous-line-empty-p)
  6910. ;; We will decide later
  6911. nil))
  6912. ;; Get a level string to fall back on.
  6913. (fix-level
  6914. (if (org-before-first-heading-p) "*"
  6915. (save-excursion
  6916. (org-back-to-heading t)
  6917. (if (org-previous-line-empty-p) (setq empty-line-p t))
  6918. (looking-at org-outline-regexp)
  6919. (make-string (1- (length (match-string 0))) ?*))))
  6920. (stars
  6921. (save-excursion
  6922. (condition-case nil
  6923. (if top-level "* "
  6924. (org-back-to-heading invisible-ok)
  6925. (when (and (not on-heading)
  6926. (featurep 'org-inlinetask)
  6927. (integerp org-inlinetask-min-level)
  6928. (>= (length (match-string 0))
  6929. org-inlinetask-min-level))
  6930. ;; Find a heading level before the inline
  6931. ;; task.
  6932. (while (and (setq level (org-up-heading-safe))
  6933. (>= level org-inlinetask-min-level)))
  6934. (if (org-at-heading-p)
  6935. (org-back-to-heading invisible-ok)
  6936. (error "This should not happen")))
  6937. (unless (and (save-excursion
  6938. (save-match-data
  6939. (org-backward-heading-same-level
  6940. 1 invisible-ok))
  6941. (= (point) (match-beginning 0)))
  6942. (not (org-previous-line-empty-p t)))
  6943. (setq empty-line-p (or empty-line-p
  6944. (org-previous-line-empty-p))))
  6945. (match-string 0))
  6946. (error (or fix-level "* ")))))
  6947. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6948. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6949. pos hide-previous previous-pos)
  6950. ;; If we insert after content, move there and clean up
  6951. ;; whitespace.
  6952. (when (and respect-content
  6953. (not (org-looking-at-p org-outline-regexp-bol)))
  6954. (if (not (org-before-first-heading-p))
  6955. (org-end-of-subtree nil t)
  6956. (re-search-forward org-outline-regexp-bol)
  6957. (beginning-of-line 0))
  6958. (skip-chars-backward " \r\t\n")
  6959. (and (not (looking-back "^\\*+" (line-beginning-position)))
  6960. (looking-at "[ \t]+") (replace-match ""))
  6961. (unless (eobp) (forward-char 1))
  6962. (when (looking-at "^\\*")
  6963. (unless (bobp) (backward-char 1))
  6964. (insert "\n")))
  6965. ;; If we are splitting, grab the text that should be moved
  6966. ;; to the new headline.
  6967. (when may-split
  6968. (if (org-on-heading-p)
  6969. ;; This is a heading: split intelligently (keeping
  6970. ;; tags).
  6971. (let ((pos (point)))
  6972. (beginning-of-line)
  6973. (unless (looking-at org-complex-heading-regexp)
  6974. (error "This should not happen"))
  6975. (when (and (match-beginning 4)
  6976. (> pos (match-beginning 4))
  6977. (< pos (match-end 4)))
  6978. (setq initial-content (buffer-substring pos (match-end 4)))
  6979. (goto-char pos)
  6980. (delete-region (point) (match-end 4))
  6981. (if (looking-at "[ \t]*$")
  6982. (replace-match "")
  6983. (insert (make-string (length initial-content) ?\s)))
  6984. (setq initial-content (org-trim initial-content)))
  6985. (goto-char pos))
  6986. ;; A normal line.
  6987. (setq initial-content
  6988. (org-trim
  6989. (delete-and-extract-region (point) (line-end-position))))))
  6990. ;; If we are at the beginning of the line, insert before it.
  6991. ;; Otherwise, after it.
  6992. (cond
  6993. ((and (bolp) (looking-at "[ \t]*$")))
  6994. ((bolp) (save-excursion (insert "\n")))
  6995. (t (end-of-line)
  6996. (insert "\n")))
  6997. ;; Insert the new heading
  6998. (insert stars)
  6999. (just-one-space)
  7000. (insert initial-content)
  7001. (unless (and blank (org-previous-line-empty-p))
  7002. (org-N-empty-lines-before-current (if blank 1 0)))
  7003. ;; Adjust visibility, which may be messed up if we removed
  7004. ;; blank lines while previous entry was hidden.
  7005. (let ((bol (line-beginning-position)))
  7006. (dolist (o (overlays-at (1- bol)))
  7007. (when (and (eq (overlay-get o 'invisible) 'outline)
  7008. (eq (overlay-end o) bol))
  7009. (move-overlay o (overlay-start o) (1- bol)))))
  7010. (run-hooks 'org-insert-heading-hook)))))))
  7011. (defun org-N-empty-lines-before-current (N)
  7012. "Make the number of empty lines before current exactly N.
  7013. So this will delete or add empty lines."
  7014. (save-excursion
  7015. (beginning-of-line)
  7016. (let ((p (point)))
  7017. (skip-chars-backward " \r\t\n")
  7018. (unless (bolp) (forward-line))
  7019. (delete-region (point) p))
  7020. (when (> N 0) (insert (make-string N ?\n)))))
  7021. (defun org-get-heading (&optional no-tags no-todo)
  7022. "Return the heading of the current entry, without the stars.
  7023. When NO-TAGS is non-nil, don't include tags.
  7024. When NO-TODO is non-nil, don't include TODO keywords."
  7025. (save-excursion
  7026. (org-back-to-heading t)
  7027. (cond
  7028. ((and no-tags no-todo)
  7029. (looking-at org-complex-heading-regexp)
  7030. (match-string 4))
  7031. (no-tags
  7032. (looking-at (concat org-outline-regexp
  7033. "\\(.*?\\)"
  7034. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  7035. (match-string 1))
  7036. (no-todo
  7037. (looking-at org-todo-line-regexp)
  7038. (match-string 3))
  7039. (t (looking-at org-heading-regexp)
  7040. (match-string 2)))))
  7041. (defvar orgstruct-mode) ; defined below
  7042. (defun org-heading-components ()
  7043. "Return the components of the current heading.
  7044. This is a list with the following elements:
  7045. - the level as an integer
  7046. - the reduced level, different if `org-odd-levels-only' is set.
  7047. - the TODO keyword, or nil
  7048. - the priority character, like ?A, or nil if no priority is given
  7049. - the headline text itself, or the tags string if no headline text
  7050. - the tags string, or nil."
  7051. (save-excursion
  7052. (org-back-to-heading t)
  7053. (if (let (case-fold-search)
  7054. (looking-at
  7055. (if orgstruct-mode
  7056. org-heading-regexp
  7057. org-complex-heading-regexp)))
  7058. (if orgstruct-mode
  7059. (list (length (match-string 1))
  7060. (org-reduced-level (length (match-string 1)))
  7061. nil
  7062. nil
  7063. (match-string 2)
  7064. nil)
  7065. (list (length (match-string 1))
  7066. (org-reduced-level (length (match-string 1)))
  7067. (org-match-string-no-properties 2)
  7068. (and (match-end 3) (aref (match-string 3) 2))
  7069. (org-match-string-no-properties 4)
  7070. (org-match-string-no-properties 5))))))
  7071. (defun org-get-entry ()
  7072. "Get the entry text, after heading, entire subtree."
  7073. (save-excursion
  7074. (org-back-to-heading t)
  7075. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  7076. (defun org-insert-heading-after-current ()
  7077. "Insert a new heading with same level as current, after current subtree."
  7078. (interactive)
  7079. (org-back-to-heading)
  7080. (org-insert-heading)
  7081. (org-move-subtree-down)
  7082. (end-of-line 1))
  7083. (defun org-insert-heading-respect-content (&optional invisible-ok)
  7084. "Insert heading with `org-insert-heading-respect-content' set to t."
  7085. (interactive)
  7086. (org-insert-heading '(4) invisible-ok))
  7087. (defun org-insert-todo-heading-respect-content (&optional force-state)
  7088. "Insert TODO heading with `org-insert-heading-respect-content' set to t."
  7089. (interactive)
  7090. (org-insert-todo-heading force-state '(4)))
  7091. (defun org-insert-todo-heading (arg &optional force-heading)
  7092. "Insert a new heading with the same level and TODO state as current heading.
  7093. If the heading has no TODO state, or if the state is DONE, use the first
  7094. state (TODO by default). Also with one prefix arg, force first state. With
  7095. two prefix args, force inserting at the end of the parent subtree."
  7096. (interactive "P")
  7097. (when (or force-heading (not (org-insert-item 'checkbox)))
  7098. (org-insert-heading (or (and (equal arg '(16)) '(16))
  7099. force-heading))
  7100. (save-excursion
  7101. (org-back-to-heading)
  7102. (outline-previous-heading)
  7103. (looking-at org-todo-line-regexp))
  7104. (let*
  7105. ((new-mark-x
  7106. (if (or (equal arg '(4))
  7107. (not (match-beginning 2))
  7108. (member (match-string 2) org-done-keywords))
  7109. (car org-todo-keywords-1)
  7110. (match-string 2)))
  7111. (new-mark
  7112. (or
  7113. (run-hook-with-args-until-success
  7114. 'org-todo-get-default-hook new-mark-x nil)
  7115. new-mark-x)))
  7116. (beginning-of-line 1)
  7117. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  7118. (if org-treat-insert-todo-heading-as-state-change
  7119. (org-todo new-mark)
  7120. (insert new-mark " "))))
  7121. (when org-provide-todo-statistics
  7122. (org-update-parent-todo-statistics))))
  7123. (defun org-insert-subheading (arg)
  7124. "Insert a new subheading and demote it.
  7125. Works for outline headings and for plain lists alike."
  7126. (interactive "P")
  7127. (org-insert-heading arg)
  7128. (cond
  7129. ((org-at-heading-p) (org-do-demote))
  7130. ((org-at-item-p) (org-indent-item))))
  7131. (defun org-insert-todo-subheading (arg)
  7132. "Insert a new subheading with TODO keyword or checkbox and demote it.
  7133. Works for outline headings and for plain lists alike."
  7134. (interactive "P")
  7135. (org-insert-todo-heading arg)
  7136. (cond
  7137. ((org-at-heading-p) (org-do-demote))
  7138. ((org-at-item-p) (org-indent-item))))
  7139. ;;; Promotion and Demotion
  7140. (defvar org-after-demote-entry-hook nil
  7141. "Hook run after an entry has been demoted.
  7142. The cursor will be at the beginning of the entry.
  7143. When a subtree is being demoted, the hook will be called for each node.")
  7144. (defvar org-after-promote-entry-hook nil
  7145. "Hook run after an entry has been promoted.
  7146. The cursor will be at the beginning of the entry.
  7147. When a subtree is being promoted, the hook will be called for each node.")
  7148. (defun org-promote-subtree ()
  7149. "Promote the entire subtree.
  7150. See also `org-promote'."
  7151. (interactive)
  7152. (save-excursion
  7153. (org-with-limited-levels (org-map-tree 'org-promote)))
  7154. (org-fix-position-after-promote))
  7155. (defun org-demote-subtree ()
  7156. "Demote the entire subtree.
  7157. See `org-demote' and `org-promote'."
  7158. (interactive)
  7159. (save-excursion
  7160. (org-with-limited-levels (org-map-tree 'org-demote)))
  7161. (org-fix-position-after-promote))
  7162. (defun org-do-promote ()
  7163. "Promote the current heading higher up the tree.
  7164. If the region is active in `transient-mark-mode', promote all
  7165. headings in the region."
  7166. (interactive)
  7167. (save-excursion
  7168. (if (org-region-active-p)
  7169. (org-map-region 'org-promote (region-beginning) (region-end))
  7170. (org-promote)))
  7171. (org-fix-position-after-promote))
  7172. (defun org-do-demote ()
  7173. "Demote the current heading lower down the tree.
  7174. If the region is active in `transient-mark-mode', demote all
  7175. headings in the region."
  7176. (interactive)
  7177. (save-excursion
  7178. (if (org-region-active-p)
  7179. (org-map-region 'org-demote (region-beginning) (region-end))
  7180. (org-demote)))
  7181. (org-fix-position-after-promote))
  7182. (defun org-fix-position-after-promote ()
  7183. "Fix cursor position and indentation after demoting/promoting."
  7184. (let ((pos (point)))
  7185. (when (save-excursion
  7186. (beginning-of-line 1)
  7187. (looking-at org-todo-line-regexp)
  7188. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  7189. (cond ((eobp) (insert " "))
  7190. ((eolp) (insert " "))
  7191. ((equal (char-after) ?\ ) (forward-char 1))))))
  7192. (defun org-current-level ()
  7193. "Return the level of the current entry, or nil if before the first headline.
  7194. The level is the number of stars at the beginning of the
  7195. headline. Use `org-reduced-level' to remove the effect of
  7196. `org-odd-levels'. Unlike to `org-outline-level', this function
  7197. ignores inlinetasks."
  7198. (let ((level (org-with-limited-levels (org-outline-level))))
  7199. (and (> level 0) level)))
  7200. (defun org-get-previous-line-level ()
  7201. "Return the outline depth of the last headline before the current line.
  7202. Returns 0 for the first headline in the buffer, and nil if before the
  7203. first headline."
  7204. (and (org-current-level)
  7205. (or (and (/= (line-beginning-position) (point-min))
  7206. (save-excursion (beginning-of-line 0) (org-current-level)))
  7207. 0)))
  7208. (defun org-reduced-level (l)
  7209. "Compute the effective level of a heading.
  7210. This takes into account the setting of `org-odd-levels-only'."
  7211. (cond
  7212. ((zerop l) 0)
  7213. (org-odd-levels-only (1+ (floor (/ l 2))))
  7214. (t l)))
  7215. (defun org-level-increment ()
  7216. "Return the number of stars that will be added or removed at a
  7217. time to headlines when structure editing, based on the value of
  7218. `org-odd-levels-only'."
  7219. (if org-odd-levels-only 2 1))
  7220. (defun org-get-valid-level (level &optional change)
  7221. "Rectify a level change under the influence of `org-odd-levels-only'
  7222. LEVEL is a current level, CHANGE is by how much the level should be
  7223. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  7224. even level numbers will become the next higher odd number."
  7225. (if org-odd-levels-only
  7226. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  7227. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  7228. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  7229. (max 1 (+ level (or change 0)))))
  7230. (if (boundp 'define-obsolete-function-alias)
  7231. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  7232. (define-obsolete-function-alias 'org-get-legal-level
  7233. 'org-get-valid-level)
  7234. (define-obsolete-function-alias 'org-get-legal-level
  7235. 'org-get-valid-level "23.1")))
  7236. (defun org-promote ()
  7237. "Promote the current heading higher up the tree."
  7238. (org-with-wide-buffer
  7239. (org-back-to-heading t)
  7240. (let* ((after-change-functions (remq 'flyspell-after-change-function
  7241. after-change-functions))
  7242. (level (save-match-data (funcall outline-level)))
  7243. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  7244. (diff (abs (- level (length up-head) -1))))
  7245. (cond
  7246. ((and (= level 1) org-allow-promoting-top-level-subtree)
  7247. (replace-match "# " nil t))
  7248. ((= level 1)
  7249. (user-error "Cannot promote to level 0. UNDO to recover if necessary"))
  7250. (t (replace-match up-head nil t)))
  7251. (unless (= level 1)
  7252. (when org-auto-align-tags (org-set-tags nil 'ignore-column))
  7253. (when org-adapt-indentation (org-fixup-indentation (- diff))))
  7254. (run-hooks 'org-after-promote-entry-hook))))
  7255. (defun org-demote ()
  7256. "Demote the current heading lower down the tree."
  7257. (org-with-wide-buffer
  7258. (org-back-to-heading t)
  7259. (let* ((after-change-functions (remq 'flyspell-after-change-function
  7260. after-change-functions))
  7261. (level (save-match-data (funcall outline-level)))
  7262. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  7263. (diff (abs (- level (length down-head) -1))))
  7264. (replace-match down-head nil t)
  7265. (when org-auto-align-tags (org-set-tags nil 'ignore-column))
  7266. (when org-adapt-indentation (org-fixup-indentation diff))
  7267. (run-hooks 'org-after-demote-entry-hook))))
  7268. (defun org-cycle-level ()
  7269. "Cycle the level of an empty headline through possible states.
  7270. This goes first to child, then to parent, level, then up the hierarchy.
  7271. After top level, it switches back to sibling level."
  7272. (interactive)
  7273. (let ((org-adapt-indentation nil))
  7274. (when (org-point-at-end-of-empty-headline)
  7275. (setq this-command 'org-cycle-level) ; Only needed for caching
  7276. (let ((cur-level (org-current-level))
  7277. (prev-level (org-get-previous-line-level)))
  7278. (cond
  7279. ;; If first headline in file, promote to top-level.
  7280. ((= prev-level 0)
  7281. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7282. do (org-do-promote)))
  7283. ;; If same level as prev, demote one.
  7284. ((= prev-level cur-level)
  7285. (org-do-demote))
  7286. ;; If parent is top-level, promote to top level if not already.
  7287. ((= prev-level 1)
  7288. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7289. do (org-do-promote)))
  7290. ;; If top-level, return to prev-level.
  7291. ((= cur-level 1)
  7292. (loop repeat (/ (- prev-level 1) (org-level-increment))
  7293. do (org-do-demote)))
  7294. ;; If less than prev-level, promote one.
  7295. ((< cur-level prev-level)
  7296. (org-do-promote))
  7297. ;; If deeper than prev-level, promote until higher than
  7298. ;; prev-level.
  7299. ((> cur-level prev-level)
  7300. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  7301. do (org-do-promote))))
  7302. t))))
  7303. (defun org-map-tree (fun)
  7304. "Call FUN for every heading underneath the current one."
  7305. (org-back-to-heading)
  7306. (let ((level (funcall outline-level)))
  7307. (save-excursion
  7308. (funcall fun)
  7309. (while (and (progn
  7310. (outline-next-heading)
  7311. (> (funcall outline-level) level))
  7312. (not (eobp)))
  7313. (funcall fun)))))
  7314. (defun org-map-region (fun beg end)
  7315. "Call FUN for every heading between BEG and END."
  7316. (let ((org-ignore-region t))
  7317. (save-excursion
  7318. (setq end (copy-marker end))
  7319. (goto-char beg)
  7320. (if (and (re-search-forward org-outline-regexp-bol nil t)
  7321. (< (point) end))
  7322. (funcall fun))
  7323. (while (and (progn
  7324. (outline-next-heading)
  7325. (< (point) end))
  7326. (not (eobp)))
  7327. (funcall fun)))))
  7328. (defun org-fixup-indentation (diff)
  7329. "Change the indentation in the current entry by DIFF.
  7330. DIFF is an integer. Indentation is done according to the
  7331. following rules:
  7332. - Planning information and property drawers are always indented
  7333. according to the new level of the headline;
  7334. - Footnote definitions and their contents are ignored;
  7335. - Inlinetasks' boundaries are not shifted;
  7336. - Empty lines are ignored;
  7337. - Other lines' indentation are shifted by DIFF columns, unless
  7338. it would introduce a structural change in the document, in
  7339. which case no shifting is done at all.
  7340. Assume point is at a heading or an inlinetask beginning."
  7341. (org-with-wide-buffer
  7342. (narrow-to-region (line-beginning-position)
  7343. (save-excursion
  7344. (if (org-with-limited-levels (org-at-heading-p))
  7345. (org-with-limited-levels (outline-next-heading))
  7346. (org-inlinetask-goto-end))
  7347. (point)))
  7348. (forward-line)
  7349. ;; Indent properly planning info and property drawer.
  7350. (when (org-looking-at-p org-planning-line-re)
  7351. (org-indent-line)
  7352. (forward-line))
  7353. (when (looking-at org-property-drawer-re)
  7354. (goto-char (match-end 0))
  7355. (forward-line)
  7356. (save-excursion (org-indent-region (match-beginning 0) (match-end 0))))
  7357. (catch 'no-shift
  7358. (when (zerop diff) (throw 'no-shift nil))
  7359. ;; If DIFF is negative, first check if a shift is possible at all
  7360. ;; (e.g., it doesn't break structure). This can only happen if
  7361. ;; some contents are not properly indented.
  7362. (let ((case-fold-search t))
  7363. (when (< diff 0)
  7364. (let ((diff (- diff))
  7365. (forbidden-re (concat org-outline-regexp
  7366. "\\|"
  7367. (substring org-footnote-definition-re 1))))
  7368. (save-excursion
  7369. (while (not (eobp))
  7370. (cond
  7371. ((org-looking-at-p "[ \t]*$") (forward-line))
  7372. ((and (org-looking-at-p org-footnote-definition-re)
  7373. (let ((e (org-element-at-point)))
  7374. (and (eq (org-element-type e) 'footnote-definition)
  7375. (goto-char (org-element-property :end e))))))
  7376. ((org-looking-at-p org-outline-regexp) (forward-line))
  7377. ;; Give up if shifting would move before column 0 or
  7378. ;; if it would introduce a headline or a footnote
  7379. ;; definition.
  7380. (t
  7381. (skip-chars-forward " \t")
  7382. (let ((ind (current-column)))
  7383. (when (or (< ind diff)
  7384. (and (= ind diff) (org-looking-at-p forbidden-re)))
  7385. (throw 'no-shift nil)))
  7386. ;; Ignore contents of example blocks and source
  7387. ;; blocks if their indentation is meant to be
  7388. ;; preserved. Jump to block's closing line.
  7389. (beginning-of-line)
  7390. (or (and (org-looking-at-p "[ \t]*#\\+BEGIN_\\(EXAMPLE\\|SRC\\)")
  7391. (let ((e (org-element-at-point)))
  7392. (and (memq (org-element-type e)
  7393. '(example-block src-block))
  7394. (or org-src-preserve-indentation
  7395. (org-element-property :preserve-indent e))
  7396. (goto-char (org-element-property :end e))
  7397. (progn (skip-chars-backward " \r\t\n")
  7398. (beginning-of-line)
  7399. t))))
  7400. (forward-line))))))))
  7401. ;; Shift lines but footnote definitions, inlinetasks boundaries
  7402. ;; by DIFF. Also skip contents of source or example blocks
  7403. ;; when indentation is meant to be preserved.
  7404. (while (not (eobp))
  7405. (cond
  7406. ((and (org-looking-at-p org-footnote-definition-re)
  7407. (let ((e (org-element-at-point)))
  7408. (and (eq (org-element-type e) 'footnote-definition)
  7409. (goto-char (org-element-property :end e))))))
  7410. ((org-looking-at-p org-outline-regexp) (forward-line))
  7411. ((org-looking-at-p "[ \t]*$") (forward-line))
  7412. (t
  7413. (org-indent-line-to (+ (org-get-indentation) diff))
  7414. (beginning-of-line)
  7415. (or (and (org-looking-at-p "[ \t]*#\\+BEGIN_\\(EXAMPLE\\|SRC\\)")
  7416. (let ((e (org-element-at-point)))
  7417. (and (memq (org-element-type e)
  7418. '(example-block src-block))
  7419. (or org-src-preserve-indentation
  7420. (org-element-property :preserve-indent e))
  7421. (goto-char (org-element-property :end e))
  7422. (progn (skip-chars-backward " \r\t\n")
  7423. (beginning-of-line)
  7424. t))))
  7425. (forward-line)))))))))
  7426. (defun org-convert-to-odd-levels ()
  7427. "Convert an org-mode file with all levels allowed to one with odd levels.
  7428. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  7429. level 5 etc."
  7430. (interactive)
  7431. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  7432. (let ((outline-level 'org-outline-level)
  7433. (org-odd-levels-only nil) n)
  7434. (save-excursion
  7435. (goto-char (point-min))
  7436. (while (re-search-forward "^\\*\\*+ " nil t)
  7437. (setq n (- (length (match-string 0)) 2))
  7438. (while (>= (setq n (1- n)) 0)
  7439. (org-demote))
  7440. (end-of-line 1))))))
  7441. (defun org-convert-to-oddeven-levels ()
  7442. "Convert an org-mode file with only odd levels to one with odd/even levels.
  7443. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  7444. file contains a section with an even level, conversion would
  7445. destroy the structure of the file. An error is signaled in this
  7446. case."
  7447. (interactive)
  7448. (goto-char (point-min))
  7449. ;; First check if there are no even levels
  7450. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  7451. (org-show-set-visibility 'canonical)
  7452. (error "Not all levels are odd in this file. Conversion not possible"))
  7453. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  7454. (let ((outline-regexp org-outline-regexp)
  7455. (outline-level 'org-outline-level)
  7456. (org-odd-levels-only nil) n)
  7457. (save-excursion
  7458. (goto-char (point-min))
  7459. (while (re-search-forward "^\\*\\*+ " nil t)
  7460. (setq n (/ (1- (length (match-string 0))) 2))
  7461. (while (>= (setq n (1- n)) 0)
  7462. (org-promote))
  7463. (end-of-line 1))))))
  7464. (defun org-tr-level (n)
  7465. "Make N odd if required."
  7466. (if org-odd-levels-only (1+ (/ n 2)) n))
  7467. ;;; Vertical tree motion, cutting and pasting of subtrees
  7468. (defun org-move-subtree-up (&optional arg)
  7469. "Move the current subtree up past ARG headlines of the same level."
  7470. (interactive "p")
  7471. (org-move-subtree-down (- (prefix-numeric-value arg))))
  7472. (defun org-move-subtree-down (&optional arg)
  7473. "Move the current subtree down past ARG headlines of the same level."
  7474. (interactive "p")
  7475. (setq arg (prefix-numeric-value arg))
  7476. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  7477. 'org-get-last-sibling))
  7478. (ins-point (make-marker))
  7479. (cnt (abs arg))
  7480. (col (current-column))
  7481. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  7482. ;; Select the tree
  7483. (org-back-to-heading)
  7484. (setq beg0 (point))
  7485. (save-excursion
  7486. (setq ne-beg (org-back-over-empty-lines))
  7487. (setq beg (point)))
  7488. (save-match-data
  7489. (save-excursion (outline-end-of-heading)
  7490. (setq folded (outline-invisible-p)))
  7491. (progn (org-end-of-subtree nil t)
  7492. (unless (eobp) (backward-char))))
  7493. (outline-next-heading)
  7494. (setq ne-end (org-back-over-empty-lines))
  7495. (setq end (point))
  7496. (goto-char beg0)
  7497. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  7498. ;; include less whitespace
  7499. (save-excursion
  7500. (goto-char beg)
  7501. (forward-line (- ne-beg ne-end))
  7502. (setq beg (point))))
  7503. ;; Find insertion point, with error handling
  7504. (while (> cnt 0)
  7505. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  7506. (progn (goto-char beg0)
  7507. (user-error "Cannot move past superior level or buffer limit")))
  7508. (setq cnt (1- cnt)))
  7509. (if (> arg 0)
  7510. ;; Moving forward - still need to move over subtree
  7511. (progn (org-end-of-subtree t t)
  7512. (save-excursion
  7513. (org-back-over-empty-lines)
  7514. (or (bolp) (newline)))))
  7515. (setq ne-ins (org-back-over-empty-lines))
  7516. (move-marker ins-point (point))
  7517. (setq txt (buffer-substring beg end))
  7518. (org-save-markers-in-region beg end)
  7519. (delete-region beg end)
  7520. (org-remove-empty-overlays-at beg)
  7521. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  7522. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  7523. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  7524. (let ((bbb (point)))
  7525. (insert-before-markers txt)
  7526. (org-reinstall-markers-in-region bbb)
  7527. (move-marker ins-point bbb))
  7528. (or (bolp) (insert "\n"))
  7529. (setq ins-end (point))
  7530. (goto-char ins-point)
  7531. (org-skip-whitespace)
  7532. (when (and (< arg 0)
  7533. (org-first-sibling-p)
  7534. (> ne-ins ne-beg))
  7535. ;; Move whitespace back to beginning
  7536. (save-excursion
  7537. (goto-char ins-end)
  7538. (let ((kill-whole-line t))
  7539. (kill-line (- ne-ins ne-beg)) (point)))
  7540. (insert (make-string (- ne-ins ne-beg) ?\n)))
  7541. (move-marker ins-point nil)
  7542. (if folded
  7543. (hide-subtree)
  7544. (org-show-entry)
  7545. (org-show-children)
  7546. (org-cycle-hide-drawers 'children))
  7547. (org-clean-visibility-after-subtree-move)
  7548. ;; move back to the initial column we were at
  7549. (move-to-column col)))
  7550. (defvar org-subtree-clip ""
  7551. "Clipboard for cut and paste of subtrees.
  7552. This is actually only a copy of the kill, because we use the normal kill
  7553. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  7554. (defvar org-subtree-clip-folded nil
  7555. "Was the last copied subtree folded?
  7556. This is used to fold the tree back after pasting.")
  7557. (defun org-cut-subtree (&optional n)
  7558. "Cut the current subtree into the clipboard.
  7559. With prefix arg N, cut this many sequential subtrees.
  7560. This is a short-hand for marking the subtree and then cutting it."
  7561. (interactive "p")
  7562. (org-copy-subtree n 'cut))
  7563. (defun org-copy-subtree (&optional n cut force-store-markers nosubtrees)
  7564. "Copy the current subtree it in the clipboard.
  7565. With prefix arg N, copy this many sequential subtrees.
  7566. This is a short-hand for marking the subtree and then copying it.
  7567. If CUT is non-nil, actually cut the subtree.
  7568. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7569. of some markers in the region, even if CUT is non-nil. This is
  7570. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7571. (interactive "p")
  7572. (let (beg end folded (beg0 (point)))
  7573. (if (org-called-interactively-p 'any)
  7574. (org-back-to-heading nil) ; take what looks like a subtree
  7575. (org-back-to-heading t)) ; take what is really there
  7576. (setq beg (point))
  7577. (skip-chars-forward " \t\r\n")
  7578. (save-match-data
  7579. (if nosubtrees
  7580. (outline-next-heading)
  7581. (save-excursion (outline-end-of-heading)
  7582. (setq folded (outline-invisible-p)))
  7583. (ignore-errors (org-forward-heading-same-level (1- n) t))
  7584. (org-end-of-subtree t t)))
  7585. ;; Include the end of an inlinetask
  7586. (when (and (featurep 'org-inlinetask)
  7587. (looking-at-p (concat (org-inlinetask-outline-regexp)
  7588. "END[ \t]*$")))
  7589. (end-of-line))
  7590. (setq end (point))
  7591. (goto-char beg0)
  7592. (when (> end beg)
  7593. (setq org-subtree-clip-folded folded)
  7594. (when (or cut force-store-markers)
  7595. (org-save-markers-in-region beg end))
  7596. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7597. (setq org-subtree-clip (current-kill 0))
  7598. (message "%s: Subtree(s) with %d characters"
  7599. (if cut "Cut" "Copied")
  7600. (length org-subtree-clip)))))
  7601. (defun org-paste-subtree (&optional level tree for-yank remove)
  7602. "Paste the clipboard as a subtree, with modification of headline level.
  7603. The entire subtree is promoted or demoted in order to match a new headline
  7604. level.
  7605. If the cursor is at the beginning of a headline, the same level as
  7606. that headline is used to paste the tree.
  7607. If not, the new level is derived from the *visible* headings
  7608. before and after the insertion point, and taken to be the inferior headline
  7609. level of the two. So if the previous visible heading is level 3 and the
  7610. next is level 4 (or vice versa), level 4 will be used for insertion.
  7611. This makes sure that the subtree remains an independent subtree and does
  7612. not swallow low level entries.
  7613. You can also force a different level, either by using a numeric prefix
  7614. argument, or by inserting the heading marker by hand. For example, if the
  7615. cursor is after \"*****\", then the tree will be shifted to level 5.
  7616. If optional TREE is given, use this text instead of the kill ring.
  7617. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7618. move back over whitespace before inserting, and move point to the end of
  7619. the inserted text when done.
  7620. When REMOVE is non-nil, remove the subtree from the clipboard."
  7621. (interactive "P")
  7622. (setq tree (or tree (and kill-ring (current-kill 0))))
  7623. (unless (org-kill-is-subtree-p tree)
  7624. (user-error "%s"
  7625. (substitute-command-keys
  7626. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7627. (org-with-limited-levels
  7628. (let* ((visp (not (outline-invisible-p)))
  7629. (txt tree)
  7630. (^re_ "\\(\\*+\\)[ \t]*")
  7631. (old-level (if (string-match org-outline-regexp-bol txt)
  7632. (- (match-end 0) (match-beginning 0) 1)
  7633. -1))
  7634. (force-level (cond (level (prefix-numeric-value level))
  7635. ((and (looking-at "[ \t]*$")
  7636. (string-match
  7637. "^\\*+$" (buffer-substring
  7638. (point-at-bol) (point))))
  7639. (- (match-end 0) (match-beginning 0)))
  7640. ((and (bolp)
  7641. (looking-at org-outline-regexp))
  7642. (- (match-end 0) (point) 1))))
  7643. (previous-level (save-excursion
  7644. (condition-case nil
  7645. (progn
  7646. (outline-previous-visible-heading 1)
  7647. (if (looking-at ^re_)
  7648. (- (match-end 0) (match-beginning 0) 1)
  7649. 1))
  7650. (error 1))))
  7651. (next-level (save-excursion
  7652. (condition-case nil
  7653. (progn
  7654. (or (looking-at org-outline-regexp)
  7655. (outline-next-visible-heading 1))
  7656. (if (looking-at ^re_)
  7657. (- (match-end 0) (match-beginning 0) 1)
  7658. 1))
  7659. (error 1))))
  7660. (new-level (or force-level (max previous-level next-level)))
  7661. (shift (if (or (= old-level -1)
  7662. (= new-level -1)
  7663. (= old-level new-level))
  7664. 0
  7665. (- new-level old-level)))
  7666. (delta (if (> shift 0) -1 1))
  7667. (func (if (> shift 0) 'org-demote 'org-promote))
  7668. (org-odd-levels-only nil)
  7669. beg end newend)
  7670. ;; Remove the forced level indicator
  7671. (if force-level
  7672. (delete-region (point-at-bol) (point)))
  7673. ;; Paste
  7674. (beginning-of-line (if (bolp) 1 2))
  7675. (setq beg (point))
  7676. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7677. (insert-before-markers txt)
  7678. (unless (string-match "\n\\'" txt) (insert "\n"))
  7679. (setq newend (point))
  7680. (org-reinstall-markers-in-region beg)
  7681. (setq end (point))
  7682. (goto-char beg)
  7683. (skip-chars-forward " \t\n\r")
  7684. (setq beg (point))
  7685. (if (and (outline-invisible-p) visp)
  7686. (save-excursion (outline-show-heading)))
  7687. ;; Shift if necessary
  7688. (unless (= shift 0)
  7689. (save-restriction
  7690. (narrow-to-region beg end)
  7691. (while (not (= shift 0))
  7692. (org-map-region func (point-min) (point-max))
  7693. (setq shift (+ delta shift)))
  7694. (goto-char (point-min))
  7695. (setq newend (point-max))))
  7696. (when (or (org-called-interactively-p 'interactive) for-yank)
  7697. (message "Clipboard pasted as level %d subtree" new-level))
  7698. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7699. kill-ring
  7700. (eq org-subtree-clip (current-kill 0))
  7701. org-subtree-clip-folded)
  7702. ;; The tree was folded before it was killed/copied
  7703. (hide-subtree))
  7704. (and for-yank (goto-char newend))
  7705. (and remove (setq kill-ring (cdr kill-ring))))))
  7706. (defun org-kill-is-subtree-p (&optional txt)
  7707. "Check if the current kill is an outline subtree, or a set of trees.
  7708. Returns nil if kill does not start with a headline, or if the first
  7709. headline level is not the largest headline level in the tree.
  7710. So this will actually accept several entries of equal levels as well,
  7711. which is OK for `org-paste-subtree'.
  7712. If optional TXT is given, check this string instead of the current kill."
  7713. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7714. (re (org-get-limited-outline-regexp))
  7715. (^re (concat "^" re))
  7716. (start-level (and kill
  7717. (string-match
  7718. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7719. kill)
  7720. (- (match-end 2) (match-beginning 2) 1)))
  7721. (start (1+ (or (match-beginning 2) -1))))
  7722. (if (not start-level)
  7723. (progn
  7724. nil) ;; does not even start with a heading
  7725. (catch 'exit
  7726. (while (setq start (string-match ^re kill (1+ start)))
  7727. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7728. (throw 'exit nil)))
  7729. t))))
  7730. (defvar org-markers-to-move nil
  7731. "Markers that should be moved with a cut-and-paste operation.
  7732. Those markers are stored together with their positions relative to
  7733. the start of the region.")
  7734. (defun org-save-markers-in-region (beg end)
  7735. "Check markers in region.
  7736. If these markers are between BEG and END, record their position relative
  7737. to BEG, so that after moving the block of text, we can put the markers back
  7738. into place.
  7739. This function gets called just before an entry or tree gets cut from the
  7740. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7741. called immediately, to move the markers with the entries."
  7742. (setq org-markers-to-move nil)
  7743. (when (featurep 'org-clock)
  7744. (org-clock-save-markers-for-cut-and-paste beg end))
  7745. (when (featurep 'org-agenda)
  7746. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7747. (defun org-check-and-save-marker (marker beg end)
  7748. "Check if MARKER is between BEG and END.
  7749. If yes, remember the marker and the distance to BEG."
  7750. (when (and (marker-buffer marker)
  7751. (equal (marker-buffer marker) (current-buffer)))
  7752. (if (and (>= marker beg) (< marker end))
  7753. (push (cons marker (- marker beg)) org-markers-to-move))))
  7754. (defun org-reinstall-markers-in-region (beg)
  7755. "Move all remembered markers to their position relative to BEG."
  7756. (mapc (lambda (x)
  7757. (move-marker (car x) (+ beg (cdr x))))
  7758. org-markers-to-move)
  7759. (setq org-markers-to-move nil))
  7760. (defun org-narrow-to-subtree ()
  7761. "Narrow buffer to the current subtree."
  7762. (interactive)
  7763. (save-excursion
  7764. (save-match-data
  7765. (org-with-limited-levels
  7766. (narrow-to-region
  7767. (progn (org-back-to-heading t) (point))
  7768. (progn (org-end-of-subtree t t)
  7769. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7770. (point)))))))
  7771. (defun org-narrow-to-block ()
  7772. "Narrow buffer to the current block."
  7773. (interactive)
  7774. (let* ((case-fold-search t)
  7775. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7776. "^[ \t]*#\\+end_.*")))
  7777. (if blockp
  7778. (narrow-to-region (car blockp) (cdr blockp))
  7779. (user-error "Not in a block"))))
  7780. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7781. "Clone the task (subtree) at point N times.
  7782. The clones will be inserted as siblings.
  7783. In interactive use, the user will be prompted for the number of
  7784. clones to be produced. If the entry has a timestamp, the user
  7785. will also be prompted for a time shift, which may be a repeater
  7786. as used in time stamps, for example `+3d'. To disable this,
  7787. you can call the function with a universal prefix argument.
  7788. When a valid repeater is given and the entry contains any time
  7789. stamps, the clones will become a sequence in time, with time
  7790. stamps in the subtree shifted for each clone produced. If SHIFT
  7791. is nil or the empty string, time stamps will be left alone. The
  7792. ID property of the original subtree is removed.
  7793. If the original subtree did contain time stamps with a repeater,
  7794. the following will happen:
  7795. - the repeater will be removed in each clone
  7796. - an additional clone will be produced, with the current, unshifted
  7797. date(s) in the entry.
  7798. - the original entry will be placed *after* all the clones, with
  7799. repeater intact.
  7800. - the start days in the repeater in the original entry will be shifted
  7801. to past the last clone.
  7802. In this way you can spell out a number of instances of a repeating task,
  7803. and still retain the repeater to cover future instances of the task.
  7804. As described above, N+1 clones are produced when the original
  7805. subtree has a repeater. Setting N to 0, then, can be used to
  7806. remove the repeater from a subtree and create a shifted clone
  7807. with the original repeater."
  7808. (interactive "nNumber of clones to produce: ")
  7809. (let ((shift
  7810. (or shift
  7811. (if (and (not (equal current-prefix-arg '(4)))
  7812. (save-excursion
  7813. (re-search-forward org-ts-regexp-both
  7814. (save-excursion
  7815. (org-end-of-subtree t)
  7816. (point)) t)))
  7817. (read-from-minibuffer
  7818. "Date shift per clone (e.g. +1w, empty to copy unchanged): ")
  7819. ""))) ;; No time shift
  7820. (n-no-remove -1)
  7821. (drawer-re org-drawer-regexp)
  7822. (org-clock-re (format "^[ \t]*%s.*$" org-clock-string))
  7823. beg end template task idprop
  7824. shift-n shift-what doshift nmin nmax)
  7825. (unless (wholenump n)
  7826. (user-error "Invalid number of replications %s" n))
  7827. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7828. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7829. shift)))
  7830. (user-error "Invalid shift specification %s" shift))
  7831. (when doshift
  7832. (setq shift-n (string-to-number (match-string 1 shift))
  7833. shift-what (cdr (assoc (match-string 2 shift)
  7834. '(("d" . day) ("w" . week)
  7835. ("m" . month) ("y" . year))))))
  7836. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7837. (setq nmin 1 nmax n)
  7838. (org-back-to-heading t)
  7839. (setq beg (point))
  7840. (setq idprop (org-entry-get nil "ID"))
  7841. (org-end-of-subtree t t)
  7842. (or (bolp) (insert "\n"))
  7843. (setq end (point))
  7844. (setq template (buffer-substring beg end))
  7845. (when (and doshift
  7846. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7847. (delete-region beg end)
  7848. (setq end beg)
  7849. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7850. (goto-char end)
  7851. (loop for n from nmin to nmax do
  7852. ;; prepare clone
  7853. (with-temp-buffer
  7854. (insert template)
  7855. (org-mode)
  7856. (goto-char (point-min))
  7857. (org-show-subtree)
  7858. (and idprop (if org-clone-delete-id
  7859. (org-entry-delete nil "ID")
  7860. (org-id-get-create t)))
  7861. (unless (= n 0)
  7862. (while (re-search-forward org-clock-re nil t)
  7863. (kill-whole-line))
  7864. (goto-char (point-min))
  7865. (while (re-search-forward drawer-re nil t)
  7866. (org-remove-empty-drawer-at (point))))
  7867. (goto-char (point-min))
  7868. (when doshift
  7869. (while (re-search-forward org-ts-regexp-both nil t)
  7870. (org-timestamp-change (* n shift-n) shift-what))
  7871. (unless (= n n-no-remove)
  7872. (goto-char (point-min))
  7873. (while (re-search-forward org-ts-regexp nil t)
  7874. (save-excursion
  7875. (goto-char (match-beginning 0))
  7876. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7877. (delete-region (match-beginning 1) (match-end 1)))))))
  7878. (setq task (buffer-string)))
  7879. (insert task))
  7880. (goto-char beg)))
  7881. ;;; Outline Sorting
  7882. (defun org-sort (with-case)
  7883. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7884. Optional argument WITH-CASE means sort case-sensitively."
  7885. (interactive "P")
  7886. (cond
  7887. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7888. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7889. (t
  7890. (org-call-with-arg 'org-sort-entries with-case))))
  7891. (defun org-sort-remove-invisible (s)
  7892. "Remove invisible links from string S."
  7893. (remove-text-properties 0 (length s) org-rm-props s)
  7894. (while (string-match org-bracket-link-regexp s)
  7895. (setq s (replace-match (if (match-end 2)
  7896. (match-string 3 s)
  7897. (match-string 1 s))
  7898. t t s)))
  7899. (let ((st (format " %s " s)))
  7900. (while (string-match org-emph-re st)
  7901. (setq st (replace-match (format " %s " (match-string 4 st)) t t st)))
  7902. (setq s (substring st 1 -1)))
  7903. s)
  7904. (defvar org-priority-regexp) ; defined later in the file
  7905. (defvar org-after-sorting-entries-or-items-hook nil
  7906. "Hook that is run after a bunch of entries or items have been sorted.
  7907. When children are sorted, the cursor is in the parent line when this
  7908. hook gets called. When a region or a plain list is sorted, the cursor
  7909. will be in the first entry of the sorted region/list.")
  7910. (defun org-sort-entries
  7911. (&optional with-case sorting-type getkey-func compare-func property)
  7912. "Sort entries on a certain level of an outline tree.
  7913. If there is an active region, the entries in the region are sorted.
  7914. Else, if the cursor is before the first entry, sort the top-level items.
  7915. Else, the children of the entry at point are sorted.
  7916. Sorting can be alphabetically, numerically, by date/time as given by
  7917. a time stamp, by a property, by priority order, or by a custom function.
  7918. The command prompts for the sorting type unless it has been given to the
  7919. function through the SORTING-TYPE argument, which needs to be a character,
  7920. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F ?k ?K). Here is
  7921. the precise meaning of each character:
  7922. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7923. c By creation time, which is assumed to be the first inactive time stamp
  7924. at the beginning of a line.
  7925. d By deadline date/time.
  7926. k By clocking time.
  7927. n Numerically, by converting the beginning of the entry/item to a number.
  7928. o By order of TODO keywords.
  7929. p By priority according to the cookie.
  7930. r By the value of a property.
  7931. s By scheduled date/time.
  7932. t By date/time, either the first active time stamp in the entry, or, if
  7933. none exist, by the first inactive one.
  7934. Capital letters will reverse the sort order.
  7935. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7936. called with point at the beginning of the record. It must return either
  7937. a string or a number that should serve as the sorting key for that record.
  7938. Comparing entries ignores case by default. However, with an optional argument
  7939. WITH-CASE, the sorting considers case as well.
  7940. Sorting is done against the visible part of the headlines, it ignores hidden
  7941. links.
  7942. When sorting is done, call `org-after-sorting-entries-or-items-hook'."
  7943. (interactive "P")
  7944. (let ((case-func (if with-case 'identity 'downcase))
  7945. (cmstr
  7946. ;; The clock marker is lost when using `sort-subr', let's
  7947. ;; store the clocking string.
  7948. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7949. (save-excursion
  7950. (goto-char org-clock-marker)
  7951. (buffer-substring-no-properties (line-beginning-position)
  7952. (point)))))
  7953. start beg end stars re re2
  7954. txt what tmp)
  7955. ;; Find beginning and end of region to sort
  7956. (cond
  7957. ((org-region-active-p)
  7958. ;; we will sort the region
  7959. (setq end (region-end)
  7960. what "region")
  7961. (goto-char (region-beginning))
  7962. (if (not (org-at-heading-p)) (outline-next-heading))
  7963. (setq start (point)))
  7964. ((or (org-at-heading-p)
  7965. (ignore-errors (progn (org-back-to-heading) t)))
  7966. ;; we will sort the children of the current headline
  7967. (org-back-to-heading)
  7968. (setq start (point)
  7969. end (progn (org-end-of-subtree t t)
  7970. (or (bolp) (insert "\n"))
  7971. (when (>= (org-back-over-empty-lines) 1)
  7972. (forward-line 1))
  7973. (point))
  7974. what "children")
  7975. (goto-char start)
  7976. (show-subtree)
  7977. (outline-next-heading))
  7978. (t
  7979. ;; we will sort the top-level entries in this file
  7980. (goto-char (point-min))
  7981. (or (org-at-heading-p) (outline-next-heading))
  7982. (setq start (point))
  7983. (goto-char (point-max))
  7984. (beginning-of-line 1)
  7985. (when (looking-at ".*?\\S-")
  7986. ;; File ends in a non-white line
  7987. (end-of-line 1)
  7988. (insert "\n"))
  7989. (setq end (point-max))
  7990. (setq what "top-level")
  7991. (goto-char start)
  7992. (show-all)))
  7993. (setq beg (point))
  7994. (when (>= beg end) (goto-char start) (user-error "Nothing to sort"))
  7995. (looking-at "\\(\\*+\\)")
  7996. (setq stars (match-string 1)
  7997. re (concat "^" (regexp-quote stars) " +")
  7998. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7999. txt (buffer-substring beg end))
  8000. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  8001. (if (and (not (equal stars "*")) (string-match re2 txt))
  8002. (user-error "Region to sort contains a level above the first entry"))
  8003. (unless sorting-type
  8004. (message
  8005. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  8006. [t]ime [s]cheduled [d]eadline [c]reated cloc[k]ing
  8007. A/N/P/R/O/F/T/S/D/C/K means reversed:"
  8008. what)
  8009. (setq sorting-type (read-char-exclusive))
  8010. (unless getkey-func
  8011. (and (= (downcase sorting-type) ?f)
  8012. (setq getkey-func
  8013. (org-icompleting-read "Sort using function: "
  8014. obarray 'fboundp t nil nil))
  8015. (setq getkey-func (intern getkey-func))))
  8016. (and (= (downcase sorting-type) ?r)
  8017. (not property)
  8018. (setq property
  8019. (org-icompleting-read "Property: "
  8020. (mapcar 'list (org-buffer-property-keys t))
  8021. nil t))))
  8022. (when (member sorting-type '(?k ?K)) (org-clock-sum))
  8023. (message "Sorting entries...")
  8024. (save-restriction
  8025. (narrow-to-region start end)
  8026. (let ((dcst (downcase sorting-type))
  8027. (case-fold-search nil)
  8028. (now (current-time)))
  8029. (sort-subr
  8030. (/= dcst sorting-type)
  8031. ;; This function moves to the beginning character of the "record" to
  8032. ;; be sorted.
  8033. (lambda nil
  8034. (if (re-search-forward re nil t)
  8035. (goto-char (match-beginning 0))
  8036. (goto-char (point-max))))
  8037. ;; This function moves to the last character of the "record" being
  8038. ;; sorted.
  8039. (lambda nil
  8040. (save-match-data
  8041. (condition-case nil
  8042. (outline-forward-same-level 1)
  8043. (error
  8044. (goto-char (point-max))))))
  8045. ;; This function returns the value that gets sorted against.
  8046. (lambda nil
  8047. (cond
  8048. ((= dcst ?n)
  8049. (if (looking-at org-complex-heading-regexp)
  8050. (string-to-number (org-sort-remove-invisible (match-string 4)))
  8051. nil))
  8052. ((= dcst ?a)
  8053. (if (looking-at org-complex-heading-regexp)
  8054. (funcall case-func (org-sort-remove-invisible (match-string 4)))
  8055. nil))
  8056. ((= dcst ?k)
  8057. (or (get-text-property (point) :org-clock-minutes) 0))
  8058. ((= dcst ?t)
  8059. (let ((end (save-excursion (outline-next-heading) (point))))
  8060. (if (or (re-search-forward org-ts-regexp end t)
  8061. (re-search-forward org-ts-regexp-both end t))
  8062. (org-time-string-to-seconds (match-string 0))
  8063. (org-float-time now))))
  8064. ((= dcst ?c)
  8065. (let ((end (save-excursion (outline-next-heading) (point))))
  8066. (if (re-search-forward
  8067. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  8068. end t)
  8069. (org-time-string-to-seconds (match-string 0))
  8070. (org-float-time now))))
  8071. ((= dcst ?s)
  8072. (let ((end (save-excursion (outline-next-heading) (point))))
  8073. (if (re-search-forward org-scheduled-time-regexp end t)
  8074. (org-time-string-to-seconds (match-string 1))
  8075. (org-float-time now))))
  8076. ((= dcst ?d)
  8077. (let ((end (save-excursion (outline-next-heading) (point))))
  8078. (if (re-search-forward org-deadline-time-regexp end t)
  8079. (org-time-string-to-seconds (match-string 1))
  8080. (org-float-time now))))
  8081. ((= dcst ?p)
  8082. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  8083. (string-to-char (match-string 2))
  8084. org-default-priority))
  8085. ((= dcst ?r)
  8086. (or (org-entry-get nil property) ""))
  8087. ((= dcst ?o)
  8088. (if (looking-at org-complex-heading-regexp)
  8089. (let* ((m (match-string 2))
  8090. (s (if (member m org-done-keywords) '- '+)))
  8091. (- 99 (funcall s (length (member m org-todo-keywords-1)))))))
  8092. ((= dcst ?f)
  8093. (if getkey-func
  8094. (progn
  8095. (setq tmp (funcall getkey-func))
  8096. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  8097. tmp)
  8098. (error "Invalid key function `%s'" getkey-func)))
  8099. (t (error "Invalid sorting type `%c'" sorting-type))))
  8100. nil
  8101. (cond
  8102. ((= dcst ?a) 'string<)
  8103. ((= dcst ?f) compare-func)
  8104. ((member dcst '(?p ?t ?s ?d ?c ?k)) '<)))))
  8105. (run-hooks 'org-after-sorting-entries-or-items-hook)
  8106. ;; Reset the clock marker if needed
  8107. (when cmstr
  8108. (save-excursion
  8109. (goto-char start)
  8110. (search-forward cmstr nil t)
  8111. (move-marker org-clock-marker (point))))
  8112. (message "Sorting entries...done")))
  8113. ;;; The orgstruct minor mode
  8114. ;; Define a minor mode which can be used in other modes in order to
  8115. ;; integrate the org-mode structure editing commands.
  8116. ;; This is really a hack, because the org-mode structure commands use
  8117. ;; keys which normally belong to the major mode. Here is how it
  8118. ;; works: The minor mode defines all the keys necessary to operate the
  8119. ;; structure commands, but wraps the commands into a function which
  8120. ;; tests if the cursor is currently at a headline or a plain list
  8121. ;; item. If that is the case, the structure command is used,
  8122. ;; temporarily setting many Org-mode variables like regular
  8123. ;; expressions for filling etc. However, when any of those keys is
  8124. ;; used at a different location, function uses `key-binding' to look
  8125. ;; up if the key has an associated command in another currently active
  8126. ;; keymap (minor modes, major mode, global), and executes that
  8127. ;; command. There might be problems if any of the keys is otherwise
  8128. ;; used as a prefix key.
  8129. (defcustom orgstruct-heading-prefix-regexp ""
  8130. "Regexp that matches the custom prefix of Org headlines in
  8131. orgstruct(++)-mode."
  8132. :group 'org
  8133. :version "24.4"
  8134. :package-version '(Org . "8.3")
  8135. :type 'regexp)
  8136. ;;;###autoload(put 'orgstruct-heading-prefix-regexp 'safe-local-variable 'stringp)
  8137. (defcustom orgstruct-setup-hook nil
  8138. "Hook run after orgstruct-mode-map is filled."
  8139. :group 'org
  8140. :version "24.4"
  8141. :package-version '(Org . "8.0")
  8142. :type 'hook)
  8143. (defvar orgstruct-initialized nil)
  8144. (defvar org-local-vars nil
  8145. "List of local variables, for use by `orgstruct-mode'.")
  8146. ;;;###autoload
  8147. (define-minor-mode orgstruct-mode
  8148. "Toggle the minor mode `orgstruct-mode'.
  8149. This mode is for using Org-mode structure commands in other
  8150. modes. The following keys behave as if Org-mode were active, if
  8151. the cursor is on a headline, or on a plain list item (both as
  8152. defined by Org-mode)."
  8153. nil " OrgStruct" (make-sparse-keymap)
  8154. (funcall (if orgstruct-mode
  8155. 'add-to-invisibility-spec
  8156. 'remove-from-invisibility-spec)
  8157. '(outline . t))
  8158. (when orgstruct-mode
  8159. (org-load-modules-maybe)
  8160. (unless orgstruct-initialized
  8161. (orgstruct-setup)
  8162. (setq orgstruct-initialized t))))
  8163. ;;;###autoload
  8164. (defun turn-on-orgstruct ()
  8165. "Unconditionally turn on `orgstruct-mode'."
  8166. (orgstruct-mode 1))
  8167. (defvar org-fb-vars nil)
  8168. (make-variable-buffer-local 'org-fb-vars)
  8169. (defun orgstruct++-mode (&optional arg)
  8170. "Toggle `orgstruct-mode', the enhanced version of it.
  8171. In addition to setting orgstruct-mode, this also exports all
  8172. indentation and autofilling variables from org-mode into the
  8173. buffer. It will also recognize item context in multiline items."
  8174. (interactive "P")
  8175. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  8176. (if (< arg 1)
  8177. (progn (orgstruct-mode -1)
  8178. (mapc (lambda(v)
  8179. (org-set-local (car v)
  8180. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  8181. org-fb-vars))
  8182. (orgstruct-mode 1)
  8183. (setq org-fb-vars nil)
  8184. (unless org-local-vars
  8185. (setq org-local-vars (org-get-local-variables)))
  8186. (let (var val)
  8187. (mapc
  8188. (lambda (x)
  8189. (when (string-match
  8190. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  8191. (symbol-name (car x)))
  8192. (setq var (car x) val (nth 1 x))
  8193. (push (list var `(quote ,(eval var))) org-fb-vars)
  8194. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  8195. org-local-vars)
  8196. (org-set-local 'orgstruct-is-++ t))))
  8197. (defvar orgstruct-is-++ nil
  8198. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  8199. (make-variable-buffer-local 'orgstruct-is-++)
  8200. ;;;###autoload
  8201. (defun turn-on-orgstruct++ ()
  8202. "Unconditionally turn on `orgstruct++-mode'."
  8203. (orgstruct++-mode 1))
  8204. (defun orgstruct-error ()
  8205. "Error when there is no default binding for a structure key."
  8206. (interactive)
  8207. (funcall (if (fboundp 'user-error)
  8208. 'user-error
  8209. 'error)
  8210. "This key has no function outside structure elements"))
  8211. (defun orgstruct-setup ()
  8212. "Setup orgstruct keymap."
  8213. (dolist (cell '((org-demote . t)
  8214. (org-metaleft . t)
  8215. (org-metaright . t)
  8216. (org-promote . t)
  8217. (org-shiftmetaleft . t)
  8218. (org-shiftmetaright . t)
  8219. org-backward-element
  8220. org-backward-heading-same-level
  8221. org-ctrl-c-ret
  8222. org-ctrl-c-minus
  8223. org-ctrl-c-star
  8224. org-cycle
  8225. org-forward-heading-same-level
  8226. org-insert-heading
  8227. org-insert-heading-respect-content
  8228. org-kill-note-or-show-branches
  8229. org-mark-subtree
  8230. org-meta-return
  8231. org-metadown
  8232. org-metaup
  8233. org-narrow-to-subtree
  8234. org-promote-subtree
  8235. org-reveal
  8236. org-shiftdown
  8237. org-shiftleft
  8238. org-shiftmetadown
  8239. org-shiftmetaup
  8240. org-shiftright
  8241. org-shifttab
  8242. org-shifttab
  8243. org-shiftup
  8244. org-show-children
  8245. org-show-subtree
  8246. org-sort
  8247. org-up-element
  8248. outline-demote
  8249. outline-next-visible-heading
  8250. outline-previous-visible-heading
  8251. outline-promote
  8252. outline-up-heading))
  8253. (let ((f (or (car-safe cell) cell))
  8254. (disable-when-heading-prefix (cdr-safe cell)))
  8255. (when (fboundp f)
  8256. (let ((new-bindings))
  8257. (dolist (binding (nconc (where-is-internal f org-mode-map)
  8258. (where-is-internal f outline-mode-map)))
  8259. (push binding new-bindings)
  8260. ;; TODO use local-function-key-map
  8261. (dolist (rep '(("<tab>" . "TAB")
  8262. ("<return>" . "RET")
  8263. ("<escape>" . "ESC")
  8264. ("<delete>" . "DEL")))
  8265. (setq binding (read-kbd-macro
  8266. (let ((case-fold-search))
  8267. (replace-regexp-in-string
  8268. (regexp-quote (cdr rep))
  8269. (car rep)
  8270. (key-description binding)))))
  8271. (pushnew binding new-bindings :test 'equal)))
  8272. (dolist (binding new-bindings)
  8273. (let ((key (lookup-key orgstruct-mode-map binding)))
  8274. (when (or (not key) (numberp key))
  8275. (ignore-errors
  8276. (org-defkey orgstruct-mode-map
  8277. binding
  8278. (orgstruct-make-binding
  8279. f binding disable-when-heading-prefix))))))))))
  8280. (run-hooks 'orgstruct-setup-hook))
  8281. (defun orgstruct-make-binding (fun key disable-when-heading-prefix)
  8282. "Create a function for binding in the structure minor mode.
  8283. FUN is the command to call inside a table. KEY is the key that
  8284. should be checked in for a command to execute outside of tables.
  8285. Non-nil `disable-when-heading-prefix' means to disable the command
  8286. if `orgstruct-heading-prefix-regexp' is not empty."
  8287. (let ((name (concat "orgstruct-hijacker-" (symbol-name fun))))
  8288. (let ((nname name)
  8289. (i 0))
  8290. (while (fboundp (intern nname))
  8291. (setq nname (format "%s-%d" name (setq i (1+ i)))))
  8292. (setq name (intern nname)))
  8293. (eval
  8294. (let ((bindings '((org-heading-regexp
  8295. (concat "^"
  8296. orgstruct-heading-prefix-regexp
  8297. "\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ ]*$"))
  8298. (org-outline-regexp
  8299. (concat orgstruct-heading-prefix-regexp "\\*+ "))
  8300. (org-outline-regexp-bol
  8301. (concat "^" org-outline-regexp))
  8302. (outline-regexp org-outline-regexp)
  8303. (outline-heading-end-regexp "\n")
  8304. (outline-level 'org-outline-level)
  8305. (outline-heading-alist))))
  8306. `(defun ,name (arg)
  8307. ,(concat "In Structure, run `" (symbol-name fun) "'.\n"
  8308. "Outside of structure, run the binding of `"
  8309. (key-description key) "'."
  8310. (when disable-when-heading-prefix
  8311. (concat
  8312. "\nIf `orgstruct-heading-prefix-regexp' is not empty, this command will always fall\n"
  8313. "back to the default binding due to limitations of Org's implementation of\n"
  8314. "`" (symbol-name fun) "'.")))
  8315. (interactive "p")
  8316. (let* ((disable
  8317. ,(and disable-when-heading-prefix
  8318. '(not (string= orgstruct-heading-prefix-regexp ""))))
  8319. (fallback
  8320. (or disable
  8321. (not
  8322. (let* ,bindings
  8323. (org-context-p 'headline 'item
  8324. ,(when (memq fun
  8325. '(org-insert-heading
  8326. org-insert-heading-respect-content
  8327. org-meta-return))
  8328. '(when orgstruct-is-++
  8329. 'item-body))))))))
  8330. (if fallback
  8331. (let* ((orgstruct-mode)
  8332. (binding
  8333. (let ((key ,key))
  8334. (catch 'exit
  8335. (dolist
  8336. (rep
  8337. '(nil
  8338. ("<\\([^>]*\\)tab>" . "\\1TAB")
  8339. ("<\\([^>]*\\)return>" . "\\1RET")
  8340. ("<\\([^>]*\\)escape>" . "\\1ESC")
  8341. ("<\\([^>]*\\)delete>" . "\\1DEL"))
  8342. nil)
  8343. (when rep
  8344. (setq key (read-kbd-macro
  8345. (let ((case-fold-search))
  8346. (replace-regexp-in-string
  8347. (car rep)
  8348. (cdr rep)
  8349. (key-description key))))))
  8350. (when (key-binding key)
  8351. (throw 'exit (key-binding key))))))))
  8352. (if (keymapp binding)
  8353. (org-set-transient-map binding)
  8354. (let ((func (or binding
  8355. (unless disable
  8356. 'orgstruct-error))))
  8357. (when func
  8358. (call-interactively func)))))
  8359. (org-run-like-in-org-mode
  8360. (lambda ()
  8361. (interactive)
  8362. (let* ,bindings
  8363. (call-interactively ',fun)))))))))
  8364. name))
  8365. (defun org-contextualize-keys (alist contexts)
  8366. "Return valid elements in ALIST depending on CONTEXTS.
  8367. `org-agenda-custom-commands' or `org-capture-templates' are the
  8368. values used for ALIST, and `org-agenda-custom-commands-contexts'
  8369. or `org-capture-templates-contexts' are the associated contexts
  8370. definitions."
  8371. (let ((contexts
  8372. ;; normalize contexts
  8373. (mapcar
  8374. (lambda(c) (cond ((listp (cadr c))
  8375. (list (car c) (car c) (cadr c)))
  8376. ((string= "" (cadr c))
  8377. (list (car c) (car c) (caddr c)))
  8378. (t c)))
  8379. contexts))
  8380. (a alist) r s)
  8381. ;; loop over all commands or templates
  8382. (dolist (c a)
  8383. (let (vrules repl)
  8384. (cond
  8385. ((not (assoc (car c) contexts))
  8386. (push c r))
  8387. ((and (assoc (car c) contexts)
  8388. (setq vrules (org-contextualize-validate-key
  8389. (car c) contexts)))
  8390. (mapc (lambda (vr)
  8391. (when (not (equal (car vr) (cadr vr)))
  8392. (setq repl vr)))
  8393. vrules)
  8394. (if (not repl) (push c r)
  8395. (push (cadr repl) s)
  8396. (push
  8397. (cons (car c)
  8398. (cdr (or (assoc (cadr repl) alist)
  8399. (error "Undefined key `%s' as contextual replacement for `%s'"
  8400. (cadr repl) (car c)))))
  8401. r))))))
  8402. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  8403. (delq
  8404. nil
  8405. (delete-dups
  8406. (mapcar (lambda (x)
  8407. (let ((tpl (car x)))
  8408. (when (not (delq
  8409. nil
  8410. (mapcar (lambda (y)
  8411. (equal y tpl))
  8412. s)))
  8413. x)))
  8414. (reverse r))))))
  8415. (defun org-contextualize-validate-key (key contexts)
  8416. "Check CONTEXTS for agenda or capture KEY."
  8417. (let (rr res)
  8418. (dolist (r contexts)
  8419. (mapc
  8420. (lambda (rr)
  8421. (when
  8422. (and (equal key (car r))
  8423. (if (functionp rr) (funcall rr)
  8424. (or (and (eq (car rr) 'in-file)
  8425. (buffer-file-name)
  8426. (string-match (cdr rr) (buffer-file-name)))
  8427. (and (eq (car rr) 'in-mode)
  8428. (string-match (cdr rr) (symbol-name major-mode)))
  8429. (and (eq (car rr) 'in-buffer)
  8430. (string-match (cdr rr) (buffer-name)))
  8431. (when (and (eq (car rr) 'not-in-file)
  8432. (buffer-file-name))
  8433. (not (string-match (cdr rr) (buffer-file-name))))
  8434. (when (eq (car rr) 'not-in-mode)
  8435. (not (string-match (cdr rr) (symbol-name major-mode))))
  8436. (when (eq (car rr) 'not-in-buffer)
  8437. (not (string-match (cdr rr) (buffer-name)))))))
  8438. (push r res)))
  8439. (car (last r))))
  8440. (delete-dups (delq nil res))))
  8441. (defun org-context-p (&rest contexts)
  8442. "Check if local context is any of CONTEXTS.
  8443. Possible values in the list of contexts are `table', `headline', and `item'."
  8444. (let ((pos (point)))
  8445. (goto-char (point-at-bol))
  8446. (prog1 (or (and (memq 'table contexts)
  8447. (looking-at "[ \t]*|"))
  8448. (and (memq 'headline contexts)
  8449. (looking-at org-outline-regexp))
  8450. (and (memq 'item contexts)
  8451. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  8452. (and (memq 'item-body contexts)
  8453. (org-in-item-p)))
  8454. (goto-char pos))))
  8455. (defun org-get-local-variables ()
  8456. "Return a list of all local variables in an Org mode buffer."
  8457. (let (varlist)
  8458. (with-current-buffer (get-buffer-create "*Org tmp*")
  8459. (erase-buffer)
  8460. (org-mode)
  8461. (setq varlist (buffer-local-variables)))
  8462. (kill-buffer "*Org tmp*")
  8463. (delq nil
  8464. (mapcar
  8465. (lambda (x)
  8466. (setq x
  8467. (if (symbolp x)
  8468. (list x)
  8469. (list (car x) (cdr x))))
  8470. (if (and (not (get (car x) 'org-state))
  8471. (string-match
  8472. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  8473. (symbol-name (car x))))
  8474. x nil))
  8475. varlist))))
  8476. (defun org-clone-local-variables (from-buffer &optional regexp)
  8477. "Clone local variables from FROM-BUFFER.
  8478. Optional argument REGEXP selects variables to clone."
  8479. (mapc
  8480. (lambda (pair)
  8481. (and (symbolp (car pair))
  8482. (or (null regexp)
  8483. (string-match regexp (symbol-name (car pair))))
  8484. (set (make-local-variable (car pair))
  8485. (cdr pair))))
  8486. (buffer-local-variables from-buffer)))
  8487. ;;;###autoload
  8488. (defun org-run-like-in-org-mode (cmd)
  8489. "Run a command, pretending that the current buffer is in Org-mode.
  8490. This will temporarily bind local variables that are typically bound in
  8491. Org-mode to the values they have in Org-mode, and then interactively
  8492. call CMD."
  8493. (org-load-modules-maybe)
  8494. (unless org-local-vars
  8495. (setq org-local-vars (org-get-local-variables)))
  8496. (let (binds)
  8497. (dolist (var org-local-vars)
  8498. (when (or (not (boundp (car var)))
  8499. (eq (symbol-value (car var))
  8500. (default-value (car var))))
  8501. (push (list (car var) `(quote ,(cadr var))) binds)))
  8502. (eval `(let ,binds
  8503. (call-interactively (quote ,cmd))))))
  8504. (defun org-get-category (&optional pos force-refresh)
  8505. "Get the category applying to position POS."
  8506. (save-match-data
  8507. (if force-refresh (org-refresh-category-properties))
  8508. (let ((pos (or pos (point))))
  8509. (or (get-text-property pos 'org-category)
  8510. (progn (org-refresh-category-properties)
  8511. (get-text-property pos 'org-category))))))
  8512. ;;; Refresh properties
  8513. (defun org-refresh-properties (dprop tprop)
  8514. "Refresh buffer text properties.
  8515. DPROP is the drawer property and TPROP is either the
  8516. corresponding text property to set, or an alist with each element
  8517. being a text property (as a symbol) and a function to apply to
  8518. the value of the drawer property."
  8519. (let ((case-fold-search t)
  8520. (inhibit-read-only t))
  8521. (org-with-silent-modifications
  8522. (save-excursion
  8523. (save-restriction
  8524. (widen)
  8525. (goto-char (point-min))
  8526. (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
  8527. (org-refresh-property tprop (org-match-string-no-properties 1))))))))
  8528. (defun org-refresh-property (tprop p)
  8529. "Refresh the buffer text property TPROP from the drawer property P.
  8530. The refresh happens only for the current tree (not subtree)."
  8531. (unless (org-before-first-heading-p)
  8532. (save-excursion
  8533. (org-back-to-heading t)
  8534. (if (symbolp tprop)
  8535. ;; TPROP is a text property symbol
  8536. (put-text-property
  8537. (point) (or (outline-next-heading) (point-max)) tprop p)
  8538. ;; TPROP is an alist with (properties . function) elements
  8539. (dolist (al tprop)
  8540. (save-excursion
  8541. (put-text-property
  8542. (line-beginning-position) (or (outline-next-heading) (point-max))
  8543. (car al)
  8544. (funcall (cdr al) p))))))))
  8545. (defun org-refresh-category-properties ()
  8546. "Refresh category text properties in the buffer."
  8547. (let ((case-fold-search t)
  8548. (inhibit-read-only t)
  8549. (default-category
  8550. (cond ((null org-category)
  8551. (if buffer-file-name
  8552. (file-name-sans-extension
  8553. (file-name-nondirectory buffer-file-name))
  8554. "???"))
  8555. ((symbolp org-category) (symbol-name org-category))
  8556. (t org-category))))
  8557. (org-with-silent-modifications
  8558. (org-with-wide-buffer
  8559. ;; Set buffer-wide category. Search last #+CATEGORY keyword.
  8560. ;; This is the default category for the buffer. If none is
  8561. ;; found, fall-back to `org-category' or buffer file name.
  8562. (put-text-property
  8563. (point-min) (point-max)
  8564. 'org-category
  8565. (catch 'buffer-category
  8566. (goto-char (point-max))
  8567. (while (re-search-backward "^[ \t]*#\\+CATEGORY:" (point-min) t)
  8568. (let ((element (org-element-at-point)))
  8569. (when (eq (org-element-type element) 'keyword)
  8570. (throw 'buffer-category
  8571. (org-element-property :value element)))))
  8572. default-category))
  8573. ;; Set sub-tree specific categories.
  8574. (goto-char (point-min))
  8575. (let ((regexp (org-re-property "CATEGORY")))
  8576. (while (re-search-forward regexp nil t)
  8577. (let ((value (org-match-string-no-properties 3)))
  8578. (when (org-at-property-p)
  8579. (put-text-property
  8580. (save-excursion (org-back-to-heading t) (point))
  8581. (save-excursion (org-end-of-subtree t t) (point))
  8582. 'org-category
  8583. value)))))))))
  8584. (defun org-refresh-stats-properties ()
  8585. "Refresh stats text properties in the buffer."
  8586. (let (stats)
  8587. (org-with-silent-modifications
  8588. (save-excursion
  8589. (save-restriction
  8590. (widen)
  8591. (goto-char (point-min))
  8592. (while (re-search-forward
  8593. (concat org-outline-regexp-bol ".*"
  8594. "\\(?:\\[\\([0-9]+\\)%\\|\\([0-9]+\\)/\\([0-9]+\\)\\]\\)")
  8595. nil t)
  8596. (setq stats (cond ((equal (match-string 3) "0") 0)
  8597. ((match-string 2)
  8598. (/ (* (string-to-number (match-string 2)) 100)
  8599. (string-to-number (match-string 3))))
  8600. (t (string-to-number (match-string 1)))))
  8601. (org-back-to-heading t)
  8602. (put-text-property (point) (progn (org-end-of-subtree t t) (point))
  8603. 'org-stats stats)))))))
  8604. (defun org-refresh-effort-properties ()
  8605. "Refresh effort properties"
  8606. (org-refresh-properties
  8607. org-effort-property
  8608. '((effort . identity)
  8609. (effort-minutes . org-duration-string-to-minutes))))
  8610. ;;;; Link Stuff
  8611. ;;; Link abbreviations
  8612. (defun org-link-expand-abbrev (link)
  8613. "Apply replacements as defined in `org-link-abbrev-alist'."
  8614. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  8615. (let* ((key (match-string 1 link))
  8616. (as (or (assoc key org-link-abbrev-alist-local)
  8617. (assoc key org-link-abbrev-alist)))
  8618. (tag (and (match-end 2) (match-string 3 link)))
  8619. rpl)
  8620. (if (not as)
  8621. link
  8622. (setq rpl (cdr as))
  8623. (cond
  8624. ((symbolp rpl) (funcall rpl tag))
  8625. ((string-match "%(\\([^)]+\\))" rpl)
  8626. (replace-match
  8627. (save-match-data
  8628. (funcall (intern-soft (match-string 1 rpl)) tag)) t t rpl))
  8629. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  8630. ((string-match "%h" rpl)
  8631. (replace-match (url-hexify-string (or tag "")) t t rpl))
  8632. (t (concat rpl tag)))))
  8633. link))
  8634. ;;; Storing and inserting links
  8635. (defvar org-insert-link-history nil
  8636. "Minibuffer history for links inserted with `org-insert-link'.")
  8637. (defvar org-stored-links nil
  8638. "Contains the links stored with `org-store-link'.")
  8639. (defvar org-store-link-plist nil
  8640. "Plist with info about the most recently link created with `org-store-link'.")
  8641. (defvar org-link-protocols nil
  8642. "Link protocols added to Org-mode using `org-add-link-type'.")
  8643. (defvar org-store-link-functions nil
  8644. "List of functions that are called to create and store a link.
  8645. Each function will be called in turn until one returns a non-nil
  8646. value. Each function should check if it is responsible for creating
  8647. this link (for example by looking at the major mode).
  8648. If not, it must exit and return nil.
  8649. If yes, it should return a non-nil value after a calling
  8650. `org-store-link-props' with a list of properties and values.
  8651. Special properties are:
  8652. :type The link prefix, like \"http\". This must be given.
  8653. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  8654. This is obligatory as well.
  8655. :description Optional default description for the second pair
  8656. of brackets in an Org-mode link. The user can still change
  8657. this when inserting this link into an Org-mode buffer.
  8658. In addition to these, any additional properties can be specified
  8659. and then used in capture templates.")
  8660. (defun org-add-link-type (type &optional follow export)
  8661. "Add TYPE to the list of `org-link-types'.
  8662. Re-compute all regular expressions depending on `org-link-types'
  8663. FOLLOW and EXPORT are two functions.
  8664. FOLLOW should take the link path as the single argument and do whatever
  8665. is necessary to follow the link, for example find a file or display
  8666. a mail message.
  8667. EXPORT should format the link path for export to one of the export formats.
  8668. It should be a function accepting three arguments:
  8669. path the path of the link, the text after the prefix (like \"http:\")
  8670. desc the description of the link, if any
  8671. format the export format, a symbol like `html' or `latex' or `ascii'.
  8672. The function may use the FORMAT information to return different values
  8673. depending on the format. The return value will be put literally into
  8674. the exported file. If the return value is nil, this means Org should
  8675. do what it normally does with links which do not have EXPORT defined.
  8676. Org mode has a built-in default for exporting links. If you are happy with
  8677. this default, there is no need to define an export function for the link
  8678. type. For a simple example of an export function, see `org-bbdb.el'."
  8679. (add-to-list 'org-link-types type t)
  8680. (org-make-link-regexps)
  8681. (org-element-update-syntax)
  8682. (if (assoc type org-link-protocols)
  8683. (setcdr (assoc type org-link-protocols) (list follow export))
  8684. (push (list type follow export) org-link-protocols)))
  8685. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  8686. (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
  8687. ;;;###autoload
  8688. (defun org-store-link (arg)
  8689. "\\<org-mode-map>Store an org-link to the current location.
  8690. This link is added to `org-stored-links' and can later be inserted
  8691. into an Org buffer with \\[org-insert-link].
  8692. For some link types, a prefix ARG is interpreted.
  8693. For links to Usenet articles, ARG negates `org-gnus-prefer-web-links'.
  8694. For file links, ARG negates `org-context-in-file-links'.
  8695. A double prefix ARG force skipping storing functions that are not
  8696. part of Org's core.
  8697. A triple prefix ARG force storing a link for each line in the
  8698. active region."
  8699. (interactive "P")
  8700. (org-load-modules-maybe)
  8701. (if (and (equal arg '(64)) (org-region-active-p))
  8702. (save-excursion
  8703. (let ((end (region-end)))
  8704. (goto-char (region-beginning))
  8705. (set-mark (point))
  8706. (while (< (point-at-eol) end)
  8707. (move-end-of-line 1) (activate-mark)
  8708. (let (current-prefix-arg)
  8709. (call-interactively 'org-store-link))
  8710. (move-beginning-of-line 2)
  8711. (set-mark (point)))))
  8712. (org-with-limited-levels
  8713. (setq org-store-link-plist nil)
  8714. (let (link cpltxt desc description search
  8715. txt custom-id agenda-link sfuns sfunsn)
  8716. (cond
  8717. ;; Store a link using an external link type
  8718. ((and (not (equal arg '(16)))
  8719. (setq sfuns
  8720. (delq
  8721. nil (mapcar (lambda (f)
  8722. (let (fs) (if (funcall f) (push f fs))))
  8723. org-store-link-functions))
  8724. sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
  8725. (or (and (cdr sfuns)
  8726. (funcall (intern
  8727. (completing-read
  8728. "Which function for creating the link? "
  8729. sfunsn nil t (car sfunsn)))))
  8730. (funcall (caar sfuns)))
  8731. (setq link (plist-get org-store-link-plist :link)
  8732. desc (or (plist-get org-store-link-plist
  8733. :description)
  8734. link))))
  8735. ;; Store a link from a source code buffer.
  8736. ((org-src-edit-buffer-p)
  8737. (cond
  8738. ((save-excursion
  8739. (beginning-of-line)
  8740. (looking-at (concat (format org-coderef-label-format "\\(.*?\\)")
  8741. "[ \t]*$")))
  8742. (setq link (format "(%s)" (org-match-string-no-properties 1))))
  8743. ((org-called-interactively-p 'any)
  8744. (let (label)
  8745. (while (or (not label)
  8746. (org-with-wide-buffer
  8747. (goto-char (point-min))
  8748. (re-search-forward
  8749. (regexp-quote (format org-coderef-label-format label))
  8750. nil t)))
  8751. (when label (message "Label exists already") (sit-for 2))
  8752. (setq label (read-string "Code line label: " label)))
  8753. (end-of-line)
  8754. (setq link (format org-coderef-label-format label))
  8755. (let ((gc (- 79 (length link))))
  8756. (if (< (current-column) gc) (org-move-to-column gc t)
  8757. (insert " ")))
  8758. (insert link)
  8759. (setq link (concat "(" label ")") desc nil)))
  8760. (t (setq link nil))))
  8761. ;; We are in the agenda, link to referenced location
  8762. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8763. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8764. (get-text-property (point) 'org-marker))))
  8765. (when m
  8766. (org-with-point-at m
  8767. (setq agenda-link
  8768. (if (org-called-interactively-p 'any)
  8769. (call-interactively 'org-store-link)
  8770. (org-store-link nil)))))))
  8771. ((eq major-mode 'calendar-mode)
  8772. (let ((cd (calendar-cursor-to-date)))
  8773. (setq link
  8774. (format-time-string
  8775. (car org-time-stamp-formats)
  8776. (apply 'encode-time
  8777. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8778. nil nil nil))))
  8779. (org-store-link-props :type "calendar" :date cd)))
  8780. ((eq major-mode 'help-mode)
  8781. (setq link (concat "help:" (save-excursion
  8782. (goto-char (point-min))
  8783. (looking-at "^[^ ]+")
  8784. (match-string 0))))
  8785. (org-store-link-props :type "help"))
  8786. ((eq major-mode 'w3-mode)
  8787. (setq cpltxt (if (and (buffer-name)
  8788. (not (string-match "Untitled" (buffer-name))))
  8789. (buffer-name)
  8790. (url-view-url t))
  8791. link (url-view-url t))
  8792. (org-store-link-props :type "w3" :url (url-view-url t)))
  8793. ((eq major-mode 'image-mode)
  8794. (setq cpltxt (concat "file:"
  8795. (abbreviate-file-name buffer-file-name))
  8796. link cpltxt)
  8797. (org-store-link-props :type "image" :file buffer-file-name))
  8798. ;; In dired, store a link to the file of the current line
  8799. ((derived-mode-p 'dired-mode)
  8800. (let ((file (dired-get-filename nil t)))
  8801. (setq file (if file
  8802. (abbreviate-file-name
  8803. (expand-file-name (dired-get-filename nil t)))
  8804. ;; otherwise, no file so use current directory.
  8805. default-directory))
  8806. (setq cpltxt (concat "file:" file)
  8807. link cpltxt)))
  8808. ((setq search (run-hook-with-args-until-success
  8809. 'org-create-file-search-functions))
  8810. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8811. "::" search))
  8812. (setq cpltxt (or description link)))
  8813. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8814. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8815. (cond
  8816. ;; Store a link using the target at point
  8817. ((org-in-regexp "[^<]<<\\([^<>]+\\)>>[^>]" 1)
  8818. (setq cpltxt
  8819. (concat "file:"
  8820. (abbreviate-file-name
  8821. (buffer-file-name (buffer-base-buffer)))
  8822. "::" (match-string 1))
  8823. link cpltxt))
  8824. ((and (featurep 'org-id)
  8825. (or (eq org-id-link-to-org-use-id t)
  8826. (and (org-called-interactively-p 'any)
  8827. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  8828. (and (eq org-id-link-to-org-use-id
  8829. 'create-if-interactive-and-no-custom-id)
  8830. (not custom-id))))
  8831. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  8832. ;; Store a link using the ID at point
  8833. (setq link (condition-case nil
  8834. (prog1 (org-id-store-link)
  8835. (setq desc (or (plist-get org-store-link-plist
  8836. :description)
  8837. "")))
  8838. (error
  8839. ;; Probably before first headline, link only to file
  8840. (concat "file:"
  8841. (abbreviate-file-name
  8842. (buffer-file-name (buffer-base-buffer))))))))
  8843. (t
  8844. ;; Just link to current headline
  8845. (setq cpltxt (concat "file:"
  8846. (abbreviate-file-name
  8847. (buffer-file-name (buffer-base-buffer)))))
  8848. ;; Add a context search string
  8849. (when (org-xor org-context-in-file-links arg)
  8850. (let* ((ee (org-element-at-point))
  8851. (et (org-element-type ee))
  8852. (ev (plist-get (cadr ee) :value))
  8853. (ek (plist-get (cadr ee) :key))
  8854. (eok (and (stringp ek) (string-match "name" ek))))
  8855. (setq txt (cond
  8856. ((org-at-heading-p) nil)
  8857. ((and (eq et 'keyword) eok) ev)
  8858. ((org-region-active-p)
  8859. (buffer-substring (region-beginning) (region-end)))))
  8860. (when (or (null txt) (string-match "\\S-" txt))
  8861. (setq cpltxt
  8862. (concat cpltxt "::"
  8863. (condition-case nil
  8864. (org-make-org-heading-search-string txt)
  8865. (error "")))
  8866. desc (or (and (eq et 'keyword) eok ev)
  8867. (nth 4 (ignore-errors (org-heading-components)))
  8868. "NONE")))))
  8869. (if (string-match "::\\'" cpltxt)
  8870. (setq cpltxt (substring cpltxt 0 -2)))
  8871. (setq link cpltxt))))
  8872. ((buffer-file-name (buffer-base-buffer))
  8873. ;; Just link to this file here.
  8874. (setq cpltxt (concat "file:"
  8875. (abbreviate-file-name
  8876. (buffer-file-name (buffer-base-buffer)))))
  8877. ;; Add a context string.
  8878. (when (org-xor org-context-in-file-links arg)
  8879. (setq txt (if (org-region-active-p)
  8880. (buffer-substring (region-beginning) (region-end))
  8881. (buffer-substring (point-at-bol) (point-at-eol))))
  8882. ;; Only use search option if there is some text.
  8883. (when (string-match "\\S-" txt)
  8884. (setq cpltxt
  8885. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8886. desc "NONE")))
  8887. (setq link cpltxt))
  8888. ((org-called-interactively-p 'interactive)
  8889. (user-error "No method for storing a link from this buffer"))
  8890. (t (setq link nil)))
  8891. ;; We're done setting link and desc, clean up
  8892. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8893. (setq link (or link cpltxt)
  8894. desc (or desc cpltxt))
  8895. (cond ((equal desc "NONE") (setq desc nil))
  8896. ((and desc (string-match org-bracket-link-analytic-regexp desc))
  8897. (let ((d0 (match-string 3 desc))
  8898. (p0 (match-string 5 desc)))
  8899. (setq desc
  8900. (replace-regexp-in-string
  8901. org-bracket-link-regexp
  8902. (concat (or p0 d0)
  8903. (if (equal (length (match-string 0 desc))
  8904. (length desc)) "*" "")) desc)))))
  8905. ;; Return the link
  8906. (if (not (and (or (org-called-interactively-p 'any)
  8907. executing-kbd-macro)
  8908. link))
  8909. (or agenda-link (and link (org-make-link-string link desc)))
  8910. (push (list link desc) org-stored-links)
  8911. (message "Stored: %s" (or desc link))
  8912. (when custom-id
  8913. (setq link (concat "file:" (abbreviate-file-name
  8914. (buffer-file-name)) "::#" custom-id))
  8915. (push (list link desc) org-stored-links))
  8916. (car org-stored-links))))))
  8917. (defun org-store-link-props (&rest plist)
  8918. "Store link properties, extract names and addresses."
  8919. (let (x adr)
  8920. (when (setq x (plist-get plist :from))
  8921. (setq adr (mail-extract-address-components x))
  8922. (setq plist (plist-put plist :fromname (car adr)))
  8923. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8924. (when (setq x (plist-get plist :to))
  8925. (setq adr (mail-extract-address-components x))
  8926. (setq plist (plist-put plist :toname (car adr)))
  8927. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8928. (let ((from (plist-get plist :from))
  8929. (to (plist-get plist :to)))
  8930. (when (and from to org-from-is-user-regexp)
  8931. (setq plist
  8932. (plist-put plist :fromto
  8933. (if (string-match org-from-is-user-regexp from)
  8934. (concat "to %t")
  8935. (concat "from %f"))))))
  8936. (setq org-store-link-plist plist))
  8937. (defun org-add-link-props (&rest plist)
  8938. "Add these properties to the link property list."
  8939. (let (key value)
  8940. (while plist
  8941. (setq key (pop plist) value (pop plist))
  8942. (setq org-store-link-plist
  8943. (plist-put org-store-link-plist key value)))))
  8944. (defun org-email-link-description (&optional fmt)
  8945. "Return the description part of an email link.
  8946. This takes information from `org-store-link-plist' and formats it
  8947. according to FMT (default from `org-email-link-description-format')."
  8948. (setq fmt (or fmt org-email-link-description-format))
  8949. (let* ((p org-store-link-plist)
  8950. (to (plist-get p :toaddress))
  8951. (from (plist-get p :fromaddress))
  8952. (table
  8953. (list
  8954. (cons "%c" (plist-get p :fromto))
  8955. (cons "%F" (plist-get p :from))
  8956. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8957. (cons "%T" (plist-get p :to))
  8958. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8959. (cons "%s" (plist-get p :subject))
  8960. (cons "%d" (plist-get p :date))
  8961. (cons "%m" (plist-get p :message-id)))))
  8962. (when (string-match "%c" fmt)
  8963. ;; Check if the user wrote this message
  8964. (if (and org-from-is-user-regexp from to
  8965. (save-match-data (string-match org-from-is-user-regexp from)))
  8966. (setq fmt (replace-match "to %t" t t fmt))
  8967. (setq fmt (replace-match "from %f" t t fmt))))
  8968. (org-replace-escapes fmt table)))
  8969. (defun org-make-org-heading-search-string (&optional string)
  8970. "Make search string for the current headline or STRING."
  8971. (let ((s (or string
  8972. (and (derived-mode-p 'org-mode)
  8973. (save-excursion
  8974. (org-back-to-heading t)
  8975. (org-element-property :raw-value (org-element-at-point))))))
  8976. (lines org-context-in-file-links))
  8977. (or string (setq s (concat "*" s))) ; Add * for headlines
  8978. (setq s (replace-regexp-in-string "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" "" s))
  8979. (when (and string (integerp lines) (> lines 0))
  8980. (let ((slines (org-split-string s "\n")))
  8981. (when (< lines (length slines))
  8982. (setq s (mapconcat
  8983. 'identity
  8984. (reverse (nthcdr (- (length slines) lines)
  8985. (reverse slines))) "\n")))))
  8986. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8987. (defun org-make-link-string (link &optional description)
  8988. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8989. (unless (org-string-nw-p link) (error "Empty link"))
  8990. (let ((uri (cond ((string-match org-link-types-re link)
  8991. (concat (match-string 1 link)
  8992. (org-link-escape (substring link (match-end 1)))))
  8993. ;; For readability, url-encode internal links only
  8994. ;; when absolutely needed (i.e, when they contain
  8995. ;; square brackets). File links however, are
  8996. ;; encoded since, e.g., spaces are significant.
  8997. ((or (file-name-absolute-p link)
  8998. (org-string-match-p "\\`\\.\\.?/\\|[][]" link))
  8999. (org-link-escape link))
  9000. (t link)))
  9001. (description
  9002. (and (org-string-nw-p description)
  9003. ;; Remove brackets from description, as they are fatal.
  9004. (replace-regexp-in-string
  9005. "[][]" (lambda (m) (if (equal "[" m) "{" "}"))
  9006. (org-trim description)))))
  9007. (format "[[%s]%s]"
  9008. uri
  9009. (if description (format "[%s]" description) ""))))
  9010. (defconst org-link-escape-chars
  9011. ;;%20 %5B %5D %25
  9012. '(?\s ?\[ ?\] ?%)
  9013. "List of characters that should be escaped in a link when stored to Org.
  9014. This is the list that is used for internal purposes.")
  9015. (defconst org-link-escape-chars-browser
  9016. ;;%20 %22
  9017. '(?\s ?\")
  9018. "List of characters to be escaped before handing over to the browser.
  9019. If you consider using this constant then you probably want to use
  9020. the function `org-link-escape-browser' instead. See there why
  9021. this constant is a candidate to be removed once Org drops support
  9022. for Emacs 24.1 and 24.2.")
  9023. (defun org-link-escape (text &optional table merge)
  9024. "Return percent escaped representation of TEXT.
  9025. TEXT is a string with the text to escape.
  9026. Optional argument TABLE is a list with characters that should be
  9027. escaped. When nil, `org-link-escape-chars' is used.
  9028. If optional argument MERGE is set, merge TABLE into
  9029. `org-link-escape-chars'."
  9030. (let ((characters-to-encode
  9031. (cond ((null table) org-link-escape-chars)
  9032. (merge (append org-link-escape-chars table))
  9033. (t table))))
  9034. (mapconcat
  9035. (lambda (c)
  9036. (if (or (memq c characters-to-encode)
  9037. (and org-url-hexify-p (or (< c 32) (> c 126))))
  9038. (mapconcat (lambda (e) (format "%%%.2X" e))
  9039. (or (encode-coding-char c 'utf-8)
  9040. (error "Unable to percent escape character: %c" c))
  9041. "")
  9042. (char-to-string c)))
  9043. text "")))
  9044. (defun org-link-escape-browser (text)
  9045. "Escape some characters before handing over to the browser.
  9046. This function is a candidate to be removed together with the
  9047. constant `org-link-escape-chars-browser' once Org drops support
  9048. for Emacs 24.1 and 24.2. All calls to this function will have to
  9049. be replaced with `url-encode-url' which is available since Emacs
  9050. 24.3.1."
  9051. ;; Example with the Org link
  9052. ;; [[http://lists.gnu.org/archive/cgi-bin/namazu.cgi?idxname=emacs-orgmode&query=%252Bsubject:"Release+8.2"]]
  9053. ;; to open the browser with +subject:"Release 8.2" filled into the
  9054. ;; query field: In this case the variable TEXT contains the
  9055. ;; unescaped [...]=%2Bsubject:"Release+8.2". Then `url-encode-url'
  9056. ;; converts correctly to [...]=%2Bsubject:%22Release+8.2%22 or
  9057. ;; `org-link-escape' with `org-link-escape-chars-browser' converts
  9058. ;; wrongly to [...]=%252Bsubject:%22Release+8.2%22.
  9059. (if (fboundp 'url-encode-url)
  9060. (url-encode-url text)
  9061. (if (org-string-match-p
  9062. (concat "[[:nonascii:]" org-link-escape-chars-browser "]")
  9063. text)
  9064. (org-link-escape text org-link-escape-chars-browser)
  9065. text)))
  9066. (defun org-link-unescape (str)
  9067. "Unhex hexified Unicode parts in string STR.
  9068. E.g. `%C3%B6' becomes the german o-Umlaut. This is the
  9069. reciprocal of `org-link-escape', which see."
  9070. (if (org-string-nw-p str)
  9071. (replace-regexp-in-string
  9072. "\\(%[0-9A-Za-z]\\{2\\}\\)+" #'org-link-unescape-compound str t t)
  9073. str))
  9074. (defun org-link-unescape-compound (hex)
  9075. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  9076. Note: this function also decodes single byte encodings like
  9077. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  9078. (save-match-data
  9079. (let* ((bytes (cdr (split-string hex "%")))
  9080. (ret "")
  9081. (eat 0)
  9082. (sum 0))
  9083. (while bytes
  9084. (let* ((val (string-to-number (pop bytes) 16))
  9085. (shift-xor
  9086. (if (= 0 eat)
  9087. (cond
  9088. ((>= val 252) (cons 6 252))
  9089. ((>= val 248) (cons 5 248))
  9090. ((>= val 240) (cons 4 240))
  9091. ((>= val 224) (cons 3 224))
  9092. ((>= val 192) (cons 2 192))
  9093. (t (cons 0 0)))
  9094. (cons 6 128))))
  9095. (if (>= val 192) (setq eat (car shift-xor)))
  9096. (setq val (logxor val (cdr shift-xor)))
  9097. (setq sum (+ (lsh sum (car shift-xor)) val))
  9098. (if (> eat 0) (setq eat (- eat 1)))
  9099. (cond
  9100. ((= 0 eat) ;multi byte
  9101. (setq ret (concat ret (org-char-to-string sum)))
  9102. (setq sum 0))
  9103. ((not bytes) ; single byte(s)
  9104. (setq ret (org-link-unescape-single-byte-sequence hex))))))
  9105. ret)))
  9106. (defun org-link-unescape-single-byte-sequence (hex)
  9107. "Unhexify hex-encoded single byte character sequences."
  9108. (mapconcat (lambda (byte)
  9109. (char-to-string (string-to-number byte 16)))
  9110. (cdr (split-string hex "%")) ""))
  9111. (defun org-xor (a b)
  9112. "Exclusive or."
  9113. (if a (not b) b))
  9114. (defun org-fixup-message-id-for-http (s)
  9115. "Replace special characters in a message id, so it can be used in an http query."
  9116. (when (string-match "%" s)
  9117. (setq s (mapconcat (lambda (c)
  9118. (if (eq c ?%)
  9119. "%25"
  9120. (char-to-string c)))
  9121. s "")))
  9122. (while (string-match "<" s)
  9123. (setq s (replace-match "%3C" t t s)))
  9124. (while (string-match ">" s)
  9125. (setq s (replace-match "%3E" t t s)))
  9126. (while (string-match "@" s)
  9127. (setq s (replace-match "%40" t t s)))
  9128. s)
  9129. (defun org-link-prettify (link)
  9130. "Return a human-readable representation of LINK.
  9131. The car of LINK must be a raw link.
  9132. The cdr of LINK must be either a link description or nil."
  9133. (let ((desc (or (cadr link) "<no description>")))
  9134. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  9135. "<" (car link) ">")))
  9136. ;;;###autoload
  9137. (defun org-insert-link-global ()
  9138. "Insert a link like Org-mode does.
  9139. This command can be called in any mode to insert a link in Org-mode syntax."
  9140. (interactive)
  9141. (org-load-modules-maybe)
  9142. (org-run-like-in-org-mode 'org-insert-link))
  9143. (defun org-insert-all-links (arg &optional pre post)
  9144. "Insert all links in `org-stored-links'.
  9145. When a universal prefix, do not delete the links from `org-stored-links'.
  9146. When `ARG' is a number, insert the last N link(s).
  9147. `PRE' and `POST' are optional arguments to define a string to
  9148. prepend or to append."
  9149. (interactive "P")
  9150. (let ((org-keep-stored-link-after-insertion (equal arg '(4)))
  9151. (links (copy-seq org-stored-links))
  9152. (pr (or pre "- "))
  9153. (po (or post "\n"))
  9154. (cnt 1) l)
  9155. (if (null org-stored-links)
  9156. (message "No link to insert")
  9157. (while (and (or (listp arg) (>= arg cnt))
  9158. (setq l (if (listp arg)
  9159. (pop links)
  9160. (pop org-stored-links))))
  9161. (setq cnt (1+ cnt))
  9162. (insert pr)
  9163. (org-insert-link nil (car l) (or (cadr l) "<no description>"))
  9164. (insert po)))))
  9165. (defun org-insert-last-stored-link (arg)
  9166. "Insert the last link stored in `org-stored-links'."
  9167. (interactive "p")
  9168. (org-insert-all-links arg "" "\n"))
  9169. (defun org-link-fontify-links-to-this-file ()
  9170. "Fontify links to the current file in `org-stored-links'."
  9171. (let ((f (buffer-file-name)) a b)
  9172. (setq a (mapcar (lambda(l)
  9173. (let ((ll (car l)))
  9174. (when (and (string-match "^file:\\(.+\\)::" ll)
  9175. (equal f (expand-file-name (match-string 1 ll))))
  9176. ll)))
  9177. org-stored-links))
  9178. (when (featurep 'org-id)
  9179. (setq b (mapcar (lambda(l)
  9180. (let ((ll (car l)))
  9181. (when (and (string-match "^id:\\(.+\\)$" ll)
  9182. (equal f (expand-file-name
  9183. (or (org-id-find-id-file
  9184. (match-string 1 ll)) ""))))
  9185. ll)))
  9186. org-stored-links)))
  9187. (mapcar (lambda(l)
  9188. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  9189. (delq nil (append a b)))))
  9190. (defvar org-link-links-in-this-file nil)
  9191. (defun org-insert-link (&optional complete-file link-location default-description)
  9192. "Insert a link. At the prompt, enter the link.
  9193. Completion can be used to insert any of the link protocol prefixes like
  9194. http or ftp in use.
  9195. The history can be used to select a link previously stored with
  9196. `org-store-link'. When the empty string is entered (i.e. if you just
  9197. press RET at the prompt), the link defaults to the most recently
  9198. stored link. As SPC triggers completion in the minibuffer, you need to
  9199. use M-SPC or C-q SPC to force the insertion of a space character.
  9200. You will also be prompted for a description, and if one is given, it will
  9201. be displayed in the buffer instead of the link.
  9202. If there is already a link at point, this command will allow you to edit link
  9203. and description parts.
  9204. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  9205. be selected using completion. The path to the file will be relative to the
  9206. current directory if the file is in the current directory or a subdirectory.
  9207. Otherwise, the link will be the absolute path as completed in the minibuffer
  9208. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  9209. option `org-link-file-path-type'.
  9210. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  9211. the current directory or below.
  9212. With three \\[universal-argument] prefixes, negate the meaning of
  9213. `org-keep-stored-link-after-insertion'.
  9214. If `org-make-link-description-function' is non-nil, this function will be
  9215. called with the link target, and the result will be the default
  9216. link description.
  9217. If the LINK-LOCATION parameter is non-nil, this value will be
  9218. used as the link location instead of reading one interactively.
  9219. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  9220. be used as the default description."
  9221. (interactive "P")
  9222. (let* ((wcf (current-window-configuration))
  9223. (origbuf (current-buffer))
  9224. (region (if (org-region-active-p)
  9225. (buffer-substring (region-beginning) (region-end))))
  9226. (remove (and region (list (region-beginning) (region-end))))
  9227. (desc region)
  9228. tmphist ; byte-compile incorrectly complains about this
  9229. (link link-location)
  9230. (abbrevs org-link-abbrev-alist-local)
  9231. entry file all-prefixes auto-desc)
  9232. (cond
  9233. (link-location) ; specified by arg, just use it.
  9234. ((org-in-regexp org-bracket-link-regexp 1)
  9235. ;; We do have a link at point, and we are going to edit it.
  9236. (setq remove (list (match-beginning 0) (match-end 0)))
  9237. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  9238. (setq link (read-string "Link: "
  9239. (org-link-unescape
  9240. (org-match-string-no-properties 1)))))
  9241. ((or (org-in-regexp org-angle-link-re)
  9242. (org-in-regexp org-plain-link-re))
  9243. ;; Convert to bracket link
  9244. (setq remove (list (match-beginning 0) (match-end 0))
  9245. link (read-string "Link: "
  9246. (org-remove-angle-brackets (match-string 0)))))
  9247. ((member complete-file '((4) (16)))
  9248. ;; Completing read for file names.
  9249. (setq link (org-file-complete-link complete-file)))
  9250. (t
  9251. ;; Read link, with completion for stored links.
  9252. (org-link-fontify-links-to-this-file)
  9253. (org-switch-to-buffer-other-window "*Org Links*")
  9254. (with-current-buffer "*Org Links*"
  9255. (erase-buffer)
  9256. (insert "Insert a link.
  9257. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  9258. (when org-stored-links
  9259. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  9260. (insert (mapconcat 'org-link-prettify
  9261. (reverse org-stored-links) "\n")))
  9262. (goto-char (point-min)))
  9263. (let ((cw (selected-window)))
  9264. (select-window (get-buffer-window "*Org Links*" 'visible))
  9265. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  9266. (unless (pos-visible-in-window-p (point-max))
  9267. (org-fit-window-to-buffer))
  9268. (and (window-live-p cw) (select-window cw)))
  9269. ;; Fake a link history, containing the stored links.
  9270. (setq tmphist (append (mapcar 'car org-stored-links)
  9271. org-insert-link-history))
  9272. (setq all-prefixes (append (mapcar 'car abbrevs)
  9273. (mapcar 'car org-link-abbrev-alist)
  9274. org-link-types))
  9275. (unwind-protect
  9276. (progn
  9277. (setq link
  9278. (org-completing-read
  9279. "Link: "
  9280. (append
  9281. (mapcar (lambda (x) (concat x ":"))
  9282. all-prefixes)
  9283. (mapcar 'car org-stored-links))
  9284. nil nil nil
  9285. 'tmphist
  9286. (caar org-stored-links)))
  9287. (if (not (string-match "\\S-" link))
  9288. (user-error "No link selected"))
  9289. (mapc (lambda(l)
  9290. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  9291. org-stored-links)
  9292. (if (or (member link all-prefixes)
  9293. (and (equal ":" (substring link -1))
  9294. (member (substring link 0 -1) all-prefixes)
  9295. (setq link (substring link 0 -1))))
  9296. (setq link (with-current-buffer origbuf
  9297. (org-link-try-special-completion link)))))
  9298. (set-window-configuration wcf)
  9299. (kill-buffer "*Org Links*"))
  9300. (setq entry (assoc link org-stored-links))
  9301. (or entry (push link org-insert-link-history))
  9302. (setq desc (or desc (nth 1 entry)))))
  9303. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  9304. (not org-keep-stored-link-after-insertion))
  9305. (setq org-stored-links (delq (assoc link org-stored-links)
  9306. org-stored-links)))
  9307. (if (and (string-match org-plain-link-re link)
  9308. (not (string-match org-ts-regexp link)))
  9309. ;; URL-like link, normalize the use of angular brackets.
  9310. (setq link (org-remove-angle-brackets link)))
  9311. ;; Check if we are linking to the current file with a search
  9312. ;; option If yes, simplify the link by using only the search
  9313. ;; option.
  9314. (when (and buffer-file-name
  9315. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  9316. (let* ((path (match-string 1 link))
  9317. (case-fold-search nil)
  9318. (search (match-string 2 link)))
  9319. (save-match-data
  9320. (if (equal (file-truename buffer-file-name) (file-truename path))
  9321. ;; We are linking to this same file, with a search option
  9322. (setq link search)))))
  9323. ;; Check if we can/should use a relative path. If yes, simplify the link
  9324. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  9325. (let* ((type (match-string 1 link))
  9326. (path (match-string 2 link))
  9327. (origpath path)
  9328. (case-fold-search nil))
  9329. (cond
  9330. ((or (eq org-link-file-path-type 'absolute)
  9331. (equal complete-file '(16)))
  9332. (setq path (abbreviate-file-name (expand-file-name path))))
  9333. ((eq org-link-file-path-type 'noabbrev)
  9334. (setq path (expand-file-name path)))
  9335. ((eq org-link-file-path-type 'relative)
  9336. (setq path (file-relative-name path)))
  9337. (t
  9338. (save-match-data
  9339. (if (string-match (concat "^" (regexp-quote
  9340. (expand-file-name
  9341. (file-name-as-directory
  9342. default-directory))))
  9343. (expand-file-name path))
  9344. ;; We are linking a file with relative path name.
  9345. (setq path (substring (expand-file-name path)
  9346. (match-end 0)))
  9347. (setq path (abbreviate-file-name (expand-file-name path)))))))
  9348. (setq link (concat type path))
  9349. (if (equal desc origpath)
  9350. (setq desc path))))
  9351. (if org-make-link-description-function
  9352. (setq desc
  9353. (or (condition-case nil
  9354. (funcall org-make-link-description-function link desc)
  9355. (error (progn (message "Can't get link description from `%s'"
  9356. (symbol-name org-make-link-description-function))
  9357. (sit-for 2) nil)))
  9358. (read-string "Description: " default-description)))
  9359. (if default-description (setq desc default-description)
  9360. (setq desc (or (and auto-desc desc)
  9361. (read-string "Description: " desc)))))
  9362. (unless (string-match "\\S-" desc) (setq desc nil))
  9363. (if remove (apply 'delete-region remove))
  9364. (insert (org-make-link-string link desc))))
  9365. (defun org-link-try-special-completion (type)
  9366. "If there is completion support for link type TYPE, offer it."
  9367. (let ((fun (intern (concat "org-" type "-complete-link"))))
  9368. (if (functionp fun)
  9369. (funcall fun)
  9370. (read-string "Link (no completion support): " (concat type ":")))))
  9371. (defun org-file-complete-link (&optional arg)
  9372. "Create a file link using completion."
  9373. (let ((file (org-iread-file-name "File: "))
  9374. (pwd (file-name-as-directory (expand-file-name ".")))
  9375. (pwd1 (file-name-as-directory (abbreviate-file-name
  9376. (expand-file-name ".")))))
  9377. (cond ((equal arg '(16))
  9378. (concat "file:"
  9379. (abbreviate-file-name (expand-file-name file))))
  9380. ((string-match
  9381. (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  9382. (concat "file:" (match-string 1 file)))
  9383. ((string-match
  9384. (concat "^" (regexp-quote pwd) "\\(.+\\)")
  9385. (expand-file-name file))
  9386. (concat "file:"
  9387. (match-string 1 (expand-file-name file))))
  9388. (t (concat "file:" file)))))
  9389. (defun org-iread-file-name (&rest args)
  9390. "Read-file-name using `ido-mode' speedup if available.
  9391. ARGS are arguments that may be passed to `ido-read-file-name' or `read-file-name'.
  9392. See `read-file-name' for a description of parameters."
  9393. (org-without-partial-completion
  9394. (if (and org-completion-use-ido
  9395. (fboundp 'ido-read-file-name)
  9396. (org-bound-and-true-p ido-mode)
  9397. (listp (nth 1 args)))
  9398. (let ((ido-enter-matching-directory nil))
  9399. (apply #'ido-read-file-name args))
  9400. (apply #'read-file-name args))))
  9401. (defun org-completing-read (&rest args)
  9402. "Completing-read with SPACE being a normal character."
  9403. (let ((enable-recursive-minibuffers t)
  9404. (minibuffer-local-completion-map
  9405. (copy-keymap minibuffer-local-completion-map)))
  9406. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  9407. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  9408. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  9409. (apply 'org-icompleting-read args)))
  9410. (defun org-completing-read-no-i (&rest args)
  9411. (let (org-completion-use-ido org-completion-use-iswitchb)
  9412. (apply 'org-completing-read args)))
  9413. (defun org-iswitchb-completing-read (prompt choices &rest args)
  9414. "Use iswitch as a completing-read replacement to choose from choices.
  9415. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  9416. from."
  9417. (let* ((iswitchb-use-virtual-buffers nil)
  9418. (iswitchb-make-buflist-hook
  9419. (lambda ()
  9420. (setq iswitchb-temp-buflist choices))))
  9421. (iswitchb-read-buffer prompt)))
  9422. (defun org-icompleting-read (&rest args)
  9423. "Completing-read using `ido-mode' or `iswitchb' speedups if available.
  9424. Should be called like `completing-read'."
  9425. (org-without-partial-completion
  9426. (if (not (listp (nth 1 args)))
  9427. ;; Ido only supports lists as the COLLECTION argument. Use
  9428. ;; default completion function when second argument is not
  9429. ;; a list.
  9430. (apply #'completing-read args)
  9431. (let ((ido-enter-matching-directory nil))
  9432. (apply (cond ((and org-completion-use-ido
  9433. (fboundp 'ido-completing-read)
  9434. (org-bound-and-true-p ido-mode))
  9435. #'ido-completing-read)
  9436. ((and org-completion-use-iswitchb
  9437. (org-bound-and-true-p iswitchb-mode))
  9438. #'org-iswitchb-completing-read)
  9439. (t #'completing-read))
  9440. args)))))
  9441. ;;; Opening/following a link
  9442. (defvar org-link-search-failed nil)
  9443. (defvar org-open-link-functions nil
  9444. "Hook for functions finding a plain text link.
  9445. These functions must take a single argument, the link content.
  9446. They will be called for links that look like [[link text][description]]
  9447. when LINK TEXT does not have a protocol like \"http:\" and does not look
  9448. like a filename (e.g. \"./blue.png\").
  9449. These functions will be called *before* Org attempts to resolve the
  9450. link by doing text searches in the current buffer - so if you want a
  9451. link \"[[target]]\" to still find \"<<target>>\", your function should
  9452. handle this as a special case.
  9453. When the function does handle the link, it must return a non-nil value.
  9454. If it decides that it is not responsible for this link, it must return
  9455. nil to indicate that that Org-mode can continue with other options
  9456. like exact and fuzzy text search.")
  9457. (defun org-next-link (&optional search-backward)
  9458. "Move forward to the next link.
  9459. If the link is in hidden text, expose it."
  9460. (interactive "P")
  9461. (when (and org-link-search-failed (eq this-command last-command))
  9462. (goto-char (point-min))
  9463. (message "Link search wrapped back to beginning of buffer"))
  9464. (setq org-link-search-failed nil)
  9465. (let* ((pos (point))
  9466. (ct (org-context))
  9467. (a (assoc :link ct))
  9468. (srch-fun (if search-backward 're-search-backward 're-search-forward)))
  9469. (cond (a (goto-char (nth (if search-backward 1 2) a)))
  9470. ((looking-at org-any-link-re)
  9471. ;; Don't stay stuck at link without an org-link face
  9472. (forward-char (if search-backward -1 1))))
  9473. (if (funcall srch-fun org-any-link-re nil t)
  9474. (progn
  9475. (goto-char (match-beginning 0))
  9476. (if (outline-invisible-p) (org-show-context)))
  9477. (goto-char pos)
  9478. (setq org-link-search-failed t)
  9479. (message "No further link found"))))
  9480. (defun org-previous-link ()
  9481. "Move backward to the previous link.
  9482. If the link is in hidden text, expose it."
  9483. (interactive)
  9484. (funcall 'org-next-link t))
  9485. (defun org-translate-link (s)
  9486. "Translate a link string if a translation function has been defined."
  9487. (with-temp-buffer
  9488. (insert (org-trim s))
  9489. (org-trim (org-element-interpret-data (org-element-context)))))
  9490. (defun org-translate-link-from-planner (type path)
  9491. "Translate a link from Emacs Planner syntax so that Org can follow it.
  9492. This is still an experimental function, your mileage may vary."
  9493. (cond
  9494. ((member type '("http" "https" "news" "ftp"))
  9495. ;; standard Internet links are the same.
  9496. nil)
  9497. ((and (equal type "irc") (string-match "^//" path))
  9498. ;; Planner has two / at the beginning of an irc link, we have 1.
  9499. ;; We should have zero, actually....
  9500. (setq path (substring path 1)))
  9501. ((and (equal type "lisp") (string-match "^/" path))
  9502. ;; Planner has a slash, we do not.
  9503. (setq type "elisp" path (substring path 1)))
  9504. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  9505. ;; A typical message link. Planner has the id after the final slash,
  9506. ;; we separate it with a hash mark
  9507. (setq path (concat (match-string 1 path) "#"
  9508. (org-remove-angle-brackets (match-string 2 path))))))
  9509. (cons type path))
  9510. (defun org-find-file-at-mouse (ev)
  9511. "Open file link or URL at mouse."
  9512. (interactive "e")
  9513. (mouse-set-point ev)
  9514. (org-open-at-point 'in-emacs))
  9515. (defun org-open-at-mouse (ev)
  9516. "Open file link or URL at mouse.
  9517. See the docstring of `org-open-file' for details."
  9518. (interactive "e")
  9519. (mouse-set-point ev)
  9520. (if (eq major-mode 'org-agenda-mode)
  9521. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  9522. (org-open-at-point))
  9523. (defvar org-window-config-before-follow-link nil
  9524. "The window configuration before following a link.
  9525. This is saved in case the need arises to restore it.")
  9526. ;;;###autoload
  9527. (defun org-open-at-point-global ()
  9528. "Follow a link like Org-mode does.
  9529. This command can be called in any mode to follow a link that has
  9530. Org-mode syntax."
  9531. (interactive)
  9532. (org-run-like-in-org-mode 'org-open-at-point))
  9533. ;;;###autoload
  9534. (defun org-open-link-from-string (s &optional arg reference-buffer)
  9535. "Open a link in the string S, as if it was in Org-mode."
  9536. (interactive "sLink: \nP")
  9537. (let ((reference-buffer (or reference-buffer (current-buffer))))
  9538. (with-temp-buffer
  9539. (let ((org-inhibit-startup (not reference-buffer)))
  9540. (org-mode)
  9541. (insert s)
  9542. (goto-char (point-min))
  9543. (when reference-buffer
  9544. (setq org-link-abbrev-alist-local
  9545. (with-current-buffer reference-buffer
  9546. org-link-abbrev-alist-local)))
  9547. (org-open-at-point arg reference-buffer)))))
  9548. (defvar org-open-at-point-functions nil
  9549. "Hook that is run when following a link at point.
  9550. Functions in this hook must return t if they identify and follow
  9551. a link at point. If they don't find anything interesting at point,
  9552. they must return nil.")
  9553. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9554. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  9555. (defun org-open-at-point (&optional arg reference-buffer)
  9556. "Open link, timestamp, footnote or tags at point.
  9557. When point is on a link, follow it. Normally, files will be
  9558. opened by an appropriate application. If the optional prefix
  9559. argument ARG is non-nil, Emacs will visit the file. With
  9560. a double prefix argument, try to open outside of Emacs, in the
  9561. application the system uses for this file type.
  9562. When point is on a timestamp, open the agenda at the day
  9563. specified.
  9564. When point is a footnote definition, move to the first reference
  9565. found. If it is on a reference, move to the associated
  9566. definition.
  9567. When point is on a headline, display a list of every link in the
  9568. entry, so it is possible to pick one, or all, of them. If point
  9569. is on a tag, call `org-tags-view' instead.
  9570. When optional argument REFERENCE-BUFFER is non-nil, it should
  9571. specify a buffer from where the link search should happen. This
  9572. is used internally by `org-open-link-from-string'.
  9573. On top of syntactically correct links, this function will open
  9574. the link at point in comments or comment blocks and the first
  9575. link in a property drawer line."
  9576. (interactive "P")
  9577. ;; On a code block, open block's results.
  9578. (unless (call-interactively 'org-babel-open-src-block-result)
  9579. (org-load-modules-maybe)
  9580. (setq org-window-config-before-follow-link (current-window-configuration))
  9581. (org-remove-occur-highlights nil nil t)
  9582. (unless (run-hook-with-args-until-success 'org-open-at-point-functions)
  9583. (let* ((context
  9584. ;; Only consider supported types, even if they are not
  9585. ;; the closest one.
  9586. (org-element-lineage
  9587. (org-element-context)
  9588. '(clock comment comment-block footnote-definition
  9589. footnote-reference headline inlinetask keyword link
  9590. node-property timestamp)
  9591. t))
  9592. (type (org-element-type context))
  9593. (value (org-element-property :value context)))
  9594. (cond
  9595. ((not context) (user-error "No link found"))
  9596. ;; Exception: open timestamps and links in properties
  9597. ;; drawers, keywords and comments.
  9598. ((memq type '(comment comment-block keyword node-property))
  9599. (cond ((org-in-regexp org-any-link-re)
  9600. (org-open-link-from-string (match-string-no-properties 0)))
  9601. ((or (org-at-timestamp-p t) (org-at-date-range-p t))
  9602. (org-follow-timestamp-link))
  9603. (t (user-error "No link found"))))
  9604. ;; On a headline or an inlinetask, but not on a timestamp,
  9605. ;; a link, a footnote reference or on tags.
  9606. ((and (memq type '(headline inlinetask))
  9607. ;; Not on tags.
  9608. (progn (save-excursion (beginning-of-line)
  9609. (looking-at org-complex-heading-regexp))
  9610. (or (not (match-beginning 5))
  9611. (< (point) (match-beginning 5)))))
  9612. (let* ((data (org-offer-links-in-entry (current-buffer) (point) arg))
  9613. (links (car data))
  9614. (links-end (cdr data)))
  9615. (if links
  9616. (dolist (link (if (stringp links) (list links) links))
  9617. (search-forward link nil links-end)
  9618. (goto-char (match-beginning 0))
  9619. (org-open-at-point))
  9620. (require 'org-attach)
  9621. (org-attach-reveal 'if-exists))))
  9622. ;; On a clock line, make sure point is on the timestamp
  9623. ;; before opening it.
  9624. ((and (eq type 'clock)
  9625. value
  9626. (>= (point) (org-element-property :begin value))
  9627. (<= (point) (org-element-property :end value)))
  9628. (org-follow-timestamp-link))
  9629. ;; Do nothing on white spaces after an object, unless point
  9630. ;; is right after it.
  9631. ((> (point)
  9632. (save-excursion
  9633. (goto-char (org-element-property :end context))
  9634. (skip-chars-backward " \t")
  9635. (point)))
  9636. (user-error "No link found"))
  9637. ((eq type 'timestamp) (org-follow-timestamp-link))
  9638. ;; On tags within a headline or an inlinetask.
  9639. ((and (memq type '(headline inlinetask))
  9640. (progn (save-excursion (beginning-of-line)
  9641. (looking-at org-complex-heading-regexp))
  9642. (and (match-beginning 5)
  9643. (>= (point) (match-beginning 5)))))
  9644. (org-tags-view arg (substring (match-string 5) 0 -1)))
  9645. ((eq type 'link)
  9646. ;; When link is located within the description of another
  9647. ;; link (e.g., an inline image), always open the parent
  9648. ;; link.
  9649. (let*((link (let ((up (org-element-property :parent context)))
  9650. (if (eq (org-element-type up) 'link) up context)))
  9651. (type (org-element-property :type link))
  9652. (path (org-link-unescape (org-element-property :path link))))
  9653. ;; Switch back to REFERENCE-BUFFER needed when called in
  9654. ;; a temporary buffer through `org-open-link-from-string'.
  9655. (with-current-buffer (or reference-buffer (current-buffer))
  9656. (cond
  9657. ((equal type "file")
  9658. (if (string-match "[*?{]" (file-name-nondirectory path))
  9659. (dired path)
  9660. ;; Look into `org-link-protocols' in order to find
  9661. ;; a DEDICATED-FUNCTION to open file. The function
  9662. ;; will be applied on raw link instead of parsed
  9663. ;; link due to the limitation in `org-add-link-type'
  9664. ;; ("open" function called with a single argument).
  9665. ;; If no such function is found, fallback to
  9666. ;; `org-open-file'.
  9667. ;;
  9668. ;; Note : "file+emacs" and "file+sys" types are
  9669. ;; hard-coded in order to escape the previous
  9670. ;; limitation.
  9671. (let* ((option (org-element-property :search-option link))
  9672. (app (org-element-property :application link))
  9673. (dedicated-function
  9674. (nth 1 (assoc app org-link-protocols))))
  9675. (if dedicated-function
  9676. (funcall dedicated-function
  9677. (concat path
  9678. (and option (concat "::" option))))
  9679. (apply #'org-open-file
  9680. path
  9681. (cond (arg)
  9682. ((equal app "emacs") 'emacs)
  9683. ((equal app "sys") 'system))
  9684. (cond ((not option) nil)
  9685. ((org-string-match-p "\\`[0-9]+\\'" option)
  9686. (list (string-to-number option)))
  9687. (t (list nil
  9688. (org-link-unescape option)))))))))
  9689. ((assoc type org-link-protocols)
  9690. (funcall (nth 1 (assoc type org-link-protocols)) path))
  9691. ((equal type "help")
  9692. (let ((f-or-v (intern path)))
  9693. (cond ((fboundp f-or-v) (describe-function f-or-v))
  9694. ((boundp f-or-v) (describe-variable f-or-v))
  9695. (t (error "Not a known function or variable")))))
  9696. ((member type '("http" "https" "ftp" "mailto" "news"))
  9697. (browse-url (org-link-escape-browser (concat type ":" path))))
  9698. ((equal type "doi")
  9699. (browse-url
  9700. (org-link-escape-browser (concat org-doi-server-url path))))
  9701. ((equal type "message") (browse-url (concat type ":" path)))
  9702. ((equal type "shell")
  9703. (let ((buf (generate-new-buffer "*Org Shell Output*"))
  9704. (cmd path))
  9705. (if (or (and (org-string-nw-p
  9706. org-confirm-shell-link-not-regexp)
  9707. (string-match
  9708. org-confirm-shell-link-not-regexp cmd))
  9709. (not org-confirm-shell-link-function)
  9710. (funcall org-confirm-shell-link-function
  9711. (format "Execute \"%s\" in shell? "
  9712. (org-add-props cmd nil
  9713. 'face 'org-warning))))
  9714. (progn
  9715. (message "Executing %s" cmd)
  9716. (shell-command cmd buf)
  9717. (when (featurep 'midnight)
  9718. (setq clean-buffer-list-kill-buffer-names
  9719. (cons (buffer-name buf)
  9720. clean-buffer-list-kill-buffer-names))))
  9721. (user-error "Abort"))))
  9722. ((equal type "elisp")
  9723. (let ((cmd path))
  9724. (if (or (and (org-string-nw-p
  9725. org-confirm-elisp-link-not-regexp)
  9726. (org-string-match-p
  9727. org-confirm-elisp-link-not-regexp cmd))
  9728. (not org-confirm-elisp-link-function)
  9729. (funcall org-confirm-elisp-link-function
  9730. (format "Execute \"%s\" as elisp? "
  9731. (org-add-props cmd nil
  9732. 'face 'org-warning))))
  9733. (message "%s => %s" cmd
  9734. (if (eq (string-to-char cmd) ?\()
  9735. (eval (read cmd))
  9736. (call-interactively (read cmd))))
  9737. (user-error "Abort"))))
  9738. ((equal type "id")
  9739. (require 'org-id)
  9740. (funcall (nth 1 (assoc "id" org-link-protocols)) path))
  9741. ((member type '("coderef" "custom-id" "fuzzy" "radio"))
  9742. (unless (run-hook-with-args-until-success
  9743. 'org-open-link-functions path)
  9744. (if (not arg) (org-mark-ring-push)
  9745. (switch-to-buffer-other-window
  9746. (org-get-buffer-for-internal-link (current-buffer))))
  9747. (let ((destination
  9748. (org-with-wide-buffer
  9749. (if (equal type "radio")
  9750. (org-search-radio-target
  9751. (org-element-property :path link))
  9752. (org-link-search
  9753. (if (member type '("custom-id" "coderef"))
  9754. (org-element-property :raw-link link)
  9755. path)
  9756. ;; Prevent fuzzy links from matching
  9757. ;; themselves.
  9758. (and (equal type "fuzzy")
  9759. (+ 2 (org-element-property :begin link)))))
  9760. (point))))
  9761. (unless (and (<= (point-min) destination)
  9762. (>= (point-max) destination))
  9763. (widen))
  9764. (goto-char destination))))
  9765. (t (browse-url-at-point))))))
  9766. ;; On a footnote reference or at a footnote definition's label.
  9767. ((or (eq type 'footnote-reference)
  9768. (and (eq type 'footnote-definition)
  9769. (save-excursion
  9770. ;; Do not validate action when point is on the
  9771. ;; spaces right after the footnote label, in
  9772. ;; order to be on par with behaviour on links.
  9773. (skip-chars-forward " \t")
  9774. (let ((begin
  9775. (org-element-property :contents-begin context)))
  9776. (if begin (< (point) begin)
  9777. (= (org-element-property :post-affiliated context)
  9778. (line-beginning-position)))))))
  9779. (org-footnote-action))
  9780. (t (user-error "No link found")))))
  9781. (run-hook-with-args 'org-follow-link-hook)))
  9782. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  9783. "Offer links in the current entry and return the selected link.
  9784. If there is only one link, return it.
  9785. If NTH is an integer, return the NTH link found.
  9786. If ZERO is a string, check also this string for a link, and if
  9787. there is one, return it."
  9788. (with-current-buffer buffer
  9789. (save-excursion
  9790. (save-restriction
  9791. (widen)
  9792. (goto-char marker)
  9793. (let ((cnt ?0)
  9794. (in-emacs (if (integerp nth) nil nth))
  9795. have-zero end links link c)
  9796. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9797. (push (match-string 0 zero) links)
  9798. (setq cnt (1- cnt) have-zero t))
  9799. (save-excursion
  9800. (org-back-to-heading t)
  9801. (setq end (save-excursion (outline-next-heading) (point)))
  9802. (while (re-search-forward org-any-link-re end t)
  9803. (push (match-string 0) links))
  9804. (setq links (org-uniquify (reverse links))))
  9805. (cond
  9806. ((null links)
  9807. (message "No links"))
  9808. ((equal (length links) 1)
  9809. (setq link (car links)))
  9810. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9811. (setq link (nth (if have-zero nth (1- nth)) links)))
  9812. (t ; we have to select a link
  9813. (save-excursion
  9814. (save-window-excursion
  9815. (delete-other-windows)
  9816. (with-output-to-temp-buffer "*Select Link*"
  9817. (mapc (lambda (l)
  9818. (if (not (string-match org-bracket-link-regexp l))
  9819. (princ (format "[%c] %s\n" (incf cnt)
  9820. (org-remove-angle-brackets l)))
  9821. (if (match-end 3)
  9822. (princ (format "[%c] %s (%s)\n" (incf cnt)
  9823. (match-string 3 l) (match-string 1 l)))
  9824. (princ (format "[%c] %s\n" (incf cnt)
  9825. (match-string 1 l))))))
  9826. links))
  9827. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9828. (message "Select link to open, RET to open all:")
  9829. (setq c (read-char-exclusive))
  9830. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9831. (when (equal c ?q) (user-error "Abort"))
  9832. (if (equal c ?\C-m)
  9833. (setq link links)
  9834. (setq nth (- c ?0))
  9835. (if have-zero (setq nth (1+ nth)))
  9836. (unless (and (integerp nth) (>= (length links) nth))
  9837. (user-error "Invalid link selection"))
  9838. (setq link (nth (1- nth) links)))))
  9839. (cons link end))))))
  9840. ;; TODO: These functions are deprecated since `org-open-at-point'
  9841. ;; hard-codes behaviour for "file+emacs" and "file+sys" types.
  9842. (defun org-open-file-with-system (path)
  9843. "Open file at PATH using the system way of opening it."
  9844. (org-open-file path 'system))
  9845. (defun org-open-file-with-emacs (path)
  9846. "Open file at PATH in Emacs."
  9847. (org-open-file path 'emacs))
  9848. ;;; File search
  9849. (defvar org-create-file-search-functions nil
  9850. "List of functions to construct the right search string for a file link.
  9851. These functions are called in turn with point at the location to
  9852. which the link should point.
  9853. A function in the hook should first test if it would like to
  9854. handle this file type, for example by checking the `major-mode'
  9855. or the file extension. If it decides not to handle this file, it
  9856. should just return nil to give other functions a chance. If it
  9857. does handle the file, it must return the search string to be used
  9858. when following the link. The search string will be part of the
  9859. file link, given after a double colon, and `org-open-at-point'
  9860. will automatically search for it. If special measures must be
  9861. taken to make the search successful, another function should be
  9862. added to the companion hook `org-execute-file-search-functions',
  9863. which see.
  9864. A function in this hook may also use `setq' to set the variable
  9865. `description' to provide a suggestion for the descriptive text to
  9866. be used for this link when it gets inserted into an Org-mode
  9867. buffer with \\[org-insert-link].")
  9868. (defvar org-execute-file-search-functions nil
  9869. "List of functions to execute a file search triggered by a link.
  9870. Functions added to this hook must accept a single argument, the
  9871. search string that was part of the file link, the part after the
  9872. double colon. The function must first check if it would like to
  9873. handle this search, for example by checking the `major-mode' or
  9874. the file extension. If it decides not to handle this search, it
  9875. should just return nil to give other functions a chance. If it
  9876. does handle the search, it must return a non-nil value to keep
  9877. other functions from trying.
  9878. Each function can access the current prefix argument through the
  9879. variable `current-prefix-arg'. Note that a single prefix is used
  9880. to force opening a link in Emacs, so it may be good to only use a
  9881. numeric or double prefix to guide the search function.
  9882. In case this is needed, a function in this hook can also restore
  9883. the window configuration before `org-open-at-point' was called using:
  9884. (set-window-configuration org-window-config-before-follow-link)")
  9885. (defun org-search-radio-target (target)
  9886. "Search a radio target matching TARGET in current buffer.
  9887. White spaces are not significant."
  9888. (let ((re (format "<<<%s>>>"
  9889. (mapconcat #'regexp-quote
  9890. (org-split-string target "[ \t\n]+")
  9891. "[ \t]+\\(?:\n[ \t]*\\)?")))
  9892. (origin (point)))
  9893. (goto-char (point-min))
  9894. (catch :radio-match
  9895. (while (re-search-forward re nil t)
  9896. (backward-char)
  9897. (let ((object (org-element-context)))
  9898. (when (eq (org-element-type object) 'radio-target)
  9899. (goto-char (org-element-property :begin object))
  9900. (org-show-context 'link-search)
  9901. (throw :radio-match nil))))
  9902. (goto-char origin)
  9903. (user-error "No match for radio target: %s" target))))
  9904. (defun org-link-search (s &optional avoid-pos stealth)
  9905. "Search for a search string S.
  9906. If S starts with \"#\", it triggers a custom ID search.
  9907. If S is enclosed within parenthesis, it initiates a coderef
  9908. search.
  9909. If S is surrounded by forward slashes, it is interpreted as
  9910. a regular expression. In Org mode files, this will create an
  9911. `org-occur' sparse tree. In ordinary files, `occur' will be used
  9912. to list matches. If the current buffer is in `dired-mode', grep
  9913. will be used to search in all files.
  9914. When AVOID-POS is given, ignore matches near that position.
  9915. When optional argument STEALTH is non-nil, do not modify
  9916. visibility around point, thus ignoring `org-show-context-detail'
  9917. variable.
  9918. Search is case-insensitive and ignores white spaces. Return type
  9919. of matched result, with is either `dedicated' or `fuzzy'."
  9920. (unless (org-string-nw-p s) (error "Invalid search string \"%s\"" s))
  9921. (let* ((case-fold-search t)
  9922. (origin (point))
  9923. (normalized (replace-regexp-in-string "\n[ \t]*" " " s))
  9924. (words (org-split-string s "[ \t\n]+"))
  9925. (s-multi-re (mapconcat #'regexp-quote words "[ \t]+\\(?:\n[ \t]*\\)?"))
  9926. (s-single-re (mapconcat #'regexp-quote words "[ \t]+"))
  9927. type)
  9928. (cond
  9929. ;; Check if there are any special search functions.
  9930. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9931. ((eq (string-to-char s) ?#)
  9932. ;; Look for a custom ID S if S starts with "#".
  9933. (let* ((id (substring normalized 1))
  9934. (match (org-find-property "CUSTOM_ID" id)))
  9935. (if match (progn (goto-char match) (setf type 'dedicated))
  9936. (error "No match for custom ID: %s" id))))
  9937. ((string-match "\\`(\\(.*\\))\\'" normalized)
  9938. ;; Look for coderef targets if S is enclosed within parenthesis.
  9939. (let ((coderef (match-string-no-properties 1 normalized))
  9940. (re (substring s-single-re 1 -1)))
  9941. (goto-char (point-min))
  9942. (catch :coderef-match
  9943. (while (re-search-forward re nil t)
  9944. (let ((element (org-element-at-point)))
  9945. (when (and (memq (org-element-type element)
  9946. '(example-block src-block))
  9947. ;; Build proper regexp according to current
  9948. ;; block's label format.
  9949. (let ((label-fmt
  9950. (regexp-quote
  9951. (or (org-element-property :label-fmt element)
  9952. org-coderef-label-format))))
  9953. (save-excursion
  9954. (beginning-of-line)
  9955. (looking-at (format ".*?\\(%s\\)[ \t]*$"
  9956. (format label-fmt coderef))))))
  9957. (setq type 'dedicated)
  9958. (goto-char (match-beginning 1))
  9959. (throw :coderef-match nil))))
  9960. (goto-char origin)
  9961. (error "No match for coderef: %s" coderef))))
  9962. ((string-match "\\`/\\(.*\\)/\\'" normalized)
  9963. ;; Look for a regular expression.
  9964. (funcall (if (derived-mode-p 'org-mode) #'org-occur #'org-do-occur)
  9965. (match-string 1 s)))
  9966. ;; Fuzzy links.
  9967. (t
  9968. (let* ((starred (eq (string-to-char normalized) ?*))
  9969. (headline-search (and (derived-mode-p 'org-mode)
  9970. (or org-link-search-must-match-exact-headline
  9971. starred))))
  9972. (cond
  9973. ;; Look for targets, only if not in a headline search.
  9974. ((and (not starred)
  9975. (let ((target (format "<<%s>>" s-multi-re)))
  9976. (catch :target-match
  9977. (goto-char (point-min))
  9978. (while (re-search-forward target nil t)
  9979. (backward-char)
  9980. (let ((context (org-element-context)))
  9981. (when (eq (org-element-type context) 'target)
  9982. (setq type 'dedicated)
  9983. (goto-char (org-element-property :begin context))
  9984. (throw :target-match t))))
  9985. nil))))
  9986. ;; Look for elements named after S, only if not in a headline
  9987. ;; search.
  9988. ((and (not starred)
  9989. (let ((name (format "^[ \t]*#\\+NAME: +%s[ \t]*$" s-single-re)))
  9990. (catch :name-match
  9991. (goto-char (point-min))
  9992. (while (re-search-forward name nil t)
  9993. (let ((element (org-element-at-point)))
  9994. (when (equal (org-split-string
  9995. (org-element-property :name element)
  9996. "[ \t]+")
  9997. words)
  9998. (setq type 'dedicated)
  9999. (beginning-of-line)
  10000. (throw :name-match t))))
  10001. nil))))
  10002. ;; Regular text search. Prefer headlines in Org mode
  10003. ;; buffers.
  10004. ((and (derived-mode-p 'org-mode)
  10005. (let* ((wspace "[ \t]")
  10006. (wspaceopt (concat wspace "*"))
  10007. (cookie (concat "\\(?:"
  10008. wspaceopt
  10009. "\\[[0-9]*\\(?:%\\|/[0-9]*\\)\\]"
  10010. wspaceopt
  10011. "\\)"))
  10012. (sep (concat "\\(?:\\(?:" wspace "\\|" cookie "\\)+\\)"))
  10013. (re (concat
  10014. org-outline-regexp-bol
  10015. "\\(?:" org-todo-regexp "[ \t]+\\)?"
  10016. "\\(?:\\[#.\\][ \t]+\\)?"
  10017. "\\(?:" org-comment-string "[ \t]+\\)?"
  10018. sep "?"
  10019. (let ((title (mapconcat #'regexp-quote
  10020. words
  10021. sep)))
  10022. (if starred (substring title 1) title))
  10023. sep "?"
  10024. (org-re "\\(?:[ \t]+:[[:alnum:]_@#%%:]+:\\)?")
  10025. "[ \t]*$")))
  10026. (goto-char (point-min))
  10027. (re-search-forward re nil t)))
  10028. (goto-char (match-beginning 0))
  10029. (setq type 'dedicated))
  10030. ;; Offer to create non-existent headline depending on
  10031. ;; `org-link-search-must-match-exact-headline'.
  10032. ((and (derived-mode-p 'org-mode)
  10033. (not org-link-search-inhibit-query)
  10034. (eq org-link-search-must-match-exact-headline 'query-to-create)
  10035. (yes-or-no-p "No match - create this as a new heading? "))
  10036. (goto-char (point-max))
  10037. (unless (bolp) (newline))
  10038. (org-insert-heading nil t t)
  10039. (insert s "\n")
  10040. (beginning-of-line 0))
  10041. ;; Only headlines are looked after. No need to process
  10042. ;; further: throw an error.
  10043. ((and (derived-mode-p 'org-mode)
  10044. (or starred org-link-search-must-match-exact-headline))
  10045. (goto-char origin)
  10046. (error "No match for fuzzy expression: %s" normalized))
  10047. ;; Regular text search.
  10048. ((catch :fuzzy-match
  10049. (goto-char (point-min))
  10050. (while (re-search-forward s-multi-re nil t)
  10051. ;; Skip match if it contains AVOID-POS or it is included
  10052. ;; in a link with a description but outside the
  10053. ;; description.
  10054. (unless (or (and avoid-pos
  10055. (<= (match-beginning 0) avoid-pos)
  10056. (> (match-end 0) avoid-pos))
  10057. (and (save-match-data
  10058. (org-in-regexp org-bracket-link-regexp))
  10059. (match-beginning 3)
  10060. (or (> (match-beginning 3) (point))
  10061. (<= (match-end 3) (point)))
  10062. (org-element-lineage
  10063. (save-match-data (org-element-context))
  10064. '(link) t)))
  10065. (goto-char (match-beginning 0))
  10066. (setq type 'fuzzy)
  10067. (throw :fuzzy-match t)))
  10068. nil))
  10069. ;; All failed. Throw an error.
  10070. (t (goto-char origin)
  10071. (error "No match for fuzzy expression: %s" normalized))))))
  10072. ;; Disclose surroundings of match, if appropriate.
  10073. (when (and (derived-mode-p 'org-mode) (not stealth))
  10074. (org-show-context 'link-search))
  10075. type))
  10076. (defun org-get-buffer-for-internal-link (buffer)
  10077. "Return a buffer to be used for displaying the link target of internal links."
  10078. (cond
  10079. ((not org-display-internal-link-with-indirect-buffer)
  10080. buffer)
  10081. ((string-match "(Clone)$" (buffer-name buffer))
  10082. (message "Buffer is already a clone, not making another one")
  10083. ;; we also do not modify visibility in this case
  10084. buffer)
  10085. (t ; make a new indirect buffer for displaying the link
  10086. (let* ((bn (buffer-name buffer))
  10087. (ibn (concat bn "(Clone)"))
  10088. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  10089. (with-current-buffer ib (org-overview))
  10090. ib))))
  10091. (defun org-do-occur (regexp &optional cleanup)
  10092. "Call the Emacs command `occur'.
  10093. If CLEANUP is non-nil, remove the printout of the regular expression
  10094. in the *Occur* buffer. This is useful if the regex is long and not useful
  10095. to read."
  10096. (occur regexp)
  10097. (when cleanup
  10098. (let ((cwin (selected-window)) win beg end)
  10099. (when (setq win (get-buffer-window "*Occur*"))
  10100. (select-window win))
  10101. (goto-char (point-min))
  10102. (when (re-search-forward "match[a-z]+" nil t)
  10103. (setq beg (match-end 0))
  10104. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  10105. (setq end (1- (match-beginning 0)))))
  10106. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  10107. (goto-char (point-min))
  10108. (select-window cwin))))
  10109. ;;; The mark ring for links jumps
  10110. (defvar org-mark-ring nil
  10111. "Mark ring for positions before jumps in Org-mode.")
  10112. (defvar org-mark-ring-last-goto nil
  10113. "Last position in the mark ring used to go back.")
  10114. ;; Fill and close the ring
  10115. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  10116. (loop for i from 1 to org-mark-ring-length do
  10117. (push (make-marker) org-mark-ring))
  10118. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  10119. org-mark-ring)
  10120. (defun org-mark-ring-push (&optional pos buffer)
  10121. "Put the current position or POS into the mark ring and rotate it."
  10122. (interactive)
  10123. (setq pos (or pos (point)))
  10124. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  10125. (move-marker (car org-mark-ring)
  10126. (or pos (point))
  10127. (or buffer (current-buffer)))
  10128. (message "%s"
  10129. (substitute-command-keys
  10130. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  10131. (defun org-mark-ring-goto (&optional n)
  10132. "Jump to the previous position in the mark ring.
  10133. With prefix arg N, jump back that many stored positions. When
  10134. called several times in succession, walk through the entire ring.
  10135. Org-mode commands jumping to a different position in the current file,
  10136. or to another Org-mode file, automatically push the old position
  10137. onto the ring."
  10138. (interactive "p")
  10139. (let (p m)
  10140. (if (eq last-command this-command)
  10141. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  10142. (setq p org-mark-ring))
  10143. (setq org-mark-ring-last-goto p)
  10144. (setq m (car p))
  10145. (org-pop-to-buffer-same-window (marker-buffer m))
  10146. (goto-char m)
  10147. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  10148. (defun org-remove-angle-brackets (s)
  10149. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  10150. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  10151. s)
  10152. (defun org-add-angle-brackets (s)
  10153. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  10154. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  10155. s)
  10156. (defun org-remove-double-quotes (s)
  10157. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  10158. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  10159. s)
  10160. ;;; Following specific links
  10161. (defun org-follow-timestamp-link ()
  10162. "Open an agenda view for the time-stamp date/range at point."
  10163. (cond
  10164. ((org-at-date-range-p t)
  10165. (let ((org-agenda-start-on-weekday)
  10166. (t1 (match-string 1))
  10167. (t2 (match-string 2)) tt1 tt2)
  10168. (setq tt1 (time-to-days (org-time-string-to-time t1))
  10169. tt2 (time-to-days (org-time-string-to-time t2)))
  10170. (let ((org-agenda-buffer-tmp-name
  10171. (format "*Org Agenda(a:%s)"
  10172. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  10173. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  10174. ((org-at-timestamp-p t)
  10175. (let ((org-agenda-buffer-tmp-name
  10176. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  10177. (org-agenda-list nil (time-to-days (org-time-string-to-time
  10178. (substring (match-string 1) 0 10)))
  10179. 1)))
  10180. (t (error "This should not happen"))))
  10181. ;;; Following file links
  10182. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  10183. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  10184. (declare-function mailcap-mime-info
  10185. "mailcap" (string &optional request no-decode))
  10186. (defvar org-wait nil)
  10187. (defun org-open-file (path &optional in-emacs line search)
  10188. "Open the file at PATH.
  10189. First, this expands any special file name abbreviations. Then the
  10190. configuration variable `org-file-apps' is checked if it contains an
  10191. entry for this file type, and if yes, the corresponding command is launched.
  10192. If no application is found, Emacs simply visits the file.
  10193. With optional prefix argument IN-EMACS, Emacs will visit the file.
  10194. With a double \\[universal-argument] \\[universal-argument] \
  10195. prefix arg, Org tries to avoid opening in Emacs
  10196. and to use an external application to visit the file.
  10197. Optional LINE specifies a line to go to, optional SEARCH a string
  10198. to search for. If LINE or SEARCH is given, the file will be
  10199. opened in Emacs, unless an entry from org-file-apps that makes
  10200. use of groups in a regexp matches.
  10201. If you want to change the way frames are used when following a
  10202. link, please customize `org-link-frame-setup'.
  10203. If the file does not exist, an error is thrown."
  10204. (let* ((file (if (equal path "")
  10205. buffer-file-name
  10206. (substitute-in-file-name (expand-file-name path))))
  10207. (file-apps (append org-file-apps (org-default-apps)))
  10208. (apps (org-remove-if
  10209. 'org-file-apps-entry-match-against-dlink-p file-apps))
  10210. (apps-dlink (org-remove-if-not
  10211. 'org-file-apps-entry-match-against-dlink-p file-apps))
  10212. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  10213. (dirp (if remp nil (file-directory-p file)))
  10214. (file (if (and dirp org-open-directory-means-index-dot-org)
  10215. (concat (file-name-as-directory file) "index.org")
  10216. file))
  10217. (a-m-a-p (assq 'auto-mode apps))
  10218. (dfile (downcase file))
  10219. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  10220. (link (cond ((and (eq line nil)
  10221. (eq search nil))
  10222. file)
  10223. (line
  10224. (concat file "::" (number-to-string line)))
  10225. (search
  10226. (concat file "::" search))))
  10227. (dlink (downcase link))
  10228. (old-buffer (current-buffer))
  10229. (old-pos (point))
  10230. (old-mode major-mode)
  10231. ext cmd link-match-data)
  10232. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  10233. (setq ext (match-string 1 dfile))
  10234. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  10235. (setq ext (match-string 1 dfile))))
  10236. (cond
  10237. ((member in-emacs '((16) system))
  10238. (setq cmd (cdr (assoc 'system apps))))
  10239. (in-emacs (setq cmd 'emacs))
  10240. (t
  10241. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  10242. (and dirp (cdr (assoc 'directory apps)))
  10243. ; first, try matching against apps-dlink
  10244. ; if we get a match here, store the match data for later
  10245. (let ((match (assoc-default dlink apps-dlink
  10246. 'string-match)))
  10247. (if match
  10248. (progn (setq link-match-data (match-data))
  10249. match)
  10250. (progn (setq in-emacs (or in-emacs line search))
  10251. nil))) ; if we have no match in apps-dlink,
  10252. ; always open the file in emacs if line or search
  10253. ; is given (for backwards compatibility)
  10254. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  10255. 'string-match)
  10256. (cdr (assoc ext apps))
  10257. (cdr (assoc t apps))))))
  10258. (when (eq cmd 'system)
  10259. (setq cmd (cdr (assoc 'system apps))))
  10260. (when (eq cmd 'default)
  10261. (setq cmd (cdr (assoc t apps))))
  10262. (when (eq cmd 'mailcap)
  10263. (require 'mailcap)
  10264. (mailcap-parse-mailcaps)
  10265. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  10266. (command (mailcap-mime-info mime-type)))
  10267. (if (stringp command)
  10268. (setq cmd command)
  10269. (setq cmd 'emacs))))
  10270. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  10271. (not (file-exists-p file))
  10272. (not org-open-non-existing-files))
  10273. (user-error "No such file: %s" file))
  10274. (cond
  10275. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  10276. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  10277. (while (string-match "['\"]%s['\"]" cmd)
  10278. (setq cmd (replace-match "%s" t t cmd)))
  10279. (while (string-match "%s" cmd)
  10280. (setq cmd (replace-match
  10281. (save-match-data
  10282. (shell-quote-argument
  10283. (convert-standard-filename file)))
  10284. t t cmd)))
  10285. ;; Replace "%1", "%2" etc. in command with group matches from regex
  10286. (save-match-data
  10287. (let ((match-index 1)
  10288. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  10289. (set-match-data link-match-data)
  10290. (while (<= match-index number-of-groups)
  10291. (let ((regex (concat "%" (number-to-string match-index)))
  10292. (replace-with (match-string match-index dlink)))
  10293. (while (string-match regex cmd)
  10294. (setq cmd (replace-match replace-with t t cmd))))
  10295. (setq match-index (+ match-index 1)))))
  10296. (save-window-excursion
  10297. (message "Running %s...done" cmd)
  10298. (start-process-shell-command cmd nil cmd)
  10299. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  10300. ((or (stringp cmd)
  10301. (eq cmd 'emacs))
  10302. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  10303. (widen)
  10304. (if line (progn (org-goto-line line)
  10305. (if (derived-mode-p 'org-mode)
  10306. (org-reveal)))
  10307. (if search (org-link-search search))))
  10308. ((consp cmd)
  10309. (let ((file (convert-standard-filename file)))
  10310. (save-match-data
  10311. (set-match-data link-match-data)
  10312. (eval cmd))))
  10313. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  10314. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  10315. (or (not (equal old-buffer (current-buffer)))
  10316. (not (equal old-pos (point))))
  10317. (org-mark-ring-push old-pos old-buffer))))
  10318. (defun org-file-apps-entry-match-against-dlink-p (entry)
  10319. "This function returns non-nil if `entry' uses a regular
  10320. expression which should be matched against the whole link by
  10321. org-open-file.
  10322. It assumes that is the case when the entry uses a regular
  10323. expression which has at least one grouping construct and the
  10324. action is either a lisp form or a command string containing
  10325. '%1', i.e. using at least one subexpression match as a
  10326. parameter."
  10327. (let ((selector (car entry))
  10328. (action (cdr entry)))
  10329. (if (stringp selector)
  10330. (and (> (regexp-opt-depth selector) 0)
  10331. (or (and (stringp action)
  10332. (string-match "%[0-9]" action))
  10333. (consp action)))
  10334. nil)))
  10335. (defun org-default-apps ()
  10336. "Return the default applications for this operating system."
  10337. (cond
  10338. ((eq system-type 'darwin)
  10339. org-file-apps-defaults-macosx)
  10340. ((eq system-type 'windows-nt)
  10341. org-file-apps-defaults-windowsnt)
  10342. (t org-file-apps-defaults-gnu)))
  10343. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  10344. "Convert extensions to regular expressions in the cars of LIST.
  10345. Also, weed out any non-string entries, because the return value is used
  10346. only for regexp matching.
  10347. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  10348. point to the symbol `emacs', indicating that the file should
  10349. be opened in Emacs."
  10350. (append
  10351. (delq nil
  10352. (mapcar (lambda (x)
  10353. (if (not (stringp (car x)))
  10354. nil
  10355. (if (string-match "\\W" (car x))
  10356. x
  10357. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  10358. list))
  10359. (if add-auto-mode
  10360. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  10361. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  10362. (defun org-file-remote-p (file)
  10363. "Test whether FILE specifies a location on a remote system.
  10364. Return non-nil if the location is indeed remote.
  10365. For example, the filename \"/user@host:/foo\" specifies a location
  10366. on the system \"/user@host:\"."
  10367. (cond ((fboundp 'file-remote-p)
  10368. (file-remote-p file))
  10369. ((fboundp 'tramp-handle-file-remote-p)
  10370. (tramp-handle-file-remote-p file))
  10371. ((and (boundp 'ange-ftp-name-format)
  10372. (string-match (car ange-ftp-name-format) file))
  10373. t)))
  10374. ;;;; Refiling
  10375. (defun org-get-org-file ()
  10376. "Read a filename, with default directory `org-directory'."
  10377. (let ((default (or org-default-notes-file remember-data-file)))
  10378. (read-file-name (format "File name [%s]: " default)
  10379. (file-name-as-directory org-directory)
  10380. default)))
  10381. (defun org-notes-order-reversed-p ()
  10382. "Check if the current file should receive notes in reversed order."
  10383. (cond
  10384. ((not org-reverse-note-order) nil)
  10385. ((eq t org-reverse-note-order) t)
  10386. ((not (listp org-reverse-note-order)) nil)
  10387. (t (catch 'exit
  10388. (dolist (entry org-reverse-note-order)
  10389. (if (string-match (car entry) buffer-file-name)
  10390. (throw 'exit (cdr entry))))))))
  10391. (defvar org-refile-target-table nil
  10392. "The list of refile targets, created by `org-refile'.")
  10393. (defvar org-agenda-new-buffers nil
  10394. "Buffers created to visit agenda files.")
  10395. (defvar org-refile-cache nil
  10396. "Cache for refile targets.")
  10397. (defvar org-refile-markers nil
  10398. "All the markers used for caching refile locations.")
  10399. (defun org-refile-marker (pos)
  10400. "Get a new refile marker, but only if caching is in use."
  10401. (if (not org-refile-use-cache)
  10402. pos
  10403. (let ((m (make-marker)))
  10404. (move-marker m pos)
  10405. (push m org-refile-markers)
  10406. m)))
  10407. (defun org-refile-cache-clear ()
  10408. "Clear the refile cache and disable all the markers."
  10409. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  10410. (setq org-refile-markers nil)
  10411. (setq org-refile-cache nil)
  10412. (message "Refile cache has been cleared"))
  10413. (defun org-refile-cache-check-set (set)
  10414. "Check if all the markers in the cache still have live buffers."
  10415. (let (marker)
  10416. (catch 'exit
  10417. (while (and set (setq marker (nth 3 (pop set))))
  10418. ;; If `org-refile-use-outline-path' is 'file, marker may be nil
  10419. (when (and marker (null (marker-buffer marker)))
  10420. (message "Please regenerate the refile cache with `C-0 C-c C-w'")
  10421. (sit-for 3)
  10422. (throw 'exit nil)))
  10423. t)))
  10424. (defun org-refile-cache-put (set &rest identifiers)
  10425. "Push the refile targets SET into the cache, under IDENTIFIERS."
  10426. (let* ((key (sha1 (prin1-to-string identifiers)))
  10427. (entry (assoc key org-refile-cache)))
  10428. (if entry
  10429. (setcdr entry set)
  10430. (push (cons key set) org-refile-cache))))
  10431. (defun org-refile-cache-get (&rest identifiers)
  10432. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  10433. (cond
  10434. ((not org-refile-cache) nil)
  10435. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  10436. (t
  10437. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  10438. org-refile-cache))))
  10439. (and set (org-refile-cache-check-set set) set)))))
  10440. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  10441. "Produce a table with refile targets."
  10442. (let ((case-fold-search nil)
  10443. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  10444. (entries (or org-refile-targets '((nil . (:level . 1)))))
  10445. targets tgs txt re files desc descre fast-path-p level pos0)
  10446. (message "Getting targets...")
  10447. (with-current-buffer (or default-buffer (current-buffer))
  10448. (dolist (entry entries)
  10449. (setq files (car entry) desc (cdr entry))
  10450. (setq fast-path-p nil)
  10451. (cond
  10452. ((null files) (setq files (list (current-buffer))))
  10453. ((eq files 'org-agenda-files)
  10454. (setq files (org-agenda-files 'unrestricted)))
  10455. ((and (symbolp files) (fboundp files))
  10456. (setq files (funcall files)))
  10457. ((and (symbolp files) (boundp files))
  10458. (setq files (symbol-value files))))
  10459. (if (stringp files) (setq files (list files)))
  10460. (cond
  10461. ((eq (car desc) :tag)
  10462. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  10463. ((eq (car desc) :todo)
  10464. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  10465. ((eq (car desc) :regexp)
  10466. (setq descre (cdr desc)))
  10467. ((eq (car desc) :level)
  10468. (setq descre (concat "^\\*\\{" (number-to-string
  10469. (if org-odd-levels-only
  10470. (1- (* 2 (cdr desc)))
  10471. (cdr desc)))
  10472. "\\}[ \t]")))
  10473. ((eq (car desc) :maxlevel)
  10474. (setq fast-path-p t)
  10475. (setq descre (concat "^\\*\\{1," (number-to-string
  10476. (if org-odd-levels-only
  10477. (1- (* 2 (cdr desc)))
  10478. (cdr desc)))
  10479. "\\}[ \t]")))
  10480. (t (error "Bad refiling target description %s" desc)))
  10481. (dolist (f files)
  10482. (with-current-buffer
  10483. (if (bufferp f) f (org-get-agenda-file-buffer f))
  10484. (or
  10485. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  10486. (progn
  10487. (if (bufferp f) (setq f (buffer-file-name
  10488. (buffer-base-buffer f))))
  10489. (setq f (and f (expand-file-name f)))
  10490. (if (eq org-refile-use-outline-path 'file)
  10491. (push (list (file-name-nondirectory f) f nil nil) tgs))
  10492. (save-excursion
  10493. (save-restriction
  10494. (widen)
  10495. (goto-char (point-min))
  10496. (while (re-search-forward descre nil t)
  10497. (goto-char (setq pos0 (point-at-bol)))
  10498. (catch 'next
  10499. (when org-refile-target-verify-function
  10500. (save-match-data
  10501. (or (funcall org-refile-target-verify-function)
  10502. (throw 'next t))))
  10503. (when (and (looking-at org-complex-heading-regexp)
  10504. (not (member (match-string 4) excluded-entries))
  10505. (match-string 4))
  10506. (setq level (org-reduced-level
  10507. (- (match-end 1) (match-beginning 1)))
  10508. txt (org-link-display-format (match-string 4))
  10509. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  10510. re (format org-complex-heading-regexp-format
  10511. (regexp-quote (match-string 4))))
  10512. (when org-refile-use-outline-path
  10513. (setq txt (mapconcat
  10514. 'org-protect-slash
  10515. (append
  10516. (if (eq org-refile-use-outline-path
  10517. 'file)
  10518. (list (file-name-nondirectory
  10519. (buffer-file-name
  10520. (buffer-base-buffer))))
  10521. (if (eq org-refile-use-outline-path
  10522. 'full-file-path)
  10523. (list (buffer-file-name
  10524. (buffer-base-buffer)))))
  10525. (org-get-outline-path fast-path-p
  10526. level txt)
  10527. (list txt))
  10528. "/")))
  10529. (push (list txt f re (org-refile-marker (point)))
  10530. tgs)))
  10531. (when (= (point) pos0)
  10532. ;; verification function has not moved point
  10533. (goto-char (point-at-eol))))))))
  10534. (when org-refile-use-cache
  10535. (org-refile-cache-put tgs (buffer-file-name) descre))
  10536. (setq targets (append tgs targets))))))
  10537. (message "Getting targets...done")
  10538. (nreverse targets)))
  10539. (defun org-protect-slash (s)
  10540. (while (string-match "/" s)
  10541. (setq s (replace-match "\\" t t s)))
  10542. s)
  10543. (defvar org-olpa (make-vector 20 nil))
  10544. (defun org-get-outline-path (&optional fastp level heading)
  10545. "Return the outline path to the current entry, as a list.
  10546. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  10547. routine which makes outline path derivations for an entire file,
  10548. avoiding backtracing. Refile target collection makes use of that."
  10549. (if fastp
  10550. (progn
  10551. (if (> level 19)
  10552. (error "Outline path failure, more than 19 levels"))
  10553. (loop for i from level upto 19 do
  10554. (aset org-olpa i nil))
  10555. (prog1
  10556. (delq nil (append org-olpa nil))
  10557. (aset org-olpa level heading)))
  10558. (let (rtn case-fold-search)
  10559. (save-excursion
  10560. (save-restriction
  10561. (widen)
  10562. (while (org-up-heading-safe)
  10563. (when (looking-at org-complex-heading-regexp)
  10564. (push (org-trim
  10565. (replace-regexp-in-string
  10566. ;; Remove statistical/checkboxes cookies
  10567. "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
  10568. (org-match-string-no-properties 4)))
  10569. rtn)))
  10570. rtn)))))
  10571. (defun org-format-outline-path (path &optional width prefix separator)
  10572. "Format the outline path PATH for display.
  10573. WIDTH is the maximum number of characters that is available.
  10574. PREFIX is a prefix to be included in the returned string,
  10575. such as the file name.
  10576. SEPARATOR is inserted between the different parts of the path,
  10577. the default is \"/\"."
  10578. (setq width (or width 79))
  10579. (if prefix (setq width (- width (length prefix))))
  10580. (if (not path)
  10581. (or prefix "")
  10582. (let* ((nsteps (length path))
  10583. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  10584. (maxwidth (if (<= total-width width)
  10585. 10000 ;; everything fits
  10586. ;; we need to shorten the level headings
  10587. (/ (- width nsteps) nsteps)))
  10588. (org-odd-levels-only nil)
  10589. (n 0)
  10590. (total (1+ (length prefix))))
  10591. (setq maxwidth (max maxwidth 10))
  10592. (concat prefix
  10593. (if prefix (or separator "/"))
  10594. (mapconcat
  10595. (lambda (h)
  10596. (setq n (1+ n))
  10597. (if (and (= n nsteps) (< maxwidth 10000))
  10598. (setq maxwidth (- total-width total)))
  10599. (if (< (length h) maxwidth)
  10600. (progn (setq total (+ total (length h) 1)) h)
  10601. (setq h (substring h 0 (- maxwidth 2))
  10602. total (+ total maxwidth 1))
  10603. (if (string-match "[ \t]+\\'" h)
  10604. (setq h (substring h 0 (match-beginning 0))))
  10605. (setq h (concat h "..")))
  10606. (org-add-props h nil 'face
  10607. (nth (% (1- n) org-n-level-faces)
  10608. org-level-faces))
  10609. h)
  10610. path (or separator "/"))))))
  10611. (defun org-display-outline-path (&optional file current separator just-return-string)
  10612. "Display the current outline path in the echo area.
  10613. If FILE is non-nil, prepend the output with the file name.
  10614. If CURRENT is non-nil, append the current heading to the output.
  10615. SEPARATOR is passed through to `org-format-outline-path'. It separates
  10616. the different parts of the path and defaults to \"/\".
  10617. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  10618. (interactive "P")
  10619. (let* (case-fold-search
  10620. (bfn (buffer-file-name (buffer-base-buffer)))
  10621. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  10622. res)
  10623. (if current (setq path (append path
  10624. (save-excursion
  10625. (org-back-to-heading t)
  10626. (if (looking-at org-complex-heading-regexp)
  10627. (list (match-string 4)))))))
  10628. (setq res
  10629. (org-format-outline-path
  10630. path
  10631. (1- (frame-width))
  10632. (and file bfn (concat (file-name-nondirectory bfn) separator))
  10633. separator))
  10634. (if just-return-string
  10635. (org-no-properties res)
  10636. (org-unlogged-message "%s" res))))
  10637. (defvar org-refile-history nil
  10638. "History for refiling operations.")
  10639. (defvar org-after-refile-insert-hook nil
  10640. "Hook run after `org-refile' has inserted its stuff at the new location.
  10641. Note that this is still *before* the stuff will be removed from
  10642. the *old* location.")
  10643. (defvar org-capture-last-stored-marker)
  10644. (defvar org-refile-keep nil
  10645. "Non-nil means `org-refile' will copy instead of refile.")
  10646. (defun org-copy ()
  10647. "Like `org-refile', but copy."
  10648. (interactive)
  10649. (let ((org-refile-keep t))
  10650. (funcall 'org-refile nil nil nil "Copy")))
  10651. (defun org-refile (&optional arg default-buffer rfloc msg)
  10652. "Move the entry or entries at point to another heading.
  10653. The list of target headings is compiled using the information in
  10654. `org-refile-targets', which see.
  10655. At the target location, the entry is filed as a subitem of the
  10656. target heading. Depending on `org-reverse-note-order', the new
  10657. subitem will either be the first or the last subitem.
  10658. If there is an active region, all entries in that region will be
  10659. refiled. However, the region must fulfill the requirement that
  10660. the first heading sets the top-level of the moved text.
  10661. With prefix arg ARG, the command will only visit the target
  10662. location and not actually move anything.
  10663. With a double prefix arg \\[universal-argument] \\[universal-argument], go to the location where the last
  10664. refiling operation has put the subtree.
  10665. With a numeric prefix argument of `2', refile to the running clock.
  10666. With a numeric prefix argument of `3', emulate `org-refile-keep'
  10667. being set to t and copy to the target location, don't move it.
  10668. Beware that keeping refiled entries may result in duplicated ID
  10669. properties.
  10670. RFLOC can be a refile location obtained in a different way.
  10671. MSG is a string to replace \"Refile\" in the default prompt with
  10672. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  10673. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  10674. If you are using target caching (see `org-refile-use-cache'), you
  10675. have to clear the target cache in order to find new targets.
  10676. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  10677. prefix argument (`C-u C-u C-u C-c C-w')."
  10678. (interactive "P")
  10679. (if (member arg '(0 (64)))
  10680. (org-refile-cache-clear)
  10681. (let* ((actionmsg (cond (msg msg)
  10682. ((equal arg 3) "Refile (and keep)")
  10683. (t "Refile")))
  10684. (cbuf (current-buffer))
  10685. (regionp (org-region-active-p))
  10686. (region-start (and regionp (region-beginning)))
  10687. (region-end (and regionp (region-end)))
  10688. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  10689. (org-refile-keep (if (equal arg 3) t org-refile-keep))
  10690. pos it nbuf file re level reversed)
  10691. (setq last-command nil)
  10692. (when regionp
  10693. (goto-char region-start)
  10694. (or (bolp) (goto-char (point-at-bol)))
  10695. (setq region-start (point))
  10696. (unless (or (org-kill-is-subtree-p
  10697. (buffer-substring region-start region-end))
  10698. (prog1 org-refile-active-region-within-subtree
  10699. (let ((s (point-at-eol)))
  10700. (org-toggle-heading)
  10701. (setq region-end (+ (- (point-at-eol) s) region-end)))))
  10702. (user-error "The region is not a (sequence of) subtree(s)")))
  10703. (if (equal arg '(16))
  10704. (org-refile-goto-last-stored)
  10705. (when (or
  10706. (and (equal arg 2)
  10707. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  10708. (prog1
  10709. (setq it (list (or org-clock-heading "running clock")
  10710. (buffer-file-name
  10711. (marker-buffer org-clock-hd-marker))
  10712. ""
  10713. (marker-position org-clock-hd-marker)))
  10714. (setq arg nil)))
  10715. (setq it (or rfloc
  10716. (let (heading-text)
  10717. (save-excursion
  10718. (unless (and arg (listp arg))
  10719. (org-back-to-heading t)
  10720. (setq heading-text
  10721. (replace-regexp-in-string
  10722. org-bracket-link-regexp
  10723. "\\3"
  10724. (nth 4 (org-heading-components)))))
  10725. (org-refile-get-location
  10726. (cond ((and arg (listp arg)) "Goto")
  10727. (regionp (concat actionmsg " region to"))
  10728. (t (concat actionmsg " subtree \""
  10729. heading-text "\" to")))
  10730. default-buffer
  10731. (and (not (equal '(4) arg))
  10732. org-refile-allow-creating-parent-nodes)
  10733. arg))))))
  10734. (setq file (nth 1 it)
  10735. re (nth 2 it)
  10736. pos (nth 3 it))
  10737. (if (and (not arg)
  10738. pos
  10739. (equal (buffer-file-name) file)
  10740. (if regionp
  10741. (and (>= pos region-start)
  10742. (<= pos region-end))
  10743. (and (>= pos (point))
  10744. (< pos (save-excursion
  10745. (org-end-of-subtree t t))))))
  10746. (error "Cannot refile to position inside the tree or region"))
  10747. (setq nbuf (or (find-buffer-visiting file)
  10748. (find-file-noselect file)))
  10749. (if (and arg (not (equal arg 3)))
  10750. (progn
  10751. (org-pop-to-buffer-same-window nbuf)
  10752. (goto-char pos)
  10753. (org-show-context 'org-goto))
  10754. (if regionp
  10755. (progn
  10756. (org-kill-new (buffer-substring region-start region-end))
  10757. (org-save-markers-in-region region-start region-end))
  10758. (org-copy-subtree 1 nil t))
  10759. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  10760. (find-file-noselect file)))
  10761. (setq reversed (org-notes-order-reversed-p))
  10762. (save-excursion
  10763. (save-restriction
  10764. (widen)
  10765. (if pos
  10766. (progn
  10767. (goto-char pos)
  10768. (looking-at org-outline-regexp)
  10769. (setq level (org-get-valid-level (funcall outline-level) 1))
  10770. (goto-char
  10771. (if reversed
  10772. (or (outline-next-heading) (point-max))
  10773. (or (save-excursion (org-get-next-sibling))
  10774. (org-end-of-subtree t t)
  10775. (point-max)))))
  10776. (setq level 1)
  10777. (if (not reversed)
  10778. (goto-char (point-max))
  10779. (goto-char (point-min))
  10780. (or (outline-next-heading) (goto-char (point-max)))))
  10781. (if (not (bolp)) (newline))
  10782. (org-paste-subtree level nil nil t)
  10783. (when org-log-refile
  10784. (org-add-log-setup 'refile nil nil 'findpos org-log-refile)
  10785. (unless (eq org-log-refile 'note)
  10786. (save-excursion (org-add-log-note))))
  10787. (and org-auto-align-tags
  10788. (let ((org-loop-over-headlines-in-active-region nil))
  10789. (org-set-tags nil t)))
  10790. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10791. :last-refile)))
  10792. (when bookmark-name
  10793. (with-demoted-errors
  10794. (bookmark-set bookmark-name))))
  10795. ;; If we are refiling for capture, make sure that the
  10796. ;; last-capture pointers point here
  10797. (when (org-bound-and-true-p org-refile-for-capture)
  10798. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10799. :last-capture-marker)))
  10800. (when bookmark-name
  10801. (with-demoted-errors
  10802. (bookmark-set bookmark-name))))
  10803. (move-marker org-capture-last-stored-marker (point)))
  10804. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10805. (run-hooks 'org-after-refile-insert-hook))))
  10806. (unless org-refile-keep
  10807. (if regionp
  10808. (delete-region (point) (+ (point) (- region-end region-start)))
  10809. (delete-region
  10810. (and (org-back-to-heading t) (point))
  10811. (min (1+ (buffer-size)) (org-end-of-subtree t t) (point)))))
  10812. (when (featurep 'org-inlinetask)
  10813. (org-inlinetask-remove-END-maybe))
  10814. (setq org-markers-to-move nil)
  10815. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  10816. (defun org-refile-goto-last-stored ()
  10817. "Go to the location where the last refile was stored."
  10818. (interactive)
  10819. (bookmark-jump (plist-get org-bookmark-names-plist :last-refile))
  10820. (message "This is the location of the last refile"))
  10821. (defun org-refile--get-location (refloc tbl)
  10822. "When user refile to REFLOC, find the associated target in TBL.
  10823. Also check `org-refile-target-table'."
  10824. (car (delq
  10825. nil
  10826. (mapcar
  10827. (lambda (r) (or (assoc r tbl)
  10828. (assoc r org-refile-target-table)))
  10829. (list (replace-regexp-in-string "/$" "" refloc)
  10830. (replace-regexp-in-string "\\([^/]\\)$" "\\1/" refloc))))))
  10831. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  10832. no-exclude)
  10833. "Prompt the user for a refile location, using PROMPT.
  10834. PROMPT should not be suffixed with a colon and a space, because
  10835. this function appends the default value from
  10836. `org-refile-history' automatically, if that is not empty.
  10837. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  10838. this is used for the GOTO interface."
  10839. (let ((org-refile-targets org-refile-targets)
  10840. (org-refile-use-outline-path org-refile-use-outline-path)
  10841. excluded-entries)
  10842. (when (and (derived-mode-p 'org-mode)
  10843. (not org-refile-use-cache)
  10844. (not no-exclude))
  10845. (org-map-tree
  10846. (lambda()
  10847. (setq excluded-entries
  10848. (append excluded-entries (list (org-get-heading t t)))))))
  10849. (setq org-refile-target-table
  10850. (org-refile-get-targets default-buffer excluded-entries)))
  10851. (unless org-refile-target-table
  10852. (user-error "No refile targets"))
  10853. (let* ((cbuf (current-buffer))
  10854. (partial-completion-mode nil)
  10855. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10856. (cfunc (if (and org-refile-use-outline-path
  10857. org-outline-path-complete-in-steps)
  10858. 'org-olpath-completing-read
  10859. 'org-icompleting-read))
  10860. (extra (if org-refile-use-outline-path "/" ""))
  10861. (cbnex (concat (buffer-name) extra))
  10862. (filename (and cfn (expand-file-name cfn)))
  10863. (tbl (mapcar
  10864. (lambda (x)
  10865. (if (and (not (member org-refile-use-outline-path
  10866. '(file full-file-path)))
  10867. (not (equal filename (nth 1 x))))
  10868. (cons (concat (car x) extra " ("
  10869. (file-name-nondirectory (nth 1 x)) ")")
  10870. (cdr x))
  10871. (cons (concat (car x) extra) (cdr x))))
  10872. org-refile-target-table))
  10873. (completion-ignore-case t)
  10874. cdef
  10875. (prompt (concat prompt
  10876. (or (and (car org-refile-history)
  10877. (concat " (default " (car org-refile-history) ")"))
  10878. (and (assoc cbnex tbl) (setq cdef cbnex)
  10879. (concat " (default " cbnex ")"))) ": "))
  10880. pa answ parent-target child parent old-hist)
  10881. (setq old-hist org-refile-history)
  10882. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10883. nil 'org-refile-history (or cdef (car org-refile-history))))
  10884. (if (setq pa (org-refile--get-location answ tbl))
  10885. (progn
  10886. (org-refile-check-position pa)
  10887. (when (or (not org-refile-history)
  10888. (not (eq old-hist org-refile-history))
  10889. (not (equal (car pa) (car org-refile-history))))
  10890. (setq org-refile-history
  10891. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10892. org-refile-history
  10893. (cdr org-refile-history))))
  10894. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  10895. (pop org-refile-history)))
  10896. pa)
  10897. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10898. (progn
  10899. (setq parent (match-string 1 answ)
  10900. child (match-string 2 answ))
  10901. (setq parent-target (org-refile--get-location parent tbl))
  10902. (when (and parent-target
  10903. (or (eq new-nodes t)
  10904. (and (eq new-nodes 'confirm)
  10905. (y-or-n-p (format "Create new node \"%s\"? "
  10906. child)))))
  10907. (org-refile-new-child parent-target child)))
  10908. (user-error "Invalid target location")))))
  10909. (declare-function org-string-nw-p "org-macs" (s))
  10910. (defun org-refile-check-position (refile-pointer)
  10911. "Check if the refile pointer matches the headline to which it points."
  10912. (let* ((file (nth 1 refile-pointer))
  10913. (re (nth 2 refile-pointer))
  10914. (pos (nth 3 refile-pointer))
  10915. buffer)
  10916. (if (and (not (markerp pos)) (not file))
  10917. (user-error "Please indicate a target file in the refile path")
  10918. (when (org-string-nw-p re)
  10919. (setq buffer (if (markerp pos)
  10920. (marker-buffer pos)
  10921. (or (find-buffer-visiting file)
  10922. (find-file-noselect file))))
  10923. (with-current-buffer buffer
  10924. (save-excursion
  10925. (save-restriction
  10926. (widen)
  10927. (goto-char pos)
  10928. (beginning-of-line 1)
  10929. (unless (org-looking-at-p re)
  10930. (user-error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  10931. (defun org-refile-new-child (parent-target child)
  10932. "Use refile target PARENT-TARGET to add new CHILD below it."
  10933. (unless parent-target
  10934. (error "Cannot find parent for new node"))
  10935. (let ((file (nth 1 parent-target))
  10936. (pos (nth 3 parent-target))
  10937. level)
  10938. (with-current-buffer (or (find-buffer-visiting file)
  10939. (find-file-noselect file))
  10940. (save-excursion
  10941. (save-restriction
  10942. (widen)
  10943. (if pos
  10944. (goto-char pos)
  10945. (goto-char (point-max))
  10946. (if (not (bolp)) (newline)))
  10947. (when (looking-at org-outline-regexp)
  10948. (setq level (funcall outline-level))
  10949. (org-end-of-subtree t t))
  10950. (org-back-over-empty-lines)
  10951. (insert "\n" (make-string
  10952. (if pos (org-get-valid-level level 1) 1) ?*)
  10953. " " child "\n")
  10954. (beginning-of-line 0)
  10955. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10956. (defun org-olpath-completing-read (prompt collection &rest args)
  10957. "Read an outline path like a file name."
  10958. (let ((thetable collection)
  10959. (org-completion-use-ido nil) ; does not work with ido.
  10960. (org-completion-use-iswitchb nil)) ; or iswitchb
  10961. (apply #'org-icompleting-read
  10962. prompt
  10963. (lambda (string predicate &optional flag)
  10964. (cond
  10965. ((eq flag nil) (try-completion string thetable))
  10966. ((eq flag t)
  10967. (let ((l (length string)))
  10968. (mapcar (lambda (x)
  10969. (let ((r (substring x l))
  10970. (f (if (string-match " ([^)]*)$" x)
  10971. (match-string 0 x)
  10972. "")))
  10973. (if (string-match "/" r)
  10974. (concat string (substring r 0 (match-end 0)) f)
  10975. x)))
  10976. (all-completions string thetable predicate))))
  10977. ;; Exact match?
  10978. ((eq flag 'lambda) (assoc string thetable))))
  10979. args)))
  10980. ;;;; Dynamic blocks
  10981. (defun org-find-dblock (name)
  10982. "Find the first dynamic block with name NAME in the buffer.
  10983. If not found, stay at current position and return nil."
  10984. (let ((case-fold-search t) pos)
  10985. (save-excursion
  10986. (goto-char (point-min))
  10987. (setq pos (and (re-search-forward
  10988. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10989. (match-beginning 0))))
  10990. (if pos (goto-char pos))
  10991. pos))
  10992. (defun org-create-dblock (plist)
  10993. "Create a dynamic block section, with parameters taken from PLIST.
  10994. PLIST must contain a :name entry which is used as the name of the block."
  10995. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10996. (end-of-line 1)
  10997. (newline))
  10998. (let ((col (current-column))
  10999. (name (plist-get plist :name)))
  11000. (insert "#+BEGIN: " name)
  11001. (while plist
  11002. (if (eq (car plist) :name)
  11003. (setq plist (cddr plist))
  11004. (insert " " (prin1-to-string (pop plist)))))
  11005. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  11006. (beginning-of-line -2)))
  11007. (defun org-prepare-dblock ()
  11008. "Prepare dynamic block for refresh.
  11009. This empties the block, puts the cursor at the insert position and returns
  11010. the property list including an extra property :name with the block name."
  11011. (unless (looking-at org-dblock-start-re)
  11012. (user-error "Not at a dynamic block"))
  11013. (let* ((begdel (1+ (match-end 0)))
  11014. (name (org-no-properties (match-string 1)))
  11015. (params (append (list :name name)
  11016. (read (concat "(" (match-string 3) ")")))))
  11017. (save-excursion
  11018. (beginning-of-line 1)
  11019. (skip-chars-forward " \t")
  11020. (setq params (plist-put params :indentation-column (current-column))))
  11021. (unless (re-search-forward org-dblock-end-re nil t)
  11022. (error "Dynamic block not terminated"))
  11023. (setq params
  11024. (append params
  11025. (list :content (buffer-substring
  11026. begdel (match-beginning 0)))))
  11027. (delete-region begdel (match-beginning 0))
  11028. (goto-char begdel)
  11029. (open-line 1)
  11030. params))
  11031. (defun org-map-dblocks (&optional command)
  11032. "Apply COMMAND to all dynamic blocks in the current buffer.
  11033. If COMMAND is not given, use `org-update-dblock'."
  11034. (let ((cmd (or command 'org-update-dblock)))
  11035. (save-excursion
  11036. (goto-char (point-min))
  11037. (while (re-search-forward org-dblock-start-re nil t)
  11038. (goto-char (match-beginning 0))
  11039. (save-excursion
  11040. (condition-case nil
  11041. (funcall cmd)
  11042. (error (message "Error during update of dynamic block"))))
  11043. (unless (re-search-forward org-dblock-end-re nil t)
  11044. (error "Dynamic block not terminated"))))))
  11045. (defun org-dblock-update (&optional arg)
  11046. "User command for updating dynamic blocks.
  11047. Update the dynamic block at point. With prefix ARG, update all dynamic
  11048. blocks in the buffer."
  11049. (interactive "P")
  11050. (if arg
  11051. (org-update-all-dblocks)
  11052. (or (looking-at org-dblock-start-re)
  11053. (org-beginning-of-dblock))
  11054. (org-update-dblock)))
  11055. (defun org-update-dblock ()
  11056. "Update the dynamic block at point.
  11057. This means to empty the block, parse for parameters and then call
  11058. the correct writing function."
  11059. (interactive)
  11060. (save-excursion
  11061. (let* ((win (selected-window))
  11062. (pos (point))
  11063. (line (org-current-line))
  11064. (params (org-prepare-dblock))
  11065. (name (plist-get params :name))
  11066. (indent (plist-get params :indentation-column))
  11067. (cmd (intern (concat "org-dblock-write:" name))))
  11068. (message "Updating dynamic block `%s' at line %d..." name line)
  11069. (funcall cmd params)
  11070. (message "Updating dynamic block `%s' at line %d...done" name line)
  11071. (goto-char pos)
  11072. (when (and indent (> indent 0))
  11073. (setq indent (make-string indent ?\ ))
  11074. (save-excursion
  11075. (select-window win)
  11076. (org-beginning-of-dblock)
  11077. (forward-line 1)
  11078. (while (not (looking-at org-dblock-end-re))
  11079. (insert indent)
  11080. (beginning-of-line 2))
  11081. (when (looking-at org-dblock-end-re)
  11082. (and (looking-at "[ \t]+")
  11083. (replace-match ""))
  11084. (insert indent)))))))
  11085. (defun org-beginning-of-dblock ()
  11086. "Find the beginning of the dynamic block at point.
  11087. Error if there is no such block at point."
  11088. (let ((pos (point))
  11089. beg)
  11090. (end-of-line 1)
  11091. (if (and (re-search-backward org-dblock-start-re nil t)
  11092. (setq beg (match-beginning 0))
  11093. (re-search-forward org-dblock-end-re nil t)
  11094. (> (match-end 0) pos))
  11095. (goto-char beg)
  11096. (goto-char pos)
  11097. (error "Not in a dynamic block"))))
  11098. (defun org-update-all-dblocks ()
  11099. "Update all dynamic blocks in the buffer.
  11100. This function can be used in a hook."
  11101. (interactive)
  11102. (when (derived-mode-p 'org-mode)
  11103. (org-map-dblocks 'org-update-dblock)))
  11104. ;;;; Completion
  11105. (declare-function org-export-backend-name "org-export" (cl-x))
  11106. (declare-function org-export-backend-options "org-export" (cl-x))
  11107. (defun org-get-export-keywords ()
  11108. "Return a list of all currently understood export keywords.
  11109. Export keywords include options, block names, attributes and
  11110. keywords relative to each registered export back-end."
  11111. (let (keywords)
  11112. (dolist (backend
  11113. (org-bound-and-true-p org-export-registered-backends)
  11114. (delq nil keywords))
  11115. ;; Back-end name (for keywords, like #+LATEX:)
  11116. (push (upcase (symbol-name (org-export-backend-name backend))) keywords)
  11117. (dolist (option-entry (org-export-backend-options backend))
  11118. ;; Back-end options.
  11119. (push (nth 1 option-entry) keywords)))))
  11120. (defconst org-options-keywords
  11121. '("ARCHIVE:" "AUTHOR:" "BIND:" "CATEGORY:" "COLUMNS:" "CREATOR:" "DATE:"
  11122. "DESCRIPTION:" "DRAWERS:" "EMAIL:" "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:"
  11123. "INDEX:" "KEYWORDS:" "LANGUAGE:" "MACRO:" "OPTIONS:" "PROPERTY:"
  11124. "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
  11125. "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:"))
  11126. (defcustom org-structure-template-alist
  11127. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC")
  11128. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE")
  11129. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE")
  11130. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE")
  11131. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM")
  11132. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER")
  11133. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX")
  11134. ("L" "#+LaTeX: ")
  11135. ("h" "#+BEGIN_HTML\n?\n#+END_HTML")
  11136. ("H" "#+HTML: ")
  11137. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
  11138. ("A" "#+ASCII: ")
  11139. ("i" "#+INDEX: ?")
  11140. ("I" "#+INCLUDE: %file ?"))
  11141. "Structure completion elements.
  11142. This is a list of abbreviation keys and values. The value gets inserted
  11143. if you type `<' followed by the key and then press the completion key,
  11144. usually `TAB'. %file will be replaced by a file name after prompting
  11145. for the file using completion. The cursor will be placed at the position
  11146. of the `?' in the template.
  11147. There are two templates for each key, the first uses the original Org syntax,
  11148. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  11149. the default when the /org-mtags.el/ module has been loaded. See also the
  11150. variable `org-mtags-prefer-muse-templates'."
  11151. :group 'org-completion
  11152. :type '(repeat
  11153. (list
  11154. (string :tag "Key")
  11155. (string :tag "Template")))
  11156. :version "25.1"
  11157. :package-version '(Org . "8.3"))
  11158. (defun org-try-structure-completion ()
  11159. "Try to complete a structure template before point.
  11160. This looks for strings like \"<e\" on an otherwise empty line and
  11161. expands them."
  11162. (let ((l (buffer-substring (point-at-bol) (point)))
  11163. a)
  11164. (when (and (looking-at "[ \t]*$")
  11165. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  11166. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  11167. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  11168. (match-beginning 1)) a)
  11169. t)))
  11170. (defun org-complete-expand-structure-template (start cell)
  11171. "Expand a structure template."
  11172. (let ((rpl (nth 1 cell))
  11173. (ind ""))
  11174. (delete-region start (point))
  11175. (when (string-match "\\`[ \t]*#\\+" rpl)
  11176. (cond
  11177. ((bolp))
  11178. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  11179. (setq ind (buffer-substring (point-at-bol) (point))))
  11180. (t (newline))))
  11181. (setq start (point))
  11182. (if (string-match "%file" rpl)
  11183. (setq rpl (replace-match
  11184. (concat
  11185. "\""
  11186. (save-match-data
  11187. (abbreviate-file-name (read-file-name "Include file: ")))
  11188. "\"")
  11189. t t rpl)))
  11190. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  11191. (concat "\n" ind)))
  11192. (insert rpl)
  11193. (if (re-search-backward "\\?" start t) (delete-char 1))))
  11194. ;;;; TODO, DEADLINE, Comments
  11195. (defun org-toggle-comment ()
  11196. "Change the COMMENT state of an entry."
  11197. (interactive)
  11198. (save-excursion
  11199. (org-back-to-heading)
  11200. (looking-at org-complex-heading-regexp)
  11201. (goto-char (or (match-end 3) (match-end 2) (match-end 1)))
  11202. (skip-chars-forward " \t")
  11203. (unless (memq (char-before) '(?\s ?\t)) (insert " "))
  11204. (if (org-in-commented-heading-p t)
  11205. (delete-region (point)
  11206. (progn (search-forward " " (line-end-position) 'move)
  11207. (skip-chars-forward " \t")
  11208. (point)))
  11209. (insert org-comment-string)
  11210. (unless (eolp) (insert " ")))))
  11211. (defvar org-last-todo-state-is-todo nil
  11212. "This is non-nil when the last TODO state change led to a TODO state.
  11213. If the last change removed the TODO tag or switched to DONE, then
  11214. this is nil.")
  11215. (defvar org-setting-tags nil) ; dynamically skipped
  11216. (defvar org-todo-setup-filter-hook nil
  11217. "Hook for functions that pre-filter todo specs.
  11218. Each function takes a todo spec and returns either nil or the spec
  11219. transformed into canonical form." )
  11220. (defvar org-todo-get-default-hook nil
  11221. "Hook for functions that get a default item for todo.
  11222. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  11223. nil or a string to be used for the todo mark." )
  11224. (defvar org-agenda-headline-snapshot-before-repeat)
  11225. (defun org-current-effective-time ()
  11226. "Return current time adjusted for `org-extend-today-until' variable."
  11227. (let* ((ct (org-current-time))
  11228. (dct (decode-time ct))
  11229. (ct1
  11230. (cond
  11231. (org-use-last-clock-out-time-as-effective-time
  11232. (or (org-clock-get-last-clock-out-time) ct))
  11233. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  11234. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  11235. (t ct))))
  11236. ct1))
  11237. (defun org-todo-yesterday (&optional arg)
  11238. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  11239. (interactive "P")
  11240. (if (eq major-mode 'org-agenda-mode)
  11241. (apply 'org-agenda-todo-yesterday arg)
  11242. (let* ((org-use-effective-time t)
  11243. (hour (third (decode-time
  11244. (org-current-time))))
  11245. (org-extend-today-until (1+ hour)))
  11246. (org-todo arg))))
  11247. (defvar org-block-entry-blocking ""
  11248. "First entry preventing the TODO state change.")
  11249. (defun org-cancel-repeater ()
  11250. "Cancel a repeater by setting its numeric value to zero."
  11251. (interactive)
  11252. (save-excursion
  11253. (org-back-to-heading t)
  11254. (let ((bound1 (point))
  11255. (bound0 (save-excursion (outline-next-heading) (point))))
  11256. (when (re-search-forward
  11257. (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11258. org-deadline-time-regexp "\\)\\|\\("
  11259. org-ts-regexp "\\)")
  11260. bound0 t)
  11261. (if (re-search-backward "[ \t]+\\(?:[.+]\\)?\\+\\([0-9]+\\)[hdwmy]" bound1 t)
  11262. (replace-match "0" t nil nil 1))))))
  11263. (defun org-todo (&optional arg)
  11264. "Change the TODO state of an item.
  11265. The state of an item is given by a keyword at the start of the heading,
  11266. like
  11267. *** TODO Write paper
  11268. *** DONE Call mom
  11269. The different keywords are specified in the variable `org-todo-keywords'.
  11270. By default the available states are \"TODO\" and \"DONE\".
  11271. So for this example: when the item starts with TODO, it is changed to DONE.
  11272. When it starts with DONE, the DONE is removed. And when neither TODO nor
  11273. DONE are present, add TODO at the beginning of the heading.
  11274. With \\[universal-argument] prefix arg, use completion to determine the new \
  11275. state.
  11276. With numeric prefix arg, switch to that state.
  11277. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  11278. keywords (nextset).
  11279. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  11280. With a numeric prefix arg of 0, inhibit note taking for the change.
  11281. With a numeric prefix arg of -1, cancel repeater to allow marking as DONE.
  11282. When called through ELisp, arg is also interpreted in the following way:
  11283. 'none -> empty state
  11284. \"\"(empty string) -> switch to empty state
  11285. 'done -> switch to DONE
  11286. 'nextset -> switch to the next set of keywords
  11287. 'previousset -> switch to the previous set of keywords
  11288. \"WAITING\" -> switch to the specified keyword, but only if it
  11289. really is a member of `org-todo-keywords'."
  11290. (interactive "P")
  11291. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11292. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11293. 'region-start-level 'region))
  11294. org-loop-over-headlines-in-active-region)
  11295. (org-map-entries
  11296. `(org-todo ,arg)
  11297. org-loop-over-headlines-in-active-region
  11298. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11299. (if (equal arg '(16)) (setq arg 'nextset))
  11300. (when (equal arg -1) (org-cancel-repeater) (setq arg nil))
  11301. (let ((org-blocker-hook org-blocker-hook)
  11302. commentp
  11303. case-fold-search)
  11304. (when (equal arg '(64))
  11305. (setq arg nil org-blocker-hook nil))
  11306. (when (and org-blocker-hook
  11307. (or org-inhibit-blocking
  11308. (org-entry-get nil "NOBLOCKING")))
  11309. (setq org-blocker-hook nil))
  11310. (save-excursion
  11311. (catch 'exit
  11312. (org-back-to-heading t)
  11313. (when (org-in-commented-heading-p t)
  11314. (org-toggle-comment)
  11315. (setq commentp t))
  11316. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  11317. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  11318. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  11319. (let* ((match-data (match-data))
  11320. (startpos (point-at-bol))
  11321. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  11322. (org-log-done org-log-done)
  11323. (org-log-repeat org-log-repeat)
  11324. (org-todo-log-states org-todo-log-states)
  11325. (org-inhibit-logging
  11326. (if (equal arg 0)
  11327. (progn (setq arg nil) 'note) org-inhibit-logging))
  11328. (this (match-string 1))
  11329. (hl-pos (match-beginning 0))
  11330. (head (org-get-todo-sequence-head this))
  11331. (ass (assoc head org-todo-kwd-alist))
  11332. (interpret (nth 1 ass))
  11333. (done-word (nth 3 ass))
  11334. (final-done-word (nth 4 ass))
  11335. (org-last-state (or this ""))
  11336. (completion-ignore-case t)
  11337. (member (member this org-todo-keywords-1))
  11338. (tail (cdr member))
  11339. (org-state (cond
  11340. ((and org-todo-key-trigger
  11341. (or (and (equal arg '(4))
  11342. (eq org-use-fast-todo-selection 'prefix))
  11343. (and (not arg) org-use-fast-todo-selection
  11344. (not (eq org-use-fast-todo-selection
  11345. 'prefix)))))
  11346. ;; Use fast selection
  11347. (org-fast-todo-selection))
  11348. ((and (equal arg '(4))
  11349. (or (not org-use-fast-todo-selection)
  11350. (not org-todo-key-trigger)))
  11351. ;; Read a state with completion
  11352. (org-icompleting-read
  11353. "State: " (mapcar 'list org-todo-keywords-1)
  11354. nil t))
  11355. ((eq arg 'right)
  11356. (if this
  11357. (if tail (car tail) nil)
  11358. (car org-todo-keywords-1)))
  11359. ((eq arg 'left)
  11360. (if (equal member org-todo-keywords-1)
  11361. nil
  11362. (if this
  11363. (nth (- (length org-todo-keywords-1)
  11364. (length tail) 2)
  11365. org-todo-keywords-1)
  11366. (org-last org-todo-keywords-1))))
  11367. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  11368. (setq arg nil))) ; hack to fall back to cycling
  11369. (arg
  11370. ;; user or caller requests a specific state
  11371. (cond
  11372. ((equal arg "") nil)
  11373. ((eq arg 'none) nil)
  11374. ((eq arg 'done) (or done-word (car org-done-keywords)))
  11375. ((eq arg 'nextset)
  11376. (or (car (cdr (member head org-todo-heads)))
  11377. (car org-todo-heads)))
  11378. ((eq arg 'previousset)
  11379. (let ((org-todo-heads (reverse org-todo-heads)))
  11380. (or (car (cdr (member head org-todo-heads)))
  11381. (car org-todo-heads))))
  11382. ((car (member arg org-todo-keywords-1)))
  11383. ((stringp arg)
  11384. (user-error "State `%s' not valid in this file" arg))
  11385. ((nth (1- (prefix-numeric-value arg))
  11386. org-todo-keywords-1))))
  11387. ((null member) (or head (car org-todo-keywords-1)))
  11388. ((equal this final-done-word) nil) ;; -> make empty
  11389. ((null tail) nil) ;; -> first entry
  11390. ((memq interpret '(type priority))
  11391. (if (eq this-command last-command)
  11392. (car tail)
  11393. (if (> (length tail) 0)
  11394. (or done-word (car org-done-keywords))
  11395. nil)))
  11396. (t
  11397. (car tail))))
  11398. (org-state (or
  11399. (run-hook-with-args-until-success
  11400. 'org-todo-get-default-hook org-state org-last-state)
  11401. org-state))
  11402. (next (if org-state (concat " " org-state " ") " "))
  11403. (change-plist (list :type 'todo-state-change :from this :to org-state
  11404. :position startpos))
  11405. dolog now-done-p)
  11406. (when org-blocker-hook
  11407. (setq org-last-todo-state-is-todo
  11408. (not (member this org-done-keywords)))
  11409. (unless (save-excursion
  11410. (save-match-data
  11411. (org-with-wide-buffer
  11412. (run-hook-with-args-until-failure
  11413. 'org-blocker-hook change-plist))))
  11414. (if (org-called-interactively-p 'interactive)
  11415. (user-error "TODO state change from %s to %s blocked (by \"%s\")"
  11416. this org-state org-block-entry-blocking)
  11417. ;; fail silently
  11418. (message "TODO state change from %s to %s blocked (by \"%s\")"
  11419. this org-state org-block-entry-blocking)
  11420. (throw 'exit nil))))
  11421. (store-match-data match-data)
  11422. (replace-match next t t)
  11423. (cond ((equal this org-state)
  11424. (message "TODO state was already %s" (org-trim next)))
  11425. ((pos-visible-in-window-p hl-pos)
  11426. (message "TODO state changed to %s" (org-trim next))))
  11427. (unless head
  11428. (setq head (org-get-todo-sequence-head org-state)
  11429. ass (assoc head org-todo-kwd-alist)
  11430. interpret (nth 1 ass)
  11431. done-word (nth 3 ass)
  11432. final-done-word (nth 4 ass)))
  11433. (when (memq arg '(nextset previousset))
  11434. (message "Keyword-Set %d/%d: %s"
  11435. (- (length org-todo-sets) -1
  11436. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  11437. (length org-todo-sets)
  11438. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  11439. (setq org-last-todo-state-is-todo
  11440. (not (member org-state org-done-keywords)))
  11441. (setq now-done-p (and (member org-state org-done-keywords)
  11442. (not (member this org-done-keywords))))
  11443. (and logging (org-local-logging logging))
  11444. (when (and (or org-todo-log-states org-log-done)
  11445. (not (eq org-inhibit-logging t))
  11446. (not (memq arg '(nextset previousset))))
  11447. ;; we need to look at recording a time and note
  11448. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  11449. (nth 2 (assoc this org-todo-log-states))))
  11450. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  11451. (setq dolog 'time))
  11452. (when (or (and (not org-state) (not org-closed-keep-when-no-todo))
  11453. (and org-state
  11454. (member org-state org-not-done-keywords)
  11455. (not (member this org-not-done-keywords))))
  11456. ;; This is now a todo state and was not one before
  11457. ;; If there was a CLOSED time stamp, get rid of it.
  11458. (org-add-planning-info nil nil 'closed))
  11459. (when (and now-done-p org-log-done)
  11460. ;; It is now done, and it was not done before
  11461. (org-add-planning-info 'closed (org-current-effective-time))
  11462. (if (and (not dolog) (eq 'note org-log-done))
  11463. (org-add-log-setup 'done org-state this 'findpos 'note)))
  11464. (when (and org-state dolog)
  11465. ;; This is a non-nil state, and we need to log it
  11466. (org-add-log-setup 'state org-state this 'findpos dolog)))
  11467. ;; Fixup tag positioning
  11468. (org-todo-trigger-tag-changes org-state)
  11469. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  11470. (when org-provide-todo-statistics
  11471. (org-update-parent-todo-statistics))
  11472. (run-hooks 'org-after-todo-state-change-hook)
  11473. (if (and arg (not (member org-state org-done-keywords)))
  11474. (setq head (org-get-todo-sequence-head org-state)))
  11475. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  11476. ;; Do we need to trigger a repeat?
  11477. (when now-done-p
  11478. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  11479. ;; This is for the agenda, take a snapshot of the headline.
  11480. (save-match-data
  11481. (setq org-agenda-headline-snapshot-before-repeat
  11482. (org-get-heading))))
  11483. (org-auto-repeat-maybe org-state))
  11484. ;; Fixup cursor location if close to the keyword
  11485. (if (and (outline-on-heading-p)
  11486. (not (bolp))
  11487. (save-excursion (beginning-of-line 1)
  11488. (looking-at org-todo-line-regexp))
  11489. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  11490. (progn
  11491. (goto-char (or (match-end 2) (match-end 1)))
  11492. (and (looking-at " ") (just-one-space))))
  11493. (when org-trigger-hook
  11494. (save-excursion
  11495. (run-hook-with-args 'org-trigger-hook change-plist)))
  11496. (when commentp (org-toggle-comment))))))))
  11497. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  11498. "Block turning an entry into a TODO, using the hierarchy.
  11499. This checks whether the current task should be blocked from state
  11500. changes. Such blocking occurs when:
  11501. 1. The task has children which are not all in a completed state.
  11502. 2. A task has a parent with the property :ORDERED:, and there
  11503. are siblings prior to the current task with incomplete
  11504. status.
  11505. 3. The parent of the task is blocked because it has siblings that should
  11506. be done first, or is child of a block grandparent TODO entry."
  11507. (if (not org-enforce-todo-dependencies)
  11508. t ; if locally turned off don't block
  11509. (catch 'dont-block
  11510. ;; If this is not a todo state change, or if this entry is already DONE,
  11511. ;; do not block
  11512. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11513. (member (plist-get change-plist :from)
  11514. (cons 'done org-done-keywords))
  11515. (member (plist-get change-plist :to)
  11516. (cons 'todo org-not-done-keywords))
  11517. (not (plist-get change-plist :to)))
  11518. (throw 'dont-block t))
  11519. ;; If this task has children, and any are undone, it's blocked
  11520. (save-excursion
  11521. (org-back-to-heading t)
  11522. (let ((this-level (funcall outline-level)))
  11523. (outline-next-heading)
  11524. (let ((child-level (funcall outline-level)))
  11525. (while (and (not (eobp))
  11526. (> child-level this-level))
  11527. ;; this todo has children, check whether they are all
  11528. ;; completed
  11529. (if (and (not (org-entry-is-done-p))
  11530. (org-entry-is-todo-p))
  11531. (progn (setq org-block-entry-blocking (org-get-heading))
  11532. (throw 'dont-block nil)))
  11533. (outline-next-heading)
  11534. (setq child-level (funcall outline-level))))))
  11535. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  11536. ;; any previous siblings are undone, it's blocked
  11537. (save-excursion
  11538. (org-back-to-heading t)
  11539. (let* ((pos (point))
  11540. (parent-pos (and (org-up-heading-safe) (point))))
  11541. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11542. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11543. (forward-line 1)
  11544. (re-search-forward org-not-done-heading-regexp pos t))
  11545. (setq org-block-entry-blocking (match-string 0))
  11546. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  11547. ;; Search further up the hierarchy, to see if an ancestor is blocked
  11548. (while t
  11549. (goto-char parent-pos)
  11550. (if (not (looking-at org-not-done-heading-regexp))
  11551. (throw 'dont-block t)) ; do not block, parent is not a TODO
  11552. (setq pos (point))
  11553. (setq parent-pos (and (org-up-heading-safe) (point)))
  11554. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11555. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11556. (forward-line 1)
  11557. (re-search-forward org-not-done-heading-regexp pos t)
  11558. (setq org-block-entry-blocking (org-get-heading)))
  11559. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  11560. (defcustom org-track-ordered-property-with-tag nil
  11561. "Should the ORDERED property also be shown as a tag?
  11562. The ORDERED property decides if an entry should require subtasks to be
  11563. completed in sequence. Since a property is not very visible, setting
  11564. this option means that toggling the ORDERED property with the command
  11565. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  11566. not relevant for the behavior, but it makes things more visible.
  11567. Note that toggling the tag with tags commands will not change the property
  11568. and therefore not influence behavior!
  11569. This can be t, meaning the tag ORDERED should be used, It can also be a
  11570. string to select a different tag for this task."
  11571. :group 'org-todo
  11572. :type '(choice
  11573. (const :tag "No tracking" nil)
  11574. (const :tag "Track with ORDERED tag" t)
  11575. (string :tag "Use other tag")))
  11576. (defun org-toggle-ordered-property ()
  11577. "Toggle the ORDERED property of the current entry.
  11578. For better visibility, you can track the value of this property with a tag.
  11579. See variable `org-track-ordered-property-with-tag'."
  11580. (interactive)
  11581. (let* ((t1 org-track-ordered-property-with-tag)
  11582. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  11583. (save-excursion
  11584. (org-back-to-heading)
  11585. (if (org-entry-get nil "ORDERED")
  11586. (progn
  11587. (org-delete-property "ORDERED")
  11588. (and tag (org-toggle-tag tag 'off))
  11589. (message "Subtasks can be completed in arbitrary order"))
  11590. (org-entry-put nil "ORDERED" "t")
  11591. (and tag (org-toggle-tag tag 'on))
  11592. (message "Subtasks must be completed in sequence")))))
  11593. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  11594. (defun org-block-todo-from-checkboxes (change-plist)
  11595. "Block turning an entry into a TODO, using checkboxes.
  11596. This checks whether the current task should be blocked from state
  11597. changes because there are unchecked boxes in this entry."
  11598. (if (not org-enforce-todo-checkbox-dependencies)
  11599. t ; if locally turned off don't block
  11600. (catch 'dont-block
  11601. ;; If this is not a todo state change, or if this entry is already DONE,
  11602. ;; do not block
  11603. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11604. (member (plist-get change-plist :from)
  11605. (cons 'done org-done-keywords))
  11606. (member (plist-get change-plist :to)
  11607. (cons 'todo org-not-done-keywords))
  11608. (not (plist-get change-plist :to)))
  11609. (throw 'dont-block t))
  11610. ;; If this task has checkboxes that are not checked, it's blocked
  11611. (save-excursion
  11612. (org-back-to-heading t)
  11613. (let ((beg (point)) end)
  11614. (outline-next-heading)
  11615. (setq end (point))
  11616. (goto-char beg)
  11617. (if (org-list-search-forward
  11618. (concat (org-item-beginning-re)
  11619. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  11620. "\\[[- ]\\]")
  11621. end t)
  11622. (progn
  11623. (if (boundp 'org-blocked-by-checkboxes)
  11624. (setq org-blocked-by-checkboxes t))
  11625. (throw 'dont-block nil)))))
  11626. t))) ; do not block
  11627. (defun org-entry-blocked-p ()
  11628. "Non-nil if entry at point is blocked."
  11629. (and (not (org-entry-get nil "NOBLOCKING"))
  11630. (member (org-entry-get nil "TODO") org-not-done-keywords)
  11631. (not (run-hook-with-args-until-failure
  11632. 'org-blocker-hook
  11633. (list :type 'todo-state-change
  11634. :position (point)
  11635. :from 'todo
  11636. :to 'done)))))
  11637. (defun org-update-statistics-cookies (all)
  11638. "Update the statistics cookie, either from TODO or from checkboxes.
  11639. This should be called with the cursor in a line with a statistics cookie."
  11640. (interactive "P")
  11641. (if all
  11642. (progn
  11643. (org-update-checkbox-count 'all)
  11644. (org-map-entries 'org-update-parent-todo-statistics))
  11645. (if (not (org-at-heading-p))
  11646. (org-update-checkbox-count)
  11647. (let ((pos (point-marker))
  11648. end l1 l2)
  11649. (ignore-errors (org-back-to-heading t))
  11650. (if (not (org-at-heading-p))
  11651. (org-update-checkbox-count)
  11652. (setq l1 (org-outline-level))
  11653. (setq end (save-excursion
  11654. (outline-next-heading)
  11655. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  11656. (point)))
  11657. (if (and (save-excursion
  11658. (re-search-forward
  11659. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  11660. (not (save-excursion (re-search-forward
  11661. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  11662. (org-update-checkbox-count)
  11663. (if (and l2 (> l2 l1))
  11664. (progn
  11665. (goto-char end)
  11666. (org-update-parent-todo-statistics))
  11667. (goto-char pos)
  11668. (beginning-of-line 1)
  11669. (while (re-search-forward
  11670. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  11671. (point-at-eol) t)
  11672. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  11673. (goto-char pos)
  11674. (move-marker pos nil)))))
  11675. (defvar org-entry-property-inherited-from) ;; defined below
  11676. (defun org-update-parent-todo-statistics ()
  11677. "Update any statistics cookie in the parent of the current headline.
  11678. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  11679. the entire subtree and this will travel up the hierarchy and update
  11680. statistics everywhere."
  11681. (let* ((prop (save-excursion (org-up-heading-safe)
  11682. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  11683. (recursive (or (not org-hierarchical-todo-statistics)
  11684. (and prop (string-match "\\<recursive\\>" prop))))
  11685. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  11686. 0))
  11687. (first t)
  11688. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  11689. level ltoggle l1 new ndel
  11690. (cnt-all 0) (cnt-done 0) is-percent kwd
  11691. checkbox-beg ov ovs ove cookie-present)
  11692. (catch 'exit
  11693. (save-excursion
  11694. (beginning-of-line 1)
  11695. (setq ltoggle (funcall outline-level))
  11696. ;; Three situations are to consider:
  11697. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  11698. ;; to the top-level ancestor on the headline;
  11699. ;; 2. If parent has "recursive" property, repeat up to the
  11700. ;; headline setting that property, taking inheritance into
  11701. ;; account;
  11702. ;; 3. Else, move up to direct parent and proceed only once.
  11703. (while (and (setq level (org-up-heading-safe))
  11704. (or recursive first)
  11705. (>= (point) lim))
  11706. (setq first nil cookie-present nil)
  11707. (unless (and level
  11708. (not (string-match
  11709. "\\<checkbox\\>"
  11710. (downcase (or (org-entry-get nil "COOKIE_DATA")
  11711. "")))))
  11712. (throw 'exit nil))
  11713. (while (re-search-forward box-re (point-at-eol) t)
  11714. (setq cnt-all 0 cnt-done 0 cookie-present t)
  11715. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  11716. (save-match-data
  11717. (unless (outline-next-heading) (throw 'exit nil))
  11718. (while (and (looking-at org-complex-heading-regexp)
  11719. (> (setq l1 (length (match-string 1))) level))
  11720. (setq kwd (and (or recursive (= l1 ltoggle))
  11721. (match-string 2)))
  11722. (if (or (eq org-provide-todo-statistics 'all-headlines)
  11723. (and (eq org-provide-todo-statistics t)
  11724. (or (member kwd org-done-keywords)))
  11725. (and (listp org-provide-todo-statistics)
  11726. (stringp (car org-provide-todo-statistics))
  11727. (or (member kwd org-provide-todo-statistics)
  11728. (member kwd org-done-keywords)))
  11729. (and (listp org-provide-todo-statistics)
  11730. (listp (car org-provide-todo-statistics))
  11731. (or (member kwd (car org-provide-todo-statistics))
  11732. (and (member kwd org-done-keywords)
  11733. (member kwd (cadr org-provide-todo-statistics))))))
  11734. (setq cnt-all (1+ cnt-all))
  11735. (if (eq org-provide-todo-statistics t)
  11736. (and kwd (setq cnt-all (1+ cnt-all)))))
  11737. (when (or (and (member org-provide-todo-statistics '(t all-headlines))
  11738. (member kwd org-done-keywords))
  11739. (and (listp org-provide-todo-statistics)
  11740. (listp (car org-provide-todo-statistics))
  11741. (member kwd org-done-keywords)
  11742. (member kwd (cadr org-provide-todo-statistics)))
  11743. (and (listp org-provide-todo-statistics)
  11744. (stringp (car org-provide-todo-statistics))
  11745. (member kwd org-done-keywords)))
  11746. (setq cnt-done (1+ cnt-done)))
  11747. (outline-next-heading)))
  11748. (setq new
  11749. (if is-percent
  11750. (format "[%d%%]" (floor (* 100.0 cnt-done)
  11751. (max 1 cnt-all)))
  11752. (format "[%d/%d]" cnt-done cnt-all))
  11753. ndel (- (match-end 0) checkbox-beg))
  11754. ;; handle overlays when updating cookie from column view
  11755. (when (setq ov (car (overlays-at checkbox-beg)))
  11756. (setq ovs (overlay-start ov) ove (overlay-end ov))
  11757. (delete-overlay ov))
  11758. (goto-char checkbox-beg)
  11759. (insert new)
  11760. (delete-region (point) (+ (point) ndel))
  11761. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  11762. (when ov (move-overlay ov ovs ove)))
  11763. (when cookie-present
  11764. (run-hook-with-args 'org-after-todo-statistics-hook
  11765. cnt-done (- cnt-all cnt-done))))))
  11766. (run-hooks 'org-todo-statistics-hook)))
  11767. (defvar org-after-todo-statistics-hook nil
  11768. "Hook that is called after a TODO statistics cookie has been updated.
  11769. Each function is called with two arguments: the number of not-done entries
  11770. and the number of done entries.
  11771. For example, the following function, when added to this hook, will switch
  11772. an entry to DONE when all children are done, and back to TODO when new
  11773. entries are set to a TODO status. Note that this hook is only called
  11774. when there is a statistics cookie in the headline!
  11775. (defun org-summary-todo (n-done n-not-done)
  11776. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  11777. (let (org-log-done org-log-states) ; turn off logging
  11778. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  11779. ")
  11780. (defvar org-todo-statistics-hook nil
  11781. "Hook that is run whenever Org thinks TODO statistics should be updated.
  11782. This hook runs even if there is no statistics cookie present, in which case
  11783. `org-after-todo-statistics-hook' would not run.")
  11784. (defun org-todo-trigger-tag-changes (state)
  11785. "Apply the changes defined in `org-todo-state-tags-triggers'."
  11786. (let ((l org-todo-state-tags-triggers)
  11787. changes)
  11788. (when (or (not state) (equal state ""))
  11789. (setq changes (append changes (cdr (assoc "" l)))))
  11790. (when (and (stringp state) (> (length state) 0))
  11791. (setq changes (append changes (cdr (assoc state l)))))
  11792. (when (member state org-not-done-keywords)
  11793. (setq changes (append changes (cdr (assoc 'todo l)))))
  11794. (when (member state org-done-keywords)
  11795. (setq changes (append changes (cdr (assoc 'done l)))))
  11796. (dolist (c changes)
  11797. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  11798. (defun org-local-logging (value)
  11799. "Get logging settings from a property VALUE."
  11800. ;; Directly set the variables, they are already local.
  11801. (setq org-log-done nil
  11802. org-log-repeat nil
  11803. org-todo-log-states nil)
  11804. (dolist (w (org-split-string value))
  11805. (let (a)
  11806. (cond
  11807. ((setq a (assoc w org-startup-options))
  11808. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  11809. (set (nth 1 a) (nth 2 a))))
  11810. ((setq a (org-extract-log-state-settings w))
  11811. (and (member (car a) org-todo-keywords-1)
  11812. (push a org-todo-log-states)))))))
  11813. (defun org-get-todo-sequence-head (kwd)
  11814. "Return the head of the TODO sequence to which KWD belongs.
  11815. If KWD is not set, check if there is a text property remembering the
  11816. right sequence."
  11817. (let (p)
  11818. (cond
  11819. ((not kwd)
  11820. (or (get-text-property (point-at-bol) 'org-todo-head)
  11821. (progn
  11822. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11823. nil (point-at-eol)))
  11824. (get-text-property p 'org-todo-head))))
  11825. ((not (member kwd org-todo-keywords-1))
  11826. (car org-todo-keywords-1))
  11827. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11828. (defun org-fast-todo-selection ()
  11829. "Fast TODO keyword selection with single keys.
  11830. Returns the new TODO keyword, or nil if no state change should occur."
  11831. (let* ((fulltable org-todo-key-alist)
  11832. (done-keywords org-done-keywords) ;; needed for the faces.
  11833. (maxlen (apply 'max (mapcar
  11834. (lambda (x)
  11835. (if (stringp (car x)) (string-width (car x)) 0))
  11836. fulltable)))
  11837. (expert nil)
  11838. (fwidth (+ maxlen 3 1 3))
  11839. (ncol (/ (- (window-width) 4) fwidth))
  11840. tg cnt e c tbl
  11841. groups ingroup)
  11842. (save-excursion
  11843. (save-window-excursion
  11844. (if expert
  11845. (set-buffer (get-buffer-create " *Org todo*"))
  11846. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11847. (erase-buffer)
  11848. (org-set-local 'org-done-keywords done-keywords)
  11849. (setq tbl fulltable cnt 0)
  11850. (while (setq e (pop tbl))
  11851. (cond
  11852. ((equal e '(:startgroup))
  11853. (push '() groups) (setq ingroup t)
  11854. (when (not (= cnt 0))
  11855. (setq cnt 0)
  11856. (insert "\n"))
  11857. (insert "{ "))
  11858. ((equal e '(:endgroup))
  11859. (setq ingroup nil cnt 0)
  11860. (insert "}\n"))
  11861. ((equal e '(:newline))
  11862. (when (not (= cnt 0))
  11863. (setq cnt 0)
  11864. (insert "\n")
  11865. (setq e (car tbl))
  11866. (while (equal (car tbl) '(:newline))
  11867. (insert "\n")
  11868. (setq tbl (cdr tbl)))))
  11869. (t
  11870. (setq tg (car e) c (cdr e))
  11871. (if ingroup (push tg (car groups)))
  11872. (setq tg (org-add-props tg nil 'face
  11873. (org-get-todo-face tg)))
  11874. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11875. (insert "[" c "] " tg (make-string
  11876. (- fwidth 4 (length tg)) ?\ ))
  11877. (when (= (setq cnt (1+ cnt)) ncol)
  11878. (insert "\n")
  11879. (if ingroup (insert " "))
  11880. (setq cnt 0)))))
  11881. (insert "\n")
  11882. (goto-char (point-min))
  11883. (if (not expert) (org-fit-window-to-buffer))
  11884. (message "[a-z..]:Set [SPC]:clear")
  11885. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11886. (cond
  11887. ((or (= c ?\C-g)
  11888. (and (= c ?q) (not (rassoc c fulltable))))
  11889. (setq quit-flag t))
  11890. ((= c ?\ ) nil)
  11891. ((setq e (rassoc c fulltable) tg (car e))
  11892. tg)
  11893. (t (setq quit-flag t)))))))
  11894. (defun org-entry-is-todo-p ()
  11895. (member (org-get-todo-state) org-not-done-keywords))
  11896. (defun org-entry-is-done-p ()
  11897. (member (org-get-todo-state) org-done-keywords))
  11898. (defun org-get-todo-state ()
  11899. "Return the TODO keyword of the current subtree."
  11900. (save-excursion
  11901. (org-back-to-heading t)
  11902. (and (looking-at org-todo-line-regexp)
  11903. (match-end 2)
  11904. (match-string 2))))
  11905. (defun org-at-date-range-p (&optional inactive-ok)
  11906. "Non-nil if point is inside a date range.
  11907. When optional argument INACTIVE-OK is non-nil, also consider
  11908. inactive time ranges.
  11909. When this function returns a non-nil value, match data is set
  11910. according to `org-tr-regexp-both' or `org-tr-regexp', depending
  11911. on INACTIVE-OK."
  11912. (interactive)
  11913. (save-excursion
  11914. (catch 'exit
  11915. (let ((pos (point)))
  11916. (skip-chars-backward "^[<\r\n")
  11917. (skip-chars-backward "<[")
  11918. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11919. (>= (match-end 0) pos)
  11920. (throw 'exit t))
  11921. (skip-chars-backward "^<[\r\n")
  11922. (skip-chars-backward "<[")
  11923. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11924. (>= (match-end 0) pos)
  11925. (throw 'exit t)))
  11926. nil)))
  11927. (defun org-get-repeat (&optional tagline)
  11928. "Check if there is a deadline/schedule with repeater in this entry."
  11929. (save-match-data
  11930. (save-excursion
  11931. (org-back-to-heading t)
  11932. (and (re-search-forward (if tagline
  11933. (concat tagline "\\s-*" org-repeat-re)
  11934. org-repeat-re)
  11935. (org-entry-end-position) t)
  11936. (match-string-no-properties 1)))))
  11937. (defvar org-last-changed-timestamp)
  11938. (defvar org-last-inserted-timestamp)
  11939. (defvar org-log-post-message)
  11940. (defvar org-log-note-purpose)
  11941. (defvar org-log-note-how nil)
  11942. (defvar org-log-note-extra)
  11943. (defun org-auto-repeat-maybe (done-word)
  11944. "Check if the current headline contains a repeated deadline/schedule.
  11945. If yes, set TODO state back to what it was and change the base date
  11946. of repeating deadline/scheduled time stamps to new date.
  11947. This function is run automatically after each state change to a DONE state."
  11948. ;; last-state is dynamically scoped into this function
  11949. (let* ((repeat (org-get-repeat))
  11950. (aa (assoc org-last-state org-todo-kwd-alist))
  11951. (interpret (nth 1 aa))
  11952. (head (nth 2 aa))
  11953. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11954. (msg "Entry repeats: ")
  11955. (org-log-done nil)
  11956. (org-todo-log-states nil)
  11957. re type n what ts time to-state)
  11958. (when (and repeat (not (zerop (string-to-number (substring repeat 1)))))
  11959. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  11960. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  11961. org-todo-repeat-to-state))
  11962. (unless (and to-state (member to-state org-todo-keywords-1))
  11963. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11964. (org-todo to-state)
  11965. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11966. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11967. (org-time-stamp-format t t))))
  11968. (when org-log-repeat
  11969. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11970. (memq 'org-add-log-note post-command-hook))
  11971. ;; OK, we are already setup for some record
  11972. (if (eq org-log-repeat 'note)
  11973. ;; make sure we take a note, not only a time stamp
  11974. (setq org-log-note-how 'note))
  11975. ;; Set up for taking a record
  11976. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  11977. org-last-state
  11978. 'findpos org-log-repeat)))
  11979. (org-back-to-heading t)
  11980. (org-add-planning-info nil nil 'closed)
  11981. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11982. org-deadline-time-regexp "\\)\\|\\("
  11983. org-ts-regexp "\\)"))
  11984. (while (re-search-forward
  11985. re (save-excursion (outline-next-heading) (point)) t)
  11986. (setq type (if (match-end 1) org-scheduled-string
  11987. (if (match-end 3) org-deadline-string "Plain:"))
  11988. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  11989. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  11990. (setq n (string-to-number (match-string 2 ts))
  11991. what (match-string 3 ts))
  11992. (if (equal what "w") (setq n (* n 7) what "d"))
  11993. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  11994. (user-error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  11995. ;; Preparation, see if we need to modify the start date for the change
  11996. (when (match-end 1)
  11997. (setq time (save-match-data (org-time-string-to-time ts)))
  11998. (cond
  11999. ((equal (match-string 1 ts) ".")
  12000. ;; Shift starting date to today
  12001. (org-timestamp-change
  12002. (- (org-today) (time-to-days time))
  12003. 'day))
  12004. ((equal (match-string 1 ts) "+")
  12005. (let ((nshiftmax 10) (nshift 0))
  12006. (while (or (= nshift 0)
  12007. (<= (time-to-days time)
  12008. (time-to-days (current-time))))
  12009. (when (= (incf nshift) nshiftmax)
  12010. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  12011. (user-error "Abort")))
  12012. (org-timestamp-change n (cdr (assoc what whata)))
  12013. (org-at-timestamp-p t)
  12014. (setq ts (match-string 1))
  12015. (setq time (save-match-data (org-time-string-to-time ts)))))
  12016. (org-timestamp-change (- n) (cdr (assoc what whata)))
  12017. ;; rematch, so that we have everything in place for the real shift
  12018. (org-at-timestamp-p t)
  12019. (setq ts (match-string 1))
  12020. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  12021. (save-excursion (org-timestamp-change n (cdr (assoc what whata)) nil t))
  12022. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  12023. (setq org-log-post-message msg)
  12024. (message "%s" msg))))
  12025. (defun org-show-todo-tree (arg)
  12026. "Make a compact tree which shows all headlines marked with TODO.
  12027. The tree will show the lines where the regexp matches, and all higher
  12028. headlines above the match.
  12029. With a \\[universal-argument] prefix, prompt for a regexp to match.
  12030. With a numeric prefix N, construct a sparse tree for the Nth element
  12031. of `org-todo-keywords-1'."
  12032. (interactive "P")
  12033. (let ((case-fold-search nil)
  12034. (kwd-re
  12035. (cond ((null arg) org-not-done-regexp)
  12036. ((equal arg '(4))
  12037. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  12038. (mapcar 'list org-todo-keywords-1))))
  12039. (concat "\\("
  12040. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  12041. "\\)\\>")))
  12042. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  12043. (regexp-quote (nth (1- (prefix-numeric-value arg))
  12044. org-todo-keywords-1)))
  12045. (t (user-error "Invalid prefix argument: %s" arg)))))
  12046. (message "%d TODO entries found"
  12047. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  12048. (defun org-deadline (arg &optional time)
  12049. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  12050. With one universal prefix argument, remove any deadline from the item.
  12051. With two universal prefix arguments, prompt for a warning delay.
  12052. With argument TIME, set the deadline at the corresponding date. TIME
  12053. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  12054. (interactive "P")
  12055. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12056. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12057. 'region-start-level 'region))
  12058. org-loop-over-headlines-in-active-region)
  12059. (org-map-entries
  12060. `(org-deadline ',arg ,time)
  12061. org-loop-over-headlines-in-active-region
  12062. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  12063. (let* ((old-date (org-entry-get nil "DEADLINE"))
  12064. (old-date-time (if old-date (org-time-string-to-time old-date)))
  12065. (repeater (and old-date
  12066. (string-match
  12067. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  12068. old-date)
  12069. (match-string 1 old-date))))
  12070. (cond
  12071. ((equal arg '(4))
  12072. (when (and old-date org-log-redeadline)
  12073. (org-add-log-setup 'deldeadline nil old-date 'findpos
  12074. org-log-redeadline))
  12075. (org-remove-timestamp-with-keyword org-deadline-string)
  12076. (message "Item no longer has a deadline."))
  12077. ((equal arg '(16))
  12078. (save-excursion
  12079. (org-back-to-heading t)
  12080. (if (re-search-forward
  12081. org-deadline-time-regexp
  12082. (save-excursion (outline-next-heading) (point)) t)
  12083. (let* ((rpl0 (match-string 1))
  12084. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  12085. (replace-match
  12086. (concat org-deadline-string
  12087. " <" rpl
  12088. (format " -%dd"
  12089. (abs
  12090. (- (time-to-days
  12091. (save-match-data
  12092. (org-read-date nil t nil "Warn starting from" old-date-time)))
  12093. (time-to-days old-date-time))))
  12094. ">") t t))
  12095. (user-error "No deadline information to update"))))
  12096. (t
  12097. (org-add-planning-info 'deadline time 'closed)
  12098. (when (and old-date
  12099. org-log-redeadline
  12100. (not (equal old-date org-last-inserted-timestamp)))
  12101. (org-add-log-setup
  12102. 'redeadline org-last-inserted-timestamp old-date 'findpos
  12103. org-log-redeadline))
  12104. (when repeater
  12105. (save-excursion
  12106. (org-back-to-heading t)
  12107. (when (re-search-forward (concat org-deadline-string " "
  12108. org-last-inserted-timestamp)
  12109. (save-excursion
  12110. (outline-next-heading) (point)) t)
  12111. (goto-char (1- (match-end 0)))
  12112. (insert " " repeater)
  12113. (setq org-last-inserted-timestamp
  12114. (concat (substring org-last-inserted-timestamp 0 -1)
  12115. " " repeater
  12116. (substring org-last-inserted-timestamp -1))))))
  12117. (message "Deadline on %s" org-last-inserted-timestamp))))))
  12118. (defun org-schedule (arg &optional time)
  12119. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  12120. With one universal prefix argument, remove any scheduling date from the item.
  12121. With two universal prefix arguments, prompt for a delay cookie.
  12122. With argument TIME, scheduled at the corresponding date. TIME can
  12123. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  12124. (interactive "P")
  12125. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12126. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12127. 'region-start-level 'region))
  12128. org-loop-over-headlines-in-active-region)
  12129. (org-map-entries
  12130. `(org-schedule ',arg ,time)
  12131. org-loop-over-headlines-in-active-region
  12132. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  12133. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  12134. (old-date-time (if old-date (org-time-string-to-time old-date)))
  12135. (repeater (and old-date
  12136. (string-match
  12137. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  12138. old-date)
  12139. (match-string 1 old-date))))
  12140. (cond
  12141. ((equal arg '(4))
  12142. (progn
  12143. (when (and old-date org-log-reschedule)
  12144. (org-add-log-setup 'delschedule nil old-date 'findpos
  12145. org-log-reschedule))
  12146. (org-remove-timestamp-with-keyword org-scheduled-string)
  12147. (message "Item is no longer scheduled.")))
  12148. ((equal arg '(16))
  12149. (save-excursion
  12150. (org-back-to-heading t)
  12151. (if (re-search-forward
  12152. org-scheduled-time-regexp
  12153. (save-excursion (outline-next-heading) (point)) t)
  12154. (let* ((rpl0 (match-string 1))
  12155. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  12156. (replace-match
  12157. (concat org-scheduled-string
  12158. " <" rpl
  12159. (format " -%dd"
  12160. (abs
  12161. (- (time-to-days
  12162. (save-match-data
  12163. (org-read-date nil t nil "Delay until" old-date-time)))
  12164. (time-to-days old-date-time))))
  12165. ">") t t))
  12166. (user-error "No scheduled information to update"))))
  12167. (t
  12168. (org-add-planning-info 'scheduled time 'closed)
  12169. (when (and old-date
  12170. org-log-reschedule
  12171. (not (equal old-date org-last-inserted-timestamp)))
  12172. (org-add-log-setup
  12173. 'reschedule org-last-inserted-timestamp old-date 'findpos
  12174. org-log-reschedule))
  12175. (when repeater
  12176. (save-excursion
  12177. (org-back-to-heading t)
  12178. (when (re-search-forward (concat org-scheduled-string " "
  12179. org-last-inserted-timestamp)
  12180. (save-excursion
  12181. (outline-next-heading) (point)) t)
  12182. (goto-char (1- (match-end 0)))
  12183. (insert " " repeater)
  12184. (setq org-last-inserted-timestamp
  12185. (concat (substring org-last-inserted-timestamp 0 -1)
  12186. " " repeater
  12187. (substring org-last-inserted-timestamp -1))))))
  12188. (message "Scheduled to %s" org-last-inserted-timestamp))))))
  12189. (defun org-get-scheduled-time (pom &optional inherit)
  12190. "Get the scheduled time as a time tuple, of a format suitable
  12191. for calling org-schedule with, or if there is no scheduling,
  12192. returns nil."
  12193. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  12194. (when time
  12195. (apply 'encode-time (org-parse-time-string time)))))
  12196. (defun org-get-deadline-time (pom &optional inherit)
  12197. "Get the deadline as a time tuple, of a format suitable for
  12198. calling org-deadline with, or if there is no scheduling, returns
  12199. nil."
  12200. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  12201. (when time
  12202. (apply 'encode-time (org-parse-time-string time)))))
  12203. (defun org-remove-timestamp-with-keyword (keyword)
  12204. "Remove all time stamps with KEYWORD in the current entry."
  12205. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  12206. beg)
  12207. (save-excursion
  12208. (org-back-to-heading t)
  12209. (setq beg (point))
  12210. (outline-next-heading)
  12211. (while (re-search-backward re beg t)
  12212. (replace-match "")
  12213. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  12214. (equal (char-before) ?\ ))
  12215. (backward-delete-char 1)
  12216. (if (string-match "^[ \t]*$" (buffer-substring
  12217. (point-at-bol) (point-at-eol)))
  12218. (delete-region (point-at-bol)
  12219. (min (point-max) (1+ (point-at-eol))))))))))
  12220. (defvar org-time-was-given) ; dynamically scoped parameter
  12221. (defvar org-end-time-was-given) ; dynamically scoped parameter
  12222. (defun org-at-planning-p ()
  12223. "Non-nil when point is on a planning info line."
  12224. ;; This is as accurate and faster than `org-element-at-point' since
  12225. ;; planning info location is fixed in the section.
  12226. (org-with-wide-buffer
  12227. (beginning-of-line)
  12228. (and (org-looking-at-p org-planning-line-re)
  12229. (eq (point)
  12230. (ignore-errors
  12231. (if (and (featurep 'org-inlinetask) (org-inlinetask-in-task-p))
  12232. (org-back-to-heading t)
  12233. (org-with-limited-levels (org-back-to-heading t)))
  12234. (line-beginning-position 2))))))
  12235. (defun org-add-planning-info (what &optional time &rest remove)
  12236. "Insert new timestamp with keyword in the planning line.
  12237. WHAT indicates what kind of time stamp to add. It is a symbol
  12238. among `closed', `deadline', `scheduled' and nil. TIME indicates
  12239. the time to use. If none is given, the user is prompted for
  12240. a date. REMOVE indicates what kind of entries to remove. An old
  12241. WHAT entry will also be removed."
  12242. (let (org-time-was-given org-end-time-was-given default-time default-input)
  12243. (catch 'exit
  12244. (when (and (memq what '(scheduled deadline))
  12245. (or (not time)
  12246. (and (stringp time)
  12247. (string-match "^[-+]+[0-9]" time))))
  12248. ;; Try to get a default date/time from existing timestamp
  12249. (save-excursion
  12250. (org-back-to-heading t)
  12251. (let ((end (save-excursion (outline-next-heading) (point))) ts)
  12252. (when (re-search-forward (if (eq what 'scheduled)
  12253. org-scheduled-time-regexp
  12254. org-deadline-time-regexp)
  12255. end t)
  12256. (setq ts (match-string 1)
  12257. default-time (apply 'encode-time (org-parse-time-string ts))
  12258. default-input (and ts (org-get-compact-tod ts)))))))
  12259. (when what
  12260. (setq time
  12261. (if (stringp time)
  12262. ;; This is a string (relative or absolute), set
  12263. ;; proper date.
  12264. (apply #'encode-time
  12265. (org-read-date-analyze
  12266. time default-time (decode-time default-time)))
  12267. ;; If necessary, get the time from the user
  12268. (or time (org-read-date nil 'to-time nil nil
  12269. default-time default-input)))))
  12270. (org-with-wide-buffer
  12271. (org-back-to-heading t)
  12272. (forward-line)
  12273. (unless (bolp) (insert "\n"))
  12274. (cond ((org-looking-at-p org-planning-line-re)
  12275. ;; Move to current indentation.
  12276. (skip-chars-forward " \t")
  12277. ;; Check if we have to remove something.
  12278. (dolist (type (if what (cons what remove) remove))
  12279. (save-excursion
  12280. (when (re-search-forward
  12281. (case type
  12282. (closed org-closed-time-regexp)
  12283. (deadline org-deadline-time-regexp)
  12284. (scheduled org-scheduled-time-regexp)
  12285. (otherwise
  12286. (error "Invalid planning type: %s" type)))
  12287. (line-end-position) t)
  12288. ;; Delete until next keyword or end of line.
  12289. (delete-region
  12290. (match-beginning 0)
  12291. (if (re-search-forward org-keyword-time-not-clock-regexp
  12292. (line-end-position)
  12293. t)
  12294. (match-beginning 0)
  12295. (line-end-position))))))
  12296. ;; If there is nothing more to add and no more keyword
  12297. ;; is left, remove the line completely.
  12298. (if (and (org-looking-at-p "[ \t]*$") (not what))
  12299. (delete-region (line-beginning-position)
  12300. (line-beginning-position 2))
  12301. ;; If we removed last keyword, do not leave trailing
  12302. ;; white space at the end of line.
  12303. (let ((p (point)))
  12304. (save-excursion
  12305. (end-of-line)
  12306. (unless (= (skip-chars-backward " \t" p) 0)
  12307. (delete-region (point) (line-end-position)))))))
  12308. ((not what) (throw 'exit nil)) ; Nothing to do.
  12309. (t (insert-before-markers "\n")
  12310. (backward-char 1)
  12311. (when org-adapt-indentation
  12312. (org-indent-to-column (1+ (org-outline-level))))))
  12313. (when what
  12314. ;; Insert planning keyword.
  12315. (insert (case what
  12316. (closed org-closed-string)
  12317. (deadline org-deadline-string)
  12318. (scheduled org-scheduled-string)
  12319. (otherwise (error "Invalid planning type: %s" what)))
  12320. " ")
  12321. ;; Insert associated timestamp.
  12322. (let ((ts (org-insert-time-stamp
  12323. time
  12324. (or org-time-was-given
  12325. (and (eq what 'closed) org-log-done-with-time))
  12326. (eq what 'closed)
  12327. nil nil (list org-end-time-was-given))))
  12328. (unless (eolp) (insert " "))
  12329. ts))))))
  12330. (defvar org-log-note-marker (make-marker))
  12331. (defvar org-log-note-purpose nil)
  12332. (defvar org-log-note-state nil)
  12333. (defvar org-log-note-previous-state nil)
  12334. (defvar org-log-note-extra nil)
  12335. (defvar org-log-note-window-configuration nil)
  12336. (defvar org-log-note-return-to (make-marker))
  12337. (defvar org-log-note-effective-time nil
  12338. "Remembered current time so that dynamically scoped
  12339. `org-extend-today-until' affects tha timestamps in state change
  12340. log")
  12341. (defvar org-log-post-message nil
  12342. "Message to be displayed after a log note has been stored.
  12343. The auto-repeater uses this.")
  12344. (defun org-add-note ()
  12345. "Add a note to the current entry.
  12346. This is done in the same way as adding a state change note."
  12347. (interactive)
  12348. (org-add-log-setup 'note nil nil 'findpos nil))
  12349. (defun org-log-beginning (&optional create)
  12350. "Return expected start of log notes in current entry.
  12351. When optional argument CREATE is non-nil, the function creates
  12352. a drawer to store notes, if necessary. Returned position ignores
  12353. narrowing."
  12354. (org-with-wide-buffer
  12355. (org-end-of-meta-data)
  12356. (let ((end (if (org-at-heading-p) (point)
  12357. (save-excursion (outline-next-heading) (point))))
  12358. (drawer (org-log-into-drawer)))
  12359. (cond
  12360. (drawer
  12361. (let ((regexp (concat "^[ \t]*:" (regexp-quote drawer) ":[ \t]*$"))
  12362. (case-fold-search t))
  12363. (catch 'exit
  12364. ;; Try to find existing drawer.
  12365. (while (re-search-forward regexp end t)
  12366. (let ((element (org-element-at-point)))
  12367. (when (eq (org-element-type element) 'drawer)
  12368. (let ((cend (org-element-property :contents-end element)))
  12369. (when (and (not org-log-states-order-reversed) cend)
  12370. (goto-char cend)))
  12371. (throw 'exit nil))))
  12372. ;; No drawer found. Create one, if permitted.
  12373. (when create
  12374. (unless (bolp) (insert "\n"))
  12375. (let ((beg (point)))
  12376. (insert ":" drawer ":\n:END:\n")
  12377. (org-indent-region beg (point)))
  12378. (end-of-line -1)))))
  12379. (org-log-state-notes-insert-after-drawers
  12380. (while (and (looking-at org-drawer-regexp)
  12381. (progn (goto-char (match-end 0))
  12382. (re-search-forward org-property-end-re end t)))
  12383. (forward-line)))))
  12384. (if (bolp) (point) (line-beginning-position 2))))
  12385. (defun org-add-log-setup (&optional purpose state prev-state findpos how extra)
  12386. "Set up the post command hook to take a note.
  12387. If this is about to TODO state change, the new state is expected in STATE.
  12388. When FINDPOS is non-nil, find the correct position for the note in
  12389. the current entry. If not, assume that it can be inserted at point.
  12390. HOW is an indicator what kind of note should be created.
  12391. EXTRA is additional text that will be inserted into the notes buffer."
  12392. (org-with-wide-buffer
  12393. (when findpos
  12394. (goto-char (org-log-beginning t))
  12395. (unless org-log-states-order-reversed
  12396. (org-skip-over-state-notes)
  12397. (skip-chars-backward " \t\n\r")
  12398. (forward-line)))
  12399. (move-marker org-log-note-marker (point))
  12400. ;; Preserve position even if a property drawer is inserted in the
  12401. ;; process.
  12402. (set-marker-insertion-type org-log-note-marker t)
  12403. (setq org-log-note-purpose purpose
  12404. org-log-note-state state
  12405. org-log-note-previous-state prev-state
  12406. org-log-note-how how
  12407. org-log-note-extra extra
  12408. org-log-note-effective-time (org-current-effective-time))
  12409. (add-hook 'post-command-hook 'org-add-log-note 'append)))
  12410. (defun org-skip-over-state-notes ()
  12411. "Skip past the list of State notes in an entry."
  12412. (when (ignore-errors (goto-char (org-in-item-p)))
  12413. (let* ((struct (org-list-struct))
  12414. (prevs (org-list-prevs-alist struct))
  12415. (regexp
  12416. (concat "[ \t]*- +"
  12417. (replace-regexp-in-string
  12418. " +" " +"
  12419. (org-replace-escapes
  12420. (regexp-quote (cdr (assq 'state org-log-note-headings)))
  12421. `(("%d" . ,org-ts-regexp-inactive)
  12422. ("%D" . ,org-ts-regexp)
  12423. ("%s" . "\"\\S-+\"")
  12424. ("%S" . "\"\\S-+\"")
  12425. ("%t" . ,org-ts-regexp-inactive)
  12426. ("%T" . ,org-ts-regexp)
  12427. ("%u" . ".*?")
  12428. ("%U" . ".*?")))))))
  12429. (while (org-looking-at-p regexp)
  12430. (goto-char (or (org-list-get-next-item (point) struct prevs)
  12431. (org-list-get-item-end (point) struct)))))))
  12432. (defun org-add-log-note (&optional purpose)
  12433. "Pop up a window for taking a note, and add this note later at point."
  12434. (remove-hook 'post-command-hook 'org-add-log-note)
  12435. (setq org-log-note-window-configuration (current-window-configuration))
  12436. (delete-other-windows)
  12437. (move-marker org-log-note-return-to (point))
  12438. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  12439. (goto-char org-log-note-marker)
  12440. (org-switch-to-buffer-other-window "*Org Note*")
  12441. (erase-buffer)
  12442. (if (memq org-log-note-how '(time state))
  12443. (let (current-prefix-arg) (org-store-log-note))
  12444. (let ((org-inhibit-startup t)) (org-mode))
  12445. (insert (format "# Insert note for %s.
  12446. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  12447. (cond
  12448. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  12449. ((eq org-log-note-purpose 'done) "closed todo item")
  12450. ((eq org-log-note-purpose 'state)
  12451. (format "state change from \"%s\" to \"%s\""
  12452. (or org-log-note-previous-state "")
  12453. (or org-log-note-state "")))
  12454. ((eq org-log-note-purpose 'reschedule)
  12455. "rescheduling")
  12456. ((eq org-log-note-purpose 'delschedule)
  12457. "no longer scheduled")
  12458. ((eq org-log-note-purpose 'redeadline)
  12459. "changing deadline")
  12460. ((eq org-log-note-purpose 'deldeadline)
  12461. "removing deadline")
  12462. ((eq org-log-note-purpose 'refile)
  12463. "refiling")
  12464. ((eq org-log-note-purpose 'note)
  12465. "this entry")
  12466. (t (error "This should not happen")))))
  12467. (if org-log-note-extra (insert org-log-note-extra))
  12468. (org-set-local 'org-finish-function 'org-store-log-note)
  12469. (run-hooks 'org-log-buffer-setup-hook)))
  12470. (defvar org-note-abort nil) ; dynamically scoped
  12471. (defun org-store-log-note ()
  12472. "Finish taking a log note, and insert it to where it belongs."
  12473. (let ((txt (buffer-string)))
  12474. (kill-buffer (current-buffer))
  12475. (let ((note (cdr (assq org-log-note-purpose org-log-note-headings))) lines)
  12476. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  12477. (setq txt (replace-match "" t t txt)))
  12478. (if (string-match "\\s-+\\'" txt)
  12479. (setq txt (replace-match "" t t txt)))
  12480. (setq lines (org-split-string txt "\n"))
  12481. (when (and note (string-match "\\S-" note))
  12482. (setq note
  12483. (org-replace-escapes
  12484. note
  12485. (list (cons "%u" (user-login-name))
  12486. (cons "%U" user-full-name)
  12487. (cons "%t" (format-time-string
  12488. (org-time-stamp-format 'long 'inactive)
  12489. org-log-note-effective-time))
  12490. (cons "%T" (format-time-string
  12491. (org-time-stamp-format 'long nil)
  12492. org-log-note-effective-time))
  12493. (cons "%d" (format-time-string
  12494. (org-time-stamp-format nil 'inactive)
  12495. org-log-note-effective-time))
  12496. (cons "%D" (format-time-string
  12497. (org-time-stamp-format nil nil)
  12498. org-log-note-effective-time))
  12499. (cons "%s" (cond
  12500. ((not org-log-note-state) "")
  12501. ((org-string-match-p org-ts-regexp
  12502. org-log-note-state)
  12503. (format "\"[%s]\""
  12504. (substring org-log-note-state 1 -1)))
  12505. (t (format "\"%s\"" org-log-note-state))))
  12506. (cons "%S"
  12507. (cond
  12508. ((not org-log-note-previous-state) "")
  12509. ((org-string-match-p org-ts-regexp
  12510. org-log-note-previous-state)
  12511. (format "\"[%s]\""
  12512. (substring
  12513. org-log-note-previous-state 1 -1)))
  12514. (t (format "\"%s\""
  12515. org-log-note-previous-state)))))))
  12516. (when lines (setq note (concat note " \\\\")))
  12517. (push note lines))
  12518. (when (or current-prefix-arg org-note-abort)
  12519. (when (org-log-into-drawer)
  12520. (org-remove-empty-drawer-at org-log-note-marker))
  12521. (setq lines nil))
  12522. (when lines
  12523. (with-current-buffer (marker-buffer org-log-note-marker)
  12524. (org-with-wide-buffer
  12525. (goto-char org-log-note-marker)
  12526. (move-marker org-log-note-marker nil)
  12527. ;; Make sure point is at the beginning of an empty line.
  12528. (cond ((not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  12529. ((looking-at "[ \t]*\\S-") (save-excursion (insert "\n"))))
  12530. ;; In an existing list, add a new item at the top level.
  12531. ;; Otherwise, indent line like a regular one.
  12532. (let ((itemp (org-in-item-p)))
  12533. (if itemp
  12534. (org-indent-line-to
  12535. (let ((struct (save-excursion
  12536. (goto-char itemp) (org-list-struct))))
  12537. (org-list-get-ind (org-list-get-top-point struct) struct)))
  12538. (org-indent-line)))
  12539. (insert (org-list-bullet-string "-") (pop lines))
  12540. (let ((ind (org-list-item-body-column (line-beginning-position))))
  12541. (dolist (line lines)
  12542. (insert "\n")
  12543. (org-indent-line-to ind)
  12544. (insert line)))
  12545. (message "Note stored")
  12546. (org-back-to-heading t)
  12547. (org-cycle-hide-drawers 'children))
  12548. ;; Fix `buffer-undo-list' when `org-store-log-note' is called
  12549. ;; from within `org-add-log-note' because `buffer-undo-list'
  12550. ;; is then modified outside of `org-with-remote-undo'.
  12551. (when (eq this-command 'org-agenda-todo)
  12552. (setcdr buffer-undo-list (cddr buffer-undo-list)))))))
  12553. ;; Don't add undo information when called from `org-agenda-todo'
  12554. (let ((buffer-undo-list (eq this-command 'org-agenda-todo)))
  12555. (set-window-configuration org-log-note-window-configuration)
  12556. (with-current-buffer (marker-buffer org-log-note-return-to)
  12557. (goto-char org-log-note-return-to))
  12558. (move-marker org-log-note-return-to nil)
  12559. (and org-log-post-message (message "%s" org-log-post-message))))
  12560. (defun org-remove-empty-drawer-at (pos)
  12561. "Remove an empty drawer at position POS.
  12562. POS may also be a marker."
  12563. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  12564. (org-with-wide-buffer
  12565. (goto-char pos)
  12566. (let ((drawer (org-element-at-point)))
  12567. (when (and (memq (org-element-type drawer) '(drawer property-drawer))
  12568. (not (org-element-property :contents-begin drawer)))
  12569. (delete-region (org-element-property :begin drawer)
  12570. (progn (goto-char (org-element-property :end drawer))
  12571. (skip-chars-backward " \r\t\n")
  12572. (forward-line)
  12573. (point))))))))
  12574. (defvar org-ts-type nil)
  12575. (defun org-sparse-tree (&optional arg type)
  12576. "Create a sparse tree, prompt for the details.
  12577. This command can create sparse trees. You first need to select the type
  12578. of match used to create the tree:
  12579. t Show all TODO entries.
  12580. T Show entries with a specific TODO keyword.
  12581. m Show entries selected by a tags/property match.
  12582. p Enter a property name and its value (both with completion on existing
  12583. names/values) and show entries with that property.
  12584. r Show entries matching a regular expression (`/' can be used as well).
  12585. b Show deadlines and scheduled items before a date.
  12586. a Show deadlines and scheduled items after a date.
  12587. d Show deadlines due within `org-deadline-warning-days'.
  12588. D Show deadlines and scheduled items between a date range."
  12589. (interactive "P")
  12590. (setq type (or type org-sparse-tree-default-date-type))
  12591. (setq org-ts-type type)
  12592. (message "Sparse tree: [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty
  12593. [d]eadlines [b]efore-date [a]fter-date [D]ates range
  12594. [c]ycle through date types: %s"
  12595. (case type
  12596. (all "all timestamps")
  12597. (scheduled "only scheduled")
  12598. (deadline "only deadline")
  12599. (active "only active timestamps")
  12600. (inactive "only inactive timestamps")
  12601. (closed "with a closed time-stamp")
  12602. (otherwise "scheduled/deadline")))
  12603. (let ((answer (read-char-exclusive)))
  12604. (case answer
  12605. (?c
  12606. (org-sparse-tree
  12607. arg
  12608. (cadr
  12609. (memq type '(nil all scheduled deadline active inactive closed)))))
  12610. (?d (call-interactively 'org-check-deadlines))
  12611. (?b (call-interactively 'org-check-before-date))
  12612. (?a (call-interactively 'org-check-after-date))
  12613. (?D (call-interactively 'org-check-dates-range))
  12614. (?t (call-interactively 'org-show-todo-tree))
  12615. (?T (org-show-todo-tree '(4)))
  12616. (?m (call-interactively 'org-match-sparse-tree))
  12617. ((?p ?P)
  12618. (let* ((kwd (org-icompleting-read
  12619. "Property: " (mapcar 'list (org-buffer-property-keys))))
  12620. (value (org-icompleting-read
  12621. "Value: " (mapcar 'list (org-property-values kwd)))))
  12622. (unless (string-match "\\`{.*}\\'" value)
  12623. (setq value (concat "\"" value "\"")))
  12624. (org-match-sparse-tree arg (concat kwd "=" value))))
  12625. ((?r ?R ?/) (call-interactively 'org-occur))
  12626. (otherwise (user-error "No such sparse tree command \"%c\"" answer)))))
  12627. (defvar org-occur-highlights nil
  12628. "List of overlays used for occur matches.")
  12629. (make-variable-buffer-local 'org-occur-highlights)
  12630. (defvar org-occur-parameters nil
  12631. "Parameters of the active org-occur calls.
  12632. This is a list, each call to org-occur pushes as cons cell,
  12633. containing the regular expression and the callback, onto the list.
  12634. The list can contain several entries if `org-occur' has been called
  12635. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  12636. will only contain one set of parameters. When the highlights are
  12637. removed (for example with `C-c C-c', or with the next edit (depending
  12638. on `org-remove-highlights-with-change'), this variable is emptied
  12639. as well.")
  12640. (make-variable-buffer-local 'org-occur-parameters)
  12641. (defun org-occur (regexp &optional keep-previous callback)
  12642. "Make a compact tree which shows all matches of REGEXP.
  12643. The tree will show the lines where the regexp matches, and all higher
  12644. headlines above the match. It will also show the heading after the match,
  12645. to make sure editing the matching entry is easy.
  12646. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  12647. call to `org-occur' will be kept, to allow stacking of calls to this
  12648. command.
  12649. If CALLBACK is non-nil, it is a function which is called to confirm
  12650. that the match should indeed be shown."
  12651. (interactive "sRegexp: \nP")
  12652. (when (equal regexp "")
  12653. (user-error "Regexp cannot be empty"))
  12654. (unless keep-previous
  12655. (org-remove-occur-highlights nil nil t))
  12656. (push (cons regexp callback) org-occur-parameters)
  12657. (let ((cnt 0))
  12658. (save-excursion
  12659. (goto-char (point-min))
  12660. (if (or (not keep-previous) ; do not want to keep
  12661. (not org-occur-highlights)) ; no previous matches
  12662. ;; hide everything
  12663. (org-overview))
  12664. (while (re-search-forward regexp nil t)
  12665. (backward-char) ;; FIXME: Match timestamps at the end of a headline
  12666. (when (or (not callback)
  12667. (save-match-data (funcall callback)))
  12668. (setq cnt (1+ cnt))
  12669. (when org-highlight-sparse-tree-matches
  12670. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  12671. (org-show-context 'occur-tree))))
  12672. (when org-remove-highlights-with-change
  12673. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  12674. nil 'local))
  12675. (unless org-sparse-tree-open-archived-trees
  12676. (org-hide-archived-subtrees (point-min) (point-max)))
  12677. (run-hooks 'org-occur-hook)
  12678. (if (org-called-interactively-p 'interactive)
  12679. (message "%d match(es) for regexp %s" cnt regexp))
  12680. cnt))
  12681. (defun org-occur-next-match (&optional n reset)
  12682. "Function for `next-error-function' to find sparse tree matches.
  12683. N is the number of matches to move, when negative move backwards.
  12684. RESET is entirely ignored - this function always goes back to the
  12685. starting point when no match is found."
  12686. (let* ((limit (if (< n 0) (point-min) (point-max)))
  12687. (search-func (if (< n 0)
  12688. 'previous-single-char-property-change
  12689. 'next-single-char-property-change))
  12690. (n (abs n))
  12691. (pos (point))
  12692. p1)
  12693. (catch 'exit
  12694. (while (setq p1 (funcall search-func (point) 'org-type))
  12695. (when (equal p1 limit)
  12696. (goto-char pos)
  12697. (user-error "No more matches"))
  12698. (when (equal (get-char-property p1 'org-type) 'org-occur)
  12699. (setq n (1- n))
  12700. (when (= n 0)
  12701. (goto-char p1)
  12702. (throw 'exit (point))))
  12703. (goto-char p1))
  12704. (goto-char p1)
  12705. (user-error "No more matches"))))
  12706. (defun org-show-context (&optional key)
  12707. "Make sure point and context are visible.
  12708. Optional argument KEY, when non-nil, is a symbol. See
  12709. `org-show-context-detail' for allowed values and how much is to
  12710. be shown."
  12711. (org-show-set-visibility
  12712. (cond ((symbolp org-show-context-detail) org-show-context-detail)
  12713. ((cdr (assq key org-show-context-detail)))
  12714. (t (cdr (assq 'default org-show-context-detail))))))
  12715. (defun org-show-set-visibility (detail)
  12716. "Set visibility around point according to DETAIL.
  12717. DETAIL is either nil, `minimal', `local', `ancestors', `lineage',
  12718. `tree', `canonical' or t. See `org-show-context-detail' for more
  12719. information."
  12720. (unless (org-before-first-heading-p)
  12721. ;; Show current heading and possibly its entry, following headline
  12722. ;; or all children.
  12723. (if (and (org-at-heading-p) (not (eq detail 'local)))
  12724. (org-flag-heading nil)
  12725. (org-show-entry)
  12726. (org-with-limited-levels
  12727. (case detail
  12728. ((tree canonical t) (org-show-children))
  12729. ((nil minimal ancestors))
  12730. (t (save-excursion
  12731. (outline-next-heading)
  12732. (org-flag-heading nil))))))
  12733. ;; Show all siblings.
  12734. (when (eq detail 'lineage) (org-show-siblings))
  12735. ;; Show ancestors, possibly with their children.
  12736. (when (memq detail '(ancestors lineage tree canonical t))
  12737. (save-excursion
  12738. (while (org-up-heading-safe)
  12739. (org-flag-heading nil)
  12740. (when (memq detail '(canonical t)) (org-show-entry))
  12741. (when (memq detail '(tree canonical t)) (org-show-children)))))))
  12742. (defvar org-reveal-start-hook nil
  12743. "Hook run before revealing a location.")
  12744. (defun org-reveal (&optional siblings)
  12745. "Show current entry, hierarchy above it, and the following headline.
  12746. This can be used to show a consistent set of context around
  12747. locations exposed with `org-show-context'.
  12748. With optional argument SIBLINGS, on each level of the hierarchy all
  12749. siblings are shown. This repairs the tree structure to what it would
  12750. look like when opened with hierarchical calls to `org-cycle'.
  12751. With double optional argument \\[universal-argument] \\[universal-argument], \
  12752. go to the parent and show the
  12753. entire tree."
  12754. (interactive "P")
  12755. (run-hooks 'org-reveal-start-hook)
  12756. (cond ((equal siblings '(4)) (org-show-set-visibility 'canonical))
  12757. ((equal siblings '(16))
  12758. (save-excursion
  12759. (when (org-up-heading-safe)
  12760. (org-show-subtree)
  12761. (run-hook-with-args 'org-cycle-hook 'subtree))))
  12762. (t (org-show-set-visibility 'lineage))))
  12763. (defun org-highlight-new-match (beg end)
  12764. "Highlight from BEG to END and mark the highlight is an occur headline."
  12765. (let ((ov (make-overlay beg end)))
  12766. (overlay-put ov 'face 'secondary-selection)
  12767. (overlay-put ov 'org-type 'org-occur)
  12768. (push ov org-occur-highlights)))
  12769. (defun org-remove-occur-highlights (&optional beg end noremove)
  12770. "Remove the occur highlights from the buffer.
  12771. BEG and END are ignored. If NOREMOVE is nil, remove this function
  12772. from the `before-change-functions' in the current buffer."
  12773. (interactive)
  12774. (unless org-inhibit-highlight-removal
  12775. (mapc 'delete-overlay org-occur-highlights)
  12776. (setq org-occur-highlights nil)
  12777. (setq org-occur-parameters nil)
  12778. (unless noremove
  12779. (remove-hook 'before-change-functions
  12780. 'org-remove-occur-highlights 'local))))
  12781. ;;;; Priorities
  12782. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  12783. "Regular expression matching the priority indicator.")
  12784. (defvar org-remove-priority-next-time nil)
  12785. (defun org-priority-up ()
  12786. "Increase the priority of the current item."
  12787. (interactive)
  12788. (org-priority 'up))
  12789. (defun org-priority-down ()
  12790. "Decrease the priority of the current item."
  12791. (interactive)
  12792. (org-priority 'down))
  12793. (defun org-priority (&optional action show)
  12794. "Change the priority of an item.
  12795. ACTION can be `set', `up', `down', or a character."
  12796. (interactive "P")
  12797. (if (equal action '(4))
  12798. (org-show-priority)
  12799. (unless org-enable-priority-commands
  12800. (user-error "Priority commands are disabled"))
  12801. (setq action (or action 'set))
  12802. (let (current new news have remove)
  12803. (save-excursion
  12804. (org-back-to-heading t)
  12805. (if (looking-at org-priority-regexp)
  12806. (setq current (string-to-char (match-string 2))
  12807. have t))
  12808. (cond
  12809. ((eq action 'remove)
  12810. (setq remove t new ?\ ))
  12811. ((or (eq action 'set)
  12812. (if (featurep 'xemacs) (characterp action) (integerp action)))
  12813. (if (not (eq action 'set))
  12814. (setq new action)
  12815. (message "Priority %c-%c, SPC to remove: "
  12816. org-highest-priority org-lowest-priority)
  12817. (save-match-data
  12818. (setq new (read-char-exclusive))))
  12819. (if (and (= (upcase org-highest-priority) org-highest-priority)
  12820. (= (upcase org-lowest-priority) org-lowest-priority))
  12821. (setq new (upcase new)))
  12822. (cond ((equal new ?\ ) (setq remove t))
  12823. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12824. (user-error "Priority must be between `%c' and `%c'"
  12825. org-highest-priority org-lowest-priority))))
  12826. ((eq action 'up)
  12827. (setq new (if have
  12828. (1- current) ; normal cycling
  12829. ;; last priority was empty
  12830. (if (eq last-command this-command)
  12831. org-lowest-priority ; wrap around empty to lowest
  12832. ;; default
  12833. (if org-priority-start-cycle-with-default
  12834. org-default-priority
  12835. (1- org-default-priority))))))
  12836. ((eq action 'down)
  12837. (setq new (if have
  12838. (1+ current) ; normal cycling
  12839. ;; last priority was empty
  12840. (if (eq last-command this-command)
  12841. org-highest-priority ; wrap around empty to highest
  12842. ;; default
  12843. (if org-priority-start-cycle-with-default
  12844. org-default-priority
  12845. (1+ org-default-priority))))))
  12846. (t (user-error "Invalid action")))
  12847. (if (or (< (upcase new) org-highest-priority)
  12848. (> (upcase new) org-lowest-priority))
  12849. (if (and (memq action '(up down))
  12850. (not have) (not (eq last-command this-command)))
  12851. ;; `new' is from default priority
  12852. (error
  12853. "The default can not be set, see `org-default-priority' why")
  12854. ;; normal cycling: `new' is beyond highest/lowest priority
  12855. ;; and is wrapped around to the empty priority
  12856. (setq remove t)))
  12857. (setq news (format "%c" new))
  12858. (if have
  12859. (if remove
  12860. (replace-match "" t t nil 1)
  12861. (replace-match news t t nil 2))
  12862. (if remove
  12863. (user-error "No priority cookie found in line")
  12864. (let ((case-fold-search nil))
  12865. (looking-at org-todo-line-regexp))
  12866. (if (match-end 2)
  12867. (progn
  12868. (goto-char (match-end 2))
  12869. (insert " [#" news "]"))
  12870. (goto-char (match-beginning 3))
  12871. (insert "[#" news "] "))))
  12872. (org-set-tags nil 'align))
  12873. (if remove
  12874. (message "Priority removed")
  12875. (message "Priority of current item set to %s" news)))))
  12876. (defun org-show-priority ()
  12877. "Show the priority of the current item.
  12878. This priority is composed of the main priority given with the [#A] cookies,
  12879. and by additional input from the age of a schedules or deadline entry."
  12880. (interactive)
  12881. (let ((pri (if (eq major-mode 'org-agenda-mode)
  12882. (org-get-at-bol 'priority)
  12883. (save-excursion
  12884. (save-match-data
  12885. (beginning-of-line)
  12886. (and (looking-at org-heading-regexp)
  12887. (org-get-priority (match-string 0))))))))
  12888. (message "Priority is %d" (if pri pri -1000))))
  12889. (defun org-get-priority (s)
  12890. "Find priority cookie and return priority."
  12891. (save-match-data
  12892. (if (functionp org-get-priority-function)
  12893. (funcall org-get-priority-function)
  12894. (if (not (string-match org-priority-regexp s))
  12895. (* 1000 (- org-lowest-priority org-default-priority))
  12896. (* 1000 (- org-lowest-priority
  12897. (string-to-char (match-string 2 s))))))))
  12898. ;;;; Tags
  12899. (defvar org-agenda-archives-mode)
  12900. (defvar org-map-continue-from nil
  12901. "Position from where mapping should continue.
  12902. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12903. (defvar org-scanner-tags nil
  12904. "The current tag list while the tags scanner is running.")
  12905. (defvar org-trust-scanner-tags nil
  12906. "Should `org-get-tags-at' use the tags for the scanner.
  12907. This is for internal dynamical scoping only.
  12908. When this is non-nil, the function `org-get-tags-at' will return the value
  12909. of `org-scanner-tags' instead of building the list by itself. This
  12910. can lead to large speed-ups when the tags scanner is used in a file with
  12911. many entries, and when the list of tags is retrieved, for example to
  12912. obtain a list of properties. Building the tags list for each entry in such
  12913. a file becomes an N^2 operation - but with this variable set, it scales
  12914. as N.")
  12915. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12916. "Scan headline tags with inheritance and produce output ACTION.
  12917. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12918. or `agenda' to produce an entry list for an agenda view. It can also be
  12919. a Lisp form or a function that should be called at each matched headline, in
  12920. this case the return value is a list of all return values from these calls.
  12921. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  12922. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  12923. only lines with a not-done TODO keyword are included in the output.
  12924. This should be the same variable that was scoped into
  12925. and set by `org-make-tags-matcher' when it constructed MATCHER.
  12926. START-LEVEL can be a string with asterisks, reducing the scope to
  12927. headlines matching this string."
  12928. (require 'org-agenda)
  12929. (let* ((re (concat "^"
  12930. (if start-level
  12931. ;; Get the correct level to match
  12932. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12933. org-outline-regexp)
  12934. " *\\(\\<\\("
  12935. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12936. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  12937. (props (list 'face 'default
  12938. 'done-face 'org-agenda-done
  12939. 'undone-face 'default
  12940. 'mouse-face 'highlight
  12941. 'org-not-done-regexp org-not-done-regexp
  12942. 'org-todo-regexp org-todo-regexp
  12943. 'org-complex-heading-regexp org-complex-heading-regexp
  12944. 'help-echo
  12945. (format "mouse-2 or RET jump to org file %s"
  12946. (abbreviate-file-name
  12947. (or (buffer-file-name (buffer-base-buffer))
  12948. (buffer-name (buffer-base-buffer)))))))
  12949. (org-map-continue-from nil)
  12950. lspos tags tags-list
  12951. (tags-alist (list (cons 0 org-file-tags)))
  12952. (llast 0) rtn rtn1 level category i txt
  12953. todo marker entry priority
  12954. ts-date ts-date-type ts-date-pair)
  12955. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  12956. (setq action (list 'lambda nil action)))
  12957. (save-excursion
  12958. (goto-char (point-min))
  12959. (when (eq action 'sparse-tree)
  12960. (org-overview)
  12961. (org-remove-occur-highlights))
  12962. (while (let (case-fold-search)
  12963. (re-search-forward re nil t))
  12964. (setq org-map-continue-from nil)
  12965. (catch :skip
  12966. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  12967. tags (if (match-end 4) (org-match-string-no-properties 4)))
  12968. (goto-char (setq lspos (match-beginning 0)))
  12969. (setq level (org-reduced-level (org-outline-level))
  12970. category (org-get-category))
  12971. (when (eq action 'agenda)
  12972. (setq ts-date-pair (org-agenda-entry-get-agenda-timestamp (point))
  12973. ts-date (car ts-date-pair)
  12974. ts-date-type (cdr ts-date-pair)))
  12975. (setq i llast llast level)
  12976. ;; remove tag lists from same and sublevels
  12977. (while (>= i level)
  12978. (when (setq entry (assoc i tags-alist))
  12979. (setq tags-alist (delete entry tags-alist)))
  12980. (setq i (1- i)))
  12981. ;; add the next tags
  12982. (when tags
  12983. (setq tags (org-split-string tags ":")
  12984. tags-alist
  12985. (cons (cons level tags) tags-alist)))
  12986. ;; compile tags for current headline
  12987. (setq tags-list
  12988. (if org-use-tag-inheritance
  12989. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  12990. tags)
  12991. org-scanner-tags tags-list)
  12992. (when org-use-tag-inheritance
  12993. (setcdr (car tags-alist)
  12994. (mapcar (lambda (x)
  12995. (setq x (copy-sequence x))
  12996. (org-add-prop-inherited x))
  12997. (cdar tags-alist))))
  12998. (when (and tags org-use-tag-inheritance
  12999. (or (not (eq t org-use-tag-inheritance))
  13000. org-tags-exclude-from-inheritance))
  13001. ;; selective inheritance, remove uninherited ones
  13002. (setcdr (car tags-alist)
  13003. (org-remove-uninherited-tags (cdar tags-alist))))
  13004. (when (and
  13005. ;; eval matcher only when the todo condition is OK
  13006. (and (or (not todo-only) (member todo org-not-done-keywords))
  13007. (let ((case-fold-search t) (org-trust-scanner-tags t))
  13008. (eval matcher)))
  13009. ;; Call the skipper, but return t if it does not skip,
  13010. ;; so that the `and' form continues evaluating
  13011. (progn
  13012. (unless (eq action 'sparse-tree) (org-agenda-skip))
  13013. t)
  13014. ;; Check if timestamps are deselecting this entry
  13015. (or (not todo-only)
  13016. (and (member todo org-not-done-keywords)
  13017. (or (not org-agenda-tags-todo-honor-ignore-options)
  13018. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  13019. ;; select this headline
  13020. (cond
  13021. ((eq action 'sparse-tree)
  13022. (and org-highlight-sparse-tree-matches
  13023. (org-get-heading) (match-end 0)
  13024. (org-highlight-new-match
  13025. (match-beginning 1) (match-end 1)))
  13026. (org-show-context 'tags-tree))
  13027. ((eq action 'agenda)
  13028. (setq txt (org-agenda-format-item
  13029. ""
  13030. (concat
  13031. (if (eq org-tags-match-list-sublevels 'indented)
  13032. (make-string (1- level) ?.) "")
  13033. (org-get-heading))
  13034. (make-string level ?\s)
  13035. category
  13036. tags-list)
  13037. priority (org-get-priority txt))
  13038. (goto-char lspos)
  13039. (setq marker (org-agenda-new-marker))
  13040. (org-add-props txt props
  13041. 'org-marker marker 'org-hd-marker marker 'org-category category
  13042. 'todo-state todo
  13043. 'ts-date ts-date
  13044. 'priority priority
  13045. 'type (concat "tagsmatch" ts-date-type))
  13046. (push txt rtn))
  13047. ((functionp action)
  13048. (setq org-map-continue-from nil)
  13049. (save-excursion
  13050. (setq rtn1 (funcall action))
  13051. (push rtn1 rtn)))
  13052. (t (user-error "Invalid action")))
  13053. ;; if we are to skip sublevels, jump to end of subtree
  13054. (unless org-tags-match-list-sublevels
  13055. (org-end-of-subtree t)
  13056. (backward-char 1))))
  13057. ;; Get the correct position from where to continue
  13058. (if org-map-continue-from
  13059. (goto-char org-map-continue-from)
  13060. (and (= (point) lspos) (end-of-line 1)))))
  13061. (when (and (eq action 'sparse-tree)
  13062. (not org-sparse-tree-open-archived-trees))
  13063. (org-hide-archived-subtrees (point-min) (point-max)))
  13064. (nreverse rtn)))
  13065. (defun org-remove-uninherited-tags (tags)
  13066. "Remove all tags that are not inherited from the list TAGS."
  13067. (cond
  13068. ((eq org-use-tag-inheritance t)
  13069. (if org-tags-exclude-from-inheritance
  13070. (org-delete-all org-tags-exclude-from-inheritance tags)
  13071. tags))
  13072. ((not org-use-tag-inheritance) nil)
  13073. ((stringp org-use-tag-inheritance)
  13074. (delq nil (mapcar
  13075. (lambda (x)
  13076. (if (and (string-match org-use-tag-inheritance x)
  13077. (not (member x org-tags-exclude-from-inheritance)))
  13078. x nil))
  13079. tags)))
  13080. ((listp org-use-tag-inheritance)
  13081. (delq nil (mapcar
  13082. (lambda (x)
  13083. (if (member x org-use-tag-inheritance) x nil))
  13084. tags)))))
  13085. (defun org-match-sparse-tree (&optional todo-only match)
  13086. "Create a sparse tree according to tags string MATCH.
  13087. MATCH can contain positive and negative selection of tags, like
  13088. \"+WORK+URGENT-WITHBOSS\".
  13089. If optional argument TODO-ONLY is non-nil, only select lines that are
  13090. also TODO lines."
  13091. (interactive "P")
  13092. (org-agenda-prepare-buffers (list (current-buffer)))
  13093. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  13094. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  13095. (defvar org-cached-props nil)
  13096. (defun org-cached-entry-get (pom property)
  13097. (if (or (eq t org-use-property-inheritance)
  13098. (and (stringp org-use-property-inheritance)
  13099. (let ((case-fold-search t))
  13100. (org-string-match-p org-use-property-inheritance property)))
  13101. (and (listp org-use-property-inheritance)
  13102. (member-ignore-case property org-use-property-inheritance)))
  13103. ;; Caching is not possible, check it directly.
  13104. (org-entry-get pom property 'inherit)
  13105. ;; Get all properties, so we can do complicated checks easily.
  13106. (cdr (assoc-string property
  13107. (or org-cached-props
  13108. (setq org-cached-props (org-entry-properties pom)))
  13109. t))))
  13110. (defun org-global-tags-completion-table (&optional files)
  13111. "Return the list of all tags in all agenda buffer/files.
  13112. Optional FILES argument is a list of files which can be used
  13113. instead of the agenda files."
  13114. (save-excursion
  13115. (org-uniquify
  13116. (delq nil
  13117. (apply 'append
  13118. (mapcar
  13119. (lambda (file)
  13120. (set-buffer (find-file-noselect file))
  13121. (append (org-get-buffer-tags)
  13122. (mapcar (lambda (x) (if (stringp (car-safe x))
  13123. (list (car-safe x)) nil))
  13124. org-tag-alist)))
  13125. (if (and files (car files))
  13126. files
  13127. (org-agenda-files))))))))
  13128. (defun org-make-tags-matcher (match)
  13129. "Create the TAGS/TODO matcher form for the selection string MATCH.
  13130. The variable `todo-only' is scoped dynamically into this function.
  13131. It will be set to t if the matcher restricts matching to TODO entries,
  13132. otherwise will not be touched.
  13133. Returns a cons of the selection string MATCH and the constructed
  13134. lisp form implementing the matcher. The matcher is to be evaluated
  13135. at an Org entry, with point on the headline, and returns t if the
  13136. entry matches the selection string MATCH. The returned lisp form
  13137. references two variables with information about the entry, which
  13138. must be bound around the form's evaluation: todo, the TODO keyword
  13139. at the entry (or nil of none); and tags-list, the list of all tags
  13140. at the entry including inherited ones. Additionally, the category
  13141. of the entry (if any) must be specified as the text property
  13142. 'org-category on the headline.
  13143. See also `org-scan-tags'.
  13144. "
  13145. (declare (special todo-only))
  13146. (unless (boundp 'todo-only)
  13147. (error "`org-make-tags-matcher' expects todo-only to be scoped in"))
  13148. (unless match
  13149. ;; Get a new match request, with completion against the global
  13150. ;; tags table and the local tags in current buffer
  13151. (let ((org-last-tags-completion-table
  13152. (org-uniquify
  13153. (delq nil (append (org-get-buffer-tags)
  13154. (org-global-tags-completion-table))))))
  13155. (setq match (org-completing-read-no-i
  13156. "Match: " 'org-tags-completion-function nil nil nil
  13157. 'org-tags-history))))
  13158. ;; Parse the string and create a lisp form
  13159. (let ((match0 match)
  13160. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  13161. minus tag mm
  13162. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  13163. orterms term orlist re-p str-p level-p level-op time-p
  13164. prop-p pn pv po gv rest (start 0) (ss 0))
  13165. ;; Expand group tags
  13166. (setq match (org-tags-expand match))
  13167. ;; Check if there is a TODO part of this match, which would be the
  13168. ;; part after a "/". TO make sure that this slash is not part of
  13169. ;; a property value to be matched against, we also check that there
  13170. ;; is no " after that slash.
  13171. ;; First, find the last slash
  13172. (while (string-match "/+" match ss)
  13173. (setq start (match-beginning 0) ss (match-end 0)))
  13174. (if (and (string-match "/+" match start)
  13175. (not (save-match-data (string-match "\"" match start))))
  13176. ;; match contains also a todo-matching request
  13177. (progn
  13178. (setq tagsmatch (substring match 0 (match-beginning 0))
  13179. todomatch (substring match (match-end 0)))
  13180. (if (string-match "^!" todomatch)
  13181. (setq todo-only t todomatch (substring todomatch 1)))
  13182. (if (string-match "^\\s-*$" todomatch)
  13183. (setq todomatch nil)))
  13184. ;; only matching tags
  13185. (setq tagsmatch match todomatch nil))
  13186. ;; Make the tags matcher
  13187. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  13188. (setq tagsmatcher t)
  13189. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  13190. (while (setq term (pop orterms))
  13191. (while (and (equal (substring term -1) "\\") orterms)
  13192. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  13193. (while (string-match re term)
  13194. (setq rest (substring term (match-end 0))
  13195. minus (and (match-end 1)
  13196. (equal (match-string 1 term) "-"))
  13197. tag (save-match-data (replace-regexp-in-string
  13198. "\\\\-" "-"
  13199. (match-string 2 term)))
  13200. re-p (equal (string-to-char tag) ?{)
  13201. level-p (match-end 4)
  13202. prop-p (match-end 5)
  13203. mm (cond
  13204. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  13205. (level-p
  13206. (setq level-op (org-op-to-function (match-string 3 term)))
  13207. `(,level-op level ,(string-to-number
  13208. (match-string 4 term))))
  13209. (prop-p
  13210. (setq pn (match-string 5 term)
  13211. po (match-string 6 term)
  13212. pv (match-string 7 term)
  13213. re-p (equal (string-to-char pv) ?{)
  13214. str-p (equal (string-to-char pv) ?\")
  13215. time-p (save-match-data
  13216. (string-match "^\"[[<].*[]>]\"$" pv))
  13217. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  13218. (if time-p (setq pv (org-matcher-time pv)))
  13219. (setq po (org-op-to-function po (if time-p 'time str-p)))
  13220. (cond
  13221. ((equal pn "CATEGORY")
  13222. (setq gv '(get-text-property (point) 'org-category)))
  13223. ((equal pn "TODO")
  13224. (setq gv 'todo))
  13225. (t
  13226. (setq gv `(org-cached-entry-get nil ,pn))))
  13227. (if re-p
  13228. (if (eq po 'org<>)
  13229. `(not (string-match ,pv (or ,gv "")))
  13230. `(string-match ,pv (or ,gv "")))
  13231. (if str-p
  13232. `(,po (or ,gv "") ,pv)
  13233. `(,po (string-to-number (or ,gv ""))
  13234. ,(string-to-number pv) ))))
  13235. (t `(member ,tag tags-list)))
  13236. mm (if minus (list 'not mm) mm)
  13237. term rest)
  13238. (push mm tagsmatcher))
  13239. (push (if (> (length tagsmatcher) 1)
  13240. (cons 'and tagsmatcher)
  13241. (car tagsmatcher))
  13242. orlist)
  13243. (setq tagsmatcher nil))
  13244. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  13245. (setq tagsmatcher
  13246. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  13247. ;; Make the todo matcher
  13248. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  13249. (setq todomatcher t)
  13250. (setq orterms (org-split-string todomatch "|") orlist nil)
  13251. (dolist (term orterms)
  13252. (while (string-match re term)
  13253. (setq minus (and (match-end 1)
  13254. (equal (match-string 1 term) "-"))
  13255. kwd (match-string 2 term)
  13256. re-p (equal (string-to-char kwd) ?{)
  13257. term (substring term (match-end 0))
  13258. mm (if re-p
  13259. `(string-match ,(substring kwd 1 -1) todo)
  13260. (list 'equal 'todo kwd))
  13261. mm (if minus (list 'not mm) mm))
  13262. (push mm todomatcher))
  13263. (push (if (> (length todomatcher) 1)
  13264. (cons 'and todomatcher)
  13265. (car todomatcher))
  13266. orlist)
  13267. (setq todomatcher nil))
  13268. (setq todomatcher (if (> (length orlist) 1)
  13269. (cons 'or orlist) (car orlist))))
  13270. ;; Return the string and lisp forms of the matcher
  13271. (setq matcher (if todomatcher
  13272. (list 'and tagsmatcher todomatcher)
  13273. tagsmatcher))
  13274. (when todo-only
  13275. (setq matcher (list 'and '(member todo org-not-done-keywords)
  13276. matcher)))
  13277. (cons match0 matcher)))
  13278. (defun org-tags-expand (match &optional single-as-list downcased tags-already-expanded)
  13279. "Expand group tags in MATCH.
  13280. This replaces every group tag in MATCH with a regexp tag search.
  13281. For example, a group tag \"Work\" defined as { Work : Lab Conf }
  13282. will be replaced like this:
  13283. Work => {\\<\\(?:Work\\|Lab\\|Conf\\)\\>}
  13284. +Work => +{\\<\\(?:Work\\|Lab\\|Conf\\)\\>}
  13285. -Work => -{\\<\\(?:Work\\|Lab\\|Conf\\)\\>}
  13286. Replacing by a regexp preserves the structure of the match.
  13287. E.g., this expansion
  13288. Work|Home => {\\(?:Work\\|Lab\\|Conf\\}|Home
  13289. will match anything tagged with \"Lab\" and \"Home\", or tagged
  13290. with \"Conf\" and \"Home\" or tagged with \"Work\" and \"home\".
  13291. A group tag in MATCH can contain regular expressions of its own.
  13292. For example, a group tag \"Proj\" defined as { Proj : {P@.+} }
  13293. will be replaced like this:
  13294. Proj => {\\<\\(?:Proj\\)\\>\\|P@.+}
  13295. When the optional argument SINGLE-AS-LIST is non-nil, MATCH is
  13296. assumed to be a single group tag, and the function will return
  13297. the list of tags in this group.
  13298. When DOWNCASE is non-nil, expand downcased TAGS."
  13299. (if org-group-tags
  13300. (let* ((case-fold-search t)
  13301. (stable org-mode-syntax-table)
  13302. (taggroups (or org-tag-groups-alist-for-agenda org-tag-groups-alist))
  13303. (taggroups (if downcased
  13304. (mapcar (lambda (tg) (mapcar #'downcase tg))
  13305. taggroups)
  13306. taggroups))
  13307. (taggroups-keys (mapcar #'car taggroups))
  13308. (return-match (if downcased (downcase match) match))
  13309. (count 0)
  13310. (work-already-expanded tags-already-expanded)
  13311. regexps-in-match tags-in-group regexp-in-group regexp-in-group-escaped)
  13312. ;; @ and _ are allowed as word-components in tags.
  13313. (modify-syntax-entry ?@ "w" stable)
  13314. (modify-syntax-entry ?_ "w" stable)
  13315. ;; Temporarily replace regexp-expressions in the match-expression.
  13316. (while (string-match "{.+?}" return-match)
  13317. (incf count)
  13318. (push (match-string 0 return-match) regexps-in-match)
  13319. (setq return-match (replace-match (format "<%d>" count) t nil return-match)))
  13320. (while (and taggroups-keys
  13321. (with-syntax-table stable
  13322. (string-match
  13323. (concat "\\(?1:[+-]?\\)\\(?2:\\<"
  13324. (regexp-opt taggroups-keys) "\\>\\)")
  13325. return-match)))
  13326. (let* ((dir (match-string 1 return-match))
  13327. (tag (match-string 2 return-match))
  13328. (tag (if downcased (downcase tag) tag)))
  13329. (unless (or (get-text-property 0 'grouptag (match-string 2 return-match))
  13330. (member tag work-already-expanded))
  13331. (setq tags-in-group (assoc tag taggroups))
  13332. (push tag work-already-expanded)
  13333. ;; Recursively expand each tag in the group, if the tag hasn't
  13334. ;; already been expanded. Restore the match-data after all recursive calls.
  13335. (save-match-data
  13336. (let (tags-expanded)
  13337. (dolist (x (cdr tags-in-group))
  13338. (if (and (member x taggroups-keys)
  13339. (not (member x work-already-expanded)))
  13340. (setq tags-expanded
  13341. (delete-dups
  13342. (append
  13343. (org-tags-expand x t downcased
  13344. work-already-expanded)
  13345. tags-expanded)))
  13346. (setq tags-expanded
  13347. (append (list x) tags-expanded)))
  13348. (setq work-already-expanded
  13349. (delete-dups
  13350. (append tags-expanded
  13351. work-already-expanded))))
  13352. (setq tags-in-group
  13353. (delete-dups (cons (car tags-in-group)
  13354. tags-expanded)))))
  13355. ;; Filter tag-regexps from tags.
  13356. (setq regexp-in-group-escaped
  13357. (delq nil (mapcar (lambda (x)
  13358. (if (stringp x)
  13359. (and (equal "{" (substring x 0 1))
  13360. (equal "}" (substring x -1))
  13361. x)
  13362. x))
  13363. tags-in-group))
  13364. regexp-in-group
  13365. (mapcar (lambda (x)
  13366. (substring x 1 -1))
  13367. regexp-in-group-escaped)
  13368. tags-in-group
  13369. (delq nil (mapcar (lambda (x)
  13370. (if (stringp x)
  13371. (and (not (equal "{" (substring x 0 1)))
  13372. (not (equal "}" (substring x -1)))
  13373. x)
  13374. x))
  13375. tags-in-group)))
  13376. ;; If single-as-list, do no more in the while-loop.
  13377. (if (not single-as-list)
  13378. (progn
  13379. (when regexp-in-group
  13380. (setq regexp-in-group
  13381. (concat "\\|"
  13382. (mapconcat 'identity regexp-in-group
  13383. "\\|"))))
  13384. (setq tags-in-group
  13385. (concat dir
  13386. "{\\<"
  13387. (regexp-opt tags-in-group)
  13388. "\\>"
  13389. regexp-in-group
  13390. "}"))
  13391. (when (stringp tags-in-group)
  13392. (org-add-props tags-in-group '(grouptag t)))
  13393. (setq return-match
  13394. (replace-match tags-in-group t t return-match)))
  13395. (setq tags-in-group
  13396. (append regexp-in-group-escaped tags-in-group))))
  13397. (setq taggroups-keys (delete tag taggroups-keys))))
  13398. ;; Add the regular expressions back into the match-expression again.
  13399. (while regexps-in-match
  13400. (setq return-match (replace-regexp-in-string (format "<%d>" count)
  13401. (pop regexps-in-match)
  13402. return-match t t))
  13403. (decf count))
  13404. (if single-as-list
  13405. (if tags-in-group tags-in-group (list return-match))
  13406. return-match))
  13407. (if single-as-list
  13408. (list (if downcased (downcase match) match))
  13409. match)))
  13410. (defun org-op-to-function (op &optional stringp)
  13411. "Turn an operator into the appropriate function."
  13412. (setq op
  13413. (cond
  13414. ((equal op "<" ) '(< string< org-time<))
  13415. ((equal op ">" ) '(> org-string> org-time>))
  13416. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  13417. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  13418. ((member op '("=" "==")) '(= string= org-time=))
  13419. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  13420. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  13421. (defun org<> (a b) (not (= a b)))
  13422. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  13423. (defun org-string>= (a b) (not (string< a b)))
  13424. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  13425. (defun org-string<> (a b) (not (string= a b)))
  13426. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  13427. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  13428. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  13429. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  13430. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  13431. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  13432. (defun org-2ft (s)
  13433. "Convert S to a floating point time.
  13434. If S is already a number, just return it. If it is a string, parse
  13435. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  13436. (cond
  13437. ((numberp s) s)
  13438. ((stringp s)
  13439. (condition-case nil
  13440. (float-time (apply 'encode-time (org-parse-time-string s)))
  13441. (error 0.)))
  13442. (t 0.)))
  13443. (defun org-time-today ()
  13444. "Time in seconds today at 0:00.
  13445. Returns the float number of seconds since the beginning of the
  13446. epoch to the beginning of today (00:00)."
  13447. (float-time (apply 'encode-time
  13448. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  13449. (defun org-matcher-time (s)
  13450. "Interpret a time comparison value."
  13451. (save-match-data
  13452. (cond
  13453. ((string= s "<now>") (float-time))
  13454. ((string= s "<today>") (org-time-today))
  13455. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  13456. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  13457. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  13458. (+ (org-time-today)
  13459. (* (string-to-number (match-string 1 s))
  13460. (cdr (assoc (match-string 2 s)
  13461. '(("d" . 86400.0) ("w" . 604800.0)
  13462. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  13463. (t (org-2ft s)))))
  13464. (defun org-match-any-p (re list)
  13465. "Does re match any element of list?"
  13466. (setq list (mapcar (lambda (x) (string-match re x)) list))
  13467. (delq nil list))
  13468. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  13469. (defvar org-tags-overlay (make-overlay 1 1))
  13470. (org-detach-overlay org-tags-overlay)
  13471. (defun org-get-local-tags-at (&optional pos)
  13472. "Get a list of tags defined in the current headline."
  13473. (org-get-tags-at pos 'local))
  13474. (defun org-get-local-tags ()
  13475. "Get a list of tags defined in the current headline."
  13476. (org-get-tags-at nil 'local))
  13477. (defun org-get-tags-at (&optional pos local)
  13478. "Get a list of all headline tags applicable at POS.
  13479. POS defaults to point. If tags are inherited, the list contains
  13480. the targets in the same sequence as the headlines appear, i.e.
  13481. the tags of the current headline come last.
  13482. When LOCAL is non-nil, only return tags from the current headline,
  13483. ignore inherited ones."
  13484. (interactive)
  13485. (if (and org-trust-scanner-tags
  13486. (or (not pos) (equal pos (point)))
  13487. (not local))
  13488. org-scanner-tags
  13489. (let (tags ltags lastpos parent)
  13490. (save-excursion
  13491. (save-restriction
  13492. (widen)
  13493. (goto-char (or pos (point)))
  13494. (save-match-data
  13495. (catch 'done
  13496. (condition-case nil
  13497. (progn
  13498. (org-back-to-heading t)
  13499. (while (not (equal lastpos (point)))
  13500. (setq lastpos (point))
  13501. (when (looking-at
  13502. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  13503. (setq ltags (org-split-string
  13504. (org-match-string-no-properties 1) ":"))
  13505. (when parent
  13506. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  13507. (setq tags (append
  13508. (if parent
  13509. (org-remove-uninherited-tags ltags)
  13510. ltags)
  13511. tags)))
  13512. (or org-use-tag-inheritance (throw 'done t))
  13513. (if local (throw 'done t))
  13514. (or (org-up-heading-safe) (error nil))
  13515. (setq parent t)))
  13516. (error nil)))))
  13517. (if local
  13518. tags
  13519. (reverse (delete-dups
  13520. (reverse (append
  13521. (org-remove-uninherited-tags
  13522. org-file-tags)
  13523. tags)))))))))
  13524. (defun org-add-prop-inherited (s)
  13525. (add-text-properties 0 (length s) '(inherited t) s)
  13526. s)
  13527. (defun org-toggle-tag (tag &optional onoff)
  13528. "Toggle the tag TAG for the current line.
  13529. If ONOFF is `on' or `off', don't toggle but set to this state."
  13530. (let (res current)
  13531. (save-excursion
  13532. (org-back-to-heading t)
  13533. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  13534. (point-at-eol) t)
  13535. (progn
  13536. (setq current (match-string 1))
  13537. (replace-match ""))
  13538. (setq current ""))
  13539. (setq current (nreverse (org-split-string current ":")))
  13540. (cond
  13541. ((eq onoff 'on)
  13542. (setq res t)
  13543. (or (member tag current) (push tag current)))
  13544. ((eq onoff 'off)
  13545. (or (not (member tag current)) (setq current (delete tag current))))
  13546. (t (if (member tag current)
  13547. (setq current (delete tag current))
  13548. (setq res t)
  13549. (push tag current))))
  13550. (end-of-line 1)
  13551. (if current
  13552. (progn
  13553. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  13554. (org-set-tags nil t))
  13555. (delete-horizontal-space))
  13556. (run-hooks 'org-after-tags-change-hook))
  13557. res))
  13558. (defun org-align-tags-here (to-col)
  13559. ;; Assumes that this is a headline
  13560. "Align tags on the current headline to TO-COL."
  13561. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  13562. (beginning-of-line 1)
  13563. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13564. (< pos (match-beginning 2)))
  13565. (progn
  13566. (setq tags-l (- (match-end 2) (match-beginning 2)))
  13567. (goto-char (match-beginning 1))
  13568. (insert " ")
  13569. (delete-region (point) (1+ (match-beginning 2)))
  13570. (setq ncol (max (current-column)
  13571. (1+ col)
  13572. (if (> to-col 0)
  13573. to-col
  13574. (- (abs to-col) tags-l))))
  13575. (setq p (point))
  13576. (insert (make-string (- ncol (current-column)) ?\ ))
  13577. (setq ncol (current-column))
  13578. (when indent-tabs-mode (tabify p (point-at-eol)))
  13579. (org-move-to-column (min ncol col)))
  13580. (goto-char pos))))
  13581. (defun org-set-tags-command (&optional arg just-align)
  13582. "Call the set-tags command for the current entry."
  13583. (interactive "P")
  13584. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  13585. (org-set-tags arg just-align)
  13586. (save-excursion
  13587. (unless (and (org-region-active-p)
  13588. org-loop-over-headlines-in-active-region)
  13589. (org-back-to-heading t))
  13590. (org-set-tags arg just-align))))
  13591. (defun org-set-tags-to (data)
  13592. "Set the tags of the current entry to DATA, replacing the current tags.
  13593. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  13594. If DATA is nil or the empty string, any tags will be removed."
  13595. (interactive "sTags: ")
  13596. (setq data
  13597. (cond
  13598. ((eq data nil) "")
  13599. ((equal data "") "")
  13600. ((stringp data)
  13601. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  13602. ":"))
  13603. ((listp data)
  13604. (concat ":" (mapconcat 'identity data ":") ":"))))
  13605. (when data
  13606. (save-excursion
  13607. (org-back-to-heading t)
  13608. (when (looking-at org-complex-heading-regexp)
  13609. (if (match-end 5)
  13610. (progn
  13611. (goto-char (match-beginning 5))
  13612. (insert data)
  13613. (delete-region (point) (point-at-eol))
  13614. (org-set-tags nil 'align))
  13615. (goto-char (point-at-eol))
  13616. (insert " " data)
  13617. (org-set-tags nil 'align)))
  13618. (beginning-of-line 1)
  13619. (if (looking-at ".*?\\([ \t]+\\)$")
  13620. (delete-region (match-beginning 1) (match-end 1))))))
  13621. (defun org-align-all-tags ()
  13622. "Align the tags i all headings."
  13623. (interactive)
  13624. (save-excursion
  13625. (or (ignore-errors (org-back-to-heading t))
  13626. (outline-next-heading))
  13627. (if (org-at-heading-p)
  13628. (org-set-tags t)
  13629. (message "No headings"))))
  13630. (defvar org-indent-indentation-per-level)
  13631. (defun org-set-tags (&optional arg just-align)
  13632. "Set the tags for the current headline.
  13633. With prefix ARG, realign all tags in headings in the current buffer.
  13634. When JUST-ALIGN is non-nil, only align tags."
  13635. (interactive "P")
  13636. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  13637. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  13638. 'region-start-level
  13639. 'region))
  13640. org-loop-over-headlines-in-active-region)
  13641. (org-map-entries
  13642. ;; We don't use ARG and JUST-ALIGN here because these args
  13643. ;; are not useful when looping over headlines.
  13644. #'org-set-tags
  13645. org-loop-over-headlines-in-active-region
  13646. cl
  13647. '(when (outline-invisible-p) (org-end-of-subtree nil t))))
  13648. (let ((org-setting-tags t))
  13649. (if arg
  13650. (save-excursion
  13651. (goto-char (point-min))
  13652. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  13653. (while (re-search-forward org-outline-regexp-bol nil t)
  13654. (org-set-tags nil t)
  13655. (end-of-line)))
  13656. (message "All tags realigned to column %d" org-tags-column))
  13657. (let* ((current (org-get-tags-string))
  13658. (col (current-column))
  13659. (tags
  13660. (if just-align current
  13661. ;; Get a new set of tags from the user.
  13662. (save-excursion
  13663. (let* ((table
  13664. (setq
  13665. org-last-tags-completion-table
  13666. (append
  13667. org-tag-persistent-alist
  13668. (or org-tag-alist (org-get-buffer-tags))
  13669. (and
  13670. org-complete-tags-always-offer-all-agenda-tags
  13671. (org-global-tags-completion-table
  13672. (org-agenda-files))))))
  13673. (current-tags (org-split-string current ":"))
  13674. (inherited-tags
  13675. (nreverse (nthcdr (length current-tags)
  13676. (nreverse (org-get-tags-at))))))
  13677. (replace-regexp-in-string
  13678. "\\([-+&]+\\|,\\)"
  13679. ":"
  13680. (if (or (eq t org-use-fast-tag-selection)
  13681. (and org-use-fast-tag-selection
  13682. (delq nil (mapcar #'cdr table))))
  13683. (org-fast-tag-selection
  13684. current-tags inherited-tags table
  13685. (and org-fast-tag-selection-include-todo
  13686. org-todo-key-alist))
  13687. (let ((org-add-colon-after-tag-completion
  13688. (< 1 (length table))))
  13689. (org-trim
  13690. (completing-read
  13691. "Tags: "
  13692. #'org-tags-completion-function
  13693. nil nil current 'org-tags-history))))))))))
  13694. (when org-tags-sort-function
  13695. (setq tags
  13696. (mapconcat
  13697. #'identity
  13698. (sort (org-split-string tags (org-re "[^[:alnum:]_@#%]+"))
  13699. org-tags-sort-function)
  13700. ":")))
  13701. (if (not (org-string-nw-p tags)) (setq tags "")
  13702. (unless (string-match ":\\'" tags) (setq tags (concat tags ":")))
  13703. (unless (string-match "\\`:" tags) (setq tags (concat ":" tags))))
  13704. ;; Insert new tags at the correct column
  13705. (beginning-of-line)
  13706. (let ((level (if (looking-at org-outline-regexp)
  13707. (- (match-end 0) (point) 1)
  13708. 1)))
  13709. (cond
  13710. ((and (equal current "") (equal tags "")))
  13711. ((re-search-forward
  13712. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  13713. (line-end-position)
  13714. t)
  13715. (if (equal tags "") (replace-match "" t t)
  13716. (goto-char (match-beginning 0))
  13717. (let* ((c0 (current-column))
  13718. ;; Compute offset for the case of org-indent-mode
  13719. ;; active.
  13720. (di (if (org-bound-and-true-p org-indent-mode)
  13721. (* (1- org-indent-indentation-per-level)
  13722. (1- level))
  13723. 0))
  13724. (p0 (if (eq (char-before) ?*) (1+ (point)) (point)))
  13725. (tc (+ org-tags-column
  13726. (if (> org-tags-column 0) (- di) di)))
  13727. (c1 (max (1+ c0)
  13728. (if (> tc 0) tc
  13729. (- (- tc) (string-width tags)))))
  13730. (rpl (concat (make-string (max 0 (- c1 c0)) ?\s) tags)))
  13731. (replace-match rpl t t)
  13732. (when (and (not (featurep 'xemacs)) indent-tabs-mode)
  13733. (tabify p0 (point))))))
  13734. (t (error "Tags alignment failed"))))
  13735. (org-move-to-column col))
  13736. (unless just-align (run-hooks 'org-after-tags-change-hook))))))
  13737. (defun org-change-tag-in-region (beg end tag off)
  13738. "Add or remove TAG for each entry in the region.
  13739. This works in the agenda, and also in an org-mode buffer."
  13740. (interactive
  13741. (list (region-beginning) (region-end)
  13742. (let ((org-last-tags-completion-table
  13743. (if (derived-mode-p 'org-mode)
  13744. (org-uniquify
  13745. (delq nil (append (org-get-buffer-tags)
  13746. (org-global-tags-completion-table))))
  13747. (org-global-tags-completion-table))))
  13748. (org-icompleting-read
  13749. "Tag: " 'org-tags-completion-function nil nil nil
  13750. 'org-tags-history))
  13751. (progn
  13752. (message "[s]et or [r]emove? ")
  13753. (equal (read-char-exclusive) ?r))))
  13754. (if (fboundp 'deactivate-mark) (deactivate-mark))
  13755. (let ((agendap (equal major-mode 'org-agenda-mode))
  13756. l1 l2 m buf pos newhead (cnt 0))
  13757. (goto-char end)
  13758. (setq l2 (1- (org-current-line)))
  13759. (goto-char beg)
  13760. (setq l1 (org-current-line))
  13761. (loop for l from l1 to l2 do
  13762. (org-goto-line l)
  13763. (setq m (get-text-property (point) 'org-hd-marker))
  13764. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  13765. (and agendap m))
  13766. (setq buf (if agendap (marker-buffer m) (current-buffer))
  13767. pos (if agendap m (point)))
  13768. (with-current-buffer buf
  13769. (save-excursion
  13770. (save-restriction
  13771. (goto-char pos)
  13772. (setq cnt (1+ cnt))
  13773. (org-toggle-tag tag (if off 'off 'on))
  13774. (setq newhead (org-get-heading)))))
  13775. (and agendap (org-agenda-change-all-lines newhead m))))
  13776. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  13777. (defun org-tags-completion-function (string predicate &optional flag)
  13778. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  13779. (confirm (lambda (x) (stringp (car x)))))
  13780. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  13781. (setq s1 (match-string 1 string)
  13782. s2 (match-string 2 string))
  13783. (setq s1 "" s2 string))
  13784. (cond
  13785. ((eq flag nil)
  13786. ;; try completion
  13787. (setq rtn (try-completion s2 ctable confirm))
  13788. (if (stringp rtn)
  13789. (setq rtn
  13790. (concat s1 s2 (substring rtn (length s2))
  13791. (if (and org-add-colon-after-tag-completion
  13792. (assoc rtn ctable))
  13793. ":" ""))))
  13794. rtn)
  13795. ((eq flag t)
  13796. ;; all-completions
  13797. (all-completions s2 ctable confirm))
  13798. ((eq flag 'lambda)
  13799. ;; exact match?
  13800. (assoc s2 ctable)))))
  13801. (defun org-fast-tag-insert (kwd tags face &optional end)
  13802. "Insert KDW, and the TAGS, the latter with face FACE.
  13803. Also insert END."
  13804. (insert (format "%-12s" (concat kwd ":"))
  13805. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  13806. (or end "")))
  13807. (defun org-fast-tag-show-exit (flag)
  13808. (save-excursion
  13809. (org-goto-line 3)
  13810. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  13811. (replace-match ""))
  13812. (when flag
  13813. (end-of-line 1)
  13814. (org-move-to-column (- (window-width) 19) t)
  13815. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  13816. (defun org-set-current-tags-overlay (current prefix)
  13817. "Add an overlay to CURRENT tag with PREFIX."
  13818. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  13819. (if (featurep 'xemacs)
  13820. (org-overlay-display org-tags-overlay (concat prefix s)
  13821. 'secondary-selection)
  13822. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  13823. (org-overlay-display org-tags-overlay (concat prefix s)))))
  13824. (defvar org-last-tag-selection-key nil)
  13825. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  13826. "Fast tag selection with single keys.
  13827. CURRENT is the current list of tags in the headline, INHERITED is the
  13828. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  13829. possibly with grouping information. TODO-TABLE is a similar table with
  13830. TODO keywords, should these have keys assigned to them.
  13831. If the keys are nil, a-z are automatically assigned.
  13832. Returns the new tags string, or nil to not change the current settings."
  13833. (let* ((fulltable (append table todo-table))
  13834. (maxlen (apply 'max (mapcar
  13835. (lambda (x)
  13836. (if (stringp (car x)) (string-width (car x)) 0))
  13837. fulltable)))
  13838. (buf (current-buffer))
  13839. (expert (eq org-fast-tag-selection-single-key 'expert))
  13840. (buffer-tags nil)
  13841. (fwidth (+ maxlen 3 1 3))
  13842. (ncol (/ (- (window-width) 4) fwidth))
  13843. (i-face 'org-done)
  13844. (c-face 'org-todo)
  13845. tg cnt e c char c1 c2 ntable tbl rtn
  13846. ov-start ov-end ov-prefix
  13847. (exit-after-next org-fast-tag-selection-single-key)
  13848. (done-keywords org-done-keywords)
  13849. groups ingroup intaggroup)
  13850. (save-excursion
  13851. (beginning-of-line 1)
  13852. (if (looking-at
  13853. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13854. (setq ov-start (match-beginning 1)
  13855. ov-end (match-end 1)
  13856. ov-prefix "")
  13857. (setq ov-start (1- (point-at-eol))
  13858. ov-end (1+ ov-start))
  13859. (skip-chars-forward "^\n\r")
  13860. (setq ov-prefix
  13861. (concat
  13862. (buffer-substring (1- (point)) (point))
  13863. (if (> (current-column) org-tags-column)
  13864. " "
  13865. (make-string (- org-tags-column (current-column)) ?\ ))))))
  13866. (move-overlay org-tags-overlay ov-start ov-end)
  13867. (save-window-excursion
  13868. (if expert
  13869. (set-buffer (get-buffer-create " *Org tags*"))
  13870. (delete-other-windows)
  13871. (set-window-buffer (split-window-vertically) (get-buffer-create " *Org tags*"))
  13872. (org-switch-to-buffer-other-window " *Org tags*"))
  13873. (erase-buffer)
  13874. (org-set-local 'org-done-keywords done-keywords)
  13875. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  13876. (org-fast-tag-insert "Current" current c-face "\n\n")
  13877. (org-fast-tag-show-exit exit-after-next)
  13878. (org-set-current-tags-overlay current ov-prefix)
  13879. (setq tbl fulltable char ?a cnt 0)
  13880. (while (setq e (pop tbl))
  13881. (cond
  13882. ((eq (car e) :startgroup)
  13883. (push '() groups) (setq ingroup t)
  13884. (unless (zerop cnt)
  13885. (setq cnt 0)
  13886. (insert "\n"))
  13887. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  13888. ((eq (car e) :endgroup)
  13889. (setq ingroup nil cnt 0)
  13890. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  13891. ((eq (car e) :startgrouptag)
  13892. (setq intaggroup t)
  13893. (unless (zerop cnt)
  13894. (setq cnt 0)
  13895. (insert "\n"))
  13896. (insert "[ "))
  13897. ((eq (car e) :endgrouptag)
  13898. (setq intaggroup nil cnt 0)
  13899. (insert "]\n"))
  13900. ((equal e '(:newline))
  13901. (unless (zerop cnt)
  13902. (setq cnt 0)
  13903. (insert "\n")
  13904. (setq e (car tbl))
  13905. (while (equal (car tbl) '(:newline))
  13906. (insert "\n")
  13907. (setq tbl (cdr tbl)))))
  13908. ((equal e '(:grouptags)) (insert " : "))
  13909. (t
  13910. (setq tg (copy-sequence (car e)) c2 nil)
  13911. (if (cdr e)
  13912. (setq c (cdr e))
  13913. ;; automatically assign a character.
  13914. (setq c1 (string-to-char
  13915. (downcase (substring
  13916. tg (if (= (string-to-char tg) ?@) 1 0)))))
  13917. (if (or (rassoc c1 ntable) (rassoc c1 table))
  13918. (while (or (rassoc char ntable) (rassoc char table))
  13919. (setq char (1+ char)))
  13920. (setq c2 c1))
  13921. (setq c (or c2 char)))
  13922. (when ingroup (push tg (car groups)))
  13923. (setq tg (org-add-props tg nil 'face
  13924. (cond
  13925. ((not (assoc tg table))
  13926. (org-get-todo-face tg))
  13927. ((member tg current) c-face)
  13928. ((member tg inherited) i-face))))
  13929. (when (equal (caar tbl) :grouptags)
  13930. (org-add-props tg nil 'face 'org-tag-group))
  13931. (when (and (zerop cnt) (not ingroup) (not intaggroup)) (insert " "))
  13932. (insert "[" c "] " tg (make-string
  13933. (- fwidth 4 (length tg)) ?\ ))
  13934. (push (cons tg c) ntable)
  13935. (when (= (incf cnt) ncol)
  13936. (insert "\n")
  13937. (when (or ingroup intaggroup) (insert " "))
  13938. (setq cnt 0)))))
  13939. (setq ntable (nreverse ntable))
  13940. (insert "\n")
  13941. (goto-char (point-min))
  13942. (unless expert (org-fit-window-to-buffer))
  13943. (setq rtn
  13944. (catch 'exit
  13945. (while t
  13946. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  13947. (if (not groups) "no " "")
  13948. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  13949. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13950. (setq org-last-tag-selection-key c)
  13951. (cond
  13952. ((= c ?\r) (throw 'exit t))
  13953. ((= c ?!)
  13954. (setq groups (not groups))
  13955. (goto-char (point-min))
  13956. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  13957. ((= c ?\C-c)
  13958. (if (not expert)
  13959. (org-fast-tag-show-exit
  13960. (setq exit-after-next (not exit-after-next)))
  13961. (setq expert nil)
  13962. (delete-other-windows)
  13963. (set-window-buffer (split-window-vertically) " *Org tags*")
  13964. (org-switch-to-buffer-other-window " *Org tags*")
  13965. (org-fit-window-to-buffer)))
  13966. ((or (= c ?\C-g)
  13967. (and (= c ?q) (not (rassoc c ntable))))
  13968. (org-detach-overlay org-tags-overlay)
  13969. (setq quit-flag t))
  13970. ((= c ?\ )
  13971. (setq current nil)
  13972. (when exit-after-next (setq exit-after-next 'now)))
  13973. ((= c ?\t)
  13974. (condition-case nil
  13975. (setq tg (org-icompleting-read
  13976. "Tag: "
  13977. (or buffer-tags
  13978. (with-current-buffer buf
  13979. (setq buffer-tags
  13980. (org-get-buffer-tags))))))
  13981. (quit (setq tg "")))
  13982. (when (string-match "\\S-" tg)
  13983. (add-to-list 'buffer-tags (list tg))
  13984. (if (member tg current)
  13985. (setq current (delete tg current))
  13986. (push tg current)))
  13987. (when exit-after-next (setq exit-after-next 'now)))
  13988. ((setq e (rassoc c todo-table) tg (car e))
  13989. (with-current-buffer buf
  13990. (save-excursion (org-todo tg)))
  13991. (when exit-after-next (setq exit-after-next 'now)))
  13992. ((setq e (rassoc c ntable) tg (car e))
  13993. (if (member tg current)
  13994. (setq current (delete tg current))
  13995. (loop for g in groups do
  13996. (when (member tg g)
  13997. (dolist (x g) (setq current (delete x current)))))
  13998. (push tg current))
  13999. (when exit-after-next (setq exit-after-next 'now))))
  14000. ;; Create a sorted list
  14001. (setq current
  14002. (sort current
  14003. (lambda (a b)
  14004. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  14005. (when (eq exit-after-next 'now) (throw 'exit t))
  14006. (goto-char (point-min))
  14007. (beginning-of-line 2)
  14008. (delete-region (point) (point-at-eol))
  14009. (org-fast-tag-insert "Current" current c-face)
  14010. (org-set-current-tags-overlay current ov-prefix)
  14011. (while (re-search-forward
  14012. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  14013. (setq tg (match-string 1))
  14014. (add-text-properties
  14015. (match-beginning 1) (match-end 1)
  14016. (list 'face
  14017. (cond
  14018. ((member tg current) c-face)
  14019. ((member tg inherited) i-face)
  14020. (t (get-text-property (match-beginning 1) 'face))))))
  14021. (goto-char (point-min)))))
  14022. (org-detach-overlay org-tags-overlay)
  14023. (if rtn
  14024. (mapconcat 'identity current ":")
  14025. nil))))
  14026. (defun org-get-tags-string ()
  14027. "Get the TAGS string in the current headline."
  14028. (unless (org-at-heading-p t)
  14029. (user-error "Not on a heading"))
  14030. (save-excursion
  14031. (beginning-of-line 1)
  14032. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  14033. (org-match-string-no-properties 1)
  14034. "")))
  14035. (defun org-get-tags ()
  14036. "Get the list of tags specified in the current headline."
  14037. (org-split-string (org-get-tags-string) ":"))
  14038. (defun org-get-buffer-tags ()
  14039. "Get a table of all tags used in the buffer, for completion."
  14040. (org-with-wide-buffer
  14041. (goto-char (point-min))
  14042. (let ((tag-re (concat org-outline-regexp-bol
  14043. "\\(?:.*?[ \t]\\)?"
  14044. (org-re ":\\([[:alnum:]_@#%:]+\\):[ \t]*$")))
  14045. tags)
  14046. (while (re-search-forward tag-re nil t)
  14047. (dolist (tag (org-split-string (org-match-string-no-properties 1) ":"))
  14048. (push tag tags)))
  14049. (mapcar #'list (append org-file-tags (org-uniquify tags))))))
  14050. ;;;; The mapping API
  14051. (defun org-map-entries (func &optional match scope &rest skip)
  14052. "Call FUNC at each headline selected by MATCH in SCOPE.
  14053. FUNC is a function or a lisp form. The function will be called without
  14054. arguments, with the cursor positioned at the beginning of the headline.
  14055. The return values of all calls to the function will be collected and
  14056. returned as a list.
  14057. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  14058. does not need to preserve point. After evaluation, the cursor will be
  14059. moved to the end of the line (presumably of the headline of the
  14060. processed entry) and search continues from there. Under some
  14061. circumstances, this may not produce the wanted results. For example,
  14062. if you have removed (e.g. archived) the current (sub)tree it could
  14063. mean that the next entry will be skipped entirely. In such cases, you
  14064. can specify the position from where search should continue by making
  14065. FUNC set the variable `org-map-continue-from' to the desired buffer
  14066. position.
  14067. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  14068. Only headlines that are matched by this query will be considered during
  14069. the iteration. When MATCH is nil or t, all headlines will be
  14070. visited by the iteration.
  14071. SCOPE determines the scope of this command. It can be any of:
  14072. nil The current buffer, respecting the restriction if any
  14073. tree The subtree started with the entry at point
  14074. region The entries within the active region, if any
  14075. region-start-level
  14076. The entries within the active region, but only those at
  14077. the same level than the first one.
  14078. file The current buffer, without restriction
  14079. file-with-archives
  14080. The current buffer, and any archives associated with it
  14081. agenda All agenda files
  14082. agenda-with-archives
  14083. All agenda files with any archive files associated with them
  14084. \(file1 file2 ...)
  14085. If this is a list, all files in the list will be scanned
  14086. The remaining args are treated as settings for the skipping facilities of
  14087. the scanner. The following items can be given here:
  14088. archive skip trees with the archive tag
  14089. comment skip trees with the COMMENT keyword
  14090. function or Emacs Lisp form:
  14091. will be used as value for `org-agenda-skip-function', so
  14092. whenever the function returns a position, FUNC will not be
  14093. called for that entry and search will continue from the
  14094. position returned
  14095. If your function needs to retrieve the tags including inherited tags
  14096. at the *current* entry, you can use the value of the variable
  14097. `org-scanner-tags' which will be much faster than getting the value
  14098. with `org-get-tags-at'. If your function gets properties with
  14099. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  14100. to t around the call to `org-entry-properties' to get the same speedup.
  14101. Note that if your function moves around to retrieve tags and properties at
  14102. a *different* entry, you cannot use these techniques."
  14103. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  14104. (not (org-region-active-p)))
  14105. (let* ((org-agenda-archives-mode nil) ; just to make sure
  14106. (org-agenda-skip-archived-trees (memq 'archive skip))
  14107. (org-agenda-skip-comment-trees (memq 'comment skip))
  14108. (org-agenda-skip-function
  14109. (car (org-delete-all '(comment archive) skip)))
  14110. (org-tags-match-list-sublevels t)
  14111. (start-level (eq scope 'region-start-level))
  14112. matcher file res
  14113. org-todo-keywords-for-agenda
  14114. org-done-keywords-for-agenda
  14115. org-todo-keyword-alist-for-agenda
  14116. org-tag-alist-for-agenda
  14117. todo-only)
  14118. (cond
  14119. ((eq match t) (setq matcher t))
  14120. ((eq match nil) (setq matcher t))
  14121. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  14122. (save-excursion
  14123. (save-restriction
  14124. (cond ((eq scope 'tree)
  14125. (org-back-to-heading t)
  14126. (org-narrow-to-subtree)
  14127. (setq scope nil))
  14128. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  14129. (org-region-active-p))
  14130. ;; If needed, set start-level to a string like "2"
  14131. (when start-level
  14132. (save-excursion
  14133. (goto-char (region-beginning))
  14134. (unless (org-at-heading-p) (outline-next-heading))
  14135. (setq start-level (org-current-level))))
  14136. (narrow-to-region (region-beginning)
  14137. (save-excursion
  14138. (goto-char (region-end))
  14139. (unless (and (bolp) (org-at-heading-p))
  14140. (outline-next-heading))
  14141. (point)))
  14142. (setq scope nil)))
  14143. (if (not scope)
  14144. (progn
  14145. (org-agenda-prepare-buffers
  14146. (and buffer-file-name (list buffer-file-name)))
  14147. (setq res (org-scan-tags func matcher todo-only start-level)))
  14148. ;; Get the right scope
  14149. (cond
  14150. ((and scope (listp scope) (symbolp (car scope)))
  14151. (setq scope (eval scope)))
  14152. ((eq scope 'agenda)
  14153. (setq scope (org-agenda-files t)))
  14154. ((eq scope 'agenda-with-archives)
  14155. (setq scope (org-agenda-files t))
  14156. (setq scope (org-add-archive-files scope)))
  14157. ((eq scope 'file)
  14158. (setq scope (and buffer-file-name (list buffer-file-name))))
  14159. ((eq scope 'file-with-archives)
  14160. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  14161. (org-agenda-prepare-buffers scope)
  14162. (dolist (file scope)
  14163. (with-current-buffer (org-find-base-buffer-visiting file)
  14164. (save-excursion
  14165. (save-restriction
  14166. (widen)
  14167. (goto-char (point-min))
  14168. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  14169. res)))
  14170. ;;; Properties API
  14171. (defconst org-special-properties
  14172. '("ALLTAGS" "BLOCKED" "CLOCKSUM" "CLOCKSUM_T" "CLOSED" "DEADLINE" "FILE"
  14173. "ITEM" "PRIORITY" "SCHEDULED" "TAGS" "TIMESTAMP" "TIMESTAMP_IA" "TODO")
  14174. "The special properties valid in Org mode.
  14175. These are properties that are not defined in the property drawer,
  14176. but in some other way.")
  14177. (defconst org-default-properties
  14178. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  14179. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  14180. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  14181. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  14182. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE" "UNNUMBERED"
  14183. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  14184. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  14185. "Some properties that are used by Org mode for various purposes.
  14186. Being in this list makes sure that they are offered for completion.")
  14187. (defun org--update-property-plist (key val props)
  14188. "Associate KEY to VAL in alist PROPS.
  14189. Modifications are made by side-effect. Return new alist."
  14190. (let* ((appending (string= (substring key -1) "+"))
  14191. (key (if appending (substring key 0 -1) key))
  14192. (old (assoc-string key props t)))
  14193. (if (not old) (cons (cons key val) props)
  14194. (setcdr old (if appending (concat (cdr old) " " val) val))
  14195. props)))
  14196. (defun org-get-property-block (&optional beg force)
  14197. "Return the (beg . end) range of the body of the property drawer.
  14198. BEG is the beginning of the current subtree, or of the part
  14199. before the first headline. If it is not given, it will be found.
  14200. If the drawer does not exist, create it if FORCE is non-nil, or
  14201. return nil."
  14202. (org-with-wide-buffer
  14203. (when beg (goto-char beg))
  14204. (unless (org-before-first-heading-p)
  14205. (let ((beg (cond (beg)
  14206. ((or (not (featurep 'org-inlinetask))
  14207. (org-inlinetask-in-task-p))
  14208. (org-back-to-heading t))
  14209. (t (org-with-limited-levels (org-back-to-heading t))))))
  14210. (forward-line)
  14211. (when (org-looking-at-p org-planning-line-re) (forward-line))
  14212. (cond ((looking-at org-property-drawer-re)
  14213. (forward-line)
  14214. (cons (point) (progn (goto-char (match-end 0))
  14215. (line-beginning-position))))
  14216. (force
  14217. (goto-char beg)
  14218. (org-insert-property-drawer)
  14219. (let ((pos (save-excursion (search-forward ":END:")
  14220. (line-beginning-position))))
  14221. (cons pos pos))))))))
  14222. (defun org-at-property-p ()
  14223. "Non-nil when point is inside a property drawer.
  14224. See `org-property-re' for match data, if applicable."
  14225. (save-excursion
  14226. (beginning-of-line)
  14227. (and (looking-at org-property-re)
  14228. (let ((property-drawer (save-match-data (org-get-property-block))))
  14229. (and property-drawer
  14230. (>= (point) (car property-drawer))
  14231. (< (point) (cdr property-drawer)))))))
  14232. (defun org-property-action ()
  14233. "Do an action on properties."
  14234. (interactive)
  14235. (unless (org-at-property-p) (user-error "Not at a property"))
  14236. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  14237. (let ((c (read-char-exclusive)))
  14238. (case c
  14239. (?s (call-interactively #'org-set-property))
  14240. (?d (call-interactively #'org-delete-property))
  14241. (?D (call-interactively #'org-delete-property-globally))
  14242. (?c (call-interactively #'org-compute-property-at-point))
  14243. (otherwise (user-error "No such property action %c" c)))))
  14244. (defun org-inc-effort ()
  14245. "Increment the value of the effort property in the current entry."
  14246. (interactive)
  14247. (org-set-effort nil t))
  14248. (defvar org-clock-effort) ; Defined in org-clock.el.
  14249. (defvar org-clock-current-task) ; Defined in org-clock.el.
  14250. (defun org-set-effort (&optional value increment)
  14251. "Set the effort property of the current entry.
  14252. With numerical prefix arg, use the nth allowed value, 0 stands for the
  14253. 10th allowed value.
  14254. When INCREMENT is non-nil, set the property to the next allowed value."
  14255. (interactive "P")
  14256. (if (equal value 0) (setq value 10))
  14257. (let* ((completion-ignore-case t)
  14258. (prop org-effort-property)
  14259. (cur (org-entry-get nil prop))
  14260. (allowed (org-property-get-allowed-values nil prop 'table))
  14261. (existing (mapcar 'list (org-property-values prop)))
  14262. (heading (nth 4 (org-heading-components)))
  14263. rpl
  14264. (val (cond
  14265. ((stringp value) value)
  14266. ((and allowed (integerp value))
  14267. (or (car (nth (1- value) allowed))
  14268. (car (org-last allowed))))
  14269. ((and allowed increment)
  14270. (or (caadr (member (list cur) allowed))
  14271. (user-error "Allowed effort values are not set")))
  14272. (allowed
  14273. (message "Select 1-9,0, [RET%s]: %s"
  14274. (if cur (concat "=" cur) "")
  14275. (mapconcat 'car allowed " "))
  14276. (setq rpl (read-char-exclusive))
  14277. (if (equal rpl ?\r)
  14278. cur
  14279. (setq rpl (- rpl ?0))
  14280. (if (equal rpl 0) (setq rpl 10))
  14281. (if (and (> rpl 0) (<= rpl (length allowed)))
  14282. (car (nth (1- rpl) allowed))
  14283. (org-completing-read "Effort: " allowed nil))))
  14284. (t
  14285. (let (org-completion-use-ido org-completion-use-iswitchb)
  14286. (org-completing-read
  14287. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  14288. (concat "[" cur "]") "")
  14289. ": ")
  14290. existing nil nil "" nil cur))))))
  14291. (unless (equal (org-entry-get nil prop) val)
  14292. (org-entry-put nil prop val))
  14293. (org-refresh-property
  14294. '((effort . identity)
  14295. (effort-minutes . org-duration-string-to-minutes))
  14296. val)
  14297. (when (string= heading org-clock-current-task)
  14298. (setq org-clock-effort (get-text-property (point-at-bol) 'effort))
  14299. (org-clock-update-mode-line))
  14300. (message "%s is now %s" prop val)))
  14301. (defun org-entry-properties (&optional pom which)
  14302. "Get all properties of the current entry.
  14303. When POM is a buffer position, get all properties from the entry
  14304. there instead.
  14305. This includes the TODO keyword, the tags, time strings for
  14306. deadline, scheduled, and clocking, and any additional properties
  14307. defined in the entry.
  14308. If WHICH is nil or `all', get all properties. If WHICH is
  14309. `special' or `standard', only get that subclass. If WHICH is
  14310. a string, only get that property.
  14311. Return value is an alist. Keys are properties, as upcased
  14312. strings."
  14313. (org-with-point-at pom
  14314. (when (and (derived-mode-p 'org-mode)
  14315. (ignore-errors (org-back-to-heading t)))
  14316. (catch 'exit
  14317. (let* ((beg (point))
  14318. (specific (and (stringp which) (upcase which)))
  14319. (which (cond ((not specific) which)
  14320. ((member specific org-special-properties) 'special)
  14321. (t 'standard)))
  14322. props)
  14323. ;; Get the special properties, like TODO and TAGS.
  14324. (when (memq which '(nil all special))
  14325. (when (or (not specific) (string= specific "CLOCKSUM"))
  14326. (let ((clocksum (get-text-property (point) :org-clock-minutes)))
  14327. (when clocksum
  14328. (push (cons "CLOCKSUM"
  14329. (org-columns-number-to-string
  14330. (/ (float clocksum) 60.) 'add_times))
  14331. props)))
  14332. (when specific (throw 'exit props)))
  14333. (when (or (not specific) (string= specific "CLOCKSUM_T"))
  14334. (let ((clocksumt (get-text-property (point)
  14335. :org-clock-minutes-today)))
  14336. (when clocksumt
  14337. (push (cons "CLOCKSUM_T"
  14338. (org-columns-number-to-string
  14339. (/ (float clocksumt) 60.) 'add_times))
  14340. props)))
  14341. (when specific (throw 'exit props)))
  14342. (when (or (not specific) (string= specific "ITEM"))
  14343. (when (looking-at org-complex-heading-regexp)
  14344. (push (cons "ITEM"
  14345. (concat
  14346. (org-match-string-no-properties 1)
  14347. (let ((title (org-match-string-no-properties 4)))
  14348. (when (org-string-nw-p title)
  14349. (concat " " (org-remove-tabs title))))))
  14350. props))
  14351. (when specific (throw 'exit props)))
  14352. (when (or (not specific) (string= specific "TODO"))
  14353. (let ((case-fold-search nil))
  14354. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  14355. (push (cons "TODO" (org-match-string-no-properties 2)) props)))
  14356. (when specific (throw 'exit props)))
  14357. (when (or (not specific) (string= specific "PRIORITY"))
  14358. (push (cons "PRIORITY"
  14359. (if (looking-at org-priority-regexp)
  14360. (org-match-string-no-properties 2)
  14361. (char-to-string org-default-priority)))
  14362. props)
  14363. (when specific (throw 'exit props)))
  14364. (when (or (not specific) (string= specific "FILE"))
  14365. (push (cons "FILE" (buffer-file-name (buffer-base-buffer)))
  14366. props)
  14367. (when specific (throw 'exit props)))
  14368. (when (or (not specific) (string= specific "TAGS"))
  14369. (let ((value (org-string-nw-p (org-get-tags-string))))
  14370. (when value (push (cons "TAGS" value) props)))
  14371. (when specific (throw 'exit props)))
  14372. (when (or (not specific) (string= specific "ALLTAGS"))
  14373. (let ((value (org-get-tags-at)))
  14374. (when value
  14375. (push (cons "ALLTAGS"
  14376. (format ":%s:" (mapconcat #'identity value ":")))
  14377. props)))
  14378. (when specific (throw 'exit props)))
  14379. (when (or (not specific) (string= specific "BLOCKED"))
  14380. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props)
  14381. (when specific (throw 'exit props)))
  14382. (when (or (not specific)
  14383. (member specific '("CLOSED" "DEADLINE" "SCHEDULED")))
  14384. (forward-line)
  14385. (when (org-looking-at-p org-planning-line-re)
  14386. (end-of-line)
  14387. (let ((bol (line-beginning-position))
  14388. ;; Backward compatibility: time keywords used to
  14389. ;; be configurable (before 8.3). Make sure we
  14390. ;; get the correct keyword.
  14391. (key-assoc `(("CLOSED" . ,org-closed-string)
  14392. ("DEADLINE" . ,org-deadline-string)
  14393. ("SCHEDULED" . ,org-scheduled-string))))
  14394. (dolist (pair (if specific (list (assoc specific key-assoc))
  14395. key-assoc))
  14396. (save-excursion
  14397. (when (search-backward (cdr pair) bol t)
  14398. (goto-char (match-end 0))
  14399. (skip-chars-forward " \t")
  14400. (and (looking-at org-ts-regexp-both)
  14401. (push (cons (car pair)
  14402. (org-match-string-no-properties 0))
  14403. props)))))))
  14404. (when specific (throw 'exit props)))
  14405. (when (or (not specific)
  14406. (member specific '("TIMESTAMP" "TIMESTAMP_IA")))
  14407. (let ((find-ts
  14408. (lambda (end ts)
  14409. (let ((regexp (cond
  14410. ((string= specific "TIMESTAMP")
  14411. org-ts-regexp)
  14412. ((string= specific "TIMESTAMP_IA")
  14413. org-ts-regexp-inactive)
  14414. ((assoc "TIMESTAMP_IA" ts)
  14415. org-ts-regexp)
  14416. ((assoc "TIMESTAMP" ts)
  14417. org-ts-regexp-inactive)
  14418. (t org-ts-regexp-both))))
  14419. (catch 'next
  14420. (while (re-search-forward regexp end t)
  14421. (backward-char)
  14422. (let ((object (org-element-context)))
  14423. ;; Accept to match timestamps in node
  14424. ;; properties, too.
  14425. (when (memq (org-element-type object)
  14426. '(node-property timestamp))
  14427. (let ((type
  14428. (org-element-property :type object)))
  14429. (cond
  14430. ((and (memq type '(active active-range))
  14431. (not (equal specific "TIMESTAMP_IA")))
  14432. (unless (assoc "TIMESTAMP" ts)
  14433. (push (cons "TIMESTAMP"
  14434. (org-element-property
  14435. :raw-value object))
  14436. ts)
  14437. (when specific (throw 'exit ts))))
  14438. ((and (memq type '(inactive inactive-range))
  14439. (not (string= specific "TIMESTAMP")))
  14440. (unless (assoc "TIMESTAMP_IA" ts)
  14441. (push (cons "TIMESTAMP_IA"
  14442. (org-element-property
  14443. :raw-value object))
  14444. ts)
  14445. (when specific (throw 'exit ts))))))
  14446. ;; Both timestamp types are found,
  14447. ;; move to next part.
  14448. (when (= (length ts) 2) (throw 'next ts)))))
  14449. ts)))))
  14450. (goto-char beg)
  14451. ;; First look for timestamps within headline.
  14452. (let ((ts (funcall find-ts (line-end-position) nil)))
  14453. (if (= (length ts) 2) (setq props (nconc ts props))
  14454. (forward-line)
  14455. ;; Then find timestamps in the section, skipping
  14456. ;; planning line.
  14457. (when (org-looking-at-p org-planning-line-re)
  14458. (forward-line))
  14459. (let ((end (save-excursion (outline-next-heading))))
  14460. (setq props (nconc (funcall find-ts end ts) props))))))))
  14461. ;; Get the standard properties, like :PROP:.
  14462. (when (memq which '(nil all standard))
  14463. ;; If we are looking after a specific property, delegate
  14464. ;; to `org-entry-get', which is faster. However, make an
  14465. ;; exception for "CATEGORY", since it can be also set
  14466. ;; through keywords (i.e. #+CATEGORY).
  14467. (if (and specific (not (equal specific "CATEGORY")))
  14468. (let ((value (org-entry-get beg specific nil t)))
  14469. (throw 'exit (and value (list (cons specific value)))))
  14470. (let ((range (org-get-property-block beg)))
  14471. (when range
  14472. (let ((end (cdr range)) seen-base)
  14473. (goto-char (car range))
  14474. ;; Unlike to `org--update-property-plist', we
  14475. ;; handle the case where base values is found
  14476. ;; after its extension. We also forbid standard
  14477. ;; properties to be named as special properties.
  14478. (while (re-search-forward org-property-re end t)
  14479. (let* ((key (upcase (org-match-string-no-properties 2)))
  14480. (extendp (org-string-match-p "\\+\\'" key))
  14481. (key-base (if extendp (substring key 0 -1) key))
  14482. (value (org-match-string-no-properties 3)))
  14483. (cond
  14484. ((member-ignore-case key-base org-special-properties))
  14485. (extendp
  14486. (setq props
  14487. (org--update-property-plist key value props)))
  14488. ((member key seen-base))
  14489. (t (push key seen-base)
  14490. (let ((p (assoc-string key props t)))
  14491. (if p (setcdr p (concat value " " (cdr p)))
  14492. (push (cons key value) props))))))))))))
  14493. (unless (assoc "CATEGORY" props)
  14494. (push (cons "CATEGORY" (org-get-category beg)) props)
  14495. (when (string= specific "CATEGORY") (throw 'exit props)))
  14496. ;; Return value.
  14497. props)))))
  14498. (defun org-property--local-values (property literal-nil)
  14499. "Return value for PROPERTY in current entry.
  14500. Value is a list whose care is the base value for PROPERTY and cdr
  14501. a list of accumulated values. Return nil if neither is found in
  14502. the entry. Also return nil when PROPERTY is set to \"nil\",
  14503. unless LITERAL-NIL is non-nil."
  14504. (let ((range (org-get-property-block)))
  14505. (when range
  14506. (goto-char (car range))
  14507. (let* ((case-fold-search t)
  14508. (end (cdr range))
  14509. (value
  14510. ;; Base value.
  14511. (save-excursion
  14512. (let ((v (and (re-search-forward
  14513. (org-re-property property nil t) end t)
  14514. (org-match-string-no-properties 3))))
  14515. (list (if literal-nil v (org-not-nil v)))))))
  14516. ;; Find additional values.
  14517. (let* ((property+ (org-re-property (concat property "+") nil t)))
  14518. (while (re-search-forward property+ end t)
  14519. (push (org-match-string-no-properties 3) value)))
  14520. ;; Return final values.
  14521. (and (not (equal value '(nil))) (nreverse value))))))
  14522. (defun org-entry-get (pom property &optional inherit literal-nil)
  14523. "Get value of PROPERTY for entry or content at point-or-marker POM.
  14524. If INHERIT is non-nil and the entry does not have the property,
  14525. then also check higher levels of the hierarchy. If INHERIT is
  14526. the symbol `selective', use inheritance only if the setting in
  14527. `org-use-property-inheritance' selects PROPERTY for inheritance.
  14528. If the property is present but empty, the return value is the
  14529. empty string. If the property is not present at all, nil is
  14530. returned. In any other case, return the value as a string.
  14531. Search is case-insensitive.
  14532. If LITERAL-NIL is set, return the string value \"nil\" as
  14533. a string, do not interpret it as the list atom nil. This is used
  14534. for inheritance when a \"nil\" value can supersede a non-nil
  14535. value higher up the hierarchy."
  14536. (org-with-point-at pom
  14537. (cond
  14538. ((member-ignore-case property (cons "CATEGORY" org-special-properties))
  14539. ;; We need a special property. Use `org-entry-properties' to
  14540. ;; retrieve it, but specify the wanted property.
  14541. (cdr (assoc-string property (org-entry-properties nil property))))
  14542. ((and inherit
  14543. (or (not (eq inherit 'selective)) (org-property-inherit-p property)))
  14544. (org-entry-get-with-inheritance property literal-nil))
  14545. (t
  14546. (let* ((local (org-property--local-values property literal-nil))
  14547. (value (and local (mapconcat #'identity (delq nil local) " "))))
  14548. (if literal-nil value (org-not-nil value)))))))
  14549. (defun org-property-or-variable-value (var &optional inherit)
  14550. "Check if there is a property fixing the value of VAR.
  14551. If yes, return this value. If not, return the current value of the variable."
  14552. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  14553. (if (and prop (stringp prop) (string-match "\\S-" prop))
  14554. (read prop)
  14555. (symbol-value var))))
  14556. (defun org-entry-delete (pom property)
  14557. "Delete PROPERTY from entry at point-or-marker POM.
  14558. Accumulated properties, i.e. PROPERTY+, are also removed. Return
  14559. non-nil when a property was removed."
  14560. (unless (member property org-special-properties)
  14561. (org-with-point-at pom
  14562. (let ((range (org-get-property-block)))
  14563. (when range
  14564. (let* ((begin (car range))
  14565. (origin (cdr range))
  14566. (end (copy-marker origin))
  14567. (re (org-re-property
  14568. (concat (regexp-quote property) "\\+?") t t)))
  14569. (goto-char begin)
  14570. (while (re-search-forward re end t)
  14571. (delete-region (match-beginning 0) (line-beginning-position 2)))
  14572. ;; If drawer is empty, remove it altogether.
  14573. (when (= begin end)
  14574. (delete-region (line-beginning-position 0)
  14575. (line-beginning-position 2)))
  14576. ;; Return non-nil if some property was removed.
  14577. (prog1 (/= end origin) (set-marker end nil))))))))
  14578. ;; Multi-values properties are properties that contain multiple values
  14579. ;; These values are assumed to be single words, separated by whitespace.
  14580. (defun org-entry-add-to-multivalued-property (pom property value)
  14581. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  14582. (let* ((old (org-entry-get pom property))
  14583. (values (and old (org-split-string old "[ \t]"))))
  14584. (setq value (org-entry-protect-space value))
  14585. (unless (member value values)
  14586. (setq values (append values (list value)))
  14587. (org-entry-put pom property
  14588. (mapconcat 'identity values " ")))))
  14589. (defun org-entry-remove-from-multivalued-property (pom property value)
  14590. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  14591. (let* ((old (org-entry-get pom property))
  14592. (values (and old (org-split-string old "[ \t]"))))
  14593. (setq value (org-entry-protect-space value))
  14594. (when (member value values)
  14595. (setq values (delete value values))
  14596. (org-entry-put pom property
  14597. (mapconcat 'identity values " ")))))
  14598. (defun org-entry-member-in-multivalued-property (pom property value)
  14599. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  14600. (let* ((old (org-entry-get pom property))
  14601. (values (and old (org-split-string old "[ \t]"))))
  14602. (setq value (org-entry-protect-space value))
  14603. (member value values)))
  14604. (defun org-entry-get-multivalued-property (pom property)
  14605. "Return a list of values in a multivalued property."
  14606. (let* ((value (org-entry-get pom property))
  14607. (values (and value (org-split-string value "[ \t]"))))
  14608. (mapcar 'org-entry-restore-space values)))
  14609. (defun org-entry-put-multivalued-property (pom property &rest values)
  14610. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  14611. VALUES should be a list of strings. Spaces will be protected."
  14612. (org-entry-put pom property
  14613. (mapconcat 'org-entry-protect-space values " "))
  14614. (let* ((value (org-entry-get pom property))
  14615. (values (and value (org-split-string value "[ \t]"))))
  14616. (mapcar 'org-entry-restore-space values)))
  14617. (defun org-entry-protect-space (s)
  14618. "Protect spaces and newline in string S."
  14619. (while (string-match " " s)
  14620. (setq s (replace-match "%20" t t s)))
  14621. (while (string-match "\n" s)
  14622. (setq s (replace-match "%0A" t t s)))
  14623. s)
  14624. (defun org-entry-restore-space (s)
  14625. "Restore spaces and newline in string S."
  14626. (while (string-match "%20" s)
  14627. (setq s (replace-match " " t t s)))
  14628. (while (string-match "%0A" s)
  14629. (setq s (replace-match "\n" t t s)))
  14630. s)
  14631. (defvar org-entry-property-inherited-from (make-marker)
  14632. "Marker pointing to the entry from where a property was inherited.
  14633. Each call to `org-entry-get-with-inheritance' will set this marker to the
  14634. location of the entry where the inheritance search matched. If there was
  14635. no match, the marker will point nowhere.
  14636. Note that also `org-entry-get' calls this function, if the INHERIT flag
  14637. is set.")
  14638. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  14639. "Get PROPERTY of entry or content at point, search higher levels if needed.
  14640. The search will stop at the first ancestor which has the property defined.
  14641. If the value found is \"nil\", return nil to show that the property
  14642. should be considered as undefined (this is the meaning of nil here).
  14643. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  14644. (move-marker org-entry-property-inherited-from nil)
  14645. (org-with-wide-buffer
  14646. (let (value)
  14647. (catch 'exit
  14648. (while t
  14649. (let ((v (org-property--local-values property literal-nil)))
  14650. (when v
  14651. (setq value
  14652. (concat (mapconcat #'identity (delq nil v) " ")
  14653. (and value " ")
  14654. value)))
  14655. (cond
  14656. ((car v)
  14657. (org-back-to-heading t)
  14658. (move-marker org-entry-property-inherited-from (point))
  14659. (throw 'exit nil))
  14660. ((org-up-heading-safe))
  14661. (t
  14662. (let ((global
  14663. (cdr (or (assoc-string property org-file-properties t)
  14664. (assoc-string property org-global-properties t)
  14665. (assoc-string property org-global-properties-fixed t)))))
  14666. (cond ((not global))
  14667. (value (setq value (concat global " " value)))
  14668. (t (setq value global))))
  14669. (throw 'exit nil))))))
  14670. (if literal-nil value (org-not-nil value)))))
  14671. (defvar org-property-changed-functions nil
  14672. "Hook called when the value of a property has changed.
  14673. Each hook function should accept two arguments, the name of the property
  14674. and the new value.")
  14675. (defun org-entry-put (pom property value)
  14676. "Set PROPERTY to VALUE for entry at point-or-marker POM.
  14677. If the value is nil, it is converted to the empty string. If
  14678. it is not a string, an error is raised.
  14679. PROPERTY can be any regular property (see
  14680. `org-special-properties'). It can also be \"TODO\",
  14681. \"PRIORITY\", \"SCHEDULED\" and \"DEADLINE\".
  14682. For the last two properties, VALUE may have any of the special
  14683. values \"earlier\" and \"later\". The function then increases or
  14684. decreases scheduled or deadline date by one day."
  14685. (cond ((null value) (setq value ""))
  14686. ((not (stringp value)) (error "Properties values should be strings")))
  14687. (org-with-point-at pom
  14688. (if (or (not (featurep 'org-inlinetask)) (org-inlinetask-in-task-p))
  14689. (org-back-to-heading t)
  14690. (org-with-limited-levels (org-back-to-heading t)))
  14691. (let ((beg (point)))
  14692. (cond
  14693. ((equal property "TODO")
  14694. (cond ((not (org-string-nw-p value)) (setq value 'none))
  14695. ((not (member value org-todo-keywords-1))
  14696. (user-error "\"%s\" is not a valid TODO state" value)))
  14697. (org-todo value)
  14698. (org-set-tags nil 'align))
  14699. ((equal property "PRIORITY")
  14700. (org-priority (if (org-string-nw-p value) (string-to-char value) ?\s))
  14701. (org-set-tags nil 'align))
  14702. ((equal property "SCHEDULED")
  14703. (forward-line)
  14704. (if (and (org-looking-at-p org-planning-line-re)
  14705. (re-search-forward
  14706. org-scheduled-time-regexp (line-end-position) t))
  14707. (cond ((string= value "earlier") (org-timestamp-change -1 'day))
  14708. ((string= value "later") (org-timestamp-change 1 'day))
  14709. ((string= value "") (org-schedule '(4)))
  14710. (t (org-schedule nil value)))
  14711. (if (member value '("earlier" "later" ""))
  14712. (call-interactively #'org-schedule)
  14713. (org-schedule nil value))))
  14714. ((equal property "DEADLINE")
  14715. (forward-line)
  14716. (if (and (org-looking-at-p org-planning-line-re)
  14717. (re-search-forward
  14718. org-deadline-time-regexp (line-end-position) t))
  14719. (cond ((string= value "earlier") (org-timestamp-change -1 'day))
  14720. ((string= value "later") (org-timestamp-change 1 'day))
  14721. ((string= value "") (org-deadline '(4)))
  14722. (t (org-deadline nil value)))
  14723. (if (member value '("earlier" "later" ""))
  14724. (call-interactively #'org-deadline)
  14725. (org-deadline nil value))))
  14726. ((member property org-special-properties)
  14727. (error "The %s property cannot be set with `org-entry-put'" property))
  14728. (t
  14729. (let* ((range (org-get-property-block beg 'force))
  14730. (end (cdr range))
  14731. (case-fold-search t))
  14732. (goto-char (car range))
  14733. (if (re-search-forward (org-re-property property nil t) end t)
  14734. (progn (delete-region (match-beginning 0) (match-end 0))
  14735. (goto-char (match-beginning 0)))
  14736. (goto-char end)
  14737. (insert "\n")
  14738. (backward-char))
  14739. (insert ":" property ":")
  14740. (when value (insert " " value))
  14741. (org-indent-line)))))
  14742. (run-hook-with-args 'org-property-changed-functions property value)))
  14743. (defun org-buffer-property-keys (&optional specials defaults columns)
  14744. "Get all property keys in the current buffer.
  14745. When SPECIALS is non-nil, also list the special properties that
  14746. reflect things like tags and TODO state.
  14747. When DEFAULTS is non-nil, also include properties that has
  14748. special meaning internally: ARCHIVE, CATEGORY, SUMMARY,
  14749. DESCRIPTION, LOCATION, and LOGGING and others.
  14750. When COLUMNS in non-nil, also include property names given in
  14751. COLUMN formats in the current buffer."
  14752. (let ((case-fold-search t)
  14753. (props (append
  14754. (and specials org-special-properties)
  14755. (and defaults (cons org-effort-property org-default-properties))
  14756. nil)))
  14757. (org-with-wide-buffer
  14758. (goto-char (point-min))
  14759. (while (re-search-forward org-property-start-re nil t)
  14760. (let ((range (org-get-property-block)))
  14761. (catch 'skip
  14762. (unless range
  14763. (when (and (not (org-before-first-heading-p))
  14764. (y-or-n-p (format "Malformed drawer at %d, repair?"
  14765. (line-beginning-position))))
  14766. (org-get-property-block nil t))
  14767. (throw 'skip nil))
  14768. (goto-char (car range))
  14769. (let ((begin (car range))
  14770. (end (cdr range)))
  14771. ;; Make sure that found property block is not located
  14772. ;; before current point, as it would generate an infloop.
  14773. ;; It can happen, for example, in the following
  14774. ;; situation:
  14775. ;;
  14776. ;; * Headline
  14777. ;; :PROPERTIES:
  14778. ;; ...
  14779. ;; :END:
  14780. ;; *************** Inlinetask
  14781. ;; #+BEGIN_EXAMPLE
  14782. ;; :PROPERTIES:
  14783. ;; #+END_EXAMPLE
  14784. ;;
  14785. (if (< begin (point)) (throw 'skip nil) (goto-char begin))
  14786. (while (< (point) end)
  14787. (let ((p (progn (looking-at org-property-re)
  14788. (org-match-string-no-properties 2))))
  14789. ;; Only add true property name, not extension symbol.
  14790. (add-to-list 'props
  14791. (if (not (org-string-match-p "\\+\\'" p)) p
  14792. (substring p 0 -1))))
  14793. (forward-line))))
  14794. (outline-next-heading)))
  14795. (when columns
  14796. (goto-char (point-min))
  14797. (while (re-search-forward "^[ \t]*\\(?:#\\+\\|:\\)COLUMNS:" nil t)
  14798. (let ((element (org-element-at-point)))
  14799. (when (memq (org-element-type element) '(keyword node-property))
  14800. (let ((value (org-element-property :value element))
  14801. (start 0))
  14802. (while (string-match "%[0-9]*\\(\\S-+\\)" value start)
  14803. (setq start (match-end 0))
  14804. (let ((p (org-match-string-no-properties 1 value)))
  14805. (unless (member-ignore-case p org-special-properties)
  14806. (add-to-list 'props p))))))))))
  14807. (sort props (lambda (a b) (string< (upcase a) (upcase b))))))
  14808. (defun org-property-values (key)
  14809. "List all non-nil values of property KEY in current buffer."
  14810. (org-with-wide-buffer
  14811. (goto-char (point-min))
  14812. (let ((case-fold-search t)
  14813. (re (org-re-property key))
  14814. values)
  14815. (while (re-search-forward re nil t)
  14816. (add-to-list 'values (org-entry-get (point) key)))
  14817. values)))
  14818. (defun org-insert-property-drawer ()
  14819. "Insert a property drawer into the current entry."
  14820. (org-with-wide-buffer
  14821. (if (or (not (featurep 'org-inlinetask)) (org-inlinetask-in-task-p))
  14822. (org-back-to-heading t)
  14823. (org-with-limited-levels (org-back-to-heading t)))
  14824. (forward-line)
  14825. (when (org-looking-at-p org-planning-line-re) (forward-line))
  14826. (unless (org-looking-at-p org-property-drawer-re)
  14827. ;; Make sure we start editing a line from current entry, not from
  14828. ;; next one. It prevents extending text properties or overlays
  14829. ;; belonging to the latter.
  14830. (when (bolp) (backward-char))
  14831. (let ((begin (1+ (point)))
  14832. (inhibit-read-only t))
  14833. (insert "\n:PROPERTIES:\n:END:")
  14834. (when (eobp) (insert "\n"))
  14835. (org-indent-region begin (point))))))
  14836. (defun org-insert-drawer (&optional arg drawer)
  14837. "Insert a drawer at point.
  14838. When optional argument ARG is non-nil, insert a property drawer.
  14839. Optional argument DRAWER, when non-nil, is a string representing
  14840. drawer's name. Otherwise, the user is prompted for a name.
  14841. If a region is active, insert the drawer around that region
  14842. instead.
  14843. Point is left between drawer's boundaries."
  14844. (interactive "P")
  14845. (let* ((drawer (if arg "PROPERTIES"
  14846. (or drawer (read-from-minibuffer "Drawer: ")))))
  14847. (cond
  14848. ;; With C-u, fall back on `org-insert-property-drawer'
  14849. (arg (org-insert-property-drawer))
  14850. ;; Check validity of suggested drawer's name.
  14851. ((not (org-string-match-p org-drawer-regexp (format ":%s:" drawer)))
  14852. (user-error "Invalid drawer name"))
  14853. ;; With an active region, insert a drawer at point.
  14854. ((not (org-region-active-p))
  14855. (progn
  14856. (unless (bolp) (insert "\n"))
  14857. (insert (format ":%s:\n\n:END:\n" drawer))
  14858. (forward-line -2)))
  14859. ;; Otherwise, insert the drawer at point
  14860. (t
  14861. (let ((rbeg (region-beginning))
  14862. (rend (copy-marker (region-end))))
  14863. (unwind-protect
  14864. (progn
  14865. (goto-char rbeg)
  14866. (beginning-of-line)
  14867. (when (save-excursion
  14868. (re-search-forward org-outline-regexp-bol rend t))
  14869. (user-error "Drawers cannot contain headlines"))
  14870. ;; Position point at the beginning of the first
  14871. ;; non-blank line in region. Insert drawer's opening
  14872. ;; there, then indent it.
  14873. (org-skip-whitespace)
  14874. (beginning-of-line)
  14875. (insert ":" drawer ":\n")
  14876. (forward-line -1)
  14877. (indent-for-tab-command)
  14878. ;; Move point to the beginning of the first blank line
  14879. ;; after the last non-blank line in region. Insert
  14880. ;; drawer's closing, then indent it.
  14881. (goto-char rend)
  14882. (skip-chars-backward " \r\t\n")
  14883. (insert "\n:END:")
  14884. (deactivate-mark t)
  14885. (indent-for-tab-command)
  14886. (unless (eolp) (insert "\n")))
  14887. ;; Clear marker, whatever the outcome of insertion is.
  14888. (set-marker rend nil)))))))
  14889. (defvar org-property-set-functions-alist nil
  14890. "Property set function alist.
  14891. Each entry should have the following format:
  14892. (PROPERTY . READ-FUNCTION)
  14893. The read function will be called with the same argument as
  14894. `org-completing-read'.")
  14895. (defun org-set-property-function (property)
  14896. "Get the function that should be used to set PROPERTY.
  14897. This is computed according to `org-property-set-functions-alist'."
  14898. (or (cdr (assoc property org-property-set-functions-alist))
  14899. 'org-completing-read))
  14900. (defun org-read-property-value (property)
  14901. "Read PROPERTY value from user."
  14902. (let* ((completion-ignore-case t)
  14903. (allowed (org-property-get-allowed-values nil property 'table))
  14904. (cur (org-entry-get nil property))
  14905. (prompt (concat property " value"
  14906. (if (and cur (string-match "\\S-" cur))
  14907. (concat " [" cur "]") "") ": "))
  14908. (set-function (org-set-property-function property))
  14909. (val (if allowed
  14910. (funcall set-function prompt allowed nil
  14911. (not (get-text-property 0 'org-unrestricted
  14912. (caar allowed))))
  14913. (let (org-completion-use-ido org-completion-use-iswitchb)
  14914. (funcall set-function prompt
  14915. (mapcar 'list (org-property-values property))
  14916. nil nil "" nil cur)))))
  14917. (org-trim val)))
  14918. (defvar org-last-set-property nil)
  14919. (defvar org-last-set-property-value nil)
  14920. (defun org-read-property-name ()
  14921. "Read a property name."
  14922. (let ((completion-ignore-case t)
  14923. (default-prop (or (and (org-at-property-p)
  14924. (org-match-string-no-properties 2))
  14925. org-last-set-property)))
  14926. (org-completing-read
  14927. (concat "Property"
  14928. (if default-prop (concat " [" default-prop "]") "")
  14929. ": ")
  14930. (mapcar #'list (org-buffer-property-keys nil t t))
  14931. nil nil nil nil default-prop)))
  14932. (defun org-set-property-and-value (use-last)
  14933. "Allow to set [PROPERTY]: [value] direction from prompt.
  14934. When use-default, don't even ask, just use the last
  14935. \"[PROPERTY]: [value]\" string from the history."
  14936. (interactive "P")
  14937. (let* ((completion-ignore-case t)
  14938. (pv (or (and use-last org-last-set-property-value)
  14939. (org-completing-read
  14940. "Enter a \"[Property]: [value]\" pair: "
  14941. nil nil nil nil nil
  14942. org-last-set-property-value)))
  14943. prop val)
  14944. (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
  14945. (setq prop (match-string 1 pv)
  14946. val (match-string 2 pv))
  14947. (org-set-property prop val))))
  14948. (defun org-set-property (property value)
  14949. "In the current entry, set PROPERTY to VALUE.
  14950. When called interactively, this will prompt for a property name, offering
  14951. completion on existing and default properties. And then it will prompt
  14952. for a value, offering completion either on allowed values (via an inherited
  14953. xxx_ALL property) or on existing values in other instances of this property
  14954. in the current file."
  14955. (interactive (list nil nil))
  14956. (let* ((property (or property (org-read-property-name)))
  14957. (value (or value (org-read-property-value property)))
  14958. (fn (cdr (assoc property org-properties-postprocess-alist))))
  14959. (setq org-last-set-property property)
  14960. (setq org-last-set-property-value (concat property ": " value))
  14961. ;; Possibly postprocess the inserted value:
  14962. (when fn (setq value (funcall fn value)))
  14963. (unless (equal (org-entry-get nil property) value)
  14964. (org-entry-put nil property value))))
  14965. (defun org-find-property (property &optional value)
  14966. "Find first entry in buffer that sets PROPERTY.
  14967. When optional argument VALUE is non-nil, only consider an entry
  14968. if it contains PROPERTY set to this value. If PROPERTY should be
  14969. explicitly set to nil, use string \"nil\" for VALUE.
  14970. Return position where the entry begins, or nil if there is no
  14971. such entry. If narrowing is in effect, only search the visible
  14972. part of the buffer."
  14973. (save-excursion
  14974. (goto-char (point-min))
  14975. (let ((case-fold-search t)
  14976. (re (org-re-property property nil (not value) value)))
  14977. (catch 'exit
  14978. (while (re-search-forward re nil t)
  14979. (when (if value (org-at-property-p)
  14980. (org-entry-get (point) property nil t))
  14981. (throw 'exit (progn (org-back-to-heading t) (point)))))))))
  14982. (defun org-delete-property (property)
  14983. "In the current entry, delete PROPERTY."
  14984. (interactive
  14985. (let* ((completion-ignore-case t)
  14986. (cat (org-entry-get (point) "CATEGORY"))
  14987. (props0 (org-entry-properties nil 'standard))
  14988. (props (if cat props0
  14989. (delete `("CATEGORY" . ,(org-get-category)) props0)))
  14990. (prop (if (< 1 (length props))
  14991. (org-icompleting-read "Property: " props nil t)
  14992. (caar props))))
  14993. (list prop)))
  14994. (if (not property)
  14995. (message "No property to delete in this entry")
  14996. (org-entry-delete nil property)
  14997. (message "Property \"%s\" deleted" property)))
  14998. (defun org-delete-property-globally (property)
  14999. "Remove PROPERTY globally, from all entries.
  15000. This function ignores narrowing, if any."
  15001. (interactive
  15002. (let* ((completion-ignore-case t)
  15003. (prop (org-icompleting-read
  15004. "Globally remove property: "
  15005. (mapcar #'list (org-buffer-property-keys)))))
  15006. (list prop)))
  15007. (org-with-wide-buffer
  15008. (goto-char (point-min))
  15009. (let ((count 0)
  15010. (re (org-re-property (concat (regexp-quote property) "\\+?") t t)))
  15011. (while (re-search-forward re nil t)
  15012. (when (org-entry-delete (point) property) (incf count)))
  15013. (message "Property \"%s\" removed from %d entries" property count))))
  15014. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  15015. (defun org-compute-property-at-point ()
  15016. "Compute the property at point.
  15017. This looks for an enclosing column format, extracts the operator and
  15018. then applies it to the property in the column format's scope."
  15019. (interactive)
  15020. (unless (org-at-property-p)
  15021. (user-error "Not at a property"))
  15022. (let ((prop (org-match-string-no-properties 2)))
  15023. (org-columns-get-format-and-top-level)
  15024. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  15025. (user-error "No operator defined for property %s" prop))
  15026. (org-columns-compute prop)))
  15027. (defvar org-property-allowed-value-functions nil
  15028. "Hook for functions supplying allowed values for a specific property.
  15029. The functions must take a single argument, the name of the property, and
  15030. return a flat list of allowed values. If \":ETC\" is one of
  15031. the values, this means that these values are intended as defaults for
  15032. completion, but that other values should be allowed too.
  15033. The functions must return nil if they are not responsible for this
  15034. property.")
  15035. (defun org-property-get-allowed-values (pom property &optional table)
  15036. "Get allowed values for the property PROPERTY.
  15037. When TABLE is non-nil, return an alist that can directly be used for
  15038. completion."
  15039. (let (vals)
  15040. (cond
  15041. ((equal property "TODO")
  15042. (setq vals (org-with-point-at pom
  15043. (append org-todo-keywords-1 '("")))))
  15044. ((equal property "PRIORITY")
  15045. (let ((n org-lowest-priority))
  15046. (while (>= n org-highest-priority)
  15047. (push (char-to-string n) vals)
  15048. (setq n (1- n)))))
  15049. ((equal property "CATEGORY"))
  15050. ((member property org-special-properties))
  15051. ((setq vals (run-hook-with-args-until-success
  15052. 'org-property-allowed-value-functions property)))
  15053. (t
  15054. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  15055. (when (and vals (string-match "\\S-" vals))
  15056. (setq vals (car (read-from-string (concat "(" vals ")"))))
  15057. (setq vals (mapcar (lambda (x)
  15058. (cond ((stringp x) x)
  15059. ((numberp x) (number-to-string x))
  15060. ((symbolp x) (symbol-name x))
  15061. (t "???")))
  15062. vals)))))
  15063. (when (member ":ETC" vals)
  15064. (setq vals (remove ":ETC" vals))
  15065. (org-add-props (car vals) '(org-unrestricted t)))
  15066. (if table (mapcar 'list vals) vals)))
  15067. (defun org-property-previous-allowed-value (&optional previous)
  15068. "Switch to the next allowed value for this property."
  15069. (interactive)
  15070. (org-property-next-allowed-value t))
  15071. (defun org-property-next-allowed-value (&optional previous)
  15072. "Switch to the next allowed value for this property."
  15073. (interactive)
  15074. (unless (org-at-property-p)
  15075. (user-error "Not at a property"))
  15076. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  15077. (key (match-string 2))
  15078. (value (match-string 3))
  15079. (allowed (or (org-property-get-allowed-values (point) key)
  15080. (and (member value '("[ ]" "[-]" "[X]"))
  15081. '("[ ]" "[X]"))))
  15082. (heading (save-match-data (nth 4 (org-heading-components))))
  15083. nval)
  15084. (unless allowed
  15085. (user-error "Allowed values for this property have not been defined"))
  15086. (if previous (setq allowed (reverse allowed)))
  15087. (if (member value allowed)
  15088. (setq nval (car (cdr (member value allowed)))))
  15089. (setq nval (or nval (car allowed)))
  15090. (if (equal nval value)
  15091. (user-error "Only one allowed value for this property"))
  15092. (org-at-property-p)
  15093. (replace-match (concat " :" key ": " nval) t t)
  15094. (org-indent-line)
  15095. (beginning-of-line 1)
  15096. (skip-chars-forward " \t")
  15097. (when (equal prop org-effort-property)
  15098. (org-refresh-property
  15099. '((effort . identity)
  15100. (effort-minutes . org-duration-string-to-minutes))
  15101. nval)
  15102. (when (string= org-clock-current-task heading)
  15103. (setq org-clock-effort nval)
  15104. (org-clock-update-mode-line)))
  15105. (run-hook-with-args 'org-property-changed-functions key nval)))
  15106. (defun org-find-olp (path &optional this-buffer)
  15107. "Return a marker pointing to the entry at outline path OLP.
  15108. If anything goes wrong, throw an error.
  15109. You can wrap this call to catch the error like this:
  15110. (condition-case msg
  15111. (org-mobile-locate-entry (match-string 4))
  15112. (error (nth 1 msg)))
  15113. The return value will then be either a string with the error message,
  15114. or a marker if everything is OK.
  15115. If THIS-BUFFER is set, the outline path does not contain a file,
  15116. only headings."
  15117. (let* ((file (if this-buffer buffer-file-name (pop path)))
  15118. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  15119. (level 1)
  15120. (lmin 1)
  15121. (lmax 1)
  15122. limit re end found pos heading cnt flevel)
  15123. (unless buffer (error "File not found :%s" file))
  15124. (with-current-buffer buffer
  15125. (save-excursion
  15126. (save-restriction
  15127. (widen)
  15128. (setq limit (point-max))
  15129. (goto-char (point-min))
  15130. (dolist (heading path)
  15131. (setq re (format org-complex-heading-regexp-format
  15132. (regexp-quote heading)))
  15133. (setq cnt 0 pos (point))
  15134. (while (re-search-forward re end t)
  15135. (setq level (- (match-end 1) (match-beginning 1)))
  15136. (if (and (>= level lmin) (<= level lmax))
  15137. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  15138. (when (= cnt 0) (error "Heading not found on level %d: %s"
  15139. lmax heading))
  15140. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  15141. lmax heading))
  15142. (goto-char found)
  15143. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  15144. (setq end (save-excursion (org-end-of-subtree t t))))
  15145. (when (org-at-heading-p)
  15146. (point-marker)))))))
  15147. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  15148. "Find node HEADING in BUFFER.
  15149. Return a marker to the heading if it was found, or nil if not.
  15150. If POS-ONLY is set, return just the position instead of a marker.
  15151. The heading text must match exact, but it may have a TODO keyword,
  15152. a priority cookie and tags in the standard locations."
  15153. (with-current-buffer (or buffer (current-buffer))
  15154. (save-excursion
  15155. (save-restriction
  15156. (widen)
  15157. (goto-char (point-min))
  15158. (let (case-fold-search)
  15159. (if (re-search-forward
  15160. (format org-complex-heading-regexp-format
  15161. (regexp-quote heading)) nil t)
  15162. (if pos-only
  15163. (match-beginning 0)
  15164. (move-marker (make-marker) (match-beginning 0)))))))))
  15165. (defun org-find-exact-heading-in-directory (heading &optional dir)
  15166. "Find Org node headline HEADING in all .org files in directory DIR.
  15167. When the target headline is found, return a marker to this location."
  15168. (let ((files (directory-files (or dir default-directory)
  15169. t "\\`[^.#].*\\.org\\'"))
  15170. visiting m buffer)
  15171. (catch 'found
  15172. (dolist (file files)
  15173. (message "trying %s" file)
  15174. (setq visiting (org-find-base-buffer-visiting file))
  15175. (setq buffer (or visiting (find-file-noselect file)))
  15176. (setq m (org-find-exact-headline-in-buffer
  15177. heading buffer))
  15178. (when (and (not m) (not visiting)) (kill-buffer buffer))
  15179. (and m (throw 'found m))))))
  15180. (defun org-find-entry-with-id (ident)
  15181. "Locate the entry that contains the ID property with exact value IDENT.
  15182. IDENT can be a string, a symbol or a number, this function will search for
  15183. the string representation of it.
  15184. Return the position where this entry starts, or nil if there is no such entry."
  15185. (interactive "sID: ")
  15186. (let ((id (cond
  15187. ((stringp ident) ident)
  15188. ((symbolp ident) (symbol-name ident))
  15189. ((numberp ident) (number-to-string ident))
  15190. (t (error "IDENT %s must be a string, symbol or number" ident)))))
  15191. (org-with-wide-buffer (org-find-property "ID" id))))
  15192. ;;;; Timestamps
  15193. (defvar org-last-changed-timestamp nil)
  15194. (defvar org-last-inserted-timestamp nil
  15195. "The last time stamp inserted with `org-insert-time-stamp'.")
  15196. (defvar org-ts-what) ; dynamically scoped parameter
  15197. (defun org-time-stamp (arg &optional inactive)
  15198. "Prompt for a date/time and insert a time stamp.
  15199. If the user specifies a time like HH:MM or if this command is
  15200. called with at least one prefix argument, the time stamp contains
  15201. the date and the time. Otherwise, only the date is included.
  15202. All parts of a date not specified by the user are filled in from
  15203. the timestamp at point, if any, or the current date/time
  15204. otherwise.
  15205. If there is already a timestamp at the cursor, it is replaced.
  15206. With two universal prefix arguments, insert an active timestamp
  15207. with the current time without prompting the user.
  15208. When called from lisp, the timestamp is inactive if INACTIVE is
  15209. non-nil."
  15210. (interactive "P")
  15211. (let* ((ts (cond
  15212. ((org-at-date-range-p t)
  15213. (match-string (if (< (point) (- (match-beginning 2) 2)) 1 2)))
  15214. ((org-at-timestamp-p t) (match-string 0))))
  15215. ;; Default time is either the timestamp at point or today.
  15216. ;; When entering a range, only the range start is considered.
  15217. (default-time (if (not ts) (current-time)
  15218. (apply #'encode-time (org-parse-time-string ts))))
  15219. (default-input (and ts (org-get-compact-tod ts)))
  15220. (repeater (and ts
  15221. (string-match "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ts)
  15222. (match-string 0 ts)))
  15223. org-time-was-given
  15224. org-end-time-was-given
  15225. (time
  15226. (and (if (equal arg '(16)) (current-time)
  15227. ;; Preserve `this-command' and `last-command'.
  15228. (let ((this-command this-command)
  15229. (last-command last-command))
  15230. (org-read-date
  15231. arg 'totime nil nil default-time default-input
  15232. inactive))))))
  15233. (cond
  15234. ((and ts
  15235. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  15236. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  15237. (insert "--")
  15238. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  15239. (ts
  15240. ;; Make sure we're on a timestamp. When in the middle of a date
  15241. ;; range, move arbitrarily to range end.
  15242. (unless (org-at-timestamp-p t)
  15243. (skip-chars-forward "-")
  15244. (org-at-timestamp-p t))
  15245. (replace-match "")
  15246. (setq org-last-changed-timestamp
  15247. (org-insert-time-stamp
  15248. time (or org-time-was-given arg)
  15249. inactive nil nil (list org-end-time-was-given)))
  15250. (when repeater
  15251. (backward-char)
  15252. (insert " " repeater)
  15253. (setq org-last-changed-timestamp
  15254. (concat (substring org-last-inserted-timestamp 0 -1)
  15255. " " repeater ">")))
  15256. (message "Timestamp updated"))
  15257. ((equal arg '(16)) (org-insert-time-stamp time t inactive))
  15258. (t (org-insert-time-stamp
  15259. time (or org-time-was-given arg) inactive nil nil
  15260. (list org-end-time-was-given))))))
  15261. ;; FIXME: can we use this for something else, like computing time differences?
  15262. (defun org-get-compact-tod (s)
  15263. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  15264. (let* ((t1 (match-string 1 s))
  15265. (h1 (string-to-number (match-string 2 s)))
  15266. (m1 (string-to-number (match-string 3 s)))
  15267. (t2 (and (match-end 4) (match-string 5 s)))
  15268. (h2 (and t2 (string-to-number (match-string 6 s))))
  15269. (m2 (and t2 (string-to-number (match-string 7 s))))
  15270. dh dm)
  15271. (if (not t2)
  15272. t1
  15273. (setq dh (- h2 h1) dm (- m2 m1))
  15274. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  15275. (concat t1 "+" (number-to-string dh)
  15276. (and (/= 0 dm) (format ":%02d" dm)))))))
  15277. (defun org-time-stamp-inactive (&optional arg)
  15278. "Insert an inactive time stamp.
  15279. An inactive time stamp is enclosed in square brackets instead of angle
  15280. brackets. It is inactive in the sense that it does not trigger agenda entries,
  15281. does not link to the calendar and cannot be changed with the S-cursor keys.
  15282. So these are more for recording a certain time/date."
  15283. (interactive "P")
  15284. (org-time-stamp arg 'inactive))
  15285. (defvar org-date-ovl (make-overlay 1 1))
  15286. (overlay-put org-date-ovl 'face 'org-date-selected)
  15287. (org-detach-overlay org-date-ovl)
  15288. (defvar org-ans1) ; dynamically scoped parameter
  15289. (defvar org-ans2) ; dynamically scoped parameter
  15290. (defvar org-plain-time-of-day-regexp) ; defined below
  15291. (defvar org-overriding-default-time nil) ; dynamically scoped
  15292. (defvar org-read-date-overlay nil)
  15293. (defvar org-dcst nil) ; dynamically scoped
  15294. (defvar org-read-date-history nil)
  15295. (defvar org-read-date-final-answer nil)
  15296. (defvar org-read-date-analyze-futurep nil)
  15297. (defvar org-read-date-analyze-forced-year nil)
  15298. (defvar org-read-date-inactive)
  15299. (defvar org-read-date-minibuffer-local-map
  15300. (let* ((map (make-sparse-keymap)))
  15301. (set-keymap-parent map minibuffer-local-map)
  15302. (org-defkey map (kbd ".")
  15303. (lambda () (interactive)
  15304. ;; Are we at the beginning of the prompt?
  15305. (if (looking-back "^[^:]+: ")
  15306. (org-eval-in-calendar '(calendar-goto-today))
  15307. (insert "."))))
  15308. (org-defkey map (kbd "C-.")
  15309. (lambda () (interactive)
  15310. (org-eval-in-calendar '(calendar-goto-today))))
  15311. (org-defkey map [(meta shift left)]
  15312. (lambda () (interactive)
  15313. (org-eval-in-calendar '(calendar-backward-month 1))))
  15314. (org-defkey map [(meta shift right)]
  15315. (lambda () (interactive)
  15316. (org-eval-in-calendar '(calendar-forward-month 1))))
  15317. (org-defkey map [(meta shift up)]
  15318. (lambda () (interactive)
  15319. (org-eval-in-calendar '(calendar-backward-year 1))))
  15320. (org-defkey map [(meta shift down)]
  15321. (lambda () (interactive)
  15322. (org-eval-in-calendar '(calendar-forward-year 1))))
  15323. (org-defkey map [?\e (shift left)]
  15324. (lambda () (interactive)
  15325. (org-eval-in-calendar '(calendar-backward-month 1))))
  15326. (org-defkey map [?\e (shift right)]
  15327. (lambda () (interactive)
  15328. (org-eval-in-calendar '(calendar-forward-month 1))))
  15329. (org-defkey map [?\e (shift up)]
  15330. (lambda () (interactive)
  15331. (org-eval-in-calendar '(calendar-backward-year 1))))
  15332. (org-defkey map [?\e (shift down)]
  15333. (lambda () (interactive)
  15334. (org-eval-in-calendar '(calendar-forward-year 1))))
  15335. (org-defkey map [(shift up)]
  15336. (lambda () (interactive)
  15337. (org-eval-in-calendar '(calendar-backward-week 1))))
  15338. (org-defkey map [(shift down)]
  15339. (lambda () (interactive)
  15340. (org-eval-in-calendar '(calendar-forward-week 1))))
  15341. (org-defkey map [(shift left)]
  15342. (lambda () (interactive)
  15343. (org-eval-in-calendar '(calendar-backward-day 1))))
  15344. (org-defkey map [(shift right)]
  15345. (lambda () (interactive)
  15346. (org-eval-in-calendar '(calendar-forward-day 1))))
  15347. (org-defkey map "!"
  15348. (lambda () (interactive)
  15349. (org-eval-in-calendar '(diary-view-entries))
  15350. (message "")))
  15351. (org-defkey map ">"
  15352. (lambda () (interactive)
  15353. (org-eval-in-calendar '(calendar-scroll-left 1))))
  15354. (org-defkey map "<"
  15355. (lambda () (interactive)
  15356. (org-eval-in-calendar '(calendar-scroll-right 1))))
  15357. (org-defkey map "\C-v"
  15358. (lambda () (interactive)
  15359. (org-eval-in-calendar
  15360. '(calendar-scroll-left-three-months 1))))
  15361. (org-defkey map "\M-v"
  15362. (lambda () (interactive)
  15363. (org-eval-in-calendar
  15364. '(calendar-scroll-right-three-months 1))))
  15365. map)
  15366. "Keymap for minibuffer commands when using `org-read-date'.")
  15367. (defvar org-def)
  15368. (defvar org-defdecode)
  15369. (defvar org-with-time)
  15370. (defun org-read-date (&optional org-with-time to-time from-string prompt
  15371. default-time default-input inactive)
  15372. "Read a date, possibly a time, and make things smooth for the user.
  15373. The prompt will suggest to enter an ISO date, but you can also enter anything
  15374. which will at least partially be understood by `parse-time-string'.
  15375. Unrecognized parts of the date will default to the current day, month, year,
  15376. hour and minute. If this command is called to replace a timestamp at point,
  15377. or to enter the second timestamp of a range, the default time is taken
  15378. from the existing stamp. Furthermore, the command prefers the future,
  15379. so if you are giving a date where the year is not given, and the day-month
  15380. combination is already past in the current year, it will assume you
  15381. mean next year. For details, see the manual. A few examples:
  15382. 3-2-5 --> 2003-02-05
  15383. feb 15 --> currentyear-02-15
  15384. 2/15 --> currentyear-02-15
  15385. sep 12 9 --> 2009-09-12
  15386. 12:45 --> today 12:45
  15387. 22 sept 0:34 --> currentyear-09-22 0:34
  15388. 12 --> currentyear-currentmonth-12
  15389. Fri --> nearest Friday after today
  15390. -Tue --> last Tuesday
  15391. etc.
  15392. Furthermore you can specify a relative date by giving, as the *first* thing
  15393. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  15394. change in days weeks, months, years.
  15395. With a single plus or minus, the date is relative to today. With a double
  15396. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  15397. +4d --> four days from today
  15398. +4 --> same as above
  15399. +2w --> two weeks from today
  15400. ++5 --> five days from default date
  15401. The function understands only English month and weekday abbreviations.
  15402. While prompting, a calendar is popped up - you can also select the
  15403. date with the mouse (button 1). The calendar shows a period of three
  15404. months. To scroll it to other months, use the keys `>' and `<'.
  15405. If you don't like the calendar, turn it off with
  15406. \(setq org-read-date-popup-calendar nil)
  15407. With optional argument TO-TIME, the date will immediately be converted
  15408. to an internal time.
  15409. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  15410. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  15411. still enter a time, and this function will inform the calling routine
  15412. about this change. The calling routine may then choose to change the
  15413. format used to insert the time stamp into the buffer to include the time.
  15414. With optional argument FROM-STRING, read from this string instead from
  15415. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  15416. the time/date that is used for everything that is not specified by the
  15417. user."
  15418. (require 'parse-time)
  15419. (let* ((org-time-stamp-rounding-minutes
  15420. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  15421. (org-dcst org-display-custom-times)
  15422. (ct (org-current-time))
  15423. (org-def (or org-overriding-default-time default-time ct))
  15424. (org-defdecode (decode-time org-def))
  15425. (dummy (progn
  15426. (when (< (nth 2 org-defdecode) org-extend-today-until)
  15427. (setcar (nthcdr 2 org-defdecode) -1)
  15428. (setcar (nthcdr 1 org-defdecode) 59)
  15429. (setq org-def (apply 'encode-time org-defdecode)
  15430. org-defdecode (decode-time org-def)))))
  15431. (cur-frame (selected-frame))
  15432. (mouse-autoselect-window nil) ; Don't let the mouse jump
  15433. (calendar-frame-setup nil)
  15434. (calendar-setup (when (eq calendar-setup 'calendar-only) 'calendar-only))
  15435. (calendar-move-hook nil)
  15436. (calendar-view-diary-initially-flag nil)
  15437. (calendar-view-holidays-initially-flag nil)
  15438. (timestr (format-time-string
  15439. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  15440. (prompt (concat (if prompt (concat prompt " ") "")
  15441. (format "Date+time [%s]: " timestr)))
  15442. ans (org-ans0 "") org-ans1 org-ans2 final cal-frame)
  15443. (cond
  15444. (from-string (setq ans from-string))
  15445. (org-read-date-popup-calendar
  15446. (save-excursion
  15447. (save-window-excursion
  15448. (calendar)
  15449. (when (eq calendar-setup 'calendar-only)
  15450. (setq cal-frame
  15451. (window-frame (get-buffer-window "*Calendar*" 'visible)))
  15452. (select-frame cal-frame))
  15453. (org-eval-in-calendar '(setq cursor-type nil) t)
  15454. (unwind-protect
  15455. (progn
  15456. (calendar-forward-day (- (time-to-days org-def)
  15457. (calendar-absolute-from-gregorian
  15458. (calendar-current-date))))
  15459. (org-eval-in-calendar nil t)
  15460. (let* ((old-map (current-local-map))
  15461. (map (copy-keymap calendar-mode-map))
  15462. (minibuffer-local-map
  15463. (copy-keymap org-read-date-minibuffer-local-map)))
  15464. (org-defkey map (kbd "RET") 'org-calendar-select)
  15465. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  15466. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  15467. (unwind-protect
  15468. (progn
  15469. (use-local-map map)
  15470. (setq org-read-date-inactive inactive)
  15471. (add-hook 'post-command-hook 'org-read-date-display)
  15472. (setq org-ans0 (read-string prompt default-input
  15473. 'org-read-date-history nil))
  15474. ;; org-ans0: from prompt
  15475. ;; org-ans1: from mouse click
  15476. ;; org-ans2: from calendar motion
  15477. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  15478. (remove-hook 'post-command-hook 'org-read-date-display)
  15479. (use-local-map old-map)
  15480. (when org-read-date-overlay
  15481. (delete-overlay org-read-date-overlay)
  15482. (setq org-read-date-overlay nil)))))
  15483. (bury-buffer "*Calendar*")
  15484. (when cal-frame
  15485. (delete-frame cal-frame)
  15486. (select-frame-set-input-focus cur-frame))))))
  15487. (t ; Naked prompt only
  15488. (unwind-protect
  15489. (setq ans (read-string prompt default-input
  15490. 'org-read-date-history timestr))
  15491. (when org-read-date-overlay
  15492. (delete-overlay org-read-date-overlay)
  15493. (setq org-read-date-overlay nil)))))
  15494. (setq final (org-read-date-analyze ans org-def org-defdecode))
  15495. (when org-read-date-analyze-forced-year
  15496. (message "Year was forced into %s"
  15497. (if org-read-date-force-compatible-dates
  15498. "compatible range (1970-2037)"
  15499. "range representable on this machine"))
  15500. (ding))
  15501. ;; One round trip to get rid of 34th of August and stuff like that....
  15502. (setq final (decode-time (apply 'encode-time final)))
  15503. (setq org-read-date-final-answer ans)
  15504. (if to-time
  15505. (apply 'encode-time final)
  15506. (if (and (boundp 'org-time-was-given) org-time-was-given)
  15507. (format "%04d-%02d-%02d %02d:%02d"
  15508. (nth 5 final) (nth 4 final) (nth 3 final)
  15509. (nth 2 final) (nth 1 final))
  15510. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  15511. (defun org-read-date-display ()
  15512. "Display the current date prompt interpretation in the minibuffer."
  15513. (when org-read-date-display-live
  15514. (when org-read-date-overlay
  15515. (delete-overlay org-read-date-overlay))
  15516. (when (minibufferp (current-buffer))
  15517. (save-excursion
  15518. (end-of-line 1)
  15519. (while (not (equal (buffer-substring
  15520. (max (point-min) (- (point) 4)) (point))
  15521. " "))
  15522. (insert " ")))
  15523. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  15524. " " (or org-ans1 org-ans2)))
  15525. (org-end-time-was-given nil)
  15526. (f (org-read-date-analyze ans org-def org-defdecode))
  15527. (fmts (if org-dcst
  15528. org-time-stamp-custom-formats
  15529. org-time-stamp-formats))
  15530. (fmt (if (or org-with-time
  15531. (and (boundp 'org-time-was-given) org-time-was-given))
  15532. (cdr fmts)
  15533. (car fmts)))
  15534. (txt (format-time-string fmt (apply 'encode-time f)))
  15535. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  15536. (txt (concat "=> " txt)))
  15537. (when (and org-end-time-was-given
  15538. (string-match org-plain-time-of-day-regexp txt))
  15539. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  15540. org-end-time-was-given
  15541. (substring txt (match-end 0)))))
  15542. (when org-read-date-analyze-futurep
  15543. (setq txt (concat txt " (=>F)")))
  15544. (setq org-read-date-overlay
  15545. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  15546. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  15547. (defun org-read-date-analyze (ans org-def org-defdecode)
  15548. "Analyze the combined answer of the date prompt."
  15549. ;; FIXME: cleanup and comment
  15550. ;; Pass `current-time' result to `decode-time' (instead of calling
  15551. ;; without arguments) so that only `current-time' has to be
  15552. ;; overriden in tests.
  15553. (let ((nowdecode (decode-time (current-time)))
  15554. delta deltan deltaw deltadef year month day
  15555. hour minute second wday pm h2 m2 tl wday1
  15556. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  15557. (setq org-read-date-analyze-futurep nil
  15558. org-read-date-analyze-forced-year nil)
  15559. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  15560. (setq ans "+0"))
  15561. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  15562. (setq ans (replace-match "" t t ans)
  15563. deltan (car delta)
  15564. deltaw (nth 1 delta)
  15565. deltadef (nth 2 delta)))
  15566. ;; Check if there is an iso week date in there. If yes, store the
  15567. ;; info and postpone interpreting it until the rest of the parsing
  15568. ;; is done.
  15569. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  15570. (setq iso-year (if (match-end 1)
  15571. (org-small-year-to-year
  15572. (string-to-number (match-string 1 ans))))
  15573. iso-weekday (if (match-end 3)
  15574. (string-to-number (match-string 3 ans)))
  15575. iso-week (string-to-number (match-string 2 ans)))
  15576. (setq ans (replace-match "" t t ans)))
  15577. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  15578. (when (string-match
  15579. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  15580. (setq year (if (match-end 2)
  15581. (string-to-number (match-string 2 ans))
  15582. (progn (setq kill-year t)
  15583. (string-to-number (format-time-string "%Y"))))
  15584. month (string-to-number (match-string 3 ans))
  15585. day (string-to-number (match-string 4 ans)))
  15586. (if (< year 100) (setq year (+ 2000 year)))
  15587. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15588. t nil ans)))
  15589. ;; Help matching dotted european dates
  15590. (when (string-match
  15591. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  15592. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  15593. (setq kill-year t)
  15594. (string-to-number (format-time-string "%Y")))
  15595. day (string-to-number (match-string 1 ans))
  15596. month (string-to-number (match-string 2 ans))
  15597. ans (replace-match (format "%04d-%02d-%02d" year month day)
  15598. t nil ans)))
  15599. ;; Help matching american dates, like 5/30 or 5/30/7
  15600. (when (string-match
  15601. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  15602. (setq year (if (match-end 4)
  15603. (string-to-number (match-string 4 ans))
  15604. (progn (setq kill-year t)
  15605. (string-to-number (format-time-string "%Y"))))
  15606. month (string-to-number (match-string 1 ans))
  15607. day (string-to-number (match-string 2 ans)))
  15608. (if (< year 100) (setq year (+ 2000 year)))
  15609. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15610. t nil ans)))
  15611. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  15612. ;; If there is a time with am/pm, and *no* time without it, we convert
  15613. ;; so that matching will be successful.
  15614. (loop for i from 1 to 2 do ; twice, for end time as well
  15615. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  15616. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  15617. (setq hour (string-to-number (match-string 1 ans))
  15618. minute (if (match-end 3)
  15619. (string-to-number (match-string 3 ans))
  15620. 0)
  15621. pm (equal ?p
  15622. (string-to-char (downcase (match-string 4 ans)))))
  15623. (if (and (= hour 12) (not pm))
  15624. (setq hour 0)
  15625. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  15626. (setq ans (replace-match (format "%02d:%02d" hour minute)
  15627. t t ans))))
  15628. ;; Check if a time range is given as a duration
  15629. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  15630. (setq hour (string-to-number (match-string 1 ans))
  15631. h2 (+ hour (string-to-number (match-string 3 ans)))
  15632. minute (string-to-number (match-string 2 ans))
  15633. m2 (+ minute (if (match-end 5) (string-to-number
  15634. (match-string 5 ans))0)))
  15635. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  15636. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  15637. t t ans)))
  15638. ;; Check if there is a time range
  15639. (when (boundp 'org-end-time-was-given)
  15640. (setq org-time-was-given nil)
  15641. (when (and (string-match org-plain-time-of-day-regexp ans)
  15642. (match-end 8))
  15643. (setq org-end-time-was-given (match-string 8 ans))
  15644. (setq ans (concat (substring ans 0 (match-beginning 7))
  15645. (substring ans (match-end 7))))))
  15646. (setq tl (parse-time-string ans)
  15647. day (or (nth 3 tl) (nth 3 org-defdecode))
  15648. month
  15649. (cond ((nth 4 tl))
  15650. ((not org-read-date-prefer-future) (nth 4 org-defdecode))
  15651. ;; Day was specified. Make sure DAY+MONTH
  15652. ;; combination happens in the future.
  15653. ((nth 3 tl)
  15654. (setq futurep t)
  15655. (if (< day (nth 3 nowdecode)) (1+ (nth 4 nowdecode))
  15656. (nth 4 nowdecode)))
  15657. (t (nth 4 org-defdecode)))
  15658. year
  15659. (cond ((and (not kill-year) (nth 5 tl)))
  15660. ((not org-read-date-prefer-future) (nth 5 org-defdecode))
  15661. ;; Month was guessed in the future and is at least
  15662. ;; equal to NOWDECODE's. Fix year accordingly.
  15663. (futurep
  15664. (if (or (> month (nth 4 nowdecode))
  15665. (>= day (nth 3 nowdecode)))
  15666. (nth 5 nowdecode)
  15667. (1+ (nth 5 nowdecode))))
  15668. ;; Month was specified. Make sure MONTH+YEAR
  15669. ;; combination happens in the future.
  15670. ((nth 4 tl)
  15671. (setq futurep t)
  15672. (cond ((> month (nth 4 nowdecode)) (nth 5 nowdecode))
  15673. ((< month (nth 4 nowdecode)) (1+ (nth 5 nowdecode)))
  15674. ((< day (nth 3 nowdecode)) (1+ (nth 5 nowdecode)))
  15675. (t (nth 5 nowdecode))))
  15676. (t (nth 5 org-defdecode)))
  15677. hour (or (nth 2 tl) (nth 2 org-defdecode))
  15678. minute (or (nth 1 tl) (nth 1 org-defdecode))
  15679. second (or (nth 0 tl) 0)
  15680. wday (nth 6 tl))
  15681. (when (and (eq org-read-date-prefer-future 'time)
  15682. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  15683. (equal day (nth 3 nowdecode))
  15684. (equal month (nth 4 nowdecode))
  15685. (equal year (nth 5 nowdecode))
  15686. (nth 2 tl)
  15687. (or (< (nth 2 tl) (nth 2 nowdecode))
  15688. (and (= (nth 2 tl) (nth 2 nowdecode))
  15689. (nth 1 tl)
  15690. (< (nth 1 tl) (nth 1 nowdecode)))))
  15691. (setq day (1+ day)
  15692. futurep t))
  15693. ;; Special date definitions below
  15694. (cond
  15695. (iso-week
  15696. ;; There was an iso week
  15697. (require 'cal-iso)
  15698. (setq futurep nil)
  15699. (setq year (or iso-year year)
  15700. day (or iso-weekday wday 1)
  15701. wday nil ; to make sure that the trigger below does not match
  15702. iso-date (calendar-gregorian-from-absolute
  15703. (calendar-iso-to-absolute
  15704. (list iso-week day year))))
  15705. ; FIXME: Should we also push ISO weeks into the future?
  15706. ; (when (and org-read-date-prefer-future
  15707. ; (not iso-year)
  15708. ; (< (calendar-absolute-from-gregorian iso-date)
  15709. ; (time-to-days (current-time))))
  15710. ; (setq year (1+ year)
  15711. ; iso-date (calendar-gregorian-from-absolute
  15712. ; (calendar-iso-to-absolute
  15713. ; (list iso-week day year)))))
  15714. (setq month (car iso-date)
  15715. year (nth 2 iso-date)
  15716. day (nth 1 iso-date)))
  15717. (deltan
  15718. (setq futurep nil)
  15719. (unless deltadef
  15720. ;; Pass `current-time' result to `decode-time' (instead of
  15721. ;; calling without arguments) so that only `current-time' has
  15722. ;; to be overriden in tests.
  15723. (let ((now (decode-time (current-time))))
  15724. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  15725. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  15726. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  15727. ((equal deltaw "m") (setq month (+ month deltan)))
  15728. ((equal deltaw "y") (setq year (+ year deltan)))))
  15729. ((and wday (not (nth 3 tl)))
  15730. ;; Weekday was given, but no day, so pick that day in the week
  15731. ;; on or after the derived date.
  15732. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  15733. (unless (equal wday wday1)
  15734. (setq day (+ day (% (- wday wday1 -7) 7))))))
  15735. (if (and (boundp 'org-time-was-given)
  15736. (nth 2 tl))
  15737. (setq org-time-was-given t))
  15738. (if (< year 100) (setq year (+ 2000 year)))
  15739. ;; Check of the date is representable
  15740. (if org-read-date-force-compatible-dates
  15741. (progn
  15742. (if (< year 1970)
  15743. (setq year 1970 org-read-date-analyze-forced-year t))
  15744. (if (> year 2037)
  15745. (setq year 2037 org-read-date-analyze-forced-year t)))
  15746. (condition-case nil
  15747. (ignore (encode-time second minute hour day month year))
  15748. (error
  15749. (setq year (nth 5 org-defdecode))
  15750. (setq org-read-date-analyze-forced-year t))))
  15751. (setq org-read-date-analyze-futurep futurep)
  15752. (list second minute hour day month year)))
  15753. (defvar parse-time-weekdays)
  15754. (defun org-read-date-get-relative (s today default)
  15755. "Check string S for special relative date string.
  15756. TODAY and DEFAULT are internal times, for today and for a default.
  15757. Return shift list (N what def-flag)
  15758. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  15759. N is the number of WHATs to shift.
  15760. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  15761. the DEFAULT date rather than TODAY."
  15762. (require 'parse-time)
  15763. (when (and
  15764. (string-match
  15765. (concat
  15766. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  15767. "\\([0-9]+\\)?"
  15768. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  15769. "\\([ \t]\\|$\\)") s)
  15770. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  15771. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  15772. (string-to-char (substring (match-string 1 s) -1))
  15773. ?+))
  15774. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  15775. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  15776. (what (if (match-end 3) (match-string 3 s) "d"))
  15777. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  15778. (date (if rel default today))
  15779. (wday (nth 6 (decode-time date)))
  15780. delta)
  15781. (if wday1
  15782. (progn
  15783. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  15784. (if (= delta 0) (setq delta 7))
  15785. (if (= dir ?-)
  15786. (progn
  15787. (setq delta (- delta 7))
  15788. (if (= delta 0) (setq delta -7))))
  15789. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  15790. (list delta "d" rel))
  15791. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  15792. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  15793. "Turn a user-specified date into the internal representation.
  15794. The internal representation needed by the calendar is (month day year).
  15795. This is a wrapper to handle the brain-dead convention in calendar that
  15796. user function argument order change dependent on argument order."
  15797. (if (boundp 'calendar-date-style)
  15798. (cond
  15799. ((eq calendar-date-style 'american)
  15800. (list arg1 arg2 arg3))
  15801. ((eq calendar-date-style 'european)
  15802. (list arg2 arg1 arg3))
  15803. ((eq calendar-date-style 'iso)
  15804. (list arg2 arg3 arg1)))
  15805. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  15806. (if (org-bound-and-true-p european-calendar-style)
  15807. (list arg2 arg1 arg3)
  15808. (list arg1 arg2 arg3)))))
  15809. (defun org-eval-in-calendar (form &optional keepdate)
  15810. "Eval FORM in the calendar window and return to current window.
  15811. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  15812. otherwise stick to the current value of `org-ans2'."
  15813. (let ((sf (selected-frame))
  15814. (sw (selected-window)))
  15815. (select-window (get-buffer-window "*Calendar*" t))
  15816. (eval form)
  15817. (when (and (not keepdate) (calendar-cursor-to-date))
  15818. (let* ((date (calendar-cursor-to-date))
  15819. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15820. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  15821. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  15822. (select-window sw)
  15823. (org-select-frame-set-input-focus sf)))
  15824. (defun org-calendar-select ()
  15825. "Return to `org-read-date' with the date currently selected.
  15826. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15827. (interactive)
  15828. (when (calendar-cursor-to-date)
  15829. (let* ((date (calendar-cursor-to-date))
  15830. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15831. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15832. (if (active-minibuffer-window) (exit-minibuffer))))
  15833. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  15834. "Insert a date stamp for the date given by the internal TIME.
  15835. See `format-time-string' for the format of TIME.
  15836. WITH-HM means use the stamp format that includes the time of the day.
  15837. INACTIVE means use square brackets instead of angular ones, so that the
  15838. stamp will not contribute to the agenda.
  15839. PRE and POST are optional strings to be inserted before and after the
  15840. stamp.
  15841. The command returns the inserted time stamp."
  15842. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  15843. stamp)
  15844. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  15845. (insert-before-markers (or pre ""))
  15846. (when (listp extra)
  15847. (setq extra (car extra))
  15848. (if (and (stringp extra)
  15849. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  15850. (setq extra (format "-%02d:%02d"
  15851. (string-to-number (match-string 1 extra))
  15852. (string-to-number (match-string 2 extra))))
  15853. (setq extra nil)))
  15854. (when extra
  15855. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  15856. (insert-before-markers (setq stamp (format-time-string fmt time)))
  15857. (insert-before-markers (or post ""))
  15858. (setq org-last-inserted-timestamp stamp)))
  15859. (defun org-toggle-time-stamp-overlays ()
  15860. "Toggle the use of custom time stamp formats."
  15861. (interactive)
  15862. (setq org-display-custom-times (not org-display-custom-times))
  15863. (unless org-display-custom-times
  15864. (let ((p (point-min)) (bmp (buffer-modified-p)))
  15865. (while (setq p (next-single-property-change p 'display))
  15866. (if (and (get-text-property p 'display)
  15867. (eq (get-text-property p 'face) 'org-date))
  15868. (remove-text-properties
  15869. p (setq p (next-single-property-change p 'display))
  15870. '(display t))))
  15871. (set-buffer-modified-p bmp)))
  15872. (if (featurep 'xemacs)
  15873. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  15874. (org-restart-font-lock)
  15875. (setq org-table-may-need-update t)
  15876. (if org-display-custom-times
  15877. (message "Time stamps are overlaid with custom format")
  15878. (message "Time stamp overlays removed")))
  15879. (defun org-display-custom-time (beg end)
  15880. "Overlay modified time stamp format over timestamp between BEG and END."
  15881. (let* ((ts (buffer-substring beg end))
  15882. t1 w1 with-hm tf time str w2 (off 0))
  15883. (save-match-data
  15884. (setq t1 (org-parse-time-string ts t))
  15885. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  15886. (setq off (- (match-end 0) (match-beginning 0)))))
  15887. (setq end (- end off))
  15888. (setq w1 (- end beg)
  15889. with-hm (and (nth 1 t1) (nth 2 t1))
  15890. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  15891. time (org-fix-decoded-time t1)
  15892. str (org-add-props
  15893. (format-time-string
  15894. (substring tf 1 -1) (apply 'encode-time time))
  15895. nil 'mouse-face 'highlight)
  15896. w2 (length str))
  15897. (if (not (= w2 w1))
  15898. (add-text-properties (1+ beg) (+ 2 beg)
  15899. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  15900. (if (featurep 'xemacs)
  15901. (progn
  15902. (put-text-property beg end 'invisible t)
  15903. (put-text-property beg end 'end-glyph (make-glyph str)))
  15904. (put-text-property beg end 'display str))))
  15905. (defun org-fix-decoded-time (time)
  15906. "Set 0 instead of nil for the first 6 elements of time.
  15907. Don't touch the rest."
  15908. (let ((n 0))
  15909. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  15910. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.4")
  15911. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  15912. "Difference between TIMESTAMP-STRING and now in days.
  15913. If SECONDS is non-nil, return the difference in seconds."
  15914. (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
  15915. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  15916. (funcall fdiff (current-time)))))
  15917. (defun org-deadline-close (timestamp-string &optional ndays)
  15918. "Is the time in TIMESTAMP-STRING close to the current date?"
  15919. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  15920. (and (< (org-time-stamp-to-now timestamp-string) ndays)
  15921. (not (org-entry-is-done-p))))
  15922. (defun org-get-wdays (ts &optional delay zero-delay)
  15923. "Get the deadline lead time appropriate for timestring TS.
  15924. When DELAY is non-nil, get the delay time for scheduled items
  15925. instead of the deadline lead time. When ZERO-DELAY is non-nil
  15926. and `org-scheduled-delay-days' is 0, enforce 0 as the delay,
  15927. don't try to find the delay cookie in the scheduled timestamp."
  15928. (let ((tv (if delay org-scheduled-delay-days
  15929. org-deadline-warning-days)))
  15930. (cond
  15931. ((or (and delay (< tv 0))
  15932. (and delay zero-delay (<= tv 0))
  15933. (and (not delay) (<= tv 0)))
  15934. ;; Enforce this value no matter what
  15935. (- tv))
  15936. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  15937. ;; lead time is specified.
  15938. (floor (* (string-to-number (match-string 1 ts))
  15939. (cdr (assoc (match-string 2 ts)
  15940. '(("d" . 1) ("w" . 7)
  15941. ("m" . 30.4) ("y" . 365.25)
  15942. ("h" . 0.041667)))))))
  15943. ;; go for the default.
  15944. (t tv))))
  15945. (defun org-calendar-select-mouse (ev)
  15946. "Return to `org-read-date' with the date currently selected.
  15947. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15948. (interactive "e")
  15949. (mouse-set-point ev)
  15950. (when (calendar-cursor-to-date)
  15951. (let* ((date (calendar-cursor-to-date))
  15952. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15953. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15954. (if (active-minibuffer-window) (exit-minibuffer))))
  15955. (defun org-check-deadlines (ndays)
  15956. "Check if there are any deadlines due or past due.
  15957. A deadline is considered due if it happens within `org-deadline-warning-days'
  15958. days from today's date. If the deadline appears in an entry marked DONE,
  15959. it is not shown. The prefix arg NDAYS can be used to test that many
  15960. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  15961. (interactive "P")
  15962. (let* ((org-warn-days
  15963. (cond
  15964. ((equal ndays '(4)) 100000)
  15965. (ndays (prefix-numeric-value ndays))
  15966. (t (abs org-deadline-warning-days))))
  15967. (case-fold-search nil)
  15968. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15969. (callback
  15970. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  15971. (message "%d deadlines past-due or due within %d days"
  15972. (org-occur regexp nil callback)
  15973. org-warn-days)))
  15974. (defsubst org-re-timestamp (type)
  15975. "Return a regexp for timestamp TYPE.
  15976. Allowed values for TYPE are:
  15977. all: all timestamps
  15978. active: only active timestamps (<...>)
  15979. inactive: only inactive timestamps ([...])
  15980. scheduled: only scheduled timestamps
  15981. deadline: only deadline timestamps
  15982. closed: only closed time-stamps
  15983. When TYPE is nil, fall back on returning a regexp that matches
  15984. both scheduled and deadline timestamps."
  15985. (case type
  15986. (all org-ts-regexp-both)
  15987. (active org-ts-regexp)
  15988. (inactive org-ts-regexp-inactive)
  15989. (scheduled org-scheduled-time-regexp)
  15990. (deadline org-deadline-time-regexp)
  15991. (closed org-closed-time-regexp)
  15992. (otherwise
  15993. (concat "\\<"
  15994. (regexp-opt (list org-deadline-string org-scheduled-string))
  15995. " *<\\([^>]+\\)>"))))
  15996. (defun org-check-before-date (date)
  15997. "Check if there are deadlines or scheduled entries before DATE."
  15998. (interactive (list (org-read-date)))
  15999. (let ((case-fold-search nil)
  16000. (regexp (org-re-timestamp org-ts-type))
  16001. (callback
  16002. `(lambda ()
  16003. (let ((match (match-string 1)))
  16004. (and ,(if (memq org-ts-type '(active inactive all))
  16005. '(eq (org-element-type (org-element-context)) 'timestamp)
  16006. '(org-at-planning-p))
  16007. (time-less-p
  16008. (org-time-string-to-time match)
  16009. (org-time-string-to-time date)))))))
  16010. (message "%d entries before %s"
  16011. (org-occur regexp nil callback) date)))
  16012. (defun org-check-after-date (date)
  16013. "Check if there are deadlines or scheduled entries after DATE."
  16014. (interactive (list (org-read-date)))
  16015. (let ((case-fold-search nil)
  16016. (regexp (org-re-timestamp org-ts-type))
  16017. (callback
  16018. `(lambda ()
  16019. (let ((match (match-string 1)))
  16020. (and ,(if (memq org-ts-type '(active inactive all))
  16021. '(eq (org-element-type (org-element-context)) 'timestamp)
  16022. '(org-at-planning-p))
  16023. (not (time-less-p
  16024. (org-time-string-to-time match)
  16025. (org-time-string-to-time date))))))))
  16026. (message "%d entries after %s"
  16027. (org-occur regexp nil callback) date)))
  16028. (defun org-check-dates-range (start-date end-date)
  16029. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  16030. (interactive (list (org-read-date nil nil nil "Range starts")
  16031. (org-read-date nil nil nil "Range end")))
  16032. (let ((case-fold-search nil)
  16033. (regexp (org-re-timestamp org-ts-type))
  16034. (callback
  16035. `(lambda ()
  16036. (let ((match (match-string 1)))
  16037. (and
  16038. ,(if (memq org-ts-type '(active inactive all))
  16039. '(eq (org-element-type (org-element-context)) 'timestamp)
  16040. '(org-at-planning-p))
  16041. (not (time-less-p
  16042. (org-time-string-to-time match)
  16043. (org-time-string-to-time start-date)))
  16044. (time-less-p
  16045. (org-time-string-to-time match)
  16046. (org-time-string-to-time end-date)))))))
  16047. (message "%d entries between %s and %s"
  16048. (org-occur regexp nil callback) start-date end-date)))
  16049. (defun org-evaluate-time-range (&optional to-buffer)
  16050. "Evaluate a time range by computing the difference between start and end.
  16051. Normally the result is just printed in the echo area, but with prefix arg
  16052. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  16053. If the time range is actually in a table, the result is inserted into the
  16054. next column.
  16055. For time difference computation, a year is assumed to be exactly 365
  16056. days in order to avoid rounding problems."
  16057. (interactive "P")
  16058. (or
  16059. (org-clock-update-time-maybe)
  16060. (save-excursion
  16061. (unless (org-at-date-range-p t)
  16062. (goto-char (point-at-bol))
  16063. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  16064. (if (not (org-at-date-range-p t))
  16065. (user-error "Not at a time-stamp range, and none found in current line")))
  16066. (let* ((ts1 (match-string 1))
  16067. (ts2 (match-string 2))
  16068. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  16069. (match-end (match-end 0))
  16070. (time1 (org-time-string-to-time ts1))
  16071. (time2 (org-time-string-to-time ts2))
  16072. (t1 (org-float-time time1))
  16073. (t2 (org-float-time time2))
  16074. (diff (abs (- t2 t1)))
  16075. (negative (< (- t2 t1) 0))
  16076. ;; (ys (floor (* 365 24 60 60)))
  16077. (ds (* 24 60 60))
  16078. (hs (* 60 60))
  16079. (fy "%dy %dd %02d:%02d")
  16080. (fy1 "%dy %dd")
  16081. (fd "%dd %02d:%02d")
  16082. (fd1 "%dd")
  16083. (fh "%02d:%02d")
  16084. y d h m align)
  16085. (if havetime
  16086. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16087. y 0
  16088. d (floor (/ diff ds)) diff (mod diff ds)
  16089. h (floor (/ diff hs)) diff (mod diff hs)
  16090. m (floor (/ diff 60)))
  16091. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16092. y 0
  16093. d (floor (+ (/ diff ds) 0.5))
  16094. h 0 m 0))
  16095. (if (not to-buffer)
  16096. (message "%s" (org-make-tdiff-string y d h m))
  16097. (if (org-at-table-p)
  16098. (progn
  16099. (goto-char match-end)
  16100. (setq align t)
  16101. (and (looking-at " *|") (goto-char (match-end 0))))
  16102. (goto-char match-end))
  16103. (if (looking-at
  16104. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  16105. (replace-match ""))
  16106. (if negative (insert " -"))
  16107. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  16108. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  16109. (insert " " (format fh h m))))
  16110. (if align (org-table-align))
  16111. (message "Time difference inserted")))))
  16112. (defun org-make-tdiff-string (y d h m)
  16113. (let ((fmt "")
  16114. (l nil))
  16115. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  16116. l (push y l)))
  16117. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  16118. l (push d l)))
  16119. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  16120. l (push h l)))
  16121. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  16122. l (push m l)))
  16123. (apply 'format fmt (nreverse l))))
  16124. (defun org-time-string-to-time (s &optional buffer pos)
  16125. "Convert a timestamp string into internal time."
  16126. (condition-case errdata
  16127. (apply 'encode-time (org-parse-time-string s))
  16128. (error (error "Bad timestamp `%s'%s\nError was: %s"
  16129. s (if (not (and buffer pos))
  16130. ""
  16131. (format " at %d in buffer `%s'" pos buffer))
  16132. (cdr errdata)))))
  16133. (defun org-time-string-to-seconds (s)
  16134. "Convert a timestamp string to a number of seconds."
  16135. (org-float-time (org-time-string-to-time s)))
  16136. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  16137. "Convert a time stamp to an absolute day number.
  16138. If there is a specifier for a cyclic time stamp, get the closest
  16139. date to DAYNR.
  16140. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  16141. The variable `date' is bound by the calendar when this is called."
  16142. (cond
  16143. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  16144. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  16145. daynr
  16146. (+ daynr 1000)))
  16147. ((and daynr (string-match "\\+\\([0-9]+\\)[hdwmy]" s)
  16148. (> (string-to-number (match-string 1 s)) 0))
  16149. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  16150. (time-to-days (current-time))) (match-string 0 s)
  16151. prefer show-all))
  16152. (t (time-to-days
  16153. (condition-case errdata
  16154. (apply 'encode-time (org-parse-time-string s))
  16155. (error (error "Bad timestamp `%s'%s\nError was: %s"
  16156. s (if (not (and buffer pos))
  16157. ""
  16158. (format " at %d in buffer `%s'" pos buffer))
  16159. (cdr errdata))))))))
  16160. (defun org-days-to-iso-week (days)
  16161. "Return the iso week number."
  16162. (require 'cal-iso)
  16163. (car (calendar-iso-from-absolute days)))
  16164. (defun org-small-year-to-year (year)
  16165. "Convert 2-digit years into 4-digit years.
  16166. YEAR is expanded into one of the 30 next years, if possible, or
  16167. into a past one. Any year larger than 99 is returned unchanged."
  16168. (if (>= year 100) year
  16169. (let* ((current (string-to-number (format-time-string "%Y" (current-time))))
  16170. (century (/ current 100))
  16171. (offset (- year (% current 100))))
  16172. (cond ((> offset 30) (+ (* (1- century) 100) year))
  16173. ((> offset -70) (+ (* century 100) year))
  16174. (t (+ (* (1+ century) 100) year))))))
  16175. (defun org-time-from-absolute (d)
  16176. "Return the time corresponding to date D.
  16177. D may be an absolute day number, or a calendar-type list (month day year)."
  16178. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  16179. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  16180. (defun org-calendar-holiday ()
  16181. "List of holidays, for Diary display in Org-mode."
  16182. (require 'holidays)
  16183. (let ((hl (funcall
  16184. (if (fboundp 'calendar-check-holidays)
  16185. 'calendar-check-holidays 'check-calendar-holidays) date)))
  16186. (if hl (mapconcat 'identity hl "; "))))
  16187. (defun org-diary-sexp-entry (sexp entry date)
  16188. "Process a SEXP diary ENTRY for DATE."
  16189. (require 'diary-lib)
  16190. (let ((result (if calendar-debug-sexp
  16191. (let ((stack-trace-on-error t))
  16192. (eval (car (read-from-string sexp))))
  16193. (condition-case nil
  16194. (eval (car (read-from-string sexp)))
  16195. (error
  16196. (beep)
  16197. (message "Bad sexp at line %d in %s: %s"
  16198. (org-current-line)
  16199. (buffer-file-name) sexp)
  16200. (sleep-for 2))))))
  16201. (cond ((stringp result) (split-string result "; "))
  16202. ((and (consp result)
  16203. (not (consp (cdr result)))
  16204. (stringp (cdr result))) (cdr result))
  16205. ((and (consp result)
  16206. (stringp (car result))) result)
  16207. (result entry))))
  16208. (defun org-diary-to-ical-string (frombuf)
  16209. "Get iCalendar entries from diary entries in buffer FROMBUF.
  16210. This uses the icalendar.el library."
  16211. (let* ((tmpdir (if (featurep 'xemacs)
  16212. (temp-directory)
  16213. temporary-file-directory))
  16214. (tmpfile (make-temp-name
  16215. (expand-file-name "orgics" tmpdir)))
  16216. buf rtn b e)
  16217. (with-current-buffer frombuf
  16218. (icalendar-export-region (point-min) (point-max) tmpfile)
  16219. (setq buf (find-buffer-visiting tmpfile))
  16220. (set-buffer buf)
  16221. (goto-char (point-min))
  16222. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  16223. (setq b (match-beginning 0)))
  16224. (goto-char (point-max))
  16225. (if (re-search-backward "^END:VEVENT" nil t)
  16226. (setq e (match-end 0)))
  16227. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  16228. (kill-buffer buf)
  16229. (delete-file tmpfile)
  16230. rtn))
  16231. (defun org-closest-date (start current change prefer show-all)
  16232. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  16233. When PREFER is `past', return a date that is either CURRENT or past.
  16234. When PREFER is `future', return a date that is either CURRENT or future.
  16235. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  16236. ;; Make the proper lists from the dates
  16237. (catch 'exit
  16238. (let ((a1 '(("h" . hour)
  16239. ("d" . day)
  16240. ("w" . week)
  16241. ("m" . month)
  16242. ("y" . year)))
  16243. (shour (nth 2 (org-parse-time-string start)))
  16244. dn dw sday cday n1 n2 n0
  16245. d m y y1 y2 date1 date2 nmonths nm ny m2)
  16246. (setq start (org-date-to-gregorian start)
  16247. current (org-date-to-gregorian
  16248. (if show-all
  16249. current
  16250. (time-to-days (current-time))))
  16251. sday (calendar-absolute-from-gregorian start)
  16252. cday (calendar-absolute-from-gregorian current))
  16253. (if (<= cday sday) (throw 'exit sday))
  16254. (when (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  16255. (setq dn (string-to-number (match-string 1 change))
  16256. dw (cdr (assoc (match-string 2 change) a1))))
  16257. (unless (and dn (> dn 0))
  16258. (user-error "Invalid change specifier: %s" change))
  16259. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  16260. (cond
  16261. ((eq dw 'hour)
  16262. (let ((missing-hours
  16263. (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
  16264. dn)))
  16265. (setq n1 (if (zerop missing-hours) cday
  16266. (- cday (1+ (floor (/ missing-hours 24)))))
  16267. n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
  16268. ((eq dw 'day)
  16269. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  16270. n2 (+ n1 dn)))
  16271. ((eq dw 'year)
  16272. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  16273. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  16274. (setq date1 (list m d y1)
  16275. n1 (calendar-absolute-from-gregorian date1)
  16276. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  16277. n2 (calendar-absolute-from-gregorian date2)))
  16278. ((eq dw 'month)
  16279. ;; approx number of month between the two dates
  16280. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  16281. ;; How often does dn fit in there?
  16282. (setq d (nth 1 start) m (car start) y (nth 2 start)
  16283. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  16284. m (+ m nm)
  16285. ny (floor (/ m 12))
  16286. y (+ y ny)
  16287. m (- m (* ny 12)))
  16288. (while (> m 12) (setq m (- m 12) y (1+ y)))
  16289. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  16290. (setq m2 (+ m dn) y2 y)
  16291. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16292. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  16293. (while (<= n2 cday)
  16294. (setq n1 n2 m m2 y y2)
  16295. (setq m2 (+ m dn) y2 y)
  16296. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16297. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  16298. ;; Make sure n1 is the earlier date
  16299. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  16300. (if show-all
  16301. (cond
  16302. ((eq prefer 'past) (if (= cday n2) n2 n1))
  16303. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16304. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  16305. (cond
  16306. ((eq prefer 'past) (if (= cday n2) n2 n1))
  16307. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16308. (t (if (= cday n1) n1 n2)))))))
  16309. (defun org-date-to-gregorian (date)
  16310. "Turn any specification of DATE into a Gregorian date for the calendar."
  16311. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  16312. ((and (listp date) (= (length date) 3)) date)
  16313. ((stringp date)
  16314. (setq date (org-parse-time-string date))
  16315. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  16316. ((listp date)
  16317. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  16318. (defun org-parse-time-string (s &optional nodefault)
  16319. "Parse the standard Org-mode time string.
  16320. This should be a lot faster than the normal `parse-time-string'.
  16321. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  16322. hour and minute fields will be nil if not given."
  16323. (cond ((string-match org-ts-regexp0 s)
  16324. (list 0
  16325. (if (or (match-beginning 8) (not nodefault))
  16326. (string-to-number (or (match-string 8 s) "0")))
  16327. (if (or (match-beginning 7) (not nodefault))
  16328. (string-to-number (or (match-string 7 s) "0")))
  16329. (string-to-number (match-string 4 s))
  16330. (string-to-number (match-string 3 s))
  16331. (string-to-number (match-string 2 s))
  16332. nil nil nil))
  16333. ((string-match "^<[^>]+>$" s)
  16334. (decode-time (seconds-to-time (org-matcher-time s))))
  16335. (t (error "Not a standard Org-mode time string: %s" s))))
  16336. (defun org-timestamp-up (&optional arg)
  16337. "Increase the date item at the cursor by one.
  16338. If the cursor is on the year, change the year. If it is on the month,
  16339. the day or the time, change that.
  16340. With prefix ARG, change by that many units."
  16341. (interactive "p")
  16342. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  16343. (defun org-timestamp-down (&optional arg)
  16344. "Decrease the date item at the cursor by one.
  16345. If the cursor is on the year, change the year. If it is on the month,
  16346. the day or the time, change that.
  16347. With prefix ARG, change by that many units."
  16348. (interactive "p")
  16349. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  16350. (defun org-timestamp-up-day (&optional arg)
  16351. "Increase the date in the time stamp by one day.
  16352. With prefix ARG, change that many days."
  16353. (interactive "p")
  16354. (if (and (not (org-at-timestamp-p t))
  16355. (org-at-heading-p))
  16356. (org-todo 'up)
  16357. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  16358. (defun org-timestamp-down-day (&optional arg)
  16359. "Decrease the date in the time stamp by one day.
  16360. With prefix ARG, change that many days."
  16361. (interactive "p")
  16362. (if (and (not (org-at-timestamp-p t))
  16363. (org-at-heading-p))
  16364. (org-todo 'down)
  16365. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  16366. (defun org-at-timestamp-p (&optional inactive-ok)
  16367. "Non-nil if point is inside a timestamp.
  16368. When optional argument INACTIVE-OK is non-nil, also consider
  16369. inactive timestamps.
  16370. When this function returns a non-nil value, match data is set
  16371. according to `org-ts-regexp3' or `org-ts-regexp2', depending on
  16372. INACTIVE-OK."
  16373. (interactive)
  16374. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  16375. (pos (point))
  16376. (ans (or (looking-at tsr)
  16377. (save-excursion
  16378. (skip-chars-backward "^[<\n\r\t")
  16379. (if (> (point) (point-min)) (backward-char 1))
  16380. (and (looking-at tsr)
  16381. (> (- (match-end 0) pos) -1))))))
  16382. (and ans
  16383. (boundp 'org-ts-what)
  16384. (setq org-ts-what
  16385. (cond
  16386. ((= pos (match-beginning 0)) 'bracket)
  16387. ;; Point is considered to be "on the bracket" whether
  16388. ;; it's really on it or right after it.
  16389. ((= pos (1- (match-end 0))) 'bracket)
  16390. ((= pos (match-end 0)) 'after)
  16391. ((org-pos-in-match-range pos 2) 'year)
  16392. ((org-pos-in-match-range pos 3) 'month)
  16393. ((org-pos-in-match-range pos 7) 'hour)
  16394. ((org-pos-in-match-range pos 8) 'minute)
  16395. ((or (org-pos-in-match-range pos 4)
  16396. (org-pos-in-match-range pos 5)) 'day)
  16397. ((and (> pos (or (match-end 8) (match-end 5)))
  16398. (< pos (match-end 0)))
  16399. (- pos (or (match-end 8) (match-end 5))))
  16400. (t 'day))))
  16401. ans))
  16402. (defun org-toggle-timestamp-type ()
  16403. "Toggle the type (<active> or [inactive]) of a time stamp."
  16404. (interactive)
  16405. (when (org-at-timestamp-p t)
  16406. (let ((beg (match-beginning 0)) (end (match-end 0))
  16407. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  16408. (save-excursion
  16409. (goto-char beg)
  16410. (while (re-search-forward "[][<>]" end t)
  16411. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  16412. t t)))
  16413. (message "Timestamp is now %sactive"
  16414. (if (equal (char-after beg) ?<) "" "in")))))
  16415. (defun org-at-clock-log-p nil
  16416. "Is the cursor on the clock log line?"
  16417. (save-excursion
  16418. (move-beginning-of-line 1)
  16419. (looking-at org-clock-line-re)))
  16420. (defvar org-clock-history) ; defined in org-clock.el
  16421. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  16422. (defun org-timestamp-change (n &optional what updown suppress-tmp-delay)
  16423. "Change the date in the time stamp at point.
  16424. The date will be changed by N times WHAT. WHAT can be `day', `month',
  16425. `year', `minute', `second'. If WHAT is not given, the cursor position
  16426. in the timestamp determines what will be changed.
  16427. When SUPPRESS-TMP-DELAY is non-nil, suppress delays like \"--2d\"."
  16428. (let ((origin (point)) origin-cat
  16429. with-hm inactive
  16430. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  16431. org-ts-what
  16432. extra rem
  16433. ts time time0 fixnext clrgx)
  16434. (if (not (org-at-timestamp-p t))
  16435. (user-error "Not at a timestamp"))
  16436. (if (and (not what) (eq org-ts-what 'bracket))
  16437. (org-toggle-timestamp-type)
  16438. ;; Point isn't on brackets. Remember the part of the time-stamp
  16439. ;; the point was in. Indeed, size of time-stamps may change,
  16440. ;; but point must be kept in the same category nonetheless.
  16441. (setq origin-cat org-ts-what)
  16442. (if (and (not what) (not (eq org-ts-what 'day))
  16443. org-display-custom-times
  16444. (get-text-property (point) 'display)
  16445. (not (get-text-property (1- (point)) 'display)))
  16446. (setq org-ts-what 'day))
  16447. (setq org-ts-what (or what org-ts-what)
  16448. inactive (= (char-after (match-beginning 0)) ?\[)
  16449. ts (match-string 0))
  16450. (replace-match "")
  16451. (when (string-match
  16452. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?-?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  16453. ts)
  16454. (setq extra (match-string 1 ts))
  16455. (if suppress-tmp-delay
  16456. (setq extra (replace-regexp-in-string " --[0-9]+[hdwmy]" "" extra))))
  16457. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  16458. (setq with-hm t))
  16459. (setq time0 (org-parse-time-string ts))
  16460. (when (and updown
  16461. (eq org-ts-what 'minute)
  16462. (not current-prefix-arg))
  16463. ;; This looks like s-up and s-down. Change by one rounding step.
  16464. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  16465. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  16466. (setcar (cdr time0) (+ (nth 1 time0)
  16467. (if (> n 0) (- rem) (- dm rem))))))
  16468. (setq time
  16469. (encode-time (or (car time0) 0)
  16470. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  16471. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  16472. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  16473. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  16474. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  16475. (nthcdr 6 time0)))
  16476. (when (and (member org-ts-what '(hour minute))
  16477. extra
  16478. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  16479. (setq extra (org-modify-ts-extra
  16480. extra
  16481. (if (eq org-ts-what 'hour) 2 5)
  16482. n dm)))
  16483. (when (integerp org-ts-what)
  16484. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  16485. (if (eq what 'calendar)
  16486. (let ((cal-date (org-get-date-from-calendar)))
  16487. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  16488. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  16489. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  16490. (setcar time0 (or (car time0) 0))
  16491. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  16492. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  16493. (setq time (apply 'encode-time time0))))
  16494. ;; Insert the new time-stamp, and ensure point stays in the same
  16495. ;; category as before (i.e. not after the last position in that
  16496. ;; category).
  16497. (let ((pos (point)))
  16498. ;; Stay before inserted string. `save-excursion' is of no use.
  16499. (setq org-last-changed-timestamp
  16500. (org-insert-time-stamp time with-hm inactive nil nil extra))
  16501. (goto-char pos))
  16502. (save-match-data
  16503. (looking-at org-ts-regexp3)
  16504. (goto-char (cond
  16505. ;; `day' category ends before `hour' if any, or at
  16506. ;; the end of the day name.
  16507. ((eq origin-cat 'day)
  16508. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  16509. ((eq origin-cat 'hour) (min (match-end 7) origin))
  16510. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  16511. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  16512. ;; `year' and `month' have both fixed size: point
  16513. ;; couldn't have moved into another part.
  16514. (t origin))))
  16515. ;; Update clock if on a CLOCK line.
  16516. (org-clock-update-time-maybe)
  16517. ;; Maybe adjust the closest clock in `org-clock-history'
  16518. (when org-clock-adjust-closest
  16519. (if (not (and (org-at-clock-log-p)
  16520. (< 1 (length (delq nil (mapcar 'marker-position
  16521. org-clock-history))))))
  16522. (message "No clock to adjust")
  16523. (cond ((save-excursion ; fix previous clock?
  16524. (re-search-backward org-ts-regexp0 nil t)
  16525. (org-looking-back (concat org-clock-string " \\[")))
  16526. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  16527. ((save-excursion ; fix next clock?
  16528. (re-search-backward org-ts-regexp0 nil t)
  16529. (looking-at (concat org-ts-regexp0 "\\] =>")))
  16530. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  16531. (save-window-excursion
  16532. ;; Find closest clock to point, adjust the previous/next one in history
  16533. (let* ((p (save-excursion (org-back-to-heading t)))
  16534. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  16535. (clfixnth
  16536. (+ fixnext (- (length cl) (or (length (member (apply 'min cl) cl)) 100))))
  16537. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  16538. (if (not clfixpos)
  16539. (message "No clock to adjust")
  16540. (save-excursion
  16541. (org-goto-marker-or-bmk clfixpos)
  16542. (org-show-subtree)
  16543. (when (re-search-forward clrgx nil t)
  16544. (goto-char (match-beginning 1))
  16545. (let (org-clock-adjust-closest)
  16546. (org-timestamp-change n org-ts-what updown))
  16547. (message "Clock adjusted in %s for heading: %s"
  16548. (file-name-nondirectory (buffer-file-name))
  16549. (org-get-heading t t)))))))))
  16550. ;; Try to recenter the calendar window, if any.
  16551. (if (and org-calendar-follow-timestamp-change
  16552. (get-buffer-window "*Calendar*" t)
  16553. (memq org-ts-what '(day month year)))
  16554. (org-recenter-calendar (time-to-days time))))))
  16555. (defun org-modify-ts-extra (s pos n dm)
  16556. "Change the different parts of the lead-time and repeat fields in timestamp."
  16557. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  16558. ng h m new rem)
  16559. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  16560. (cond
  16561. ((or (org-pos-in-match-range pos 2)
  16562. (org-pos-in-match-range pos 3))
  16563. (setq m (string-to-number (match-string 3 s))
  16564. h (string-to-number (match-string 2 s)))
  16565. (if (org-pos-in-match-range pos 2)
  16566. (setq h (+ h n))
  16567. (setq n (* dm (org-no-warnings (signum n))))
  16568. (when (not (= 0 (setq rem (% m dm))))
  16569. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  16570. (setq m (+ m n)))
  16571. (if (< m 0) (setq m (+ m 60) h (1- h)))
  16572. (if (> m 59) (setq m (- m 60) h (1+ h)))
  16573. (setq h (min 24 (max 0 h)))
  16574. (setq ng 1 new (format "-%02d:%02d" h m)))
  16575. ((org-pos-in-match-range pos 6)
  16576. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  16577. ((org-pos-in-match-range pos 5)
  16578. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  16579. ((org-pos-in-match-range pos 9)
  16580. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  16581. ((org-pos-in-match-range pos 8)
  16582. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  16583. (when ng
  16584. (setq s (concat
  16585. (substring s 0 (match-beginning ng))
  16586. new
  16587. (substring s (match-end ng))))))
  16588. s))
  16589. (defun org-recenter-calendar (date)
  16590. "If the calendar is visible, recenter it to DATE."
  16591. (let ((cwin (get-buffer-window "*Calendar*" t)))
  16592. (when cwin
  16593. (let ((calendar-move-hook nil))
  16594. (with-selected-window cwin
  16595. (calendar-goto-date (if (listp date) date
  16596. (calendar-gregorian-from-absolute date))))))))
  16597. (defun org-goto-calendar (&optional arg)
  16598. "Go to the Emacs calendar at the current date.
  16599. If there is a time stamp in the current line, go to that date.
  16600. A prefix ARG can be used to force the current date."
  16601. (interactive "P")
  16602. (let ((tsr org-ts-regexp) diff
  16603. (calendar-move-hook nil)
  16604. (calendar-view-holidays-initially-flag nil)
  16605. (calendar-view-diary-initially-flag nil))
  16606. (if (or (org-at-timestamp-p)
  16607. (save-excursion
  16608. (beginning-of-line 1)
  16609. (looking-at (concat ".*" tsr))))
  16610. (let ((d1 (time-to-days (current-time)))
  16611. (d2 (time-to-days
  16612. (org-time-string-to-time (match-string 1)))))
  16613. (setq diff (- d2 d1))))
  16614. (calendar)
  16615. (calendar-goto-today)
  16616. (if (and diff (not arg)) (calendar-forward-day diff))))
  16617. (defun org-get-date-from-calendar ()
  16618. "Return a list (month day year) of date at point in calendar."
  16619. (with-current-buffer "*Calendar*"
  16620. (save-match-data
  16621. (calendar-cursor-to-date))))
  16622. (defun org-date-from-calendar ()
  16623. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  16624. If there is already a time stamp at the cursor position, update it."
  16625. (interactive)
  16626. (if (org-at-timestamp-p t)
  16627. (org-timestamp-change 0 'calendar)
  16628. (let ((cal-date (org-get-date-from-calendar)))
  16629. (org-insert-time-stamp
  16630. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  16631. (defcustom org-effort-durations
  16632. `(("min" . 1)
  16633. ("h" . 60)
  16634. ("d" . ,(* 60 8))
  16635. ("w" . ,(* 60 8 5))
  16636. ("m" . ,(* 60 8 5 4))
  16637. ("y" . ,(* 60 8 5 40)))
  16638. "Conversion factor to minutes for an effort modifier.
  16639. Each entry has the form (MODIFIER . MINUTES).
  16640. In an effort string, a number followed by MODIFIER is multiplied
  16641. by the specified number of MINUTES to obtain an effort in
  16642. minutes.
  16643. For example, if the value of this variable is ((\"hours\" . 60)), then an
  16644. effort string \"2hours\" is equivalent to 120 minutes."
  16645. :group 'org-agenda
  16646. :version "25.1"
  16647. :package-version '(Org . "8.3")
  16648. :type '(alist :key-type (string :tag "Modifier")
  16649. :value-type (number :tag "Minutes")))
  16650. (defun org-minutes-to-clocksum-string (m)
  16651. "Format number of minutes as a clocksum string.
  16652. The format is determined by `org-time-clocksum-format',
  16653. `org-time-clocksum-use-fractional' and
  16654. `org-time-clocksum-fractional-format' and
  16655. `org-time-clocksum-use-effort-durations'."
  16656. (let ((clocksum "")
  16657. (m (round m)) ; Don't allow fractions of minutes
  16658. h d w mo y fmt n)
  16659. (setq h (if org-time-clocksum-use-effort-durations
  16660. (cdr (assoc "h" org-effort-durations)) 60)
  16661. d (if org-time-clocksum-use-effort-durations
  16662. (/ (cdr (assoc "d" org-effort-durations)) h) 24)
  16663. w (if org-time-clocksum-use-effort-durations
  16664. (/ (cdr (assoc "w" org-effort-durations)) (* d h)) 7)
  16665. mo (if org-time-clocksum-use-effort-durations
  16666. (/ (cdr (assoc "m" org-effort-durations)) (* d h)) 30)
  16667. y (if org-time-clocksum-use-effort-durations
  16668. (/ (cdr (assoc "y" org-effort-durations)) (* d h)) 365))
  16669. ;; fractional format
  16670. (if org-time-clocksum-use-fractional
  16671. (cond
  16672. ;; single format string
  16673. ((stringp org-time-clocksum-fractional-format)
  16674. (format org-time-clocksum-fractional-format (/ m (float h))))
  16675. ;; choice of fractional formats for different time units
  16676. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16677. (> (/ (truncate m) (* y d h)) 0))
  16678. (format fmt (/ m (* y d (float h)))))
  16679. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16680. (> (/ (truncate m) (* mo d h)) 0))
  16681. (format fmt (/ m (* mo d (float h)))))
  16682. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16683. (> (/ (truncate m) (* w d h)) 0))
  16684. (format fmt (/ m (* w d (float h)))))
  16685. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16686. (> (/ (truncate m) (* d h)) 0))
  16687. (format fmt (/ m (* d (float h)))))
  16688. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16689. (> (/ (truncate m) h) 0))
  16690. (format fmt (/ m (float h))))
  16691. ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
  16692. (format fmt m))
  16693. ;; fall back to smallest time unit with a format
  16694. ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16695. (format fmt (/ m (float h))))
  16696. ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16697. (format fmt (/ m (* d (float h)))))
  16698. ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16699. (format fmt (/ m (* w d (float h)))))
  16700. ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16701. (format fmt (/ m (* mo d (float h)))))
  16702. ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16703. (format fmt (/ m (* y d (float h))))))
  16704. ;; standard (non-fractional) format, with single format string
  16705. (if (stringp org-time-clocksum-format)
  16706. (format org-time-clocksum-format (setq n (/ m h)) (- m (* h n)))
  16707. ;; separate formats components
  16708. (and (setq fmt (plist-get org-time-clocksum-format :years))
  16709. (or (> (setq n (/ (truncate m) (* y d h))) 0)
  16710. (plist-get org-time-clocksum-format :require-years))
  16711. (setq clocksum (concat clocksum (format fmt n))
  16712. m (- m (* n y d h))))
  16713. (and (setq fmt (plist-get org-time-clocksum-format :months))
  16714. (or (> (setq n (/ (truncate m) (* mo d h))) 0)
  16715. (plist-get org-time-clocksum-format :require-months))
  16716. (setq clocksum (concat clocksum (format fmt n))
  16717. m (- m (* n mo d h))))
  16718. (and (setq fmt (plist-get org-time-clocksum-format :weeks))
  16719. (or (> (setq n (/ (truncate m) (* w d h))) 0)
  16720. (plist-get org-time-clocksum-format :require-weeks))
  16721. (setq clocksum (concat clocksum (format fmt n))
  16722. m (- m (* n w d h))))
  16723. (and (setq fmt (plist-get org-time-clocksum-format :days))
  16724. (or (> (setq n (/ (truncate m) (* d h))) 0)
  16725. (plist-get org-time-clocksum-format :require-days))
  16726. (setq clocksum (concat clocksum (format fmt n))
  16727. m (- m (* n d h))))
  16728. (and (setq fmt (plist-get org-time-clocksum-format :hours))
  16729. (or (> (setq n (/ (truncate m) h)) 0)
  16730. (plist-get org-time-clocksum-format :require-hours))
  16731. (setq clocksum (concat clocksum (format fmt n))
  16732. m (- m (* n h))))
  16733. (and (setq fmt (plist-get org-time-clocksum-format :minutes))
  16734. (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
  16735. (setq clocksum (concat clocksum (format fmt m))))
  16736. ;; return formatted time duration
  16737. clocksum))))
  16738. (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
  16739. (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
  16740. "Org mode version 8.0")
  16741. (defun org-hours-to-clocksum-string (n)
  16742. (org-minutes-to-clocksum-string (* n 60)))
  16743. (defun org-hh:mm-string-to-minutes (s)
  16744. "Convert a string H:MM to a number of minutes.
  16745. If the string is just a number, interpret it as minutes.
  16746. In fact, the first hh:mm or number in the string will be taken,
  16747. there can be extra stuff in the string.
  16748. If no number is found, the return value is 0."
  16749. (cond
  16750. ((integerp s) s)
  16751. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  16752. (+ (* (string-to-number (match-string 1 s)) 60)
  16753. (string-to-number (match-string 2 s))))
  16754. ((string-match "\\([0-9]+\\)" s)
  16755. (string-to-number (match-string 1 s)))
  16756. (t 0)))
  16757. (defcustom org-image-actual-width t
  16758. "Should we use the actual width of images when inlining them?
  16759. When set to t, always use the image width.
  16760. When set to a number, use imagemagick (when available) to set
  16761. the image's width to this value.
  16762. When set to a number in a list, try to get the width from any
  16763. #+ATTR.* keyword if it matches a width specification like
  16764. #+ATTR_HTML: :width 300px
  16765. and fall back on that number if none is found.
  16766. When set to nil, try to get the width from an #+ATTR.* keyword
  16767. and fall back on the original width if none is found.
  16768. This requires Emacs >= 24.1, build with imagemagick support."
  16769. :group 'org-appearance
  16770. :version "24.4"
  16771. :package-version '(Org . "8.0")
  16772. :type '(choice
  16773. (const :tag "Use the image width" t)
  16774. (integer :tag "Use a number of pixels")
  16775. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  16776. (const :tag "Use #+ATTR* or don't resize" nil)))
  16777. (defcustom org-agenda-inhibit-startup nil
  16778. "Inhibit startup when preparing agenda buffers.
  16779. When this variable is t, the initialization of the Org agenda
  16780. buffers is inhibited: e.g. the visibility state is not set, the
  16781. tables are not re-aligned, etc."
  16782. :type 'boolean
  16783. :version "24.3"
  16784. :group 'org-agenda)
  16785. (define-obsolete-variable-alias
  16786. 'org-agenda-ignore-drawer-properties
  16787. 'org-agenda-ignore-properties "25.1")
  16788. (defcustom org-agenda-ignore-properties nil
  16789. "Avoid updating text properties when building the agenda.
  16790. Properties are used to prepare buffers for effort estimates,
  16791. appointments, statistics and subtree-local categories.
  16792. If you don't use these in the agenda, you can add them to this
  16793. list and agenda building will be a bit faster.
  16794. The value is a list, with zero or more of the symbols `effort', `appt',
  16795. `stats' or `category'."
  16796. :type '(set :greedy t
  16797. (const effort)
  16798. (const appt)
  16799. (const stats)
  16800. (const category))
  16801. :version "25.1"
  16802. :package-version '(Org . "8.3")
  16803. :group 'org-agenda)
  16804. (defun org-duration-string-to-minutes (s &optional output-to-string)
  16805. "Convert a duration string S to minutes.
  16806. A bare number is interpreted as minutes, modifiers can be set by
  16807. customizing `org-effort-durations' (which see).
  16808. Entries containing a colon are interpreted as H:MM by
  16809. `org-hh:mm-string-to-minutes'."
  16810. (let ((result 0)
  16811. (re (concat "\\([0-9.]+\\) *\\("
  16812. (regexp-opt (mapcar 'car org-effort-durations))
  16813. "\\)")))
  16814. (while (string-match re s)
  16815. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  16816. (string-to-number (match-string 1 s))))
  16817. (setq s (replace-match "" nil t s)))
  16818. (setq result (floor result))
  16819. (incf result (org-hh:mm-string-to-minutes s))
  16820. (if output-to-string (number-to-string result) result)))
  16821. ;;;; Files
  16822. (defun org-save-all-org-buffers ()
  16823. "Save all Org-mode buffers without user confirmation."
  16824. (interactive)
  16825. (message "Saving all Org-mode buffers...")
  16826. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  16827. (when (featurep 'org-id) (org-id-locations-save))
  16828. (message "Saving all Org-mode buffers... done"))
  16829. (defun org-revert-all-org-buffers ()
  16830. "Revert all Org-mode buffers.
  16831. Prompt for confirmation when there are unsaved changes.
  16832. Be sure you know what you are doing before letting this function
  16833. overwrite your changes.
  16834. This function is useful in a setup where one tracks org files
  16835. with a version control system, to revert on one machine after pulling
  16836. changes from another. I believe the procedure must be like this:
  16837. 1. M-x org-save-all-org-buffers
  16838. 2. Pull changes from the other machine, resolve conflicts
  16839. 3. M-x org-revert-all-org-buffers"
  16840. (interactive)
  16841. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  16842. (user-error "Abort"))
  16843. (save-excursion
  16844. (save-window-excursion
  16845. (mapc
  16846. (lambda (b)
  16847. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  16848. (with-current-buffer b buffer-file-name))
  16849. (org-pop-to-buffer-same-window b)
  16850. (revert-buffer t 'no-confirm)))
  16851. (buffer-list))
  16852. (when (and (featurep 'org-id) org-id-track-globally)
  16853. (org-id-locations-load)))))
  16854. ;;;; Agenda files
  16855. ;;;###autoload
  16856. (defun org-switchb (&optional arg)
  16857. "Switch between Org buffers.
  16858. With one prefix argument, restrict available buffers to files.
  16859. With two prefix arguments, restrict available buffers to agenda files.
  16860. Defaults to `iswitchb' for buffer name completion.
  16861. Set `org-completion-use-ido' to make it use ido instead."
  16862. (interactive "P")
  16863. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  16864. ((equal arg '(16)) (org-buffer-list 'agenda))
  16865. (t (org-buffer-list))))
  16866. (org-completion-use-iswitchb org-completion-use-iswitchb)
  16867. (org-completion-use-ido org-completion-use-ido))
  16868. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  16869. (setq org-completion-use-iswitchb t))
  16870. (org-pop-to-buffer-same-window
  16871. (org-icompleting-read "Org buffer: "
  16872. (mapcar 'list (mapcar 'buffer-name blist))
  16873. nil t))))
  16874. ;;; Define some older names previously used for this functionality
  16875. ;;;###autoload
  16876. (defalias 'org-ido-switchb 'org-switchb)
  16877. ;;;###autoload
  16878. (defalias 'org-iswitchb 'org-switchb)
  16879. (defun org-buffer-list (&optional predicate exclude-tmp)
  16880. "Return a list of Org buffers.
  16881. PREDICATE can be `export', `files' or `agenda'.
  16882. export restrict the list to Export buffers.
  16883. files restrict the list to buffers visiting Org files.
  16884. agenda restrict the list to buffers visiting agenda files.
  16885. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  16886. (let* ((bfn nil)
  16887. (agenda-files (and (eq predicate 'agenda)
  16888. (mapcar 'file-truename (org-agenda-files t))))
  16889. (filter
  16890. (cond
  16891. ((eq predicate 'files)
  16892. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  16893. ((eq predicate 'export)
  16894. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  16895. ((eq predicate 'agenda)
  16896. (lambda (b)
  16897. (with-current-buffer b
  16898. (and (derived-mode-p 'org-mode)
  16899. (setq bfn (buffer-file-name b))
  16900. (member (file-truename bfn) agenda-files)))))
  16901. (t (lambda (b) (with-current-buffer b
  16902. (or (derived-mode-p 'org-mode)
  16903. (string-match "\*Org .*Export"
  16904. (buffer-name b)))))))))
  16905. (delq nil
  16906. (mapcar
  16907. (lambda(b)
  16908. (if (and (funcall filter b)
  16909. (or (not exclude-tmp)
  16910. (not (string-match "tmp" (buffer-name b)))))
  16911. b
  16912. nil))
  16913. (buffer-list)))))
  16914. (defun org-agenda-files (&optional unrestricted archives)
  16915. "Get the list of agenda files.
  16916. Optional UNRESTRICTED means return the full list even if a restriction
  16917. is currently in place.
  16918. When ARCHIVES is t, include all archive files that are really being
  16919. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  16920. `org-agenda-archives-mode' is t."
  16921. (let ((files
  16922. (cond
  16923. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  16924. ((stringp org-agenda-files) (org-read-agenda-file-list))
  16925. ((listp org-agenda-files) org-agenda-files)
  16926. (t (error "Invalid value of `org-agenda-files'")))))
  16927. (setq files (apply 'append
  16928. (mapcar (lambda (f)
  16929. (if (file-directory-p f)
  16930. (directory-files
  16931. f t org-agenda-file-regexp)
  16932. (list f)))
  16933. files)))
  16934. (when org-agenda-skip-unavailable-files
  16935. (setq files (delq nil
  16936. (mapcar (function
  16937. (lambda (file)
  16938. (and (file-readable-p file) file)))
  16939. files))))
  16940. (when (or (eq archives t)
  16941. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  16942. (setq files (org-add-archive-files files)))
  16943. files))
  16944. (defun org-agenda-file-p (&optional file)
  16945. "Return non-nil, if FILE is an agenda file.
  16946. If FILE is omitted, use the file associated with the current
  16947. buffer."
  16948. (let ((fname (or file (buffer-file-name))))
  16949. (and fname
  16950. (member (file-truename fname)
  16951. (mapcar #'file-truename (org-agenda-files t))))))
  16952. (defun org-edit-agenda-file-list ()
  16953. "Edit the list of agenda files.
  16954. Depending on setup, this either uses customize to edit the variable
  16955. `org-agenda-files', or it visits the file that is holding the list. In the
  16956. latter case, the buffer is set up in a way that saving it automatically kills
  16957. the buffer and restores the previous window configuration."
  16958. (interactive)
  16959. (if (stringp org-agenda-files)
  16960. (let ((cw (current-window-configuration)))
  16961. (find-file org-agenda-files)
  16962. (org-set-local 'org-window-configuration cw)
  16963. (org-add-hook 'after-save-hook
  16964. (lambda ()
  16965. (set-window-configuration
  16966. (prog1 org-window-configuration
  16967. (kill-buffer (current-buffer))))
  16968. (org-install-agenda-files-menu)
  16969. (message "New agenda file list installed"))
  16970. nil 'local)
  16971. (message "%s" (substitute-command-keys
  16972. "Edit list and finish with \\[save-buffer]")))
  16973. (customize-variable 'org-agenda-files)))
  16974. (defun org-store-new-agenda-file-list (list)
  16975. "Set new value for the agenda file list and save it correctly."
  16976. (if (stringp org-agenda-files)
  16977. (let ((fe (org-read-agenda-file-list t)) b u)
  16978. (while (setq b (find-buffer-visiting org-agenda-files))
  16979. (kill-buffer b))
  16980. (with-temp-file org-agenda-files
  16981. (insert
  16982. (mapconcat
  16983. (lambda (f) ;; Keep un-expanded entries.
  16984. (if (setq u (assoc f fe))
  16985. (cdr u)
  16986. f))
  16987. list "\n")
  16988. "\n")))
  16989. (let ((org-mode-hook nil) (org-inhibit-startup t)
  16990. (org-insert-mode-line-in-empty-file nil))
  16991. (setq org-agenda-files list)
  16992. (customize-save-variable 'org-agenda-files org-agenda-files))))
  16993. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  16994. "Read the list of agenda files from a file.
  16995. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  16996. filenames, used by `org-store-new-agenda-file-list' to write back
  16997. un-expanded file names."
  16998. (when (file-directory-p org-agenda-files)
  16999. (error "`org-agenda-files' cannot be a single directory"))
  17000. (when (stringp org-agenda-files)
  17001. (with-temp-buffer
  17002. (insert-file-contents org-agenda-files)
  17003. (mapcar
  17004. (lambda (f)
  17005. (let ((e (expand-file-name (substitute-in-file-name f)
  17006. org-directory)))
  17007. (if pair-with-expansion
  17008. (cons e f)
  17009. e)))
  17010. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  17011. ;;;###autoload
  17012. (defun org-cycle-agenda-files ()
  17013. "Cycle through the files in `org-agenda-files'.
  17014. If the current buffer visits an agenda file, find the next one in the list.
  17015. If the current buffer does not, find the first agenda file."
  17016. (interactive)
  17017. (let* ((fs (or (org-agenda-files t)
  17018. (user-error "No agenda files")))
  17019. (files (copy-sequence fs))
  17020. (tcf (and buffer-file-name (file-truename buffer-file-name)))
  17021. file)
  17022. (when tcf
  17023. (while (and (setq file (pop files))
  17024. (not (equal (file-truename file) tcf)))))
  17025. (find-file (car (or files fs)))
  17026. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  17027. (defun org-agenda-file-to-front (&optional to-end)
  17028. "Move/add the current file to the top of the agenda file list.
  17029. If the file is not present in the list, it is added to the front. If it is
  17030. present, it is moved there. With optional argument TO-END, add/move to the
  17031. end of the list."
  17032. (interactive "P")
  17033. (let ((org-agenda-skip-unavailable-files nil)
  17034. (file-alist (mapcar (lambda (x)
  17035. (cons (file-truename x) x))
  17036. (org-agenda-files t)))
  17037. (ctf (file-truename
  17038. (or buffer-file-name
  17039. (user-error "Please save the current buffer to a file"))))
  17040. x had)
  17041. (setq x (assoc ctf file-alist) had x)
  17042. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  17043. (if to-end
  17044. (setq file-alist (append (delq x file-alist) (list x)))
  17045. (setq file-alist (cons x (delq x file-alist))))
  17046. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  17047. (org-install-agenda-files-menu)
  17048. (message "File %s to %s of agenda file list"
  17049. (if had "moved" "added") (if to-end "end" "front"))))
  17050. (defun org-remove-file (&optional file)
  17051. "Remove current file from the list of files in variable `org-agenda-files'.
  17052. These are the files which are being checked for agenda entries.
  17053. Optional argument FILE means use this file instead of the current."
  17054. (interactive)
  17055. (let* ((org-agenda-skip-unavailable-files nil)
  17056. (file (or file buffer-file-name
  17057. (user-error "Current buffer does not visit a file")))
  17058. (true-file (file-truename file))
  17059. (afile (abbreviate-file-name file))
  17060. (files (delq nil (mapcar
  17061. (lambda (x)
  17062. (if (equal true-file
  17063. (file-truename x))
  17064. nil x))
  17065. (org-agenda-files t)))))
  17066. (if (not (= (length files) (length (org-agenda-files t))))
  17067. (progn
  17068. (org-store-new-agenda-file-list files)
  17069. (org-install-agenda-files-menu)
  17070. (message "Removed from Org Agenda list: %s" afile))
  17071. (message "File was not in list: %s (not removed)" afile))))
  17072. (defun org-file-menu-entry (file)
  17073. (vector file (list 'find-file file) t))
  17074. (defun org-check-agenda-file (file)
  17075. "Make sure FILE exists. If not, ask user what to do."
  17076. (when (not (file-exists-p file))
  17077. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  17078. (abbreviate-file-name file))
  17079. (let ((r (downcase (read-char-exclusive))))
  17080. (cond
  17081. ((equal r ?r)
  17082. (org-remove-file file)
  17083. (throw 'nextfile t))
  17084. (t (user-error "Abort"))))))
  17085. (defun org-get-agenda-file-buffer (file)
  17086. "Get an agenda buffer visiting FILE.
  17087. If the buffer needs to be created, add it to the list of buffers
  17088. which might be released later."
  17089. (let ((buf (org-find-base-buffer-visiting file)))
  17090. (if buf
  17091. buf ; just return it
  17092. ;; Make a new buffer and remember it
  17093. (setq buf (find-file-noselect file))
  17094. (if buf (push buf org-agenda-new-buffers))
  17095. buf)))
  17096. (defun org-release-buffers (blist)
  17097. "Release all buffers in list, asking the user for confirmation when needed.
  17098. When a buffer is unmodified, it is just killed. When modified, it is saved
  17099. \(if the user agrees) and then killed."
  17100. (let (file)
  17101. (dolist (buf blist)
  17102. (setq file (buffer-file-name buf))
  17103. (when (and (buffer-modified-p buf)
  17104. file
  17105. (y-or-n-p (format "Save file %s? " file)))
  17106. (with-current-buffer buf (save-buffer)))
  17107. (kill-buffer buf))))
  17108. (defun org-agenda-prepare-buffers (files)
  17109. "Create buffers for all agenda files, protect archived trees and comments."
  17110. (interactive)
  17111. (let ((pa '(:org-archived t))
  17112. (pc '(:org-comment t))
  17113. (pall '(:org-archived t :org-comment t))
  17114. (inhibit-read-only t)
  17115. (org-inhibit-startup org-agenda-inhibit-startup)
  17116. (rea (concat ":" org-archive-tag ":"))
  17117. file re pos)
  17118. (setq org-tag-alist-for-agenda nil
  17119. org-tag-groups-alist-for-agenda nil)
  17120. (save-excursion
  17121. (save-restriction
  17122. (dolist (file files)
  17123. (catch 'nextfile
  17124. (if (bufferp file)
  17125. (set-buffer file)
  17126. (org-check-agenda-file file)
  17127. (set-buffer (org-get-agenda-file-buffer file)))
  17128. (widen)
  17129. (org-set-regexps-and-options 'tags-only)
  17130. (setq pos (point))
  17131. (or (memq 'category org-agenda-ignore-properties)
  17132. (org-refresh-category-properties))
  17133. (or (memq 'stats org-agenda-ignore-properties)
  17134. (org-refresh-stats-properties))
  17135. (or (memq 'effort org-agenda-ignore-properties)
  17136. (org-refresh-effort-properties))
  17137. (or (memq 'appt org-agenda-ignore-properties)
  17138. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime))
  17139. (setq org-todo-keywords-for-agenda
  17140. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  17141. (setq org-done-keywords-for-agenda
  17142. (append org-done-keywords-for-agenda org-done-keywords))
  17143. (setq org-todo-keyword-alist-for-agenda
  17144. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  17145. (setq org-tag-alist-for-agenda
  17146. (org-uniquify
  17147. (append org-tag-alist-for-agenda
  17148. org-tag-alist
  17149. org-tag-persistent-alist)))
  17150. (if org-group-tags
  17151. (setq org-tag-groups-alist-for-agenda
  17152. (org-uniquify-alist
  17153. (append org-tag-groups-alist-for-agenda org-tag-groups-alist))))
  17154. (org-with-silent-modifications
  17155. (save-excursion
  17156. (remove-text-properties (point-min) (point-max) pall)
  17157. (when org-agenda-skip-archived-trees
  17158. (goto-char (point-min))
  17159. (while (re-search-forward rea nil t)
  17160. (if (org-at-heading-p t)
  17161. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  17162. (goto-char (point-min))
  17163. (setq re (format "^\\*+ .*\\<%s\\>" org-comment-string))
  17164. (while (re-search-forward re nil t)
  17165. (when (save-match-data (org-in-commented-heading-p t))
  17166. (add-text-properties
  17167. (match-beginning 0) (org-end-of-subtree t) pc)))))
  17168. (goto-char pos)))))
  17169. (setq org-todo-keywords-for-agenda
  17170. (org-uniquify org-todo-keywords-for-agenda))
  17171. (setq org-todo-keyword-alist-for-agenda
  17172. (org-uniquify org-todo-keyword-alist-for-agenda))))
  17173. ;;;; CDLaTeX minor mode
  17174. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  17175. "Keymap for the minor `org-cdlatex-mode'.")
  17176. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  17177. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  17178. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  17179. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  17180. (org-defkey org-cdlatex-mode-map "\C-c{" 'org-cdlatex-environment-indent)
  17181. (defvar org-cdlatex-texmathp-advice-is-done nil
  17182. "Flag remembering if we have applied the advice to texmathp already.")
  17183. (define-minor-mode org-cdlatex-mode
  17184. "Toggle the minor `org-cdlatex-mode'.
  17185. This mode supports entering LaTeX environment and math in LaTeX fragments
  17186. in Org-mode.
  17187. \\{org-cdlatex-mode-map}"
  17188. nil " OCDL" nil
  17189. (when org-cdlatex-mode
  17190. (require 'cdlatex)
  17191. (run-hooks 'cdlatex-mode-hook)
  17192. (cdlatex-compute-tables))
  17193. (unless org-cdlatex-texmathp-advice-is-done
  17194. (setq org-cdlatex-texmathp-advice-is-done t)
  17195. (defadvice texmathp (around org-math-always-on activate)
  17196. "Always return t in org-mode buffers.
  17197. This is because we want to insert math symbols without dollars even outside
  17198. the LaTeX math segments. If Orgmode thinks that point is actually inside
  17199. an embedded LaTeX fragment, let texmathp do its job.
  17200. \\[org-cdlatex-mode-map]"
  17201. (interactive)
  17202. (let (p)
  17203. (cond
  17204. ((not (derived-mode-p 'org-mode)) ad-do-it)
  17205. ((eq this-command 'cdlatex-math-symbol)
  17206. (setq ad-return-value t
  17207. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  17208. (t
  17209. (let ((p (org-inside-LaTeX-fragment-p)))
  17210. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  17211. (setq ad-return-value t
  17212. texmathp-why '("Org-mode embedded math" . 0))
  17213. (if p ad-do-it)))))))))
  17214. (defun turn-on-org-cdlatex ()
  17215. "Unconditionally turn on `org-cdlatex-mode'."
  17216. (org-cdlatex-mode 1))
  17217. (defun org-try-cdlatex-tab ()
  17218. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  17219. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  17220. - inside a LaTeX fragment, or
  17221. - after the first word in a line, where an abbreviation expansion could
  17222. insert a LaTeX environment."
  17223. (when org-cdlatex-mode
  17224. (cond
  17225. ;; Before any word on the line: No expansion possible.
  17226. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  17227. ;; Just after first word on the line: Expand it. Make sure it
  17228. ;; cannot happen on headlines, though.
  17229. ((save-excursion
  17230. (skip-chars-backward "a-zA-Z0-9*")
  17231. (skip-chars-backward " \t")
  17232. (and (bolp) (not (org-at-heading-p))))
  17233. (cdlatex-tab) t)
  17234. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  17235. (defun org-cdlatex-underscore-caret (&optional arg)
  17236. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  17237. Revert to the normal definition outside of these fragments."
  17238. (interactive "P")
  17239. (if (org-inside-LaTeX-fragment-p)
  17240. (call-interactively 'cdlatex-sub-superscript)
  17241. (let (org-cdlatex-mode)
  17242. (call-interactively (key-binding (vector last-input-event))))))
  17243. (defun org-cdlatex-math-modify (&optional arg)
  17244. "Execute `cdlatex-math-modify' in LaTeX fragments.
  17245. Revert to the normal definition outside of these fragments."
  17246. (interactive "P")
  17247. (if (org-inside-LaTeX-fragment-p)
  17248. (call-interactively 'cdlatex-math-modify)
  17249. (let (org-cdlatex-mode)
  17250. (call-interactively (key-binding (vector last-input-event))))))
  17251. (defun org-cdlatex-environment-indent (&optional environment item)
  17252. "Execute `cdlatex-environment' and indent the inserted environment.
  17253. ENVIRONMENT and ITEM are passed to `cdlatex-environment'.
  17254. The inserted environment is indented to current indentation
  17255. unless point is at the beginning of the line, in which the
  17256. environment remains unintended."
  17257. (interactive)
  17258. ;; cdlatex-environment always return nil. Therefore, capture output
  17259. ;; first and determine if an environment was selected.
  17260. (let* ((beg (point-marker))
  17261. (end (copy-marker (point) t))
  17262. (inserted (progn
  17263. (ignore-errors (cdlatex-environment environment item))
  17264. (< beg end)))
  17265. ;; Figure out how many lines to move forward after the
  17266. ;; environment has been inserted.
  17267. (lines (when inserted
  17268. (save-excursion
  17269. (- (loop while (< beg (point))
  17270. with x = 0
  17271. do (forward-line -1)
  17272. (incf x)
  17273. finally return x)
  17274. (if (progn (goto-char beg)
  17275. (and (progn (skip-chars-forward " \t") (eolp))
  17276. (progn (skip-chars-backward " \t") (bolp))))
  17277. 1 0)))))
  17278. (env (org-trim (delete-and-extract-region beg end))))
  17279. (when inserted
  17280. ;; Get indentation of next line unless at column 0.
  17281. (let ((ind (if (bolp) 0
  17282. (save-excursion
  17283. (org-return-indent)
  17284. (prog1 (org-get-indentation)
  17285. (when (progn (skip-chars-forward " \t") (eolp))
  17286. (delete-region beg (point)))))))
  17287. (bol (progn (skip-chars-backward " \t") (bolp))))
  17288. ;; Insert a newline before environment unless at column zero
  17289. ;; to "escape" the current line. Insert a newline if
  17290. ;; something is one the same line as \end{ENVIRONMENT}.
  17291. (insert
  17292. (concat (unless bol "\n") env
  17293. (when (and (skip-chars-forward " \t") (not (eolp))) "\n")))
  17294. (unless (zerop ind)
  17295. (save-excursion
  17296. (goto-char beg)
  17297. (while (< (point) end)
  17298. (unless (eolp) (org-indent-line-to ind))
  17299. (forward-line))))
  17300. (goto-char beg)
  17301. (forward-line lines)
  17302. (org-indent-line-to ind)))
  17303. (set-marker beg nil)
  17304. (set-marker end nil)))
  17305. ;;;; LaTeX fragments
  17306. (defun org-inside-LaTeX-fragment-p ()
  17307. "Test if point is inside a LaTeX fragment.
  17308. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  17309. sequence appearing also before point.
  17310. Even though the matchers for math are configurable, this function assumes
  17311. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  17312. delimiters are skipped when they have been removed by customization.
  17313. The return value is nil, or a cons cell with the delimiter and the
  17314. position of this delimiter.
  17315. This function does a reasonably good job, but can locally be fooled by
  17316. for example currency specifications. For example it will assume being in
  17317. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  17318. fragments that are properly closed, but during editing, we have to live
  17319. with the uncertainty caused by missing closing delimiters. This function
  17320. looks only before point, not after."
  17321. (catch 'exit
  17322. (let ((pos (point))
  17323. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  17324. (lim (progn
  17325. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  17326. (point)))
  17327. dd-on str (start 0) m re)
  17328. (goto-char pos)
  17329. (when dodollar
  17330. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  17331. re (nth 1 (assoc "$" org-latex-regexps)))
  17332. (while (string-match re str start)
  17333. (cond
  17334. ((= (match-end 0) (length str))
  17335. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  17336. ((= (match-end 0) (- (length str) 5))
  17337. (throw 'exit nil))
  17338. (t (setq start (match-end 0))))))
  17339. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  17340. (goto-char pos)
  17341. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  17342. (and (match-beginning 2) (throw 'exit nil))
  17343. ;; count $$
  17344. (while (re-search-backward "\\$\\$" lim t)
  17345. (setq dd-on (not dd-on)))
  17346. (goto-char pos)
  17347. (if dd-on (cons "$$" m))))))
  17348. (defun org-inside-latex-macro-p ()
  17349. "Is point inside a LaTeX macro or its arguments?"
  17350. (save-match-data
  17351. (org-in-regexp
  17352. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  17353. (defvar org-latex-fragment-image-overlays nil
  17354. "List of overlays carrying the images of latex fragments.")
  17355. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  17356. (defun org-remove-latex-fragment-image-overlays (&optional beg end)
  17357. "Remove all overlays with LaTeX fragment images in current buffer.
  17358. When optional arguments BEG and END are non-nil, remove all
  17359. overlays between them instead. Return t when some overlays were
  17360. removed, nil otherwise."
  17361. (let (removedp)
  17362. (setq org-latex-fragment-image-overlays
  17363. (let ((beg (or beg (point-min)))
  17364. (end (or end (point-max))))
  17365. (org-remove-if
  17366. (lambda (o)
  17367. (and (>= (overlay-start o) beg)
  17368. (<= (overlay-end o) end)
  17369. (progn (delete-overlay o)
  17370. (or removedp (setq removedp t)))))
  17371. org-latex-fragment-image-overlays)))
  17372. removedp))
  17373. (define-obsolete-function-alias
  17374. 'org-preview-latex-fragment 'org-toggle-latex-fragment "24.4")
  17375. (defun org-toggle-latex-fragment (&optional arg)
  17376. "Preview the LaTeX fragment at point, or all locally or globally.
  17377. If the cursor is on a LaTeX fragment, create the image and overlay
  17378. it over the source code, if there is none. Remove it otherwise.
  17379. If there is no fragment at point, display all fragments in the
  17380. current section.
  17381. With prefix ARG, preview or clear image for all fragments in the
  17382. current subtree or in the whole buffer when used before the first
  17383. headline. With a double prefix ARG \\[universal-argument] \
  17384. \\[universal-argument] preview or clear images
  17385. for all fragments in the buffer."
  17386. (interactive "P")
  17387. (unless (buffer-file-name (buffer-base-buffer))
  17388. (user-error "Can't preview LaTeX fragment in a non-file buffer"))
  17389. (when (display-graphic-p)
  17390. (catch 'exit
  17391. (save-excursion
  17392. (let ((window-start (window-start)) msg)
  17393. (save-restriction
  17394. (cond
  17395. ((or (equal arg '(16))
  17396. (and (equal arg '(4))
  17397. (org-with-limited-levels (org-before-first-heading-p))))
  17398. (if (org-remove-latex-fragment-image-overlays)
  17399. (progn (message "LaTeX fragments images removed from buffer")
  17400. (throw 'exit nil))
  17401. (setq msg "Creating images for buffer...")))
  17402. ((equal arg '(4))
  17403. (org-with-limited-levels (org-back-to-heading t))
  17404. (let ((beg (point))
  17405. (end (progn (org-end-of-subtree t) (point))))
  17406. (if (org-remove-latex-fragment-image-overlays beg end)
  17407. (progn
  17408. (message "LaTeX fragment images removed from subtree")
  17409. (throw 'exit nil))
  17410. (setq msg "Creating images for subtree...")
  17411. (narrow-to-region beg end))))
  17412. ((let ((datum (org-element-context)))
  17413. (when (memq (org-element-type datum)
  17414. '(latex-environment latex-fragment))
  17415. (let* ((beg (org-element-property :begin datum))
  17416. (end (org-element-property :end datum)))
  17417. (if (org-remove-latex-fragment-image-overlays beg end)
  17418. (progn (message "LaTeX fragment image removed")
  17419. (throw 'exit nil))
  17420. (narrow-to-region beg end)
  17421. (setq msg "Creating image..."))))))
  17422. (t
  17423. (org-with-limited-levels
  17424. (let ((beg (if (org-at-heading-p) (line-beginning-position)
  17425. (outline-previous-heading)
  17426. (point)))
  17427. (end (progn (outline-next-heading) (point))))
  17428. (if (org-remove-latex-fragment-image-overlays beg end)
  17429. (progn
  17430. (message "LaTeX fragment images removed from section")
  17431. (throw 'exit nil))
  17432. (setq msg "Creating images for section...")
  17433. (narrow-to-region beg end))))))
  17434. (let ((file (buffer-file-name (buffer-base-buffer))))
  17435. (org-format-latex
  17436. (concat org-latex-preview-ltxpng-directory
  17437. (file-name-sans-extension (file-name-nondirectory file)))
  17438. ;; Emacs cannot overlay images from remote hosts.
  17439. ;; Create it in `temporary-file-directory' instead.
  17440. (if (file-remote-p file) temporary-file-directory
  17441. default-directory)
  17442. 'overlays msg 'forbuffer
  17443. org-latex-create-formula-image-program)))
  17444. ;; Work around a bug that doesn't restore window's start
  17445. ;; when widening back the buffer.
  17446. (set-window-start nil window-start)
  17447. (message (concat msg "done")))))))
  17448. (defun org-format-latex
  17449. (prefix &optional dir overlays msg forbuffer processing-type)
  17450. "Replace LaTeX fragments with links to an image, and produce images.
  17451. When optional argument OVERLAYS is non-nil, display the image on
  17452. top of the fragment instead of replacing it.
  17453. PROCESSING-TYPE is the conversion method to use, as a symbol.
  17454. Some of the options can be changed using the variable
  17455. `org-format-latex-options', which see."
  17456. (when (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  17457. (unless (eq processing-type 'verbatim)
  17458. (let* ((math-regexp "\\$\\|\\\\[([]\\|^[ \t]*\\\\begin{[A-Za-z0-9*]+}")
  17459. (cnt 0)
  17460. checkdir-flag)
  17461. (goto-char (point-min))
  17462. ;; Optimize overlay creation: (info "(elisp) Managing Overlays").
  17463. (when (and overlays (memq processing-type '(dvipng imagemagick)))
  17464. (overlay-recenter (point-max)))
  17465. (while (re-search-forward math-regexp nil t)
  17466. (unless (and overlays
  17467. (eq (get-char-property (point) 'org-overlay-type)
  17468. 'org-latex-overlay))
  17469. (let* ((context (org-element-context))
  17470. (type (org-element-type context)))
  17471. (when (memq type '(latex-environment latex-fragment))
  17472. (let ((block-type (eq type 'latex-environment))
  17473. (value (org-element-property :value context))
  17474. (beg (org-element-property :begin context))
  17475. (end (save-excursion
  17476. (goto-char (org-element-property :end context))
  17477. (skip-chars-backward " \r\t\n")
  17478. (point))))
  17479. (case processing-type
  17480. (mathjax
  17481. ;; Prepare for MathJax processing.
  17482. (if (eq (char-after beg) ?$)
  17483. (save-excursion
  17484. (delete-region beg end)
  17485. (insert "\\(" (substring value 1 -1) "\\)"))
  17486. (goto-char end)))
  17487. ((dvipng imagemagick)
  17488. ;; Process to an image.
  17489. (incf cnt)
  17490. (goto-char beg)
  17491. (let* ((face (face-at-point))
  17492. ;; Get the colors from the face at point.
  17493. (fg
  17494. (let ((color (plist-get org-format-latex-options
  17495. :foreground)))
  17496. (if (and forbuffer (eq color 'auto))
  17497. (face-attribute face :foreground nil 'default)
  17498. color)))
  17499. (bg
  17500. (let ((color (plist-get org-format-latex-options
  17501. :background)))
  17502. (if (and forbuffer (eq color 'auto))
  17503. (face-attribute face :background nil 'default)
  17504. color)))
  17505. (hash (sha1 (prin1-to-string
  17506. (list org-format-latex-header
  17507. org-latex-default-packages-alist
  17508. org-latex-packages-alist
  17509. org-format-latex-options
  17510. forbuffer value fg bg))))
  17511. (absprefix (expand-file-name prefix dir))
  17512. (linkfile (format "%s_%s.png" prefix hash))
  17513. (movefile (format "%s_%s.png" absprefix hash))
  17514. (sep (and block-type "\n\n"))
  17515. (link (concat sep "[[file:" linkfile "]]" sep))
  17516. (options
  17517. (org-combine-plists
  17518. org-format-latex-options
  17519. `(:foreground ,fg :background ,bg))))
  17520. (when msg (message msg cnt))
  17521. (unless checkdir-flag ; Ensure the directory exists.
  17522. (setq checkdir-flag t)
  17523. (let ((todir (file-name-directory absprefix)))
  17524. (unless (file-directory-p todir)
  17525. (make-directory todir t))))
  17526. (unless (file-exists-p movefile)
  17527. (org-create-formula-image
  17528. value movefile options forbuffer processing-type))
  17529. (if overlays
  17530. (progn
  17531. (dolist (o (overlays-in beg end))
  17532. (when (eq (overlay-get o 'org-overlay-type)
  17533. 'org-latex-overlay)
  17534. (delete-overlay o)))
  17535. (let ((ov (make-overlay beg end)))
  17536. (overlay-put ov
  17537. 'org-overlay-type
  17538. 'org-latex-overlay)
  17539. (overlay-put ov 'evaporate t)
  17540. (if (featurep 'xemacs)
  17541. (progn
  17542. (overlay-put ov 'invisible t)
  17543. (overlay-put
  17544. ov 'end-glyph
  17545. (make-glyph
  17546. (vector 'png :file movefile))))
  17547. (overlay-put
  17548. ov 'display
  17549. (list 'image
  17550. :type 'png
  17551. :file movefile
  17552. :ascent 'center)))
  17553. (push ov org-latex-fragment-image-overlays))
  17554. (goto-char end))
  17555. (delete-region beg end)
  17556. (insert
  17557. (org-add-props link
  17558. (list 'org-latex-src
  17559. (replace-regexp-in-string "\"" "" value)
  17560. 'org-latex-src-embed-type
  17561. (if block-type 'paragraph 'character)))))))
  17562. (mathml
  17563. ;; Process to MathML.
  17564. (unless (org-format-latex-mathml-available-p)
  17565. (user-error "LaTeX to MathML converter not configured"))
  17566. (incf cnt)
  17567. (when msg (message msg cnt))
  17568. (goto-char beg)
  17569. (delete-region beg end)
  17570. (insert (org-format-latex-as-mathml
  17571. value block-type prefix dir)))
  17572. (otherwise
  17573. (error "Unknown conversion type %s for LaTeX fragments"
  17574. processing-type)))))))))))
  17575. (defun org-create-math-formula (latex-frag &optional mathml-file)
  17576. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  17577. Use `org-latex-to-mathml-convert-command'. If the conversion is
  17578. sucessful, return the portion between \"<math...> </math>\"
  17579. elements otherwise return nil. When MATHML-FILE is specified,
  17580. write the results in to that file. When invoked as an
  17581. interactive command, prompt for LATEX-FRAG, with initial value
  17582. set to the current active region and echo the results for user
  17583. inspection."
  17584. (interactive (list (let ((frag (when (org-region-active-p)
  17585. (buffer-substring-no-properties
  17586. (region-beginning) (region-end)))))
  17587. (read-string "LaTeX Fragment: " frag nil frag))))
  17588. (unless latex-frag (user-error "Invalid LaTeX fragment"))
  17589. (let* ((tmp-in-file (file-relative-name
  17590. (make-temp-name (expand-file-name "ltxmathml-in"))))
  17591. (ignore (write-region latex-frag nil tmp-in-file))
  17592. (tmp-out-file (file-relative-name
  17593. (make-temp-name (expand-file-name "ltxmathml-out"))))
  17594. (cmd (format-spec
  17595. org-latex-to-mathml-convert-command
  17596. `((?j . ,(and org-latex-to-mathml-jar-file
  17597. (shell-quote-argument
  17598. (expand-file-name
  17599. org-latex-to-mathml-jar-file))))
  17600. (?I . ,(shell-quote-argument tmp-in-file))
  17601. (?i . ,latex-frag)
  17602. (?o . ,(shell-quote-argument tmp-out-file)))))
  17603. mathml shell-command-output)
  17604. (when (org-called-interactively-p 'any)
  17605. (unless (org-format-latex-mathml-available-p)
  17606. (user-error "LaTeX to MathML converter not configured")))
  17607. (message "Running %s" cmd)
  17608. (setq shell-command-output (shell-command-to-string cmd))
  17609. (setq mathml
  17610. (when (file-readable-p tmp-out-file)
  17611. (with-current-buffer (find-file-noselect tmp-out-file t)
  17612. (goto-char (point-min))
  17613. (when (re-search-forward
  17614. (concat
  17615. (regexp-quote
  17616. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\"")
  17617. "[^>]*?>"
  17618. "\\(.\\|\n\\)*"
  17619. "</math>")
  17620. nil t)
  17621. (prog1 (match-string 0) (kill-buffer))))))
  17622. (cond
  17623. (mathml
  17624. (setq mathml
  17625. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  17626. (when mathml-file
  17627. (write-region mathml nil mathml-file))
  17628. (when (org-called-interactively-p 'any)
  17629. (message mathml)))
  17630. ((message "LaTeX to MathML conversion failed")
  17631. (message shell-command-output)))
  17632. (delete-file tmp-in-file)
  17633. (when (file-exists-p tmp-out-file)
  17634. (delete-file tmp-out-file))
  17635. mathml))
  17636. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  17637. prefix &optional dir)
  17638. "Use `org-create-math-formula' but check local cache first."
  17639. (let* ((absprefix (expand-file-name prefix dir))
  17640. (print-length nil) (print-level nil)
  17641. (formula-id (concat
  17642. "formula-"
  17643. (sha1
  17644. (prin1-to-string
  17645. (list latex-frag
  17646. org-latex-to-mathml-convert-command)))))
  17647. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  17648. (formula-cache-dir (file-name-directory formula-cache)))
  17649. (unless (file-directory-p formula-cache-dir)
  17650. (make-directory formula-cache-dir t))
  17651. (unless (file-exists-p formula-cache)
  17652. (org-create-math-formula latex-frag formula-cache))
  17653. (if (file-exists-p formula-cache)
  17654. ;; Successful conversion. Return the link to MathML file.
  17655. (org-add-props
  17656. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  17657. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  17658. 'org-latex-src-embed-type (if latex-frag-type
  17659. 'paragraph 'character)))
  17660. ;; Failed conversion. Return the LaTeX fragment verbatim
  17661. latex-frag)))
  17662. (defun org-create-formula-image (string tofile options buffer &optional type)
  17663. "Create an image from LaTeX source using dvipng or convert.
  17664. This function calls either `org-create-formula-image-with-dvipng'
  17665. or `org-create-formula-image-with-imagemagick' depending on the
  17666. value of `org-latex-create-formula-image-program' or on the value
  17667. of the optional TYPE variable.
  17668. Note: ultimately these two function should be combined as they
  17669. share a good deal of logic."
  17670. (org-check-external-command
  17671. "latex" "needed to convert LaTeX fragments to images")
  17672. (funcall
  17673. (case (or type org-latex-create-formula-image-program)
  17674. (dvipng
  17675. (org-check-external-command
  17676. "dvipng" "needed to convert LaTeX fragments to images")
  17677. #'org-create-formula-image-with-dvipng)
  17678. (imagemagick
  17679. (org-check-external-command
  17680. "convert" "you need to install imagemagick")
  17681. #'org-create-formula-image-with-imagemagick)
  17682. (t (error
  17683. "Invalid value of `org-latex-create-formula-image-program'")))
  17684. string tofile options buffer))
  17685. (declare-function org-export-get-backend "ox" (name))
  17686. (declare-function org-export--get-global-options "ox" (&optional backend))
  17687. (declare-function org-export--get-inbuffer-options "ox" (&optional backend))
  17688. (declare-function org-latex-guess-inputenc "ox-latex" (header))
  17689. (declare-function org-latex-guess-babel-language "ox-latex" (header info))
  17690. (defun org-create-formula--latex-header ()
  17691. "Return LaTeX header appropriate for previewing a LaTeX snippet."
  17692. (let ((info (org-combine-plists (org-export--get-global-options
  17693. (org-export-get-backend 'latex))
  17694. (org-export--get-inbuffer-options
  17695. (org-export-get-backend 'latex)))))
  17696. (org-latex-guess-babel-language
  17697. (org-latex-guess-inputenc
  17698. (org-splice-latex-header
  17699. org-format-latex-header
  17700. org-latex-default-packages-alist
  17701. org-latex-packages-alist t
  17702. (plist-get info :latex-header)))
  17703. info)))
  17704. (defun org--get-display-dpi ()
  17705. "Get the DPI of the display.
  17706. Assumes that the display has the same pixel width in the
  17707. horizontal and vertical directions."
  17708. (if (display-graphic-p)
  17709. (round (/ (display-pixel-height)
  17710. (/ (display-mm-height) 25.4)))
  17711. (error "Attempt to calculate the dpi of a non-graphic display")))
  17712. ;; This function borrows from Ganesh Swami's latex2png.el
  17713. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  17714. "This calls dvipng."
  17715. (require 'ox-latex)
  17716. (let* ((tmpdir (if (featurep 'xemacs)
  17717. (temp-directory)
  17718. temporary-file-directory))
  17719. (texfilebase (make-temp-name
  17720. (expand-file-name "orgtex" tmpdir)))
  17721. (texfile (concat texfilebase ".tex"))
  17722. (dvifile (concat texfilebase ".dvi"))
  17723. (pngfile (concat texfilebase ".png"))
  17724. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17725. ;; This assumes that the display has the same pixel width in
  17726. ;; the horizontal and vertical directions
  17727. (dpi (number-to-string (* scale (if buffer (org--get-display-dpi) 120))))
  17728. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17729. "Black"))
  17730. (bg (or (plist-get options (if buffer :background :html-background))
  17731. "Transparent")))
  17732. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
  17733. (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
  17734. (if (eq bg 'default) (setq bg (org-dvipng-color :background))
  17735. (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
  17736. (let ((latex-header (org-create-formula--latex-header)))
  17737. (with-temp-file texfile
  17738. (insert latex-header)
  17739. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")))
  17740. (let ((dir default-directory))
  17741. (ignore-errors
  17742. (cd tmpdir)
  17743. (call-process "latex" nil nil nil texfile))
  17744. (cd dir))
  17745. (if (not (file-exists-p dvifile))
  17746. (progn (message "Failed to create dvi file from %s" texfile) nil)
  17747. (ignore-errors
  17748. (if (featurep 'xemacs)
  17749. (call-process "dvipng" nil nil nil
  17750. "-fg" fg "-bg" bg
  17751. "-T" "tight"
  17752. "-o" pngfile
  17753. dvifile)
  17754. (call-process "dvipng" nil nil nil
  17755. "-fg" fg "-bg" bg
  17756. "-D" dpi
  17757. ;;"-x" scale "-y" scale
  17758. "-T" "tight"
  17759. "-o" pngfile
  17760. dvifile)))
  17761. (if (not (file-exists-p pngfile))
  17762. (if org-format-latex-signal-error
  17763. (error "Failed to create png file from %s" texfile)
  17764. (message "Failed to create png file from %s" texfile)
  17765. nil)
  17766. ;; Use the requested file name and clean up
  17767. (copy-file pngfile tofile 'replace)
  17768. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  17769. (if (file-exists-p (concat texfilebase e))
  17770. (delete-file (concat texfilebase e))))
  17771. pngfile))))
  17772. (declare-function org-latex-compile "ox-latex" (texfile &optional snippet))
  17773. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  17774. "This calls convert, which is included into imagemagick."
  17775. (require 'ox-latex)
  17776. (let* ((tmpdir (if (featurep 'xemacs)
  17777. (temp-directory)
  17778. temporary-file-directory))
  17779. (texfilebase (make-temp-name
  17780. (expand-file-name "orgtex" tmpdir)))
  17781. (texfile (concat texfilebase ".tex"))
  17782. (pdffile (concat texfilebase ".pdf"))
  17783. (pngfile (concat texfilebase ".png"))
  17784. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17785. (dpi (number-to-string (* scale (if buffer (org--get-display-dpi) 120))))
  17786. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17787. "black"))
  17788. (bg (or (plist-get options (if buffer :background :html-background))
  17789. "white")))
  17790. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  17791. (setq fg (org-latex-color-format fg)))
  17792. (if (eq bg 'default) (setq bg (org-latex-color :background))
  17793. (setq bg (org-latex-color-format
  17794. (if (string= bg "Transparent") "white" bg))))
  17795. (let ((latex-header (org-create-formula--latex-header)))
  17796. (with-temp-file texfile
  17797. (insert latex-header)
  17798. (insert "\n\\begin{document}\n"
  17799. "\\definecolor{fg}{rgb}{" fg "}\n"
  17800. "\\definecolor{bg}{rgb}{" bg "}\n"
  17801. "\n\\pagecolor{bg}\n"
  17802. "\n{\\color{fg}\n"
  17803. string
  17804. "\n}\n"
  17805. "\n\\end{document}\n")))
  17806. (org-latex-compile texfile t)
  17807. (if (not (file-exists-p pdffile))
  17808. (progn (message "Failed to create pdf file from %s" texfile) nil)
  17809. (ignore-errors
  17810. (if (featurep 'xemacs)
  17811. (call-process "convert" nil nil nil
  17812. "-density" "96"
  17813. "-trim"
  17814. "-antialias"
  17815. pdffile
  17816. "-quality" "100"
  17817. ;; "-sharpen" "0x1.0"
  17818. pngfile)
  17819. (call-process "convert" nil nil nil
  17820. "-density" dpi
  17821. "-trim"
  17822. "-antialias"
  17823. pdffile
  17824. "-quality" "100"
  17825. ;; "-sharpen" "0x1.0"
  17826. pngfile)))
  17827. (if (not (file-exists-p pngfile))
  17828. (if org-format-latex-signal-error
  17829. (error "Failed to create png file from %s" texfile)
  17830. (message "Failed to create png file from %s" texfile)
  17831. nil)
  17832. ;; Use the requested file name and clean up
  17833. (copy-file pngfile tofile 'replace)
  17834. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  17835. (if (file-exists-p (concat texfilebase e))
  17836. (delete-file (concat texfilebase e))))
  17837. pngfile))))
  17838. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  17839. "Fill a LaTeX header template TPL.
  17840. In the template, the following place holders will be recognized:
  17841. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  17842. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  17843. [PACKAGES] \\usepackage statements for PKG
  17844. [NO-PACKAGES] do not include PKG
  17845. [EXTRA] the string EXTRA
  17846. [NO-EXTRA] do not include EXTRA
  17847. For backward compatibility, if both the positive and the negative place
  17848. holder is missing, the positive one (without the \"NO-\") will be
  17849. assumed to be present at the end of the template.
  17850. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  17851. EXTRA is a string.
  17852. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  17853. (let (rpl (end ""))
  17854. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  17855. (setq rpl (if (or (match-end 1) (not def-pkg))
  17856. "" (org-latex-packages-to-string def-pkg snippets-p t))
  17857. tpl (replace-match rpl t t tpl))
  17858. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  17859. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  17860. (setq rpl (if (or (match-end 1) (not pkg))
  17861. "" (org-latex-packages-to-string pkg snippets-p t))
  17862. tpl (replace-match rpl t t tpl))
  17863. (if pkg (setq end
  17864. (concat end "\n"
  17865. (org-latex-packages-to-string pkg snippets-p)))))
  17866. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  17867. (setq rpl (if (or (match-end 1) (not extra))
  17868. "" (concat extra "\n"))
  17869. tpl (replace-match rpl t t tpl))
  17870. (if (and extra (string-match "\\S-" extra))
  17871. (setq end (concat end "\n" extra))))
  17872. (if (string-match "\\S-" end)
  17873. (concat tpl "\n" end)
  17874. tpl)))
  17875. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  17876. "Turn an alist of packages into a string with the \\usepackage macros."
  17877. (setq pkg (mapconcat (lambda(p)
  17878. (cond
  17879. ((stringp p) p)
  17880. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  17881. (format "%% Package %s omitted" (cadr p)))
  17882. ((equal "" (car p))
  17883. (format "\\usepackage{%s}" (cadr p)))
  17884. (t
  17885. (format "\\usepackage[%s]{%s}"
  17886. (car p) (cadr p)))))
  17887. pkg
  17888. "\n"))
  17889. (if newline (concat pkg "\n") pkg))
  17890. (defun org-dvipng-color (attr)
  17891. "Return a RGB color specification for dvipng."
  17892. (apply 'format "rgb %s %s %s"
  17893. (mapcar 'org-normalize-color
  17894. (if (featurep 'xemacs)
  17895. (color-rgb-components
  17896. (face-property 'default
  17897. (cond ((eq attr :foreground) 'foreground)
  17898. ((eq attr :background) 'background))))
  17899. (color-values (face-attribute 'default attr nil))))))
  17900. (defun org-dvipng-color-format (color-name)
  17901. "Convert COLOR-NAME to a RGB color value for dvipng."
  17902. (apply 'format "rgb %s %s %s"
  17903. (mapcar 'org-normalize-color
  17904. (color-values color-name))))
  17905. (defun org-latex-color (attr)
  17906. "Return a RGB color for the LaTeX color package."
  17907. (apply 'format "%s,%s,%s"
  17908. (mapcar 'org-normalize-color
  17909. (if (featurep 'xemacs)
  17910. (color-rgb-components
  17911. (face-property 'default
  17912. (cond ((eq attr :foreground) 'foreground)
  17913. ((eq attr :background) 'background))))
  17914. (color-values (face-attribute 'default attr nil))))))
  17915. (defun org-latex-color-format (color-name)
  17916. "Convert COLOR-NAME to a RGB color value."
  17917. (apply 'format "%s,%s,%s"
  17918. (mapcar 'org-normalize-color
  17919. (color-values color-name))))
  17920. (defun org-normalize-color (value)
  17921. "Return string to be used as color value for an RGB component."
  17922. (format "%g" (/ value 65535.0)))
  17923. ;; Image display
  17924. (defvar org-inline-image-overlays nil)
  17925. (make-variable-buffer-local 'org-inline-image-overlays)
  17926. (defun org-toggle-inline-images (&optional include-linked)
  17927. "Toggle the display of inline images.
  17928. INCLUDE-LINKED is passed to `org-display-inline-images'."
  17929. (interactive "P")
  17930. (if org-inline-image-overlays
  17931. (progn
  17932. (org-remove-inline-images)
  17933. (when (org-called-interactively-p 'interactive)
  17934. (message "Inline image display turned off")))
  17935. (org-display-inline-images include-linked)
  17936. (when (org-called-interactively-p 'interactive)
  17937. (message (if org-inline-image-overlays
  17938. (format "%d images displayed inline"
  17939. (length org-inline-image-overlays))
  17940. "No images to display inline")))))
  17941. (defun org-redisplay-inline-images ()
  17942. "Refresh the display of inline images."
  17943. (interactive)
  17944. (if (not org-inline-image-overlays)
  17945. (org-toggle-inline-images)
  17946. (org-toggle-inline-images)
  17947. (org-toggle-inline-images)))
  17948. (defun org-display-inline-images (&optional include-linked refresh beg end)
  17949. "Display inline images.
  17950. An inline image is a link which follows either of these
  17951. conventions:
  17952. 1. Its path is a file with an extension matching return value
  17953. from `image-file-name-regexp' and it has no contents.
  17954. 2. Its description consists in a single link of the previous
  17955. type.
  17956. When optional argument INCLUDE-LINKED is non-nil, also links with
  17957. a text description part will be inlined. This can be nice for
  17958. a quick look at those images, but it does not reflect what
  17959. exported files will look like.
  17960. When optional argument REFRESH is non-nil, refresh existing
  17961. images between BEG and END. This will create new image displays
  17962. only if necessary. BEG and END default to the buffer
  17963. boundaries."
  17964. (interactive "P")
  17965. (when (display-graphic-p)
  17966. (unless refresh
  17967. (org-remove-inline-images)
  17968. (when (fboundp 'clear-image-cache) (clear-image-cache)))
  17969. (org-with-wide-buffer
  17970. (goto-char (or beg (point-min)))
  17971. (let ((case-fold-search t)
  17972. (file-extension-re (org-image-file-name-regexp)))
  17973. (while (re-search-forward "[][]\\[\\(?:file\\|[./~]\\)" end t)
  17974. (let ((link (save-match-data (org-element-context))))
  17975. ;; Check if we're at an inline image.
  17976. (when (and (equal (org-element-property :type link) "file")
  17977. (or include-linked
  17978. (not (org-element-property :contents-begin link)))
  17979. (let ((parent (org-element-property :parent link)))
  17980. (or (not (eq (org-element-type parent) 'link))
  17981. (not (cdr (org-element-contents parent)))))
  17982. (org-string-match-p file-extension-re
  17983. (org-element-property :path link)))
  17984. (let ((file (expand-file-name
  17985. (org-link-unescape
  17986. (org-element-property :path link)))))
  17987. (when (file-exists-p file)
  17988. (let ((width
  17989. ;; Apply `org-image-actual-width' specifications.
  17990. (cond
  17991. ((not (image-type-available-p 'imagemagick)) nil)
  17992. ((eq org-image-actual-width t) nil)
  17993. ((listp org-image-actual-width)
  17994. (or
  17995. ;; First try to find a width among
  17996. ;; attributes associated to the paragraph
  17997. ;; containing link.
  17998. (let ((paragraph
  17999. (let ((e link))
  18000. (while (and (setq e (org-element-property
  18001. :parent e))
  18002. (not (eq (org-element-type e)
  18003. 'paragraph))))
  18004. e)))
  18005. (when paragraph
  18006. (save-excursion
  18007. (goto-char (org-element-property :begin paragraph))
  18008. (when
  18009. (re-search-forward
  18010. "^[ \t]*#\\+attr_.*?: +.*?:width +\\(\\S-+\\)"
  18011. (org-element-property
  18012. :post-affiliated paragraph)
  18013. t)
  18014. (string-to-number (match-string 1))))))
  18015. ;; Otherwise, fall-back to provided number.
  18016. (car org-image-actual-width)))
  18017. ((numberp org-image-actual-width)
  18018. org-image-actual-width)))
  18019. (old (get-char-property-and-overlay
  18020. (org-element-property :begin link)
  18021. 'org-image-overlay)))
  18022. (if (and (car-safe old) refresh)
  18023. (image-refresh (overlay-get (cdr old) 'display))
  18024. (let ((image (create-image file
  18025. (and width 'imagemagick)
  18026. nil
  18027. :width width)))
  18028. (when image
  18029. (let* ((link
  18030. ;; If inline image is the description
  18031. ;; of another link, be sure to
  18032. ;; consider the latter as the one to
  18033. ;; apply the overlay on.
  18034. (let ((parent
  18035. (org-element-property :parent link)))
  18036. (if (eq (org-element-type parent) 'link)
  18037. parent
  18038. link)))
  18039. (ov (make-overlay
  18040. (org-element-property :begin link)
  18041. (progn
  18042. (goto-char
  18043. (org-element-property :end link))
  18044. (skip-chars-backward " \t")
  18045. (point)))))
  18046. (overlay-put ov 'display image)
  18047. (overlay-put ov 'face 'default)
  18048. (overlay-put ov 'org-image-overlay t)
  18049. (overlay-put
  18050. ov 'modification-hooks
  18051. (list 'org-display-inline-remove-overlay))
  18052. (push ov org-inline-image-overlays)))))))))))))))
  18053. (define-obsolete-function-alias
  18054. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  18055. (defun org-display-inline-remove-overlay (ov after beg end &optional len)
  18056. "Remove inline-display overlay if a corresponding region is modified."
  18057. (let ((inhibit-modification-hooks t))
  18058. (when (and ov after)
  18059. (delete ov org-inline-image-overlays)
  18060. (delete-overlay ov))))
  18061. (defun org-remove-inline-images ()
  18062. "Remove inline display of images."
  18063. (interactive)
  18064. (mapc 'delete-overlay org-inline-image-overlays)
  18065. (setq org-inline-image-overlays nil))
  18066. ;;;; Key bindings
  18067. ;; Outline functions from `outline-mode-prefix-map'
  18068. ;; that can be remapped in Org:
  18069. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  18070. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  18071. (define-key org-mode-map [remap outline-forward-same-level]
  18072. 'org-forward-heading-same-level)
  18073. (define-key org-mode-map [remap outline-backward-same-level]
  18074. 'org-backward-heading-same-level)
  18075. (define-key org-mode-map [remap show-branches]
  18076. 'org-kill-note-or-show-branches)
  18077. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  18078. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  18079. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  18080. (define-key org-mode-map [remap outline-next-visible-heading]
  18081. 'org-next-visible-heading)
  18082. (define-key org-mode-map [remap outline-previous-visible-heading]
  18083. 'org-previous-visible-heading)
  18084. (define-key org-mode-map [remap show-children] 'org-show-children)
  18085. ;; Outline functions from `outline-mode-prefix-map' that can not
  18086. ;; be remapped in Org:
  18087. ;; - the column "key binding" shows whether the Outline function is still
  18088. ;; available in Org mode on the same key that it has been bound to in
  18089. ;; Outline mode:
  18090. ;; - "overridden": key used for a different functionality in Org mode
  18091. ;; - else: key still bound to the same Outline function in Org mode
  18092. ;; | Outline function | key binding | Org replacement |
  18093. ;; |------------------------------------+-------------+--------------------------|
  18094. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  18095. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  18096. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  18097. ;; | `show-entry' | overridden | no replacement |
  18098. ;; | `show-branches' | `C-c C-k' | still same function |
  18099. ;; | `show-subtree' | overridden | visibility cycling |
  18100. ;; | `show-all' | overridden | no replacement |
  18101. ;; | `hide-subtree' | overridden | visibility cycling |
  18102. ;; | `hide-body' | overridden | no replacement |
  18103. ;; | `hide-entry' | overridden | visibility cycling |
  18104. ;; | `hide-leaves' | overridden | no replacement |
  18105. ;; | `hide-sublevels' | overridden | no replacement |
  18106. ;; | `hide-other' | overridden | no replacement |
  18107. ;; Make `C-c C-x' a prefix key
  18108. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  18109. ;; TAB key with modifiers
  18110. (org-defkey org-mode-map "\C-i" 'org-cycle)
  18111. (org-defkey org-mode-map [(tab)] 'org-cycle)
  18112. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  18113. (org-defkey org-mode-map "\M-\t" #'pcomplete)
  18114. ;; The following line is necessary under Suse GNU/Linux
  18115. (unless (featurep 'xemacs)
  18116. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  18117. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  18118. (define-key org-mode-map [backtab] 'org-shifttab)
  18119. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  18120. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  18121. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  18122. ;; Cursor keys with modifiers
  18123. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  18124. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  18125. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  18126. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  18127. (org-defkey org-mode-map [(control meta shift right)] 'org-increase-number-at-point)
  18128. (org-defkey org-mode-map [(control meta shift left)] 'org-decrease-number-at-point)
  18129. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  18130. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  18131. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  18132. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  18133. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  18134. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  18135. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  18136. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  18137. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  18138. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  18139. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  18140. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  18141. ;; Babel keys
  18142. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  18143. (mapc (lambda (pair)
  18144. (define-key org-babel-map (car pair) (cdr pair)))
  18145. org-babel-key-bindings)
  18146. ;;; Extra keys for tty access.
  18147. ;; We only set them when really needed because otherwise the
  18148. ;; menus don't show the simple keys
  18149. (when (or org-use-extra-keys
  18150. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  18151. (not window-system))
  18152. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  18153. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  18154. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  18155. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  18156. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  18157. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  18158. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  18159. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  18160. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  18161. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  18162. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  18163. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  18164. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  18165. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  18166. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  18167. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  18168. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  18169. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  18170. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  18171. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  18172. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  18173. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  18174. (org-defkey org-mode-map [?\e (tab)] #'pcomplete)
  18175. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  18176. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  18177. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  18178. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  18179. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  18180. ;; All the other keys
  18181. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  18182. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  18183. (if (boundp 'narrow-map)
  18184. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  18185. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  18186. (if (boundp 'narrow-map)
  18187. (org-defkey narrow-map "b" 'org-narrow-to-block)
  18188. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  18189. (if (boundp 'narrow-map)
  18190. (org-defkey narrow-map "e" 'org-narrow-to-element)
  18191. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  18192. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  18193. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  18194. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  18195. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  18196. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  18197. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  18198. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  18199. (org-defkey org-mode-map "\C-c\M-f" 'org-next-block)
  18200. (org-defkey org-mode-map "\C-c\M-b" 'org-previous-block)
  18201. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  18202. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-archive-subtree)
  18203. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  18204. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  18205. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  18206. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  18207. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  18208. (org-defkey org-mode-map "\C-c\C-xq" 'org-toggle-tags-groups)
  18209. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  18210. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  18211. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  18212. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  18213. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  18214. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  18215. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  18216. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  18217. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  18218. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  18219. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  18220. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  18221. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  18222. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  18223. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  18224. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  18225. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  18226. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  18227. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  18228. (org-defkey org-mode-map "\C-c\M-l" 'org-insert-last-stored-link)
  18229. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  18230. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  18231. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  18232. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  18233. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  18234. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  18235. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  18236. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  18237. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  18238. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  18239. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  18240. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  18241. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  18242. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  18243. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  18244. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  18245. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  18246. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  18247. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  18248. (org-defkey org-mode-map "\C-c^" 'org-sort)
  18249. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  18250. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  18251. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  18252. (org-defkey org-mode-map [remap open-line] 'org-open-line)
  18253. (org-defkey org-mode-map [remap comment-dwim] 'org-comment-dwim)
  18254. (org-defkey org-mode-map [remap forward-paragraph] 'org-forward-paragraph)
  18255. (org-defkey org-mode-map [remap backward-paragraph] 'org-backward-paragraph)
  18256. (org-defkey org-mode-map "\M-^" 'org-delete-indentation)
  18257. (org-defkey org-mode-map "\C-m" 'org-return)
  18258. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  18259. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  18260. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  18261. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  18262. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  18263. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  18264. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  18265. (org-defkey org-mode-map "\C-c\"a" 'orgtbl-ascii-plot)
  18266. (org-defkey org-mode-map "\C-c\"g" 'org-plot/gnuplot)
  18267. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  18268. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  18269. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  18270. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  18271. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  18272. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  18273. (org-defkey org-mode-map "\C-c\C-e" 'org-export-dispatch)
  18274. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width)
  18275. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  18276. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  18277. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  18278. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  18279. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  18280. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  18281. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  18282. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  18283. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  18284. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  18285. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  18286. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  18287. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  18288. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  18289. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  18290. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  18291. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  18292. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  18293. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  18294. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  18295. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  18296. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-toggle-latex-fragment)
  18297. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  18298. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  18299. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  18300. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  18301. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  18302. (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
  18303. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  18304. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  18305. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  18306. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  18307. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  18308. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  18309. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  18310. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  18311. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  18312. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  18313. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  18314. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  18315. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  18316. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  18317. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  18318. (when (featurep 'xemacs)
  18319. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  18320. (defconst org-speed-commands-default
  18321. '(
  18322. ("Outline Navigation")
  18323. ("n" . (org-speed-move-safe 'org-next-visible-heading))
  18324. ("p" . (org-speed-move-safe 'org-previous-visible-heading))
  18325. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  18326. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  18327. ("F" . org-next-block)
  18328. ("B" . org-previous-block)
  18329. ("u" . (org-speed-move-safe 'outline-up-heading))
  18330. ("j" . org-goto)
  18331. ("g" . (org-refile t))
  18332. ("Outline Visibility")
  18333. ("c" . org-cycle)
  18334. ("C" . org-shifttab)
  18335. (" " . org-display-outline-path)
  18336. ("s" . org-narrow-to-subtree)
  18337. ("=" . org-columns)
  18338. ("Outline Structure Editing")
  18339. ("U" . org-metaup)
  18340. ("D" . org-metadown)
  18341. ("r" . org-metaright)
  18342. ("l" . org-metaleft)
  18343. ("R" . org-shiftmetaright)
  18344. ("L" . org-shiftmetaleft)
  18345. ("i" . (progn (forward-char 1) (call-interactively
  18346. 'org-insert-heading-respect-content)))
  18347. ("^" . org-sort)
  18348. ("w" . org-refile)
  18349. ("a" . org-archive-subtree-default-with-confirmation)
  18350. ("@" . org-mark-subtree)
  18351. ("#" . org-toggle-comment)
  18352. ("Clock Commands")
  18353. ("I" . org-clock-in)
  18354. ("O" . org-clock-out)
  18355. ("Meta Data Editing")
  18356. ("t" . org-todo)
  18357. ("," . (org-priority))
  18358. ("0" . (org-priority ?\ ))
  18359. ("1" . (org-priority ?A))
  18360. ("2" . (org-priority ?B))
  18361. ("3" . (org-priority ?C))
  18362. (":" . org-set-tags-command)
  18363. ("e" . org-set-effort)
  18364. ("E" . org-inc-effort)
  18365. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  18366. (org-entry-put (point) "APPT_WARNTIME" m)))
  18367. ("Agenda Views etc")
  18368. ("v" . org-agenda)
  18369. ("/" . org-sparse-tree)
  18370. ("Misc")
  18371. ("o" . org-open-at-point)
  18372. ("?" . org-speed-command-help)
  18373. ("<" . (org-agenda-set-restriction-lock 'subtree))
  18374. (">" . (org-agenda-remove-restriction-lock))
  18375. )
  18376. "The default speed commands.")
  18377. (defun org-print-speed-command (e)
  18378. (if (> (length (car e)) 1)
  18379. (progn
  18380. (princ "\n")
  18381. (princ (car e))
  18382. (princ "\n")
  18383. (princ (make-string (length (car e)) ?-))
  18384. (princ "\n"))
  18385. (princ (car e))
  18386. (princ " ")
  18387. (if (symbolp (cdr e))
  18388. (princ (symbol-name (cdr e)))
  18389. (prin1 (cdr e)))
  18390. (princ "\n")))
  18391. (defun org-speed-command-help ()
  18392. "Show the available speed commands."
  18393. (interactive)
  18394. (if (not org-use-speed-commands)
  18395. (user-error "Speed commands are not activated, customize `org-use-speed-commands'")
  18396. (with-output-to-temp-buffer "*Help*"
  18397. (princ "User-defined Speed commands\n===========================\n")
  18398. (mapc 'org-print-speed-command org-speed-commands-user)
  18399. (princ "\n")
  18400. (princ "Built-in Speed commands\n=======================\n")
  18401. (mapc 'org-print-speed-command org-speed-commands-default))
  18402. (with-current-buffer "*Help*"
  18403. (setq truncate-lines t))))
  18404. (defun org-speed-move-safe (cmd)
  18405. "Execute CMD, but make sure that the cursor always ends up in a headline.
  18406. If not, return to the original position and throw an error."
  18407. (interactive)
  18408. (let ((pos (point)))
  18409. (call-interactively cmd)
  18410. (unless (and (bolp) (org-at-heading-p))
  18411. (goto-char pos)
  18412. (error "Boundary reached while executing %s" cmd))))
  18413. (defvar org-self-insert-command-undo-counter 0)
  18414. (defvar org-table-auto-blank-field) ; defined in org-table.el
  18415. (defvar org-speed-command nil)
  18416. (define-obsolete-function-alias
  18417. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  18418. (defun org-speed-command-activate (keys)
  18419. "Hook for activating single-letter speed commands.
  18420. `org-speed-commands-default' specifies a minimal command set.
  18421. Use `org-speed-commands-user' for further customization."
  18422. (when (or (and (bolp) (looking-at org-outline-regexp))
  18423. (and (functionp org-use-speed-commands)
  18424. (funcall org-use-speed-commands)))
  18425. (cdr (assoc keys (append org-speed-commands-user
  18426. org-speed-commands-default)))))
  18427. (define-obsolete-function-alias
  18428. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  18429. (defun org-babel-speed-command-activate (keys)
  18430. "Hook for activating single-letter code block commands."
  18431. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  18432. (cdr (assoc keys org-babel-key-bindings))))
  18433. (defcustom org-speed-command-hook
  18434. '(org-speed-command-default-hook org-babel-speed-command-hook)
  18435. "Hook for activating speed commands at strategic locations.
  18436. Hook functions are called in sequence until a valid handler is
  18437. found.
  18438. Each hook takes a single argument, a user-pressed command key
  18439. which is also a `self-insert-command' from the global map.
  18440. Within the hook, examine the cursor position and the command key
  18441. and return nil or a valid handler as appropriate. Handler could
  18442. be one of an interactive command, a function, or a form.
  18443. Set `org-use-speed-commands' to non-nil value to enable this
  18444. hook. The default setting is `org-speed-command-activate'."
  18445. :group 'org-structure
  18446. :version "24.1"
  18447. :type 'hook)
  18448. (defun org-self-insert-command (N)
  18449. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  18450. If the cursor is in a table looking at whitespace, the whitespace is
  18451. overwritten, and the table is not marked as requiring realignment."
  18452. (interactive "p")
  18453. (org-check-before-invisible-edit 'insert)
  18454. (cond
  18455. ((and org-use-speed-commands
  18456. (let ((kv (this-command-keys-vector)))
  18457. (setq org-speed-command
  18458. (run-hook-with-args-until-success
  18459. 'org-speed-command-hook
  18460. (make-string 1 (aref kv (1- (length kv))))))))
  18461. (cond
  18462. ((commandp org-speed-command)
  18463. (setq this-command org-speed-command)
  18464. (call-interactively org-speed-command))
  18465. ((functionp org-speed-command)
  18466. (funcall org-speed-command))
  18467. ((and org-speed-command (listp org-speed-command))
  18468. (eval org-speed-command))
  18469. (t (let (org-use-speed-commands)
  18470. (call-interactively 'org-self-insert-command)))))
  18471. ((and
  18472. (org-table-p)
  18473. (progn
  18474. ;; check if we blank the field, and if that triggers align
  18475. (and (featurep 'org-table) org-table-auto-blank-field
  18476. (memq last-command
  18477. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  18478. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  18479. ;; got extra space, this field does not determine column width
  18480. (let (org-table-may-need-update) (org-table-blank-field))
  18481. ;; no extra space, this field may determine column width
  18482. (org-table-blank-field)))
  18483. t)
  18484. (eq N 1)
  18485. (looking-at "[^|\n]* |"))
  18486. (let (org-table-may-need-update)
  18487. (goto-char (1- (match-end 0)))
  18488. (backward-delete-char 1)
  18489. (goto-char (match-beginning 0))
  18490. (self-insert-command N)))
  18491. (t
  18492. (setq org-table-may-need-update t)
  18493. (self-insert-command N)
  18494. (org-fix-tags-on-the-fly)
  18495. (if org-self-insert-cluster-for-undo
  18496. (if (not (eq last-command 'org-self-insert-command))
  18497. (setq org-self-insert-command-undo-counter 1)
  18498. (if (>= org-self-insert-command-undo-counter 20)
  18499. (setq org-self-insert-command-undo-counter 1)
  18500. (and (> org-self-insert-command-undo-counter 0)
  18501. buffer-undo-list (listp buffer-undo-list)
  18502. (not (cadr buffer-undo-list)) ; remove nil entry
  18503. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  18504. (setq org-self-insert-command-undo-counter
  18505. (1+ org-self-insert-command-undo-counter))))))))
  18506. (defun org-check-before-invisible-edit (kind)
  18507. "Check is editing if kind KIND would be dangerous with invisible text around.
  18508. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  18509. ;; First, try to get out of here as quickly as possible, to reduce overhead
  18510. (if (and org-catch-invisible-edits
  18511. (or (not (boundp 'visible-mode)) (not visible-mode))
  18512. (or (get-char-property (point) 'invisible)
  18513. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  18514. ;; OK, we need to take a closer look
  18515. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  18516. (invisible-before-point (if (bobp) nil (get-char-property
  18517. (1- (point)) 'invisible)))
  18518. (border-and-ok-direction
  18519. (or
  18520. ;; Check if we are acting predictably before invisible text
  18521. (and invisible-at-point (not invisible-before-point)
  18522. (memq kind '(insert delete-backward)))
  18523. ;; Check if we are acting predictably after invisible text
  18524. ;; This works not well, and I have turned it off. It seems
  18525. ;; better to always show and stop after invisible text.
  18526. ;; (and (not invisible-at-point) invisible-before-point
  18527. ;; (memq kind '(insert delete)))
  18528. )))
  18529. (when (or (memq invisible-at-point '(outline org-hide-block t))
  18530. (memq invisible-before-point '(outline org-hide-block t)))
  18531. (if (eq org-catch-invisible-edits 'error)
  18532. (user-error "Editing in invisible areas is prohibited, make them visible first"))
  18533. (if (and org-custom-properties-overlays
  18534. (y-or-n-p "Display invisible properties in this buffer? "))
  18535. (org-toggle-custom-properties-visibility)
  18536. ;; Make the area visible
  18537. (save-excursion
  18538. (if invisible-before-point
  18539. (goto-char (previous-single-char-property-change
  18540. (point) 'invisible)))
  18541. (show-subtree))
  18542. (cond
  18543. ((eq org-catch-invisible-edits 'show)
  18544. ;; That's it, we do the edit after showing
  18545. (message
  18546. "Unfolding invisible region around point before editing")
  18547. (sit-for 1))
  18548. ((and (eq org-catch-invisible-edits 'smart)
  18549. border-and-ok-direction)
  18550. (message "Unfolding invisible region around point before editing"))
  18551. (t
  18552. ;; Don't do the edit, make the user repeat it in full visibility
  18553. (user-error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  18554. (defun org-fix-tags-on-the-fly ()
  18555. (when (and (equal (char-after (point-at-bol)) ?*)
  18556. (org-at-heading-p))
  18557. (org-align-tags-here org-tags-column)))
  18558. (defun org-delete-backward-char (N)
  18559. "Like `delete-backward-char', insert whitespace at field end in tables.
  18560. When deleting backwards, in tables this function will insert whitespace in
  18561. front of the next \"|\" separator, to keep the table aligned. The table will
  18562. still be marked for re-alignment if the field did fill the entire column,
  18563. because, in this case the deletion might narrow the column."
  18564. (interactive "p")
  18565. (save-match-data
  18566. (org-check-before-invisible-edit 'delete-backward)
  18567. (if (and (org-table-p)
  18568. (eq N 1)
  18569. (string-match "|" (buffer-substring (point-at-bol) (point)))
  18570. (looking-at ".*?|"))
  18571. (let ((pos (point))
  18572. (noalign (looking-at "[^|\n\r]* |"))
  18573. (c org-table-may-need-update))
  18574. (backward-delete-char N)
  18575. (if (not overwrite-mode)
  18576. (progn
  18577. (skip-chars-forward "^|")
  18578. (insert " ")
  18579. (goto-char (1- pos))))
  18580. ;; noalign: if there were two spaces at the end, this field
  18581. ;; does not determine the width of the column.
  18582. (if noalign (setq org-table-may-need-update c)))
  18583. (backward-delete-char N)
  18584. (org-fix-tags-on-the-fly))))
  18585. (defun org-delete-char (N)
  18586. "Like `delete-char', but insert whitespace at field end in tables.
  18587. When deleting characters, in tables this function will insert whitespace in
  18588. front of the next \"|\" separator, to keep the table aligned. The table will
  18589. still be marked for re-alignment if the field did fill the entire column,
  18590. because, in this case the deletion might narrow the column."
  18591. (interactive "p")
  18592. (save-match-data
  18593. (org-check-before-invisible-edit 'delete)
  18594. (if (and (org-table-p)
  18595. (not (bolp))
  18596. (not (= (char-after) ?|))
  18597. (eq N 1))
  18598. (if (looking-at ".*?|")
  18599. (let ((pos (point))
  18600. (noalign (looking-at "[^|\n\r]* |"))
  18601. (c org-table-may-need-update))
  18602. (replace-match
  18603. (concat (substring (match-string 0) 1 -1) " |") nil t)
  18604. (goto-char pos)
  18605. ;; noalign: if there were two spaces at the end, this field
  18606. ;; does not determine the width of the column.
  18607. (if noalign (setq org-table-may-need-update c)))
  18608. (delete-char N))
  18609. (delete-char N)
  18610. (org-fix-tags-on-the-fly))))
  18611. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  18612. (put 'org-self-insert-command 'delete-selection
  18613. (lambda ()
  18614. (not (run-hook-with-args-until-success
  18615. 'self-insert-uses-region-functions))))
  18616. (put 'orgtbl-self-insert-command 'delete-selection
  18617. (lambda ()
  18618. (not (run-hook-with-args-until-success
  18619. 'self-insert-uses-region-functions))))
  18620. (put 'org-delete-char 'delete-selection 'supersede)
  18621. (put 'org-delete-backward-char 'delete-selection 'supersede)
  18622. (put 'org-yank 'delete-selection 'yank)
  18623. ;; Make `flyspell-mode' delay after some commands
  18624. (put 'org-self-insert-command 'flyspell-delayed t)
  18625. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  18626. (put 'org-delete-char 'flyspell-delayed t)
  18627. (put 'org-delete-backward-char 'flyspell-delayed t)
  18628. ;; Make pabbrev-mode expand after org-mode commands
  18629. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  18630. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  18631. (defun org-remap (map &rest commands)
  18632. "In MAP, remap the functions given in COMMANDS.
  18633. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  18634. (let (new old)
  18635. (while commands
  18636. (setq old (pop commands) new (pop commands))
  18637. (if (fboundp 'command-remapping)
  18638. (org-defkey map (vector 'remap old) new)
  18639. (substitute-key-definition old new map global-map)))))
  18640. (defun org-transpose-words ()
  18641. "Transpose words for Org.
  18642. This uses the `org-mode-transpose-word-syntax-table' syntax
  18643. table, which interprets characters in `org-emphasis-alist' as
  18644. word constituents."
  18645. (interactive)
  18646. (with-syntax-table org-mode-transpose-word-syntax-table
  18647. (call-interactively 'transpose-words)))
  18648. (org-remap org-mode-map 'transpose-words 'org-transpose-words)
  18649. (when (eq org-enable-table-editor 'optimized)
  18650. ;; If the user wants maximum table support, we need to hijack
  18651. ;; some standard editing functions
  18652. (org-remap org-mode-map
  18653. 'self-insert-command 'org-self-insert-command
  18654. 'delete-char 'org-delete-char
  18655. 'delete-backward-char 'org-delete-backward-char)
  18656. (org-defkey org-mode-map "|" 'org-force-self-insert))
  18657. (defvar org-ctrl-c-ctrl-c-hook nil
  18658. "Hook for functions attaching themselves to `C-c C-c'.
  18659. This can be used to add additional functionality to the C-c C-c
  18660. key which executes context-dependent commands. This hook is run
  18661. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  18662. run after the last test.
  18663. Each function will be called with no arguments. The function
  18664. must check if the context is appropriate for it to act. If yes,
  18665. it should do its thing and then return a non-nil value. If the
  18666. context is wrong, just do nothing and return nil.")
  18667. (defvar org-ctrl-c-ctrl-c-final-hook nil
  18668. "Hook for functions attaching themselves to `C-c C-c'.
  18669. This can be used to add additional functionality to the C-c C-c
  18670. key which executes context-dependent commands. This hook is run
  18671. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  18672. before the first test.
  18673. Each function will be called with no arguments. The function
  18674. must check if the context is appropriate for it to act. If yes,
  18675. it should do its thing and then return a non-nil value. If the
  18676. context is wrong, just do nothing and return nil.")
  18677. (defvar org-tab-first-hook nil
  18678. "Hook for functions to attach themselves to TAB.
  18679. See `org-ctrl-c-ctrl-c-hook' for more information.
  18680. This hook runs as the first action when TAB is pressed, even before
  18681. `org-cycle' messes around with the `outline-regexp' to cater for
  18682. inline tasks and plain list item folding.
  18683. If any function in this hook returns t, any other actions that
  18684. would have been caused by TAB (such as table field motion or visibility
  18685. cycling) will not occur.")
  18686. (defvar org-tab-after-check-for-table-hook nil
  18687. "Hook for functions to attach themselves to TAB.
  18688. See `org-ctrl-c-ctrl-c-hook' for more information.
  18689. This hook runs after it has been established that the cursor is not in a
  18690. table, but before checking if the cursor is in a headline or if global cycling
  18691. should be done.
  18692. If any function in this hook returns t, not other actions like visibility
  18693. cycling will be done.")
  18694. (defvar org-tab-after-check-for-cycling-hook nil
  18695. "Hook for functions to attach themselves to TAB.
  18696. See `org-ctrl-c-ctrl-c-hook' for more information.
  18697. This hook runs after it has been established that not table field motion and
  18698. not visibility should be done because of current context. This is probably
  18699. the place where a package like yasnippets can hook in.")
  18700. (defvar org-tab-before-tab-emulation-hook nil
  18701. "Hook for functions to attach themselves to TAB.
  18702. See `org-ctrl-c-ctrl-c-hook' for more information.
  18703. This hook runs after every other options for TAB have been exhausted, but
  18704. before indentation and \t insertion takes place.")
  18705. (defvar org-metaleft-hook nil
  18706. "Hook for functions attaching themselves to `M-left'.
  18707. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18708. (defvar org-metaright-hook nil
  18709. "Hook for functions attaching themselves to `M-right'.
  18710. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18711. (defvar org-metaup-hook nil
  18712. "Hook for functions attaching themselves to `M-up'.
  18713. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18714. (defvar org-metadown-hook nil
  18715. "Hook for functions attaching themselves to `M-down'.
  18716. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18717. (defvar org-shiftmetaleft-hook nil
  18718. "Hook for functions attaching themselves to `M-S-left'.
  18719. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18720. (defvar org-shiftmetaright-hook nil
  18721. "Hook for functions attaching themselves to `M-S-right'.
  18722. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18723. (defvar org-shiftmetaup-hook nil
  18724. "Hook for functions attaching themselves to `M-S-up'.
  18725. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18726. (defvar org-shiftmetadown-hook nil
  18727. "Hook for functions attaching themselves to `M-S-down'.
  18728. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18729. (defvar org-metareturn-hook nil
  18730. "Hook for functions attaching themselves to `M-RET'.
  18731. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18732. (defvar org-shiftup-hook nil
  18733. "Hook for functions attaching themselves to `S-up'.
  18734. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18735. (defvar org-shiftup-final-hook nil
  18736. "Hook for functions attaching themselves to `S-up'.
  18737. This one runs after all other options except shift-select have been excluded.
  18738. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18739. (defvar org-shiftdown-hook nil
  18740. "Hook for functions attaching themselves to `S-down'.
  18741. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18742. (defvar org-shiftdown-final-hook nil
  18743. "Hook for functions attaching themselves to `S-down'.
  18744. This one runs after all other options except shift-select have been excluded.
  18745. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18746. (defvar org-shiftleft-hook nil
  18747. "Hook for functions attaching themselves to `S-left'.
  18748. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18749. (defvar org-shiftleft-final-hook nil
  18750. "Hook for functions attaching themselves to `S-left'.
  18751. This one runs after all other options except shift-select have been excluded.
  18752. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18753. (defvar org-shiftright-hook nil
  18754. "Hook for functions attaching themselves to `S-right'.
  18755. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18756. (defvar org-shiftright-final-hook nil
  18757. "Hook for functions attaching themselves to `S-right'.
  18758. This one runs after all other options except shift-select have been excluded.
  18759. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18760. (defun org-modifier-cursor-error ()
  18761. "Throw an error, a modified cursor command was applied in wrong context."
  18762. (user-error "This command is active in special context like tables, headlines or items"))
  18763. (defun org-shiftselect-error ()
  18764. "Throw an error because Shift-Cursor command was applied in wrong context."
  18765. (if (and (boundp 'shift-select-mode) shift-select-mode)
  18766. (user-error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  18767. (user-error "This command works only in special context like headlines or timestamps")))
  18768. (defun org-call-for-shift-select (cmd)
  18769. (let ((this-command-keys-shift-translated t))
  18770. (call-interactively cmd)))
  18771. (defun org-shifttab (&optional arg)
  18772. "Global visibility cycling or move to previous table field.
  18773. Call `org-table-previous-field' within a table.
  18774. When ARG is nil, cycle globally through visibility states.
  18775. When ARG is a numeric prefix, show contents of this level."
  18776. (interactive "P")
  18777. (cond
  18778. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  18779. ((integerp arg)
  18780. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  18781. (message "Content view to level: %d" arg)
  18782. (org-content (prefix-numeric-value arg2))
  18783. (org-cycle-show-empty-lines t)
  18784. (setq org-cycle-global-status 'overview)))
  18785. (t (call-interactively 'org-global-cycle))))
  18786. (defun org-shiftmetaleft ()
  18787. "Promote subtree or delete table column.
  18788. Calls `org-promote-subtree', `org-outdent-item-tree', or
  18789. `org-table-delete-column', depending on context. See the
  18790. individual commands for more information."
  18791. (interactive)
  18792. (cond
  18793. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  18794. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  18795. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  18796. ((if (not (org-region-active-p)) (org-at-item-p)
  18797. (save-excursion (goto-char (region-beginning))
  18798. (org-at-item-p)))
  18799. (call-interactively 'org-outdent-item-tree))
  18800. (t (org-modifier-cursor-error))))
  18801. (defun org-shiftmetaright ()
  18802. "Demote subtree or insert table column.
  18803. Calls `org-demote-subtree', `org-indent-item-tree', or
  18804. `org-table-insert-column', depending on context. See the
  18805. individual commands for more information."
  18806. (interactive)
  18807. (cond
  18808. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  18809. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  18810. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  18811. ((if (not (org-region-active-p)) (org-at-item-p)
  18812. (save-excursion (goto-char (region-beginning))
  18813. (org-at-item-p)))
  18814. (call-interactively 'org-indent-item-tree))
  18815. (t (org-modifier-cursor-error))))
  18816. (defun org-shiftmetaup (&optional arg)
  18817. "Drag the line at point up.
  18818. In a table, kill the current row.
  18819. On a clock timestamp, update the value of the timestamp like `S-<up>'
  18820. but also adjust the previous clocked item in the clock history.
  18821. Everywhere else, drag the line at point up."
  18822. (interactive "P")
  18823. (cond
  18824. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  18825. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  18826. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18827. (call-interactively 'org-timestamp-up)))
  18828. (t (call-interactively 'org-drag-line-backward))))
  18829. (defun org-shiftmetadown (&optional arg)
  18830. "Drag the line at point down.
  18831. In a table, insert an empty row at the current line.
  18832. On a clock timestamp, update the value of the timestamp like `S-<down>'
  18833. but also adjust the previous clocked item in the clock history.
  18834. Everywhere else, drag the line at point down."
  18835. (interactive "P")
  18836. (cond
  18837. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  18838. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  18839. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18840. (call-interactively 'org-timestamp-down)))
  18841. (t (call-interactively 'org-drag-line-forward))))
  18842. (defsubst org-hidden-tree-error ()
  18843. (user-error
  18844. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  18845. (defun org-metaleft (&optional arg)
  18846. "Promote heading, list item at point or move table column left.
  18847. Calls `org-do-promote', `org-outdent-item' or `org-table-move-column',
  18848. depending on context. With no specific context, calls the Emacs
  18849. default `backward-word'. See the individual commands for more
  18850. information.
  18851. This function runs the hook `org-metaleft-hook' as a first step,
  18852. and returns at first non-nil value."
  18853. (interactive "P")
  18854. (cond
  18855. ((run-hook-with-args-until-success 'org-metaleft-hook))
  18856. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  18857. ((org-with-limited-levels
  18858. (or (org-at-heading-p)
  18859. (and (org-region-active-p)
  18860. (save-excursion
  18861. (goto-char (region-beginning))
  18862. (org-at-heading-p)))))
  18863. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18864. (call-interactively 'org-do-promote))
  18865. ;; At an inline task.
  18866. ((org-at-heading-p)
  18867. (call-interactively 'org-inlinetask-promote))
  18868. ((or (org-at-item-p)
  18869. (and (org-region-active-p)
  18870. (save-excursion
  18871. (goto-char (region-beginning))
  18872. (org-at-item-p))))
  18873. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18874. (call-interactively 'org-outdent-item))
  18875. (t (call-interactively 'backward-word))))
  18876. (defun org-metaright (&optional arg)
  18877. "Demote heading, list item at point or move table column right.
  18878. In front of a drawer or a block keyword, indent it correctly.
  18879. Calls `org-do-demote', `org-indent-item', `org-table-move-column',
  18880. `org-indnet-drawer' or `org-indent-block' depending on context.
  18881. With no specific context, calls the Emacs default `forward-word'.
  18882. See the individual commands for more information.
  18883. This function runs the hook `org-metaright-hook' as a first step,
  18884. and returns at first non-nil value."
  18885. (interactive "P")
  18886. (cond
  18887. ((run-hook-with-args-until-success 'org-metaright-hook))
  18888. ((org-at-table-p) (call-interactively 'org-table-move-column))
  18889. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  18890. ((org-at-block-p) (call-interactively 'org-indent-block))
  18891. ((org-with-limited-levels
  18892. (or (org-at-heading-p)
  18893. (and (org-region-active-p)
  18894. (save-excursion
  18895. (goto-char (region-beginning))
  18896. (org-at-heading-p)))))
  18897. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18898. (call-interactively 'org-do-demote))
  18899. ;; At an inline task.
  18900. ((org-at-heading-p)
  18901. (call-interactively 'org-inlinetask-demote))
  18902. ((or (org-at-item-p)
  18903. (and (org-region-active-p)
  18904. (save-excursion
  18905. (goto-char (region-beginning))
  18906. (org-at-item-p))))
  18907. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18908. (call-interactively 'org-indent-item))
  18909. (t (call-interactively 'forward-word))))
  18910. (defun org-check-for-hidden (what)
  18911. "Check if there are hidden headlines/items in the current visual line.
  18912. WHAT can be either `headlines' or `items'. If the current line is
  18913. an outline or item heading and it has a folded subtree below it,
  18914. this function returns t, nil otherwise."
  18915. (let ((re (cond
  18916. ((eq what 'headlines) org-outline-regexp-bol)
  18917. ((eq what 'items) (org-item-beginning-re))
  18918. (t (error "This should not happen"))))
  18919. beg end)
  18920. (save-excursion
  18921. (catch 'exit
  18922. (unless (org-region-active-p)
  18923. (setq beg (point-at-bol))
  18924. (beginning-of-line 2)
  18925. (while (and (not (eobp)) ;; this is like `next-line'
  18926. (get-char-property (1- (point)) 'invisible))
  18927. (beginning-of-line 2))
  18928. (setq end (point))
  18929. (goto-char beg)
  18930. (goto-char (point-at-eol))
  18931. (setq end (max end (point)))
  18932. (while (re-search-forward re end t)
  18933. (if (get-char-property (match-beginning 0) 'invisible)
  18934. (throw 'exit t))))
  18935. nil))))
  18936. (defun org-metaup (&optional arg)
  18937. "Move subtree up or move table row up.
  18938. Calls `org-move-subtree-up' or `org-table-move-row' or
  18939. `org-move-item-up', depending on context. See the individual commands
  18940. for more information."
  18941. (interactive "P")
  18942. (cond
  18943. ((run-hook-with-args-until-success 'org-metaup-hook))
  18944. ((org-region-active-p)
  18945. (let* ((a (min (region-beginning) (region-end)))
  18946. (b (1- (max (region-beginning) (region-end))))
  18947. (c (save-excursion (goto-char a)
  18948. (move-beginning-of-line 0)))
  18949. (d (save-excursion (goto-char a)
  18950. (move-end-of-line 0) (point))))
  18951. (transpose-regions a b c d)
  18952. (goto-char c)))
  18953. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  18954. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18955. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18956. (t (org-drag-element-backward))))
  18957. (defun org-metadown (&optional arg)
  18958. "Move subtree down or move table row down.
  18959. Calls `org-move-subtree-down' or `org-table-move-row' or
  18960. `org-move-item-down', depending on context. See the individual
  18961. commands for more information."
  18962. (interactive "P")
  18963. (cond
  18964. ((run-hook-with-args-until-success 'org-metadown-hook))
  18965. ((org-region-active-p)
  18966. (let* ((a (min (region-beginning) (region-end)))
  18967. (b (max (region-beginning) (region-end)))
  18968. (c (save-excursion (goto-char b)
  18969. (move-beginning-of-line 1)))
  18970. (d (save-excursion (goto-char b)
  18971. (move-end-of-line 1) (1+ (point)))))
  18972. (transpose-regions a b c d)
  18973. (goto-char d)))
  18974. ((org-at-table-p) (call-interactively 'org-table-move-row))
  18975. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18976. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18977. (t (org-drag-element-forward))))
  18978. (defun org-shiftup (&optional arg)
  18979. "Increase item in timestamp or increase priority of current headline.
  18980. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  18981. depending on context. See the individual commands for more information."
  18982. (interactive "P")
  18983. (cond
  18984. ((run-hook-with-args-until-success 'org-shiftup-hook))
  18985. ((and org-support-shift-select (org-region-active-p))
  18986. (org-call-for-shift-select 'previous-line))
  18987. ((org-at-timestamp-p t)
  18988. (call-interactively (if org-edit-timestamp-down-means-later
  18989. 'org-timestamp-down 'org-timestamp-up)))
  18990. ((and (not (eq org-support-shift-select 'always))
  18991. org-enable-priority-commands
  18992. (org-at-heading-p))
  18993. (call-interactively 'org-priority-up))
  18994. ((and (not org-support-shift-select) (org-at-item-p))
  18995. (call-interactively 'org-previous-item))
  18996. ((org-clocktable-try-shift 'up arg))
  18997. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  18998. (org-support-shift-select
  18999. (org-call-for-shift-select 'previous-line))
  19000. (t (org-shiftselect-error))))
  19001. (defun org-shiftdown (&optional arg)
  19002. "Decrease item in timestamp or decrease priority of current headline.
  19003. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  19004. depending on context. See the individual commands for more information."
  19005. (interactive "P")
  19006. (cond
  19007. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  19008. ((and org-support-shift-select (org-region-active-p))
  19009. (org-call-for-shift-select 'next-line))
  19010. ((org-at-timestamp-p t)
  19011. (call-interactively (if org-edit-timestamp-down-means-later
  19012. 'org-timestamp-up 'org-timestamp-down)))
  19013. ((and (not (eq org-support-shift-select 'always))
  19014. org-enable-priority-commands
  19015. (org-at-heading-p))
  19016. (call-interactively 'org-priority-down))
  19017. ((and (not org-support-shift-select) (org-at-item-p))
  19018. (call-interactively 'org-next-item))
  19019. ((org-clocktable-try-shift 'down arg))
  19020. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  19021. (org-support-shift-select
  19022. (org-call-for-shift-select 'next-line))
  19023. (t (org-shiftselect-error))))
  19024. (defun org-shiftright (&optional arg)
  19025. "Cycle the thing at point or in the current line, depending on context.
  19026. Depending on context, this does one of the following:
  19027. - switch a timestamp at point one day into the future
  19028. - on a headline, switch to the next TODO keyword.
  19029. - on an item, switch entire list to the next bullet type
  19030. - on a property line, switch to the next allowed value
  19031. - on a clocktable definition line, move time block into the future"
  19032. (interactive "P")
  19033. (cond
  19034. ((run-hook-with-args-until-success 'org-shiftright-hook))
  19035. ((and org-support-shift-select (org-region-active-p))
  19036. (org-call-for-shift-select 'forward-char))
  19037. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  19038. ((and (not (eq org-support-shift-select 'always))
  19039. (org-at-heading-p))
  19040. (let ((org-inhibit-logging
  19041. (not org-treat-S-cursor-todo-selection-as-state-change))
  19042. (org-inhibit-blocking
  19043. (not org-treat-S-cursor-todo-selection-as-state-change)))
  19044. (org-call-with-arg 'org-todo 'right)))
  19045. ((or (and org-support-shift-select
  19046. (not (eq org-support-shift-select 'always))
  19047. (org-at-item-bullet-p))
  19048. (and (not org-support-shift-select) (org-at-item-p)))
  19049. (org-call-with-arg 'org-cycle-list-bullet nil))
  19050. ((and (not (eq org-support-shift-select 'always))
  19051. (org-at-property-p))
  19052. (call-interactively 'org-property-next-allowed-value))
  19053. ((org-clocktable-try-shift 'right arg))
  19054. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  19055. (org-support-shift-select
  19056. (org-call-for-shift-select 'forward-char))
  19057. (t (org-shiftselect-error))))
  19058. (defun org-shiftleft (&optional arg)
  19059. "Cycle the thing at point or in the current line, depending on context.
  19060. Depending on context, this does one of the following:
  19061. - switch a timestamp at point one day into the past
  19062. - on a headline, switch to the previous TODO keyword.
  19063. - on an item, switch entire list to the previous bullet type
  19064. - on a property line, switch to the previous allowed value
  19065. - on a clocktable definition line, move time block into the past"
  19066. (interactive "P")
  19067. (cond
  19068. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  19069. ((and org-support-shift-select (org-region-active-p))
  19070. (org-call-for-shift-select 'backward-char))
  19071. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  19072. ((and (not (eq org-support-shift-select 'always))
  19073. (org-at-heading-p))
  19074. (let ((org-inhibit-logging
  19075. (not org-treat-S-cursor-todo-selection-as-state-change))
  19076. (org-inhibit-blocking
  19077. (not org-treat-S-cursor-todo-selection-as-state-change)))
  19078. (org-call-with-arg 'org-todo 'left)))
  19079. ((or (and org-support-shift-select
  19080. (not (eq org-support-shift-select 'always))
  19081. (org-at-item-bullet-p))
  19082. (and (not org-support-shift-select) (org-at-item-p)))
  19083. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  19084. ((and (not (eq org-support-shift-select 'always))
  19085. (org-at-property-p))
  19086. (call-interactively 'org-property-previous-allowed-value))
  19087. ((org-clocktable-try-shift 'left arg))
  19088. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  19089. (org-support-shift-select
  19090. (org-call-for-shift-select 'backward-char))
  19091. (t (org-shiftselect-error))))
  19092. (defun org-shiftcontrolright ()
  19093. "Switch to next TODO set."
  19094. (interactive)
  19095. (cond
  19096. ((and org-support-shift-select (org-region-active-p))
  19097. (org-call-for-shift-select 'forward-word))
  19098. ((and (not (eq org-support-shift-select 'always))
  19099. (org-at-heading-p))
  19100. (org-call-with-arg 'org-todo 'nextset))
  19101. (org-support-shift-select
  19102. (org-call-for-shift-select 'forward-word))
  19103. (t (org-shiftselect-error))))
  19104. (defun org-shiftcontrolleft ()
  19105. "Switch to previous TODO set."
  19106. (interactive)
  19107. (cond
  19108. ((and org-support-shift-select (org-region-active-p))
  19109. (org-call-for-shift-select 'backward-word))
  19110. ((and (not (eq org-support-shift-select 'always))
  19111. (org-at-heading-p))
  19112. (org-call-with-arg 'org-todo 'previousset))
  19113. (org-support-shift-select
  19114. (org-call-for-shift-select 'backward-word))
  19115. (t (org-shiftselect-error))))
  19116. (defun org-shiftcontrolup (&optional n)
  19117. "Change timestamps synchronously up in CLOCK log lines.
  19118. Optional argument N tells to change by that many units."
  19119. (interactive "P")
  19120. (if (and (org-at-clock-log-p) (org-at-timestamp-p t))
  19121. (let (org-support-shift-select)
  19122. (org-clock-timestamps-up n))
  19123. (user-error "Not at a clock log")))
  19124. (defun org-shiftcontroldown (&optional n)
  19125. "Change timestamps synchronously down in CLOCK log lines.
  19126. Optional argument N tells to change by that many units."
  19127. (interactive "P")
  19128. (if (and (org-at-clock-log-p) (org-at-timestamp-p t))
  19129. (let (org-support-shift-select)
  19130. (org-clock-timestamps-down n))
  19131. (user-error "Not at a clock log")))
  19132. (defun org-increase-number-at-point (&optional inc)
  19133. "Increment the number at point.
  19134. With an optional prefix numeric argument INC, increment using
  19135. this numeric value."
  19136. (interactive "p")
  19137. (if (not (number-at-point))
  19138. (user-error "Not on a number")
  19139. (unless inc (setq inc 1))
  19140. (let ((pos (point))
  19141. (beg (skip-chars-backward "-+^/*0-9eE."))
  19142. (end (skip-chars-forward "-+^/*0-9eE^.")) nap)
  19143. (setq nap (buffer-substring-no-properties
  19144. (+ pos beg) (+ pos beg end)))
  19145. (delete-region (+ pos beg) (+ pos beg end))
  19146. (insert (calc-eval (concat (number-to-string inc) "+" nap))))
  19147. (when (org-at-table-p)
  19148. (org-table-align)
  19149. (org-table-end-of-field 1))))
  19150. (defun org-decrease-number-at-point (&optional inc)
  19151. "Decrement the number at point.
  19152. With an optional prefix numeric argument INC, decrement using
  19153. this numeric value."
  19154. (interactive "p")
  19155. (org-increase-number-at-point (- (or inc 1))))
  19156. (defun org-ctrl-c-ret ()
  19157. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  19158. (interactive)
  19159. (cond
  19160. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  19161. (t (call-interactively 'org-insert-heading))))
  19162. (defun org-find-visible ()
  19163. (let ((s (point)))
  19164. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  19165. (get-char-property s 'invisible)))
  19166. s))
  19167. (defun org-find-invisible ()
  19168. (let ((s (point)))
  19169. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  19170. (not (get-char-property s 'invisible))))
  19171. s))
  19172. (defun org-copy-visible (beg end)
  19173. "Copy the visible parts of the region."
  19174. (interactive "r")
  19175. (let (snippets s)
  19176. (save-excursion
  19177. (save-restriction
  19178. (narrow-to-region beg end)
  19179. (setq s (goto-char (point-min)))
  19180. (while (not (= (point) (point-max)))
  19181. (goto-char (org-find-invisible))
  19182. (push (buffer-substring s (point)) snippets)
  19183. (setq s (goto-char (org-find-visible))))))
  19184. (kill-new (apply 'concat (nreverse snippets)))))
  19185. (defun org-copy-special ()
  19186. "Copy region in table or copy current subtree.
  19187. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  19188. See the individual commands for more information."
  19189. (interactive)
  19190. (call-interactively
  19191. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  19192. (defun org-cut-special ()
  19193. "Cut region in table or cut current subtree.
  19194. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  19195. See the individual commands for more information."
  19196. (interactive)
  19197. (call-interactively
  19198. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  19199. (defun org-paste-special (arg)
  19200. "Paste rectangular region into table, or past subtree relative to level.
  19201. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  19202. See the individual commands for more information."
  19203. (interactive "P")
  19204. (if (org-at-table-p)
  19205. (org-table-paste-rectangle)
  19206. (org-paste-subtree arg)))
  19207. (defsubst org-in-fixed-width-region-p ()
  19208. "Is point in a fixed-width region?"
  19209. (save-match-data
  19210. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  19211. (defun org-edit-special (&optional arg)
  19212. "Call a special editor for the element at point.
  19213. When at a table, call the formula editor with `org-table-edit-formulas'.
  19214. When in a source code block, call `org-edit-src-code'.
  19215. When in a fixed-width region, call `org-edit-fixed-width-region'.
  19216. When in an export block, call `org-edit-export-block'.
  19217. When at an #+INCLUDE keyword, visit the included file.
  19218. When at a footnote reference, call `org-edit-footnote-reference'
  19219. On a link, call `ffap' to visit the link at point.
  19220. Otherwise, return a user error."
  19221. (interactive "P")
  19222. (let ((element (org-element-at-point)))
  19223. (assert (not buffer-read-only) nil
  19224. "Buffer is read-only: %s" (buffer-name))
  19225. (case (org-element-type element)
  19226. (src-block
  19227. (if (not arg) (org-edit-src-code)
  19228. (let* ((info (org-babel-get-src-block-info))
  19229. (lang (nth 0 info))
  19230. (params (nth 2 info))
  19231. (session (cdr (assq :session params))))
  19232. (if (not session) (org-edit-src-code)
  19233. ;; At a src-block with a session and function called with
  19234. ;; an ARG: switch to the buffer related to the inferior
  19235. ;; process.
  19236. (switch-to-buffer
  19237. (funcall (intern (concat "org-babel-prep-session:" lang))
  19238. session params))))))
  19239. (keyword
  19240. (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
  19241. (org-open-link-from-string
  19242. (format "[[%s]]"
  19243. (expand-file-name
  19244. (let ((value (org-element-property :value element)))
  19245. (cond ((not (org-string-nw-p value))
  19246. (user-error "No file to edit"))
  19247. ((string-match "\\`\"\\(.*?\\)\"" value)
  19248. (match-string 1 value))
  19249. ((string-match "\\`[^ \t\"]\\S-*" value)
  19250. (match-string 0 value))
  19251. (t (user-error "No valid file specified")))))))
  19252. (user-error "No special environment to edit here")))
  19253. (table
  19254. (if (eq (org-element-property :type element) 'table.el)
  19255. (org-edit-table.el)
  19256. (call-interactively 'org-table-edit-formulas)))
  19257. ;; Only Org tables contain `table-row' type elements.
  19258. (table-row (call-interactively 'org-table-edit-formulas))
  19259. (example-block (org-edit-src-code))
  19260. (export-block (org-edit-export-block))
  19261. (fixed-width (org-edit-fixed-width-region))
  19262. (otherwise
  19263. ;; No notable element at point. Though, we may be at a link or
  19264. ;; a footnote reference, which are objects. Thus, scan deeper.
  19265. (let ((context (org-element-context element)))
  19266. (case (org-element-type context)
  19267. (link (call-interactively #'ffap))
  19268. (footnote-reference (org-edit-footnote-reference))
  19269. (t (user-error "No special environment to edit here"))))))))
  19270. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  19271. (defun org-ctrl-c-ctrl-c (&optional arg)
  19272. "Set tags in headline, or update according to changed information at point.
  19273. This command does many different things, depending on context:
  19274. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  19275. this is what we do.
  19276. - If the cursor is on a statistics cookie, update it.
  19277. - If the cursor is in a headline, prompt for tags and insert them
  19278. into the current line, aligned to `org-tags-column'. When called
  19279. with prefix arg, realign all tags in the current buffer.
  19280. - If the cursor is in one of the special #+KEYWORD lines, this
  19281. triggers scanning the buffer for these lines and updating the
  19282. information.
  19283. - If the cursor is inside a table, realign the table. This command
  19284. works even if the automatic table editor has been turned off.
  19285. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  19286. the entire table.
  19287. - If the cursor is at a footnote reference or definition, jump to
  19288. the corresponding definition or references, respectively.
  19289. - If the cursor is a the beginning of a dynamic block, update it.
  19290. - If the current buffer is a capture buffer, close note and file it.
  19291. - If the cursor is on a <<<target>>>, update radio targets and
  19292. corresponding links in this buffer.
  19293. - If the cursor is on a numbered item in a plain list, renumber the
  19294. ordered list.
  19295. - If the cursor is on a checkbox, toggle it.
  19296. - If the cursor is on a code block, evaluate it. The variable
  19297. `org-confirm-babel-evaluate' can be used to control prompting
  19298. before code block evaluation, by default every code block
  19299. evaluation requires confirmation. Code block evaluation can be
  19300. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  19301. (interactive "P")
  19302. (cond
  19303. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  19304. org-occur-highlights)
  19305. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  19306. (org-remove-occur-highlights)
  19307. (message "Temporary highlights/overlays removed from current buffer"))
  19308. ((and (local-variable-p 'org-finish-function (current-buffer))
  19309. (fboundp org-finish-function))
  19310. (funcall org-finish-function))
  19311. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  19312. (t
  19313. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  19314. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  19315. (user-error "C-c C-c can do nothing useful at this location"))
  19316. (let* ((context (org-element-context))
  19317. (type (org-element-type context)))
  19318. (case type
  19319. ;; When at a link, act according to the parent instead.
  19320. (link (setq context (org-element-property :parent context))
  19321. (setq type (org-element-type context)))
  19322. ;; Unsupported object types: refer to the first supported
  19323. ;; element or object containing it.
  19324. ((bold code entity export-snippet inline-babel-call inline-src-block
  19325. italic latex-fragment line-break macro strike-through subscript
  19326. superscript underline verbatim)
  19327. (setq context
  19328. (org-element-lineage
  19329. context '(radio-target paragraph verse-block table-cell)))))
  19330. ;; For convenience: at the first line of a paragraph on the
  19331. ;; same line as an item, apply function on that item instead.
  19332. (when (eq type 'paragraph)
  19333. (let ((parent (org-element-property :parent context)))
  19334. (when (and (eq (org-element-type parent) 'item)
  19335. (= (line-beginning-position)
  19336. (org-element-property :begin parent)))
  19337. (setq context parent type 'item))))
  19338. ;; Act according to type of element or object at point.
  19339. (case type
  19340. (clock (org-clock-update-time-maybe))
  19341. (dynamic-block
  19342. (save-excursion
  19343. (goto-char (org-element-property :post-affiliated context))
  19344. (org-update-dblock)))
  19345. (footnote-definition
  19346. (goto-char (org-element-property :post-affiliated context))
  19347. (call-interactively 'org-footnote-action))
  19348. (footnote-reference (call-interactively 'org-footnote-action))
  19349. ((headline inlinetask)
  19350. (save-excursion (goto-char (org-element-property :begin context))
  19351. (call-interactively 'org-set-tags)))
  19352. (item
  19353. ;; At an item: a double C-u set checkbox to "[-]"
  19354. ;; unconditionally, whereas a single one will toggle its
  19355. ;; presence. Without a universal argument, if the item
  19356. ;; has a checkbox, toggle it. Otherwise repair the list.
  19357. (let* ((box (org-element-property :checkbox context))
  19358. (struct (org-element-property :structure context))
  19359. (old-struct (copy-tree struct))
  19360. (parents (org-list-parents-alist struct))
  19361. (prevs (org-list-prevs-alist struct))
  19362. (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
  19363. (org-list-set-checkbox
  19364. (org-element-property :begin context) struct
  19365. (cond ((equal arg '(16)) "[-]")
  19366. ((and (not box) (equal arg '(4))) "[ ]")
  19367. ((or (not box) (equal arg '(4))) nil)
  19368. ((eq box 'on) "[ ]")
  19369. (t "[X]")))
  19370. ;; Mimic `org-list-write-struct' but with grabbing
  19371. ;; a return value from `org-list-struct-fix-box'.
  19372. (org-list-struct-fix-ind struct parents 2)
  19373. (org-list-struct-fix-item-end struct)
  19374. (org-list-struct-fix-bul struct prevs)
  19375. (org-list-struct-fix-ind struct parents)
  19376. (let ((block-item
  19377. (org-list-struct-fix-box struct parents prevs orderedp)))
  19378. (if (and box (equal struct old-struct))
  19379. (if (equal arg '(16))
  19380. (message "Checkboxes already reset")
  19381. (user-error "Cannot toggle this checkbox: %s"
  19382. (if (eq box 'on)
  19383. "all subitems checked"
  19384. "unchecked subitems")))
  19385. (org-list-struct-apply-struct struct old-struct)
  19386. (org-update-checkbox-count-maybe))
  19387. (when block-item
  19388. (message "Checkboxes were removed due to empty box at line %d"
  19389. (org-current-line block-item))))))
  19390. (keyword
  19391. (let ((org-inhibit-startup-visibility-stuff t)
  19392. (org-startup-align-all-tables nil))
  19393. (when (boundp 'org-table-coordinate-overlays)
  19394. (mapc 'delete-overlay org-table-coordinate-overlays)
  19395. (setq org-table-coordinate-overlays nil))
  19396. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  19397. (message "Local setup has been refreshed"))
  19398. (plain-list
  19399. ;; At a plain list, with a double C-u argument, set
  19400. ;; checkboxes of each item to "[-]", whereas a single one
  19401. ;; will toggle their presence according to the state of the
  19402. ;; first item in the list. Without an argument, repair the
  19403. ;; list.
  19404. (let* ((begin (org-element-property :contents-begin context))
  19405. (beginm (move-marker (make-marker) begin))
  19406. (struct (org-element-property :structure context))
  19407. (old-struct (copy-tree struct))
  19408. (first-box (save-excursion
  19409. (goto-char begin)
  19410. (looking-at org-list-full-item-re)
  19411. (match-string-no-properties 3)))
  19412. (new-box (cond ((equal arg '(16)) "[-]")
  19413. ((equal arg '(4)) (unless first-box "[ ]"))
  19414. ((equal first-box "[X]") "[ ]")
  19415. (t "[X]"))))
  19416. (cond
  19417. (arg
  19418. (mapc (lambda (pos) (org-list-set-checkbox pos struct new-box))
  19419. (org-list-get-all-items
  19420. begin struct (org-list-prevs-alist struct))))
  19421. ((and first-box (eq (point) begin))
  19422. ;; For convenience, when point is at bol on the first
  19423. ;; item of the list and no argument is provided, simply
  19424. ;; toggle checkbox of that item, if any.
  19425. (org-list-set-checkbox begin struct new-box)))
  19426. (org-list-write-struct
  19427. struct (org-list-parents-alist struct) old-struct)
  19428. (org-update-checkbox-count-maybe)
  19429. (save-excursion (goto-char beginm) (org-list-send-list 'maybe))))
  19430. ((property-drawer node-property)
  19431. (call-interactively 'org-property-action))
  19432. ((radio-target target)
  19433. (call-interactively 'org-update-radio-target-regexp))
  19434. (statistics-cookie
  19435. (call-interactively 'org-update-statistics-cookies))
  19436. ((table table-cell table-row)
  19437. ;; At a table, recalculate every field and align it. Also
  19438. ;; send the table if necessary. If the table has
  19439. ;; a `table.el' type, just give up. At a table row or
  19440. ;; cell, maybe recalculate line but always align table.
  19441. (if (eq (org-element-property :type context) 'table.el)
  19442. (message "Use C-c ' to edit table.el tables")
  19443. (let ((org-enable-table-editor t))
  19444. (if (or (eq type 'table)
  19445. ;; Check if point is at a TBLFM line.
  19446. (and (eq type 'table-row)
  19447. (= (point) (org-element-property :end context))))
  19448. (save-excursion
  19449. (if (org-at-TBLFM-p)
  19450. (progn (require 'org-table)
  19451. (org-table-calc-current-TBLFM))
  19452. (goto-char (org-element-property :contents-begin context))
  19453. (org-call-with-arg 'org-table-recalculate (or arg t))
  19454. (orgtbl-send-table 'maybe)))
  19455. (org-table-maybe-eval-formula)
  19456. (cond (arg (call-interactively 'org-table-recalculate))
  19457. ((org-table-maybe-recalculate-line))
  19458. (t (org-table-align)))))))
  19459. (timestamp (org-timestamp-change 0 'day))
  19460. (otherwise
  19461. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  19462. (user-error
  19463. "C-c C-c can do nothing useful at this location")))))))))
  19464. (defun org-mode-restart ()
  19465. (interactive)
  19466. (let ((indent-status (org-bound-and-true-p org-indent-mode)))
  19467. (funcall major-mode)
  19468. (hack-local-variables)
  19469. (when (and indent-status (not (org-bound-and-true-p org-indent-mode)))
  19470. (org-indent-mode -1)))
  19471. (message "%s restarted" major-mode))
  19472. (defun org-kill-note-or-show-branches ()
  19473. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  19474. (interactive)
  19475. (if (not org-finish-function)
  19476. (progn
  19477. (hide-subtree)
  19478. (call-interactively 'show-branches))
  19479. (let ((org-note-abort t))
  19480. (funcall org-finish-function))))
  19481. (defun org-delete-indentation (&optional ARG)
  19482. "Join current line to previous and fix whitespace at join.
  19483. If previous line is a headline add to headline title. Otherwise
  19484. the function calls `delete-indentation'.
  19485. With argument, join this line to following line."
  19486. (interactive "*P")
  19487. (if (save-excursion
  19488. (if ARG (beginning-of-line)
  19489. (forward-line -1))
  19490. (looking-at org-complex-heading-regexp))
  19491. ;; At headline.
  19492. (let ((tags-column (when (match-beginning 5)
  19493. (save-excursion (goto-char (match-beginning 5))
  19494. (current-column))))
  19495. (string (concat " " (progn (when ARG (forward-line 1))
  19496. (org-trim (delete-and-extract-region
  19497. (line-beginning-position)
  19498. (line-end-position)))))))
  19499. (unless (bobp) (delete-region (point) (1- (point))))
  19500. (goto-char (or (match-end 4)
  19501. (match-beginning 5)
  19502. (match-end 0)))
  19503. (skip-chars-backward " \t")
  19504. (save-excursion (insert string))
  19505. ;; Adjust alignment of tags.
  19506. (when tags-column
  19507. (org-align-tags-here (if org-auto-align-tags
  19508. org-tags-column
  19509. tags-column))))
  19510. (delete-indentation ARG)))
  19511. (defun org-open-line (n)
  19512. "Insert a new row in tables, call `open-line' elsewhere.
  19513. If `org-special-ctrl-o' is nil, just call `open-line' everywhere."
  19514. (interactive "*p")
  19515. (cond
  19516. ((not org-special-ctrl-o)
  19517. (open-line n))
  19518. ((org-at-table-p)
  19519. (org-table-insert-row))
  19520. (t
  19521. (open-line n))))
  19522. (defun org-return (&optional indent)
  19523. "Goto next table row or insert a newline.
  19524. Calls `org-table-next-row' or `newline', depending on context.
  19525. When optional INDENT argument is non-nil, call
  19526. `newline-and-indent' instead of `newline'.
  19527. When `org-return-follows-link' is non-nil and point is on
  19528. a timestamp or a link, call `org-open-at-point'. However, it
  19529. will not happen if point is in a table or on a \"dead\"
  19530. object (e.g., within a comment). In these case, you need to use
  19531. `org-open-at-point' directly."
  19532. (interactive)
  19533. (let* ((context (if org-return-follows-link (org-element-context)
  19534. (org-element-at-point)))
  19535. (type (org-element-type context)))
  19536. (cond
  19537. ;; In a table, call `org-table-next-row'.
  19538. ((or (and (eq type 'table)
  19539. (>= (point) (org-element-property :contents-begin context))
  19540. (< (point) (org-element-property :contents-end context)))
  19541. (org-element-lineage context '(table-row table-cell) t))
  19542. (org-table-justify-field-maybe)
  19543. (call-interactively #'org-table-next-row))
  19544. ;; On a link or a timestamp but not on white spaces after it,
  19545. ;; call `org-open-line' if `org-return-follows-link' allows it.
  19546. ((and org-return-follows-link
  19547. (memq type '(link timestamp))
  19548. (< (point)
  19549. (save-excursion (goto-char (org-element-property :end context))
  19550. (skip-chars-backward " \t")
  19551. (point))))
  19552. (call-interactively #'org-open-at-point))
  19553. ;; Insert newline in heading, but preserve tags.
  19554. ((and (not (bolp))
  19555. (save-excursion (beginning-of-line)
  19556. (looking-at org-complex-heading-regexp)))
  19557. ;; At headline. Split line. However, if point is on keyword,
  19558. ;; priority cookie or tags, do not break any of them: add
  19559. ;; a newline after the headline instead.
  19560. (let ((tags-column (and (match-beginning 5)
  19561. (save-excursion (goto-char (match-beginning 5))
  19562. (current-column))))
  19563. (string
  19564. (when (and (match-end 4)
  19565. (>= (point)
  19566. (or (match-end 3) (match-end 2) (1+ (match-end 1))))
  19567. (<= (point) (match-end 4)))
  19568. (delete-and-extract-region (point) (match-end 4)))))
  19569. (when (and tags-column string) ; Adjust tag alignment.
  19570. (org-align-tags-here
  19571. (if org-auto-align-tags org-tags-column tags-column)))
  19572. (end-of-line)
  19573. (org-show-entry)
  19574. (if indent (newline-and-indent) (newline))
  19575. (when string (save-excursion (insert (org-trim string))))))
  19576. ;; In a list, make sure indenting keeps trailing text within.
  19577. ((and indent
  19578. (not (eolp))
  19579. (org-element-lineage context '(item)))
  19580. (let ((trailing-data
  19581. (delete-and-extract-region (point) (line-end-position))))
  19582. (newline-and-indent)
  19583. (save-excursion (insert trailing-data))))
  19584. (t (if indent (newline-and-indent) (newline))))))
  19585. (defun org-return-indent ()
  19586. "Goto next table row or insert a newline and indent.
  19587. Calls `org-table-next-row' or `newline-and-indent', depending on
  19588. context. See the individual commands for more information."
  19589. (interactive)
  19590. (org-return t))
  19591. (defun org-ctrl-c-star ()
  19592. "Compute table, or change heading status of lines.
  19593. Calls `org-table-recalculate' or `org-toggle-heading',
  19594. depending on context."
  19595. (interactive)
  19596. (cond
  19597. ((org-at-table-p)
  19598. (call-interactively 'org-table-recalculate))
  19599. (t
  19600. ;; Convert all lines in region to list items
  19601. (call-interactively 'org-toggle-heading))))
  19602. (defun org-ctrl-c-minus ()
  19603. "Insert separator line in table or modify bullet status of line.
  19604. Also turns a plain line or a region of lines into list items.
  19605. Calls `org-table-insert-hline', `org-toggle-item', or
  19606. `org-cycle-list-bullet', depending on context."
  19607. (interactive)
  19608. (cond
  19609. ((org-at-table-p)
  19610. (call-interactively 'org-table-insert-hline))
  19611. ((org-region-active-p)
  19612. (call-interactively 'org-toggle-item))
  19613. ((org-in-item-p)
  19614. (call-interactively 'org-cycle-list-bullet))
  19615. (t
  19616. (call-interactively 'org-toggle-item))))
  19617. (defun org-toggle-item (arg)
  19618. "Convert headings or normal lines to items, items to normal lines.
  19619. If there is no active region, only the current line is considered.
  19620. If the first non blank line in the region is a headline, convert
  19621. all headlines to items, shifting text accordingly.
  19622. If it is an item, convert all items to normal lines.
  19623. If it is normal text, change region into a list of items.
  19624. With a prefix argument ARG, change the region in a single item."
  19625. (interactive "P")
  19626. (let ((shift-text
  19627. (function
  19628. ;; Shift text in current section to IND, from point to END.
  19629. ;; The function leaves point to END line.
  19630. (lambda (ind end)
  19631. (let ((min-i 1000) (end (copy-marker end)))
  19632. ;; First determine the minimum indentation (MIN-I) of
  19633. ;; the text.
  19634. (save-excursion
  19635. (catch 'exit
  19636. (while (< (point) end)
  19637. (let ((i (org-get-indentation)))
  19638. (cond
  19639. ;; Skip blank lines and inline tasks.
  19640. ((looking-at "^[ \t]*$"))
  19641. ((looking-at org-outline-regexp-bol))
  19642. ;; We can't find less than 0 indentation.
  19643. ((zerop i) (throw 'exit (setq min-i 0)))
  19644. ((< i min-i) (setq min-i i))))
  19645. (forward-line))))
  19646. ;; Then indent each line so that a line indented to
  19647. ;; MIN-I becomes indented to IND. Ignore blank lines
  19648. ;; and inline tasks in the process.
  19649. (let ((delta (- ind min-i)))
  19650. (while (< (point) end)
  19651. (unless (or (looking-at "^[ \t]*$")
  19652. (looking-at org-outline-regexp-bol))
  19653. (org-indent-line-to (+ (org-get-indentation) delta)))
  19654. (forward-line)))))))
  19655. (skip-blanks
  19656. (function
  19657. ;; Return beginning of first non-blank line, starting from
  19658. ;; line at POS.
  19659. (lambda (pos)
  19660. (save-excursion
  19661. (goto-char pos)
  19662. (skip-chars-forward " \r\t\n")
  19663. (point-at-bol)))))
  19664. beg end)
  19665. ;; Determine boundaries of changes.
  19666. (if (org-region-active-p)
  19667. (setq beg (funcall skip-blanks (region-beginning))
  19668. end (copy-marker (region-end)))
  19669. (setq beg (funcall skip-blanks (point-at-bol))
  19670. end (copy-marker (point-at-eol))))
  19671. ;; Depending on the starting line, choose an action on the text
  19672. ;; between BEG and END.
  19673. (org-with-limited-levels
  19674. (save-excursion
  19675. (goto-char beg)
  19676. (cond
  19677. ;; Case 1. Start at an item: de-itemize. Note that it only
  19678. ;; happens when a region is active: `org-ctrl-c-minus'
  19679. ;; would call `org-cycle-list-bullet' otherwise.
  19680. ((org-at-item-p)
  19681. (while (< (point) end)
  19682. (when (org-at-item-p)
  19683. (skip-chars-forward " \t")
  19684. (delete-region (point) (match-end 0)))
  19685. (forward-line)))
  19686. ;; Case 2. Start at an heading: convert to items.
  19687. ((org-at-heading-p)
  19688. (let* ((bul (org-list-bullet-string "-"))
  19689. (bul-len (length bul))
  19690. (done (org-entry-is-done-p))
  19691. (todo (org-entry-is-todo-p))
  19692. ;; Indentation of the first heading. It should be
  19693. ;; relative to the indentation of its parent, if any.
  19694. (start-ind (save-excursion
  19695. (cond
  19696. ((not org-adapt-indentation) 0)
  19697. ((not (outline-previous-heading)) 0)
  19698. (t (length (match-string 0))))))
  19699. ;; Level of first heading. Further headings will be
  19700. ;; compared to it to determine hierarchy in the list.
  19701. (ref-level (org-reduced-level (org-outline-level))))
  19702. (when (or done todo) (org-todo ""))
  19703. (while (< (point) end)
  19704. (let* ((level (org-reduced-level (org-outline-level)))
  19705. (delta (max 0 (- level ref-level))))
  19706. ;; If current headline is less indented than the first
  19707. ;; one, set it as reference, in order to preserve
  19708. ;; subtrees.
  19709. (when (< level ref-level) (setq ref-level level))
  19710. (replace-match bul t t)
  19711. (org-indent-line-to (+ start-ind (* delta bul-len)))
  19712. (when (or done todo)
  19713. (let* ((struct (org-list-struct))
  19714. (old (copy-tree struct)))
  19715. (org-list-set-checkbox (line-beginning-position)
  19716. struct
  19717. (if done "[X]" "[ ]"))
  19718. (org-list-write-struct struct
  19719. (org-list-parents-alist struct)
  19720. old)))
  19721. ;; Ensure all text down to END (or SECTION-END) belongs
  19722. ;; to the newly created item.
  19723. (let ((section-end (save-excursion
  19724. (or (outline-next-heading) (point)))))
  19725. (forward-line)
  19726. (funcall shift-text
  19727. (+ start-ind (* (1+ delta) bul-len))
  19728. (min end section-end)))))))
  19729. ;; Case 3. Normal line with ARG: make the first line of region
  19730. ;; an item, and shift indentation of others lines to
  19731. ;; set them as item's body.
  19732. (arg (let* ((bul (org-list-bullet-string "-"))
  19733. (bul-len (length bul))
  19734. (ref-ind (org-get-indentation)))
  19735. (skip-chars-forward " \t")
  19736. (insert bul)
  19737. (forward-line)
  19738. (while (< (point) end)
  19739. ;; Ensure that lines less indented than first one
  19740. ;; still get included in item body.
  19741. (funcall shift-text
  19742. (+ ref-ind bul-len)
  19743. (min end (save-excursion (or (outline-next-heading)
  19744. (point)))))
  19745. (forward-line))))
  19746. ;; Case 4. Normal line without ARG: turn each non-item line
  19747. ;; into an item.
  19748. (t
  19749. (while (< (point) end)
  19750. (unless (or (org-at-heading-p) (org-at-item-p))
  19751. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  19752. (replace-match
  19753. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  19754. (forward-line))))))))
  19755. (defun org-toggle-heading (&optional nstars)
  19756. "Convert headings to normal text, or items or text to headings.
  19757. If there is no active region, only convert the current line.
  19758. With a \\[universal-argument] prefix, convert the whole list at
  19759. point into heading.
  19760. In a region:
  19761. - If the first non blank line is a headline, remove the stars
  19762. from all headlines in the region.
  19763. - If it is a normal line, turn each and every normal line (i.e.,
  19764. not an heading or an item) in the region into headings. If you
  19765. want to convert only the first line of this region, use one
  19766. universal prefix argument.
  19767. - If it is a plain list item, turn all plain list items into headings.
  19768. When converting a line into a heading, the number of stars is chosen
  19769. such that the lines become children of the current entry. However,
  19770. when a numeric prefix argument is given, its value determines the
  19771. number of stars to add."
  19772. (interactive "P")
  19773. (let ((skip-blanks
  19774. (function
  19775. ;; Return beginning of first non-blank line, starting from
  19776. ;; line at POS.
  19777. (lambda (pos)
  19778. (save-excursion
  19779. (goto-char pos)
  19780. (while (org-at-comment-p) (forward-line))
  19781. (skip-chars-forward " \r\t\n")
  19782. (point-at-bol)))))
  19783. beg end toggled)
  19784. ;; Determine boundaries of changes. If a universal prefix has
  19785. ;; been given, put the list in a region. If region ends at a bol,
  19786. ;; do not consider the last line to be in the region.
  19787. (when (and current-prefix-arg (org-at-item-p))
  19788. (if (listp current-prefix-arg) (setq current-prefix-arg 1))
  19789. (org-mark-element))
  19790. (if (org-region-active-p)
  19791. (setq beg (funcall skip-blanks (region-beginning))
  19792. end (copy-marker (save-excursion
  19793. (goto-char (region-end))
  19794. (if (bolp) (point) (point-at-eol)))))
  19795. (setq beg (funcall skip-blanks (point-at-bol))
  19796. end (copy-marker (point-at-eol))))
  19797. ;; Ensure inline tasks don't count as headings.
  19798. (org-with-limited-levels
  19799. (save-excursion
  19800. (goto-char beg)
  19801. (cond
  19802. ;; Case 1. Started at an heading: de-star headings.
  19803. ((org-at-heading-p)
  19804. (while (< (point) end)
  19805. (when (org-at-heading-p t)
  19806. (looking-at org-outline-regexp) (replace-match "")
  19807. (setq toggled t))
  19808. (forward-line)))
  19809. ;; Case 2. Started at an item: change items into headlines.
  19810. ;; One star will be added by `org-list-to-subtree'.
  19811. ((org-at-item-p)
  19812. (while (< (point) end)
  19813. (when (org-at-item-p)
  19814. ;; Pay attention to cases when region ends before list.
  19815. (let* ((struct (org-list-struct))
  19816. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  19817. (save-restriction
  19818. (narrow-to-region (point) list-end)
  19819. (insert (org-list-to-subtree (org-list-parse-list t)))))
  19820. (setq toggled t))
  19821. (forward-line)))
  19822. ;; Case 3. Started at normal text: make every line an heading,
  19823. ;; skipping headlines and items.
  19824. (t (let* ((stars
  19825. (make-string
  19826. (if (numberp nstars) nstars (or (org-current-level) 0)) ?*))
  19827. (add-stars
  19828. (cond (nstars "") ; stars from prefix only
  19829. ((equal stars "") "*") ; before first heading
  19830. (org-odd-levels-only "**") ; inside heading, odd
  19831. (t "*"))) ; inside heading, oddeven
  19832. (rpl (concat stars add-stars " "))
  19833. (lend (if (listp nstars) (save-excursion (end-of-line) (point)))))
  19834. (while (< (point) (if (equal nstars '(4)) lend end))
  19835. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  19836. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  19837. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  19838. (forward-line)))))))
  19839. (unless toggled (message "Cannot toggle heading from here"))))
  19840. (defun org-meta-return (&optional _arg)
  19841. "Insert a new heading or wrap a region in a table.
  19842. Calls `org-insert-heading' or `org-table-wrap-region', depending
  19843. on context. See the individual commands for more information."
  19844. (interactive)
  19845. (org-check-before-invisible-edit 'insert)
  19846. (or (run-hook-with-args-until-success 'org-metareturn-hook)
  19847. (call-interactively (if (org-at-table-p) #'org-table-wrap-region
  19848. #'org-insert-heading))))
  19849. ;;; Menu entries
  19850. (defsubst org-in-subtree-not-table-p ()
  19851. "Are we in a subtree and not in a table?"
  19852. (and (not (org-before-first-heading-p))
  19853. (not (org-at-table-p))))
  19854. ;; Define the Org-mode menus
  19855. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  19856. '("Tbl"
  19857. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  19858. ["Next Field" org-cycle (org-at-table-p)]
  19859. ["Previous Field" org-shifttab (org-at-table-p)]
  19860. ["Next Row" org-return (org-at-table-p)]
  19861. "--"
  19862. ["Blank Field" org-table-blank-field (org-at-table-p)]
  19863. ["Edit Field" org-table-edit-field (org-at-table-p)]
  19864. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  19865. "--"
  19866. ("Column"
  19867. ["Move Column Left" org-metaleft (org-at-table-p)]
  19868. ["Move Column Right" org-metaright (org-at-table-p)]
  19869. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  19870. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  19871. ("Row"
  19872. ["Move Row Up" org-metaup (org-at-table-p)]
  19873. ["Move Row Down" org-metadown (org-at-table-p)]
  19874. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  19875. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  19876. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  19877. "--"
  19878. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  19879. ("Rectangle"
  19880. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  19881. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  19882. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  19883. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  19884. "--"
  19885. ("Calculate"
  19886. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  19887. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  19888. ["Edit Formulas" org-edit-special (org-at-table-p)]
  19889. "--"
  19890. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  19891. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  19892. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  19893. "--"
  19894. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  19895. "--"
  19896. ["Sum Column/Rectangle" org-table-sum
  19897. (or (org-at-table-p) (org-region-active-p))]
  19898. ["Which Column?" org-table-current-column (org-at-table-p)])
  19899. ["Debug Formulas"
  19900. org-table-toggle-formula-debugger
  19901. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  19902. ["Show Col/Row Numbers"
  19903. org-table-toggle-coordinate-overlays
  19904. :style toggle
  19905. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  19906. "--"
  19907. ["Create" org-table-create (and (not (org-at-table-p))
  19908. org-enable-table-editor)]
  19909. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  19910. ["Import from File" org-table-import (not (org-at-table-p))]
  19911. ["Export to File" org-table-export (org-at-table-p)]
  19912. "--"
  19913. ["Create/Convert from/to table.el" org-table-create-with-table.el t]
  19914. "--"
  19915. ("Plot"
  19916. ["Ascii plot" orgtbl-ascii-plot :active (org-at-table-p) :keys "C-c \" a"]
  19917. ["Gnuplot" org-plot/gnuplot :active (org-at-table-p) :keys "C-c \" g"])))
  19918. (easy-menu-define org-org-menu org-mode-map "Org menu"
  19919. '("Org"
  19920. ("Show/Hide"
  19921. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  19922. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  19923. ["Sparse Tree..." org-sparse-tree t]
  19924. ["Reveal Context" org-reveal t]
  19925. ["Show All" show-all t]
  19926. "--"
  19927. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  19928. "--"
  19929. ["New Heading" org-insert-heading t]
  19930. ("Navigate Headings"
  19931. ["Up" outline-up-heading t]
  19932. ["Next" outline-next-visible-heading t]
  19933. ["Previous" outline-previous-visible-heading t]
  19934. ["Next Same Level" outline-forward-same-level t]
  19935. ["Previous Same Level" outline-backward-same-level t]
  19936. "--"
  19937. ["Jump" org-goto t])
  19938. ("Edit Structure"
  19939. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  19940. "--"
  19941. ["Move Subtree Up" org-metaup (org-at-heading-p)]
  19942. ["Move Subtree Down" org-metadown (org-at-heading-p)]
  19943. "--"
  19944. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  19945. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  19946. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  19947. "--"
  19948. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  19949. "--"
  19950. ["Copy visible text" org-copy-visible t]
  19951. "--"
  19952. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  19953. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  19954. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  19955. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  19956. "--"
  19957. ["Sort Region/Children" org-sort t]
  19958. "--"
  19959. ["Convert to odd levels" org-convert-to-odd-levels t]
  19960. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  19961. ("Editing"
  19962. ["Emphasis..." org-emphasize t]
  19963. ["Edit Source Example" org-edit-special t]
  19964. "--"
  19965. ["Footnote new/jump" org-footnote-action t]
  19966. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  19967. ("Archive"
  19968. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  19969. "--"
  19970. ["Move Subtree to Archive file" org-archive-subtree (org-in-subtree-not-table-p)]
  19971. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  19972. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  19973. )
  19974. "--"
  19975. ("Hyperlinks"
  19976. ["Store Link (Global)" org-store-link t]
  19977. ["Find existing link to here" org-occur-link-in-agenda-files t]
  19978. ["Insert Link" org-insert-link t]
  19979. ["Follow Link" org-open-at-point t]
  19980. "--"
  19981. ["Next link" org-next-link t]
  19982. ["Previous link" org-previous-link t]
  19983. "--"
  19984. ["Descriptive Links"
  19985. org-toggle-link-display
  19986. :style radio
  19987. :selected org-descriptive-links
  19988. ]
  19989. ["Literal Links"
  19990. org-toggle-link-display
  19991. :style radio
  19992. :selected (not org-descriptive-links)])
  19993. "--"
  19994. ("TODO Lists"
  19995. ["TODO/DONE/-" org-todo t]
  19996. ("Select keyword"
  19997. ["Next keyword" org-shiftright (org-at-heading-p)]
  19998. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  19999. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  20000. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  20001. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  20002. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  20003. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  20004. "--"
  20005. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  20006. :selected org-enforce-todo-dependencies :style toggle :active t]
  20007. "Settings for tree at point"
  20008. ["Do Children sequentially" org-toggle-ordered-property :style radio
  20009. :selected (org-entry-get nil "ORDERED")
  20010. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  20011. ["Do Children parallel" org-toggle-ordered-property :style radio
  20012. :selected (not (org-entry-get nil "ORDERED"))
  20013. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  20014. "--"
  20015. ["Set Priority" org-priority t]
  20016. ["Priority Up" org-shiftup t]
  20017. ["Priority Down" org-shiftdown t]
  20018. "--"
  20019. ["Get news from all feeds" org-feed-update-all t]
  20020. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  20021. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  20022. ("TAGS and Properties"
  20023. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  20024. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  20025. "--"
  20026. ["Set property" org-set-property (not (org-before-first-heading-p))]
  20027. ["Column view of properties" org-columns t]
  20028. ["Insert Column View DBlock" org-insert-columns-dblock t])
  20029. ("Dates and Scheduling"
  20030. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  20031. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  20032. ("Change Date"
  20033. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  20034. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  20035. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  20036. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  20037. ["Compute Time Range" org-evaluate-time-range t]
  20038. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  20039. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  20040. "--"
  20041. ["Custom time format" org-toggle-time-stamp-overlays
  20042. :style radio :selected org-display-custom-times]
  20043. "--"
  20044. ["Goto Calendar" org-goto-calendar t]
  20045. ["Date from Calendar" org-date-from-calendar t]
  20046. "--"
  20047. ["Start/Restart Timer" org-timer-start t]
  20048. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  20049. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  20050. ["Insert Timer String" org-timer t]
  20051. ["Insert Timer Item" org-timer-item t])
  20052. ("Logging work"
  20053. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  20054. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  20055. ["Clock out" org-clock-out t]
  20056. ["Clock cancel" org-clock-cancel t]
  20057. "--"
  20058. ["Mark as default task" org-clock-mark-default-task t]
  20059. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  20060. ["Goto running clock" org-clock-goto t]
  20061. "--"
  20062. ["Display times" org-clock-display t]
  20063. ["Create clock table" org-clock-report t]
  20064. "--"
  20065. ["Record DONE time"
  20066. (progn (setq org-log-done (not org-log-done))
  20067. (message "Switching to %s will %s record a timestamp"
  20068. (car org-done-keywords)
  20069. (if org-log-done "automatically" "not")))
  20070. :style toggle :selected org-log-done])
  20071. "--"
  20072. ["Agenda Command..." org-agenda t]
  20073. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  20074. ("File List for Agenda")
  20075. ("Special views current file"
  20076. ["TODO Tree" org-show-todo-tree t]
  20077. ["Check Deadlines" org-check-deadlines t]
  20078. ["Timeline" org-timeline t]
  20079. ["Tags/Property tree" org-match-sparse-tree t])
  20080. "--"
  20081. ["Export/Publish..." org-export-dispatch t]
  20082. ("LaTeX"
  20083. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  20084. :selected org-cdlatex-mode]
  20085. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  20086. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  20087. ["Modify math symbol" org-cdlatex-math-modify
  20088. (org-inside-LaTeX-fragment-p)]
  20089. ["Insert citation" org-reftex-citation t]
  20090. "--"
  20091. ["Template for BEAMER" (org-beamer-insert-options-template) t])
  20092. "--"
  20093. ("MobileOrg"
  20094. ["Push Files and Views" org-mobile-push t]
  20095. ["Get Captured and Flagged" org-mobile-pull t]
  20096. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  20097. "--"
  20098. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  20099. "--"
  20100. ("Documentation"
  20101. ["Show Version" org-version t]
  20102. ["Info Documentation" org-info t])
  20103. ("Customize"
  20104. ["Browse Org Group" org-customize t]
  20105. "--"
  20106. ["Expand This Menu" org-create-customize-menu
  20107. (fboundp 'customize-menu-create)])
  20108. ["Send bug report" org-submit-bug-report t]
  20109. "--"
  20110. ("Refresh/Reload"
  20111. ["Refresh setup current buffer" org-mode-restart t]
  20112. ["Reload Org (after update)" org-reload t]
  20113. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
  20114. ))
  20115. (defun org-info (&optional node)
  20116. "Read documentation for Org-mode in the info system.
  20117. With optional NODE, go directly to that node."
  20118. (interactive)
  20119. (info (format "(org)%s" (or node ""))))
  20120. ;;;###autoload
  20121. (defun org-submit-bug-report ()
  20122. "Submit a bug report on Org-mode via mail.
  20123. Don't hesitate to report any problems or inaccurate documentation.
  20124. If you don't have setup sending mail from (X)Emacs, please copy the
  20125. output buffer into your mail program, as it gives us important
  20126. information about your Org-mode version and configuration."
  20127. (interactive)
  20128. (require 'reporter)
  20129. (defvar reporter-prompt-for-summary-p)
  20130. (org-load-modules-maybe)
  20131. (org-require-autoloaded-modules)
  20132. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  20133. (reporter-submit-bug-report
  20134. "emacs-orgmode@gnu.org"
  20135. (org-version nil 'full)
  20136. (let (list)
  20137. (save-window-excursion
  20138. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  20139. (delete-other-windows)
  20140. (erase-buffer)
  20141. (insert "You are about to submit a bug report to the Org-mode mailing list.
  20142. We would like to add your full Org-mode and Outline configuration to the
  20143. bug report. This greatly simplifies the work of the maintainer and
  20144. other experts on the mailing list.
  20145. HOWEVER, some variables you have customized may contain private
  20146. information. The names of customers, colleagues, or friends, might
  20147. appear in the form of file names, tags, todo states, or search strings.
  20148. If you answer yes to the prompt, you might want to check and remove
  20149. such private information before sending the email.")
  20150. (add-text-properties (point-min) (point-max) '(face org-warning))
  20151. (when (yes-or-no-p "Include your Org-mode configuration ")
  20152. (mapatoms
  20153. (lambda (v)
  20154. (and (boundp v)
  20155. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  20156. (or (and (symbol-value v)
  20157. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  20158. (and
  20159. (get v 'custom-type) (get v 'standard-value)
  20160. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  20161. (push v list)))))
  20162. (kill-buffer (get-buffer "*Warn about privacy*"))
  20163. list))
  20164. nil nil
  20165. "Remember to cover the basics, that is, what you expected to happen and
  20166. what in fact did happen. You don't know how to make a good report? See
  20167. http://orgmode.org/manual/Feedback.html#Feedback
  20168. Your bug report will be posted to the Org-mode mailing list.
  20169. ------------------------------------------------------------------------")
  20170. (save-excursion
  20171. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  20172. (replace-match "\\1Bug: \\3 [\\2]")))))
  20173. (defun org-install-agenda-files-menu ()
  20174. (let ((bl (buffer-list)))
  20175. (save-excursion
  20176. (while bl
  20177. (set-buffer (pop bl))
  20178. (if (derived-mode-p 'org-mode) (setq bl nil)))
  20179. (when (derived-mode-p 'org-mode)
  20180. (easy-menu-change
  20181. '("Org") "File List for Agenda"
  20182. (append
  20183. (list
  20184. ["Edit File List" (org-edit-agenda-file-list) t]
  20185. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  20186. ["Remove Current File from List" org-remove-file t]
  20187. ["Cycle through agenda files" org-cycle-agenda-files t]
  20188. ["Occur in all agenda files" org-occur-in-agenda-files t]
  20189. "--")
  20190. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  20191. ;;;; Documentation
  20192. (defun org-require-autoloaded-modules ()
  20193. (interactive)
  20194. (mapc 'require
  20195. '(org-agenda org-archive org-attach org-clock org-colview org-id
  20196. org-table org-timer)))
  20197. ;;;###autoload
  20198. (defun org-reload (&optional uncompiled)
  20199. "Reload all org lisp files.
  20200. With prefix arg UNCOMPILED, load the uncompiled versions."
  20201. (interactive "P")
  20202. (require 'loadhist)
  20203. (let* ((org-dir (org-find-library-dir "org"))
  20204. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  20205. (feature-re "^\\(org\\|ob\\|ox\\)\\(-.*\\)?")
  20206. (remove-re (mapconcat 'identity
  20207. (mapcar (lambda (f) (concat "^" f "$"))
  20208. (list (if (featurep 'xemacs)
  20209. "org-colview"
  20210. "org-colview-xemacs")
  20211. "org" "org-loaddefs" "org-version"))
  20212. "\\|"))
  20213. (feats (delete-dups
  20214. (mapcar 'file-name-sans-extension
  20215. (mapcar 'file-name-nondirectory
  20216. (delq nil
  20217. (mapcar 'feature-file
  20218. features))))))
  20219. (lfeat (append
  20220. (sort
  20221. (setq feats
  20222. (delq nil (mapcar
  20223. (lambda (f)
  20224. (if (and (string-match feature-re f)
  20225. (not (string-match remove-re f)))
  20226. f nil))
  20227. feats)))
  20228. 'string-lessp)
  20229. (list "org-version" "org")))
  20230. (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  20231. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  20232. load-uncore load-misses)
  20233. (setq load-misses
  20234. (delq 't
  20235. (mapcar (lambda (f)
  20236. (or (org-load-noerror-mustsuffix (concat org-dir f))
  20237. (and (string= org-dir contrib-dir)
  20238. (org-load-noerror-mustsuffix (concat contrib-dir f)))
  20239. (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
  20240. (add-to-list 'load-uncore f 'append)
  20241. 't)
  20242. f))
  20243. lfeat)))
  20244. (if load-uncore
  20245. (message "The following feature%s found in load-path, please check if that's correct:\n%s"
  20246. (if (> (length load-uncore) 1) "s were" " was") load-uncore))
  20247. (if load-misses
  20248. (message "Some error occurred while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
  20249. (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
  20250. (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
  20251. ;;;###autoload
  20252. (defun org-customize ()
  20253. "Call the customize function with org as argument."
  20254. (interactive)
  20255. (org-load-modules-maybe)
  20256. (org-require-autoloaded-modules)
  20257. (customize-browse 'org))
  20258. (defun org-create-customize-menu ()
  20259. "Create a full customization menu for Org-mode, insert it into the menu."
  20260. (interactive)
  20261. (org-load-modules-maybe)
  20262. (org-require-autoloaded-modules)
  20263. (if (fboundp 'customize-menu-create)
  20264. (progn
  20265. (easy-menu-change
  20266. '("Org") "Customize"
  20267. `(["Browse Org group" org-customize t]
  20268. "--"
  20269. ,(customize-menu-create 'org)
  20270. ["Set" Custom-set t]
  20271. ["Save" Custom-save t]
  20272. ["Reset to Current" Custom-reset-current t]
  20273. ["Reset to Saved" Custom-reset-saved t]
  20274. ["Reset to Standard Settings" Custom-reset-standard t]))
  20275. (message "\"Org\"-menu now contains full customization menu"))
  20276. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  20277. ;;;; Miscellaneous stuff
  20278. ;;; Generally useful functions
  20279. (defsubst org-get-at-eol (property n)
  20280. "Get text property PROPERTY at the end of line less N characters."
  20281. (get-text-property (- (point-at-eol) n) property))
  20282. (defun org-find-text-property-in-string (prop s)
  20283. "Return the first non-nil value of property PROP in string S."
  20284. (or (get-text-property 0 prop s)
  20285. (get-text-property (or (next-single-property-change 0 prop s) 0)
  20286. prop s)))
  20287. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  20288. "Display the given MESSAGE as a warning."
  20289. (if (fboundp 'display-warning)
  20290. (display-warning 'org message
  20291. (if (featurep 'xemacs) 'warning :warning))
  20292. (let ((buf (get-buffer-create "*Org warnings*")))
  20293. (with-current-buffer buf
  20294. (goto-char (point-max))
  20295. (insert "Warning (Org): " message)
  20296. (unless (bolp)
  20297. (newline)))
  20298. (display-buffer buf)
  20299. (sit-for 0))))
  20300. (defun org-eval (form)
  20301. "Eval FORM and return result."
  20302. (condition-case error
  20303. (eval form)
  20304. (error (format "%%![Error: %s]" error))))
  20305. (defun org-in-clocktable-p ()
  20306. "Check if the cursor is in a clocktable."
  20307. (let ((pos (point)) start)
  20308. (save-excursion
  20309. (end-of-line 1)
  20310. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  20311. (setq start (match-beginning 0))
  20312. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  20313. (>= (match-end 0) pos)
  20314. start))))
  20315. (defun org-in-verbatim-emphasis ()
  20316. (save-match-data
  20317. (and (org-in-regexp org-emph-re 2)
  20318. (>= (point) (match-beginning 3))
  20319. (<= (point) (match-end 4))
  20320. (member (match-string 3) '("=" "~")))))
  20321. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  20322. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  20323. (if (and marker (marker-buffer marker)
  20324. (buffer-live-p (marker-buffer marker)))
  20325. (progn
  20326. (org-pop-to-buffer-same-window (marker-buffer marker))
  20327. (if (or (> marker (point-max)) (< marker (point-min)))
  20328. (widen))
  20329. (goto-char marker)
  20330. (org-show-context 'org-goto))
  20331. (if bookmark
  20332. (bookmark-jump bookmark)
  20333. (error "Cannot find location"))))
  20334. (defun org-quote-csv-field (s)
  20335. "Quote field for inclusion in CSV material."
  20336. (if (string-match "[\",]" s)
  20337. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  20338. s))
  20339. (defun org-force-self-insert (N)
  20340. "Needed to enforce self-insert under remapping."
  20341. (interactive "p")
  20342. (self-insert-command N))
  20343. (defun org-string-width (s)
  20344. "Compute width of string, ignoring invisible characters.
  20345. This ignores character with invisibility property `org-link', and also
  20346. characters with property `org-cwidth', because these will become invisible
  20347. upon the next fontification round."
  20348. (let (b l)
  20349. (when (or (eq t buffer-invisibility-spec)
  20350. (assq 'org-link buffer-invisibility-spec))
  20351. (while (setq b (text-property-any 0 (length s)
  20352. 'invisible 'org-link s))
  20353. (setq s (concat (substring s 0 b)
  20354. (substring s (or (next-single-property-change
  20355. b 'invisible s)
  20356. (length s)))))))
  20357. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  20358. (setq s (concat (substring s 0 b)
  20359. (substring s (or (next-single-property-change
  20360. b 'org-cwidth s)
  20361. (length s))))))
  20362. (setq l (string-width s) b -1)
  20363. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  20364. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  20365. l))
  20366. (defun org-shorten-string (s maxlength)
  20367. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  20368. If the string is shorter or has length MAXLENGTH, just return the
  20369. original string. If it is longer, the functions finds a space in the
  20370. string, breaks this string off at that locations and adds three dots
  20371. as ellipsis. Including the ellipsis, the string will not be longer
  20372. than MAXLENGTH. If finding a good breaking point in the string does
  20373. not work, the string is just chopped off in the middle of a word
  20374. if necessary."
  20375. (if (<= (length s) maxlength)
  20376. s
  20377. (let* ((n (max (- maxlength 4) 1))
  20378. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  20379. (if (string-match re s)
  20380. (concat (match-string 1 s) "...")
  20381. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  20382. (defun org-get-indentation (&optional line)
  20383. "Get the indentation of the current line, interpreting tabs.
  20384. When LINE is given, assume it represents a line and compute its indentation."
  20385. (if line
  20386. (if (string-match "^ *" (org-remove-tabs line))
  20387. (match-end 0))
  20388. (save-excursion
  20389. (beginning-of-line 1)
  20390. (skip-chars-forward " \t")
  20391. (current-column))))
  20392. (defun org-get-string-indentation (s)
  20393. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  20394. (let ((n -1) (i 0) (w tab-width) c)
  20395. (catch 'exit
  20396. (while (< (setq n (1+ n)) (length s))
  20397. (setq c (aref s n))
  20398. (cond ((= c ?\ ) (setq i (1+ i)))
  20399. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  20400. (t (throw 'exit t)))))
  20401. i))
  20402. (defun org-remove-tabs (s &optional width)
  20403. "Replace tabulators in S with spaces.
  20404. Assumes that s is a single line, starting in column 0."
  20405. (setq width (or width tab-width))
  20406. (while (string-match "\t" s)
  20407. (setq s (replace-match
  20408. (make-string
  20409. (- (* width (/ (+ (match-beginning 0) width) width))
  20410. (match-beginning 0)) ?\ )
  20411. t t s)))
  20412. s)
  20413. (defun org-fix-indentation (line ind)
  20414. "Fix indentation in LINE.
  20415. IND is a cons cell with target and minimum indentation.
  20416. If the current indentation in LINE is smaller than the minimum,
  20417. leave it alone. If it is larger than ind, set it to the target."
  20418. (let* ((l (org-remove-tabs line))
  20419. (i (org-get-indentation l))
  20420. (i1 (car ind)) (i2 (cdr ind)))
  20421. (if (>= i i2) (setq l (substring line i2)))
  20422. (if (> i1 0)
  20423. (concat (make-string i1 ?\ ) l)
  20424. l)))
  20425. (defun org-remove-indentation (code &optional n)
  20426. "Remove the maximum common indentation from the lines in CODE.
  20427. N may optionally be the number of spaces to remove."
  20428. (with-temp-buffer
  20429. (insert code)
  20430. (org-do-remove-indentation n)
  20431. (buffer-string)))
  20432. (defun org-do-remove-indentation (&optional n)
  20433. "Remove the maximum common indentation from the buffer."
  20434. (untabify (point-min) (point-max))
  20435. (let ((min 10000) re)
  20436. (if n
  20437. (setq min n)
  20438. (goto-char (point-min))
  20439. (while (re-search-forward "^ *[^ \n]" nil t)
  20440. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  20441. (unless (or (= min 0) (= min 10000))
  20442. (setq re (format "^ \\{%d\\}" min))
  20443. (goto-char (point-min))
  20444. (while (re-search-forward re nil t)
  20445. (replace-match "")
  20446. (end-of-line 1))
  20447. min)))
  20448. (defun org-fill-template (template alist)
  20449. "Find each %key of ALIST in TEMPLATE and replace it."
  20450. (let ((case-fold-search nil))
  20451. (dolist (entry (sort (copy-sequence alist)
  20452. (lambda (a b) (< (length (car a)) (length (car b))))))
  20453. (setq template
  20454. (replace-regexp-in-string
  20455. (concat "%" (regexp-quote (car entry)))
  20456. (or (cdr entry) "") template t t)))
  20457. template))
  20458. (defun org-base-buffer (buffer)
  20459. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  20460. (if (not buffer)
  20461. buffer
  20462. (or (buffer-base-buffer buffer)
  20463. buffer)))
  20464. (defun org-wrap (string &optional width lines)
  20465. "Wrap string to either a number of lines, or a width in characters.
  20466. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  20467. that costs. If there is a word longer than WIDTH, the text is actually
  20468. wrapped to the length of that word.
  20469. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  20470. many lines, whatever width that takes.
  20471. The return value is a list of lines, without newlines at the end."
  20472. (let* ((words (org-split-string string "[ \t\n]+"))
  20473. (maxword (apply 'max (mapcar 'org-string-width words)))
  20474. w ll)
  20475. (cond (width
  20476. (org-do-wrap words (max maxword width)))
  20477. (lines
  20478. (setq w maxword)
  20479. (setq ll (org-do-wrap words maxword))
  20480. (if (<= (length ll) lines)
  20481. ll
  20482. (setq ll words)
  20483. (while (> (length ll) lines)
  20484. (setq w (1+ w))
  20485. (setq ll (org-do-wrap words w)))
  20486. ll))
  20487. (t (error "Cannot wrap this")))))
  20488. (defun org-do-wrap (words width)
  20489. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  20490. (let (lines line)
  20491. (while words
  20492. (setq line (pop words))
  20493. (while (and words (< (+ (length line) (length (car words))) width))
  20494. (setq line (concat line " " (pop words))))
  20495. (setq lines (push line lines)))
  20496. (nreverse lines)))
  20497. (defun org-split-string (string &optional separators)
  20498. "Splits STRING into substrings at SEPARATORS.
  20499. SEPARATORS is a regular expression.
  20500. No empty strings are returned if there are matches at the beginning
  20501. and end of string."
  20502. ;; FIXME: why not use (split-string STRING SEPARATORS t)?
  20503. (let ((start 0) notfirst list)
  20504. (while (and (string-match (or separators "[ \f\t\n\r\v]+") string
  20505. (if (and notfirst
  20506. (= start (match-beginning 0))
  20507. (< start (length string)))
  20508. (1+ start) start))
  20509. (< (match-beginning 0) (length string)))
  20510. (setq notfirst t)
  20511. (or (eq (match-beginning 0) 0)
  20512. (and (eq (match-beginning 0) (match-end 0))
  20513. (eq (match-beginning 0) start))
  20514. (push (substring string start (match-beginning 0)) list))
  20515. (setq start (match-end 0)))
  20516. (or (eq start (length string))
  20517. (push (substring string start) list))
  20518. (nreverse list)))
  20519. (defun org-quote-vert (s)
  20520. "Replace \"|\" with \"\\vert\"."
  20521. (while (string-match "|" s)
  20522. (setq s (replace-match "\\vert" t t s)))
  20523. s)
  20524. (defun org-uuidgen-p (s)
  20525. "Is S an ID created by UUIDGEN?"
  20526. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  20527. (defun org-in-src-block-p (&optional inside)
  20528. "Whether point is in a code source block.
  20529. When INSIDE is non-nil, don't consider we are within a src block
  20530. when point is at #+BEGIN_SRC or #+END_SRC."
  20531. (let ((case-fold-search t) ov)
  20532. (or (and (eq (get-char-property (point) 'src-block) t))
  20533. (and (not inside)
  20534. (save-match-data
  20535. (save-excursion
  20536. (beginning-of-line)
  20537. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  20538. (defun org-context ()
  20539. "Return a list of contexts of the current cursor position.
  20540. If several contexts apply, all are returned.
  20541. Each context entry is a list with a symbol naming the context, and
  20542. two positions indicating start and end of the context. Possible
  20543. contexts are:
  20544. :headline anywhere in a headline
  20545. :headline-stars on the leading stars in a headline
  20546. :todo-keyword on a TODO keyword (including DONE) in a headline
  20547. :tags on the TAGS in a headline
  20548. :priority on the priority cookie in a headline
  20549. :item on the first line of a plain list item
  20550. :item-bullet on the bullet/number of a plain list item
  20551. :checkbox on the checkbox in a plain list item
  20552. :table in an org-mode table
  20553. :table-special on a special filed in a table
  20554. :table-table in a table.el table
  20555. :clocktable in a clocktable
  20556. :src-block in a source block
  20557. :link on a hyperlink
  20558. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT.
  20559. :target on a <<target>>
  20560. :radio-target on a <<<radio-target>>>
  20561. :latex-fragment on a LaTeX fragment
  20562. :latex-preview on a LaTeX fragment with overlaid preview image
  20563. This function expects the position to be visible because it uses font-lock
  20564. faces as a help to recognize the following contexts: :table-special, :link,
  20565. and :keyword."
  20566. (let* ((f (get-text-property (point) 'face))
  20567. (faces (if (listp f) f (list f)))
  20568. (case-fold-search t)
  20569. (p (point)) clist o)
  20570. ;; First the large context
  20571. (cond
  20572. ((org-at-heading-p t)
  20573. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  20574. (when (progn
  20575. (beginning-of-line 1)
  20576. (looking-at org-todo-line-tags-regexp))
  20577. (push (org-point-in-group p 1 :headline-stars) clist)
  20578. (push (org-point-in-group p 2 :todo-keyword) clist)
  20579. (push (org-point-in-group p 4 :tags) clist))
  20580. (goto-char p)
  20581. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  20582. (if (looking-at "\\[#[A-Z0-9]\\]")
  20583. (push (org-point-in-group p 0 :priority) clist)))
  20584. ((org-at-item-p)
  20585. (push (org-point-in-group p 2 :item-bullet) clist)
  20586. (push (list :item (point-at-bol)
  20587. (save-excursion (org-end-of-item) (point)))
  20588. clist)
  20589. (and (org-at-item-checkbox-p)
  20590. (push (org-point-in-group p 0 :checkbox) clist)))
  20591. ((org-at-table-p)
  20592. (push (list :table (org-table-begin) (org-table-end)) clist)
  20593. (if (memq 'org-formula faces)
  20594. (push (list :table-special
  20595. (previous-single-property-change p 'face)
  20596. (next-single-property-change p 'face)) clist)))
  20597. ((org-at-table-p 'any)
  20598. (push (list :table-table) clist)))
  20599. (goto-char p)
  20600. (let ((case-fold-search t))
  20601. ;; New the "medium" contexts: clocktables, source blocks
  20602. (cond ((org-in-clocktable-p)
  20603. (push (list :clocktable
  20604. (and (or (looking-at "[ \t]*\\(#\\+BEGIN: clocktable\\)")
  20605. (re-search-backward "[ \t]*\\(#+BEGIN: clocktable\\)" nil t))
  20606. (match-beginning 1))
  20607. (and (re-search-forward "[ \t]*#\\+END:?" nil t)
  20608. (match-end 0))) clist))
  20609. ((org-in-src-block-p)
  20610. (push (list :src-block
  20611. (and (or (looking-at "[ \t]*\\(#\\+BEGIN_SRC\\)")
  20612. (re-search-backward "[ \t]*\\(#+BEGIN_SRC\\)" nil t))
  20613. (match-beginning 1))
  20614. (and (search-forward "#+END_SRC" nil t)
  20615. (match-beginning 0))) clist))))
  20616. (goto-char p)
  20617. ;; Now the small context
  20618. (cond
  20619. ((org-at-timestamp-p)
  20620. (push (org-point-in-group p 0 :timestamp) clist))
  20621. ((memq 'org-link faces)
  20622. (push (list :link
  20623. (previous-single-property-change p 'face)
  20624. (next-single-property-change p 'face)) clist))
  20625. ((memq 'org-special-keyword faces)
  20626. (push (list :keyword
  20627. (previous-single-property-change p 'face)
  20628. (next-single-property-change p 'face)) clist))
  20629. ((org-at-target-p)
  20630. (push (org-point-in-group p 0 :target) clist)
  20631. (goto-char (1- (match-beginning 0)))
  20632. (if (looking-at org-radio-target-regexp)
  20633. (push (org-point-in-group p 0 :radio-target) clist))
  20634. (goto-char p))
  20635. ((setq o (car (delq nil
  20636. (mapcar
  20637. (lambda (x)
  20638. (if (memq x org-latex-fragment-image-overlays) x))
  20639. (overlays-at (point))))))
  20640. (push (list :latex-fragment
  20641. (overlay-start o) (overlay-end o)) clist)
  20642. (push (list :latex-preview
  20643. (overlay-start o) (overlay-end o)) clist))
  20644. ((org-inside-LaTeX-fragment-p)
  20645. ;; FIXME: positions wrong.
  20646. (push (list :latex-fragment (point) (point)) clist)))
  20647. (setq clist (nreverse (delq nil clist)))
  20648. clist))
  20649. (defun org-in-regexp (re &optional nlines visually)
  20650. "Check if point is inside a match of RE.
  20651. Normally only the current line is checked, but you can include
  20652. NLINES extra lines after point into the search. If VISUALLY is
  20653. set, require that the cursor is not after the match but really
  20654. on, so that the block visually is on the match."
  20655. (catch 'exit
  20656. (let ((pos (point))
  20657. (eol (point-at-eol (+ 1 (or nlines 0))))
  20658. (inc (if visually 1 0)))
  20659. (save-excursion
  20660. (beginning-of-line (- 1 (or nlines 0)))
  20661. (while (re-search-forward re eol t)
  20662. (if (and (<= (match-beginning 0) pos)
  20663. (>= (+ inc (match-end 0)) pos))
  20664. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  20665. (define-obsolete-function-alias 'org-at-regexp-p 'org-in-regexp
  20666. "Org mode 8.3")
  20667. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  20668. "Non-nil when point is between matches of START-RE and END-RE.
  20669. Also return a non-nil value when point is on one of the matches.
  20670. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  20671. buffer positions. Default values are the positions of headlines
  20672. surrounding the point.
  20673. The functions returns a cons cell whose car (resp. cdr) is the
  20674. position before START-RE (resp. after END-RE)."
  20675. (save-match-data
  20676. (let ((pos (point))
  20677. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  20678. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  20679. beg end)
  20680. (save-excursion
  20681. ;; Point is on a block when on START-RE or if START-RE can be
  20682. ;; found before it...
  20683. (and (or (org-in-regexp start-re)
  20684. (re-search-backward start-re limit-up t))
  20685. (setq beg (match-beginning 0))
  20686. ;; ... and END-RE after it...
  20687. (goto-char (match-end 0))
  20688. (re-search-forward end-re limit-down t)
  20689. (> (setq end (match-end 0)) pos)
  20690. ;; ... without another START-RE in-between.
  20691. (goto-char (match-beginning 0))
  20692. (not (re-search-backward start-re (1+ beg) t))
  20693. ;; Return value.
  20694. (cons beg end))))))
  20695. (defun org-in-block-p (names)
  20696. "Non-nil when point belongs to a block whose name belongs to NAMES.
  20697. NAMES is a list of strings containing names of blocks.
  20698. Return first block name matched, or nil. Beware that in case of
  20699. nested blocks, the returned name may not belong to the closest
  20700. block from point."
  20701. (save-match-data
  20702. (catch 'exit
  20703. (let ((case-fold-search t)
  20704. (lim-up (save-excursion (outline-previous-heading)))
  20705. (lim-down (save-excursion (outline-next-heading))))
  20706. (mapc (lambda (name)
  20707. (let ((n (regexp-quote name)))
  20708. (when (org-between-regexps-p
  20709. (concat "^[ \t]*#\\+begin_" n)
  20710. (concat "^[ \t]*#\\+end_" n)
  20711. lim-up lim-down)
  20712. (throw 'exit n))))
  20713. names))
  20714. nil)))
  20715. (defun org-occur-in-agenda-files (regexp &optional _nlines)
  20716. "Call `multi-occur' with buffers for all agenda files."
  20717. (interactive "sOrg-files matching: ")
  20718. (let* ((files (org-agenda-files))
  20719. (tnames (mapcar #'file-truename files))
  20720. (extra org-agenda-text-search-extra-files))
  20721. (when (eq (car extra) 'agenda-archives)
  20722. (setq extra (cdr extra))
  20723. (setq files (org-add-archive-files files)))
  20724. (dolist (f extra)
  20725. (unless (member (file-truename f) tnames)
  20726. (unless (member f files) (setq files (append files (list f))))
  20727. (setq tnames (append tnames (list (file-truename f))))))
  20728. (multi-occur
  20729. (mapcar (lambda (x)
  20730. (with-current-buffer
  20731. ;; FIXME: Why not just (find-file-noselect x)?
  20732. ;; Is it to avoid the "revert buffer" prompt?
  20733. (or (get-file-buffer x) (find-file-noselect x))
  20734. (widen)
  20735. (current-buffer)))
  20736. files)
  20737. regexp)))
  20738. (if (boundp 'occur-mode-find-occurrence-hook)
  20739. ;; Emacs 23
  20740. (add-hook 'occur-mode-find-occurrence-hook
  20741. (lambda ()
  20742. (when (derived-mode-p 'org-mode)
  20743. (org-reveal))))
  20744. ;; Emacs 22
  20745. (defadvice occur-mode-goto-occurrence
  20746. (after org-occur-reveal activate)
  20747. (and (derived-mode-p 'org-mode) (org-reveal)))
  20748. (defadvice occur-mode-goto-occurrence-other-window
  20749. (after org-occur-reveal activate)
  20750. (and (derived-mode-p 'org-mode) (org-reveal)))
  20751. (defadvice occur-mode-display-occurrence
  20752. (after org-occur-reveal activate)
  20753. (when (derived-mode-p 'org-mode)
  20754. (let ((pos (occur-mode-find-occurrence)))
  20755. (with-current-buffer (marker-buffer pos)
  20756. (save-excursion
  20757. (goto-char pos)
  20758. (org-reveal)))))))
  20759. (defun org-occur-link-in-agenda-files ()
  20760. "Create a link and search for it in the agendas.
  20761. The link is not stored in `org-stored-links', it is just created
  20762. for the search purpose."
  20763. (interactive)
  20764. (let ((link (condition-case nil
  20765. (org-store-link nil)
  20766. (error "Unable to create a link to here"))))
  20767. (org-occur-in-agenda-files (regexp-quote link))))
  20768. (defun org-reverse-string (string)
  20769. "Return the reverse of STRING."
  20770. (apply 'string (reverse (string-to-list string))))
  20771. ;; defsubst org-uniquify must be defined before first use
  20772. (defun org-uniquify-alist (alist)
  20773. "Merge elements of ALIST with the same key.
  20774. For example, in this alist:
  20775. \(org-uniquify-alist '((a 1) (b 2) (a 3)))
  20776. => '((a 1 3) (b 2))
  20777. merge (a 1) and (a 3) into (a 1 3).
  20778. The function returns the new ALIST."
  20779. (let (rtn)
  20780. (mapc
  20781. (lambda (e)
  20782. (let (n)
  20783. (if (not (assoc (car e) rtn))
  20784. (push e rtn)
  20785. (setq n (cons (car e) (append (cdr (assoc (car e) rtn)) (cdr e))))
  20786. (setq rtn (assq-delete-all (car e) rtn))
  20787. (push n rtn))))
  20788. alist)
  20789. rtn))
  20790. (defun org-delete-all (elts list)
  20791. "Remove all elements in ELTS from LIST."
  20792. (while elts
  20793. (setq list (delete (pop elts) list)))
  20794. list)
  20795. (defun org-count (cl-item cl-seq)
  20796. "Count the number of occurrences of ITEM in SEQ.
  20797. Taken from `count' in cl-seq.el with all keyword arguments removed."
  20798. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  20799. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  20800. (while (< cl-start cl-end)
  20801. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  20802. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  20803. (setq cl-start (1+ cl-start)))
  20804. cl-count))
  20805. (defun org-remove-if (predicate seq)
  20806. "Remove everything from SEQ that fulfills PREDICATE."
  20807. (let (res e)
  20808. (while seq
  20809. (setq e (pop seq))
  20810. (if (not (funcall predicate e)) (push e res)))
  20811. (nreverse res)))
  20812. (defun org-remove-if-not (predicate seq)
  20813. "Remove everything from SEQ that does not fulfill PREDICATE."
  20814. (let (res e)
  20815. (while seq
  20816. (setq e (pop seq))
  20817. (if (funcall predicate e) (push e res)))
  20818. (nreverse res)))
  20819. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  20820. "Reduce two-argument FUNCTION across SEQ.
  20821. Taken from `reduce' in cl-seq.el with all keyword arguments but
  20822. \":initial-value\" removed."
  20823. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  20824. (cadr (memq :initial-value cl-keys)))
  20825. (cl-seq (pop cl-seq))
  20826. (t (funcall cl-func)))))
  20827. (while cl-seq
  20828. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  20829. cl-accum))
  20830. (defun org-every (pred seq)
  20831. "Return true if PREDICATE is true of every element of SEQ.
  20832. Adapted from `every' in cl.el."
  20833. (catch 'org-every
  20834. (mapc (lambda (e) (unless (funcall pred e) (throw 'org-every nil))) seq)
  20835. t))
  20836. (defun org-some (pred seq)
  20837. "Return true if PREDICATE is true of any element of SEQ.
  20838. Adapted from `some' in cl.el."
  20839. (catch 'org-some
  20840. (mapc (lambda (e) (when (funcall pred e) (throw 'org-some t))) seq)
  20841. nil))
  20842. (defun org-back-over-empty-lines ()
  20843. "Move backwards over whitespace, to the beginning of the first empty line.
  20844. Returns the number of empty lines passed."
  20845. (let ((pos (point)))
  20846. (if (cdr (assoc 'heading org-blank-before-new-entry))
  20847. (skip-chars-backward " \t\n\r")
  20848. (unless (eobp)
  20849. (forward-line -1)))
  20850. (beginning-of-line 2)
  20851. (goto-char (min (point) pos))
  20852. (count-lines (point) pos)))
  20853. (defun org-skip-whitespace ()
  20854. (skip-chars-forward " \t\n\r"))
  20855. (defun org-point-in-group (point group &optional context)
  20856. "Check if POINT is in match-group GROUP.
  20857. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  20858. match. If the match group does not exist or point is not inside it,
  20859. return nil."
  20860. (and (match-beginning group)
  20861. (>= point (match-beginning group))
  20862. (<= point (match-end group))
  20863. (if context
  20864. (list context (match-beginning group) (match-end group))
  20865. t)))
  20866. (defun org-switch-to-buffer-other-window (&rest args)
  20867. "Switch to buffer in a second window on the current frame.
  20868. In particular, do not allow pop-up frames.
  20869. Returns the newly created buffer."
  20870. (org-no-popups
  20871. (apply 'switch-to-buffer-other-window args)))
  20872. (defun org-combine-plists (&rest plists)
  20873. "Create a single property list from all plists in PLISTS.
  20874. The process starts by copying the first list, and then setting properties
  20875. from the other lists. Settings in the last list are the most significant
  20876. ones and overrule settings in the other lists."
  20877. (let ((rtn (copy-sequence (pop plists)))
  20878. p v ls)
  20879. (while plists
  20880. (setq ls (pop plists))
  20881. (while ls
  20882. (setq p (pop ls) v (pop ls))
  20883. (setq rtn (plist-put rtn p v))))
  20884. rtn))
  20885. (defun org-replace-escapes (string table)
  20886. "Replace %-escapes in STRING with values in TABLE.
  20887. TABLE is an association list with keys like \"%a\" and string values.
  20888. The sequences in STRING may contain normal field width and padding information,
  20889. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  20890. so values can contain further %-escapes if they are define later in TABLE."
  20891. (let ((tbl (copy-alist table))
  20892. (case-fold-search nil)
  20893. (pchg 0)
  20894. e re rpl)
  20895. (dolist (e tbl)
  20896. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  20897. (when (and (cdr e) (string-match re (cdr e)))
  20898. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  20899. (safe "SREF"))
  20900. (add-text-properties 0 3 (list 'sref sref) safe)
  20901. (setcdr e (replace-match safe t t (cdr e)))))
  20902. (while (string-match re string)
  20903. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  20904. (cdr e)))
  20905. (setq string (replace-match rpl t t string))))
  20906. (while (setq pchg (next-property-change pchg string))
  20907. (let ((sref (get-text-property pchg 'sref string)))
  20908. (when (and sref (string-match "SREF" string pchg))
  20909. (setq string (replace-match sref t t string)))))
  20910. string))
  20911. (defun org-sublist (list start end)
  20912. "Return a section of LIST, from START to END.
  20913. Counting starts at 1."
  20914. (let (rtn (c start))
  20915. (setq list (nthcdr (1- start) list))
  20916. (while (and list (<= c end))
  20917. (push (pop list) rtn)
  20918. (setq c (1+ c)))
  20919. (nreverse rtn)))
  20920. (defun org-find-base-buffer-visiting (file)
  20921. "Like `find-buffer-visiting' but always return the base buffer and
  20922. not an indirect buffer."
  20923. (let ((buf (or (get-file-buffer file)
  20924. (find-buffer-visiting file))))
  20925. (if buf
  20926. (or (buffer-base-buffer buf) buf)
  20927. nil)))
  20928. (defun org-image-file-name-regexp (&optional extensions)
  20929. "Return regexp matching the file names of images.
  20930. If EXTENSIONS is given, only match these."
  20931. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  20932. (image-file-name-regexp)
  20933. (let ((image-file-name-extensions
  20934. (or extensions
  20935. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  20936. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  20937. (concat "\\."
  20938. (regexp-opt (nconc (mapcar 'upcase
  20939. image-file-name-extensions)
  20940. image-file-name-extensions)
  20941. t)
  20942. "\\'"))))
  20943. (defun org-file-image-p (file &optional extensions)
  20944. "Return non-nil if FILE is an image."
  20945. (save-match-data
  20946. (string-match (org-image-file-name-regexp extensions) file)))
  20947. (defun org-get-cursor-date (&optional with-time)
  20948. "Return the date at cursor in as a time.
  20949. This works in the calendar and in the agenda, anywhere else it just
  20950. returns the current time.
  20951. If WITH-TIME is non-nil, returns the time of the event at point (in
  20952. the agenda) or the current time of the day."
  20953. (let (date day defd tp hod mod)
  20954. (when with-time
  20955. (setq tp (get-text-property (point) 'time))
  20956. (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
  20957. (setq hod (string-to-number (match-string 1 tp))
  20958. mod (string-to-number (match-string 2 tp))))
  20959. (or tp (let ((now (decode-time)))
  20960. (setq hod (nth 2 now)
  20961. mod (nth 1 now)))))
  20962. (cond
  20963. ((eq major-mode 'calendar-mode)
  20964. (setq date (calendar-cursor-to-date)
  20965. defd (encode-time 0 (or mod 0) (or hod 0)
  20966. (nth 1 date) (nth 0 date) (nth 2 date))))
  20967. ((eq major-mode 'org-agenda-mode)
  20968. (setq day (get-text-property (point) 'day))
  20969. (if day
  20970. (setq date (calendar-gregorian-from-absolute day)
  20971. defd (encode-time 0 (or mod 0) (or hod 0)
  20972. (nth 1 date) (nth 0 date) (nth 2 date))))))
  20973. (or defd (current-time))))
  20974. (defun org-mark-subtree (&optional up)
  20975. "Mark the current subtree.
  20976. This puts point at the start of the current subtree, and mark at
  20977. the end. If a numeric prefix UP is given, move up into the
  20978. hierarchy of headlines by UP levels before marking the subtree."
  20979. (interactive "P")
  20980. (org-with-limited-levels
  20981. (cond ((org-at-heading-p) (beginning-of-line))
  20982. ((org-before-first-heading-p) (user-error "Not in a subtree"))
  20983. (t (outline-previous-visible-heading 1))))
  20984. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  20985. (if (org-called-interactively-p 'any)
  20986. (call-interactively 'org-mark-element)
  20987. (org-mark-element)))
  20988. ;;; Indentation
  20989. (defun org--get-expected-indentation (element contentsp)
  20990. "Expected indentation column for current line, according to ELEMENT.
  20991. ELEMENT is an element containing point. CONTENTSP is non-nil
  20992. when indentation is to be computed according to contents of
  20993. ELEMENT."
  20994. (let ((type (org-element-type element))
  20995. (start (org-element-property :begin element))
  20996. (post-affiliated (org-element-property :post-affiliated element)))
  20997. (org-with-wide-buffer
  20998. (cond
  20999. (contentsp
  21000. (case type
  21001. ((diary-sexp footnote-definition) 0)
  21002. ((headline inlinetask nil)
  21003. (if (not org-adapt-indentation) 0
  21004. (let ((level (org-current-level)))
  21005. (if level (1+ level) 0))))
  21006. ((item plain-list) (org-list-item-body-column post-affiliated))
  21007. (t
  21008. (goto-char start)
  21009. (org-get-indentation))))
  21010. ((memq type '(headline inlinetask nil))
  21011. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  21012. (org--get-expected-indentation element t)
  21013. 0))
  21014. ((memq type '(diary-sexp footnote-definition)) 0)
  21015. ;; First paragraph of a footnote definition or an item.
  21016. ;; Indent like parent.
  21017. ((< (line-beginning-position) start)
  21018. (org--get-expected-indentation
  21019. (org-element-property :parent element) t))
  21020. ;; At first line: indent according to previous sibling, if any,
  21021. ;; ignoring footnote definitions and inline tasks, or parent's
  21022. ;; contents.
  21023. ((= (line-beginning-position) start)
  21024. (catch 'exit
  21025. (while t
  21026. (if (= (point-min) start) (throw 'exit 0)
  21027. (goto-char (1- start))
  21028. (let* ((previous (org-element-at-point))
  21029. (parent previous))
  21030. (while (and parent (<= (org-element-property :end parent) start))
  21031. (setq previous parent
  21032. parent (org-element-property :parent parent)))
  21033. (cond
  21034. ((not previous) (throw 'exit 0))
  21035. ((> (org-element-property :end previous) start)
  21036. (throw 'exit (org--get-expected-indentation previous t)))
  21037. ((memq (org-element-type previous)
  21038. '(footnote-definition inlinetask))
  21039. (setq start (org-element-property :begin previous)))
  21040. (t (goto-char (org-element-property :begin previous))
  21041. (throw 'exit
  21042. (if (bolp) (org-get-indentation)
  21043. ;; At first paragraph in an item or
  21044. ;; a footnote definition.
  21045. (org--get-expected-indentation
  21046. (org-element-property :parent previous) t))))))))))
  21047. ;; Otherwise, move to the first non-blank line above.
  21048. (t
  21049. (beginning-of-line)
  21050. (let ((pos (point)))
  21051. (skip-chars-backward " \r\t\n")
  21052. (cond
  21053. ;; Two blank lines end a footnote definition or a plain
  21054. ;; list. When we indent an empty line after them, the
  21055. ;; containing list or footnote definition is over, so it
  21056. ;; qualifies as a previous sibling. Therefore, we indent
  21057. ;; like its first line.
  21058. ((and (memq type '(footnote-definition plain-list))
  21059. (> (count-lines (point) pos) 2))
  21060. (goto-char start)
  21061. (org-get-indentation))
  21062. ;; Line above is the first one of a paragraph at the
  21063. ;; beginning of an item or a footnote definition. Indent
  21064. ;; like parent.
  21065. ((< (line-beginning-position) start)
  21066. (org--get-expected-indentation
  21067. (org-element-property :parent element) t))
  21068. ;; Line above is the beginning of an element, i.e., point
  21069. ;; was originally on the blank lines between element's start
  21070. ;; and contents.
  21071. ((= (line-beginning-position) post-affiliated)
  21072. (org--get-expected-indentation element t))
  21073. ;; POS is after contents in a greater element. Indent like
  21074. ;; the beginning of the element.
  21075. ;;
  21076. ;; As a special case, if point is at the end of a footnote
  21077. ;; definition or an item, indent like the very last element
  21078. ;; within.
  21079. ((and (not (eq type 'paragraph))
  21080. (let ((cend (org-element-property :contents-end element)))
  21081. (and cend (<= cend pos))))
  21082. (if (memq type '(footnote-definition item plain-list))
  21083. (let ((last (org-element-at-point)))
  21084. (org--get-expected-indentation
  21085. last
  21086. (memq (org-element-type last)
  21087. '(footnote-definition item plain-list))))
  21088. (goto-char start)
  21089. (org-get-indentation)))
  21090. ;; In any other case, indent like the current line.
  21091. (t (org-get-indentation)))))))))
  21092. (defun org--align-node-property ()
  21093. "Align node property at point.
  21094. Alignment is done according to `org-property-format', which see."
  21095. (when (save-excursion
  21096. (beginning-of-line)
  21097. (looking-at org-property-re))
  21098. (replace-match
  21099. (concat (match-string 4)
  21100. (org-trim
  21101. (format org-property-format (match-string 1) (match-string 3))))
  21102. t t)))
  21103. (defun org-indent-line ()
  21104. "Indent line depending on context.
  21105. Indentation is done according to the following rules:
  21106. - Footnote definitions, diary sexps, headlines and inline tasks
  21107. have to start at column 0.
  21108. - On the very first line of an element, consider, in order, the
  21109. next rules until one matches:
  21110. 1. If there's a sibling element before, ignoring footnote
  21111. definitions and inline tasks, indent like its first line.
  21112. 2. If element has a parent, indent like its contents. More
  21113. precisely, if parent is an item, indent after the
  21114. description part, if any, or the bullet (see
  21115. `org-list-description-max-indent'). Else, indent like
  21116. parent's first line.
  21117. 3. Otherwise, indent relatively to current level, if
  21118. `org-adapt-indentation' is non-nil, or to left margin.
  21119. - On a blank line at the end of an element, indent according to
  21120. the type of the element. More precisely
  21121. 1. If element is a plain list, an item, or a footnote
  21122. definition, indent like the very last element within.
  21123. 2. If element is a paragraph, indent like its last non blank
  21124. line.
  21125. 3. Otherwise, indent like its very first line.
  21126. - In the code part of a source block, use language major mode
  21127. to indent current line if `org-src-tab-acts-natively' is
  21128. non-nil. If it is nil, do nothing.
  21129. - Otherwise, indent like the first non-blank line above.
  21130. The function doesn't indent an item as it could break the whole
  21131. list structure. Instead, use \\<org-mode-map>\\[org-shiftmetaleft] or \
  21132. \\[org-shiftmetaright].
  21133. Also align node properties according to `org-property-format'."
  21134. (interactive)
  21135. (cond
  21136. (orgstruct-is-++
  21137. (let ((indent-line-function
  21138. (cadadr (assq 'indent-line-function org-fb-vars))))
  21139. (indent-according-to-mode)))
  21140. ((org-at-heading-p) 'noindent)
  21141. (t
  21142. (let* ((element (save-excursion (beginning-of-line) (org-element-at-point)))
  21143. (type (org-element-type element)))
  21144. (cond ((and (memq type '(plain-list item))
  21145. (= (line-beginning-position)
  21146. (org-element-property :post-affiliated element)))
  21147. 'noindent)
  21148. ((and (eq type 'src-block)
  21149. org-src-tab-acts-natively
  21150. (> (line-beginning-position)
  21151. (org-element-property :post-affiliated element))
  21152. (< (line-beginning-position)
  21153. (org-with-wide-buffer
  21154. (goto-char (org-element-property :end element))
  21155. (skip-chars-backward " \r\t\n")
  21156. (line-beginning-position))))
  21157. (org-babel-do-key-sequence-in-edit-buffer (kbd "TAB")))
  21158. (t
  21159. (let ((column (org--get-expected-indentation element nil)))
  21160. ;; Preserve current column.
  21161. (if (<= (current-column) (current-indentation))
  21162. (org-indent-line-to column)
  21163. (save-excursion (org-indent-line-to column))))
  21164. ;; Align node property. Also preserve current column.
  21165. (when (eq type 'node-property)
  21166. (let ((column (current-column)))
  21167. (org--align-node-property)
  21168. (org-move-to-column column)))))))))
  21169. (defun org-indent-region (start end)
  21170. "Indent each non-blank line in the region.
  21171. Called from a program, START and END specify the region to
  21172. indent. The function will not indent contents of example blocks,
  21173. verse blocks and export blocks as leading white spaces are
  21174. assumed to be significant there."
  21175. (interactive "r")
  21176. (save-excursion
  21177. (goto-char start)
  21178. (skip-chars-forward " \r\t\n")
  21179. (unless (eobp) (beginning-of-line))
  21180. (let ((indent-to
  21181. (lambda (ind pos)
  21182. ;; Set IND as indentation for all lines between point and
  21183. ;; POS or END, whichever comes first. Blank lines are
  21184. ;; ignored. Leave point after POS once done.
  21185. (let ((limit (copy-marker (min end pos))))
  21186. (while (< (point) limit)
  21187. (unless (org-looking-at-p "[ \t]*$") (org-indent-line-to ind))
  21188. (forward-line))
  21189. (set-marker limit nil))))
  21190. (end (copy-marker end)))
  21191. (while (< (point) end)
  21192. (if (or (org-looking-at-p " \r\t\n") (org-at-heading-p)) (forward-line)
  21193. (let* ((element (org-element-at-point))
  21194. (type (org-element-type element))
  21195. (element-end (copy-marker (org-element-property :end element)))
  21196. (ind (org--get-expected-indentation element nil)))
  21197. (cond
  21198. ((or (memq type '(paragraph table table-row))
  21199. (not (or (org-element-property :contents-begin element)
  21200. (memq type
  21201. '(example-block export-block src-block)))))
  21202. ;; Elements here are indented as a single block. Also
  21203. ;; align node properties.
  21204. (when (eq type 'node-property)
  21205. (org--align-node-property)
  21206. (beginning-of-line))
  21207. (funcall indent-to ind element-end))
  21208. (t
  21209. ;; Elements in this category consist of three parts:
  21210. ;; before the contents, the contents, and after the
  21211. ;; contents. The contents are treated specially,
  21212. ;; according to the element type, or not indented at
  21213. ;; all. Other parts are indented as a single block.
  21214. (let* ((post (copy-marker
  21215. (org-element-property :post-affiliated element)))
  21216. (cbeg
  21217. (copy-marker
  21218. (cond
  21219. ((not (org-element-property :contents-begin element))
  21220. ;; Fake contents for source blocks.
  21221. (org-with-wide-buffer
  21222. (goto-char post)
  21223. (forward-line)
  21224. (point)))
  21225. ((memq type '(footnote-definition item plain-list))
  21226. ;; Contents in these elements could start on
  21227. ;; the same line as the beginning of the
  21228. ;; element. Make sure we start indenting
  21229. ;; from the second line.
  21230. (org-with-wide-buffer
  21231. (goto-char post)
  21232. (end-of-line)
  21233. (skip-chars-forward " \r\t\n")
  21234. (if (eobp) (point) (line-beginning-position))))
  21235. (t (org-element-property :contents-begin element)))))
  21236. (cend (copy-marker
  21237. (or (org-element-property :contents-end element)
  21238. ;; Fake contents for source blocks.
  21239. (org-with-wide-buffer
  21240. (goto-char element-end)
  21241. (skip-chars-backward " \r\t\n")
  21242. (line-beginning-position)))
  21243. t)))
  21244. ;; Do not change items indentation individually as it
  21245. ;; might break the list as a whole. On the other
  21246. ;; hand, when at a plain list, indent it as a whole.
  21247. (cond ((eq type 'plain-list)
  21248. (let ((offset (- ind (org-get-indentation))))
  21249. (unless (zerop offset)
  21250. (indent-rigidly (org-element-property :begin element)
  21251. (org-element-property :end element)
  21252. offset))
  21253. (goto-char cbeg)))
  21254. ((eq type 'item) (goto-char cbeg))
  21255. (t (funcall indent-to ind cbeg)))
  21256. (when (< (point) end)
  21257. (case type
  21258. ((example-block export-block verse-block))
  21259. (src-block
  21260. ;; In a source block, indent source code
  21261. ;; according to language major mode, but only if
  21262. ;; `org-src-tab-acts-natively' is non-nil.
  21263. (when (and (< (point) end) org-src-tab-acts-natively)
  21264. (ignore-errors
  21265. (org-babel-do-in-edit-buffer
  21266. (indent-region (point-min) (point-max))))))
  21267. (t (org-indent-region (point) (min cend end))))
  21268. (goto-char (min cend end))
  21269. (when (< (point) end) (funcall indent-to ind element-end)))
  21270. (set-marker post nil)
  21271. (set-marker cbeg nil)
  21272. (set-marker cend nil))))
  21273. (set-marker element-end nil))))
  21274. (set-marker end nil))))
  21275. (defun org-indent-drawer ()
  21276. "Indent the drawer at point."
  21277. (interactive)
  21278. (unless (save-excursion
  21279. (beginning-of-line)
  21280. (org-looking-at-p org-drawer-regexp))
  21281. (user-error "Not at a drawer"))
  21282. (let ((element (org-element-at-point)))
  21283. (unless (memq (org-element-type element) '(drawer property-drawer))
  21284. (user-error "Not at a drawer"))
  21285. (org-with-wide-buffer
  21286. (org-indent-region (org-element-property :begin element)
  21287. (org-element-property :end element))))
  21288. (message "Drawer at point indented"))
  21289. (defun org-indent-block ()
  21290. "Indent the block at point."
  21291. (interactive)
  21292. (unless (save-excursion
  21293. (beginning-of-line)
  21294. (let ((case-fold-search t))
  21295. (org-looking-at-p "[ \t]*#\\+\\(begin\\|end\\)_")))
  21296. (user-error "Not at a block"))
  21297. (let ((element (org-element-at-point)))
  21298. (unless (memq (org-element-type element)
  21299. '(comment-block center-block dynamic-block example-block
  21300. export-block quote-block special-block
  21301. src-block verse-block))
  21302. (user-error "Not at a block"))
  21303. (org-with-wide-buffer
  21304. (org-indent-region (org-element-property :begin element)
  21305. (org-element-property :end element))))
  21306. (message "Block at point indented"))
  21307. ;;; Filling
  21308. ;; We use our own fill-paragraph and auto-fill functions.
  21309. ;; `org-fill-paragraph' relies on adaptive filling and context
  21310. ;; checking. Appropriate `fill-prefix' is computed with
  21311. ;; `org-adaptive-fill-function'.
  21312. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  21313. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  21314. ;; `org-adaptive-fill-function' value. Internally,
  21315. ;; `org-comment-line-break-function' breaks the line.
  21316. ;; `org-setup-filling' installs filling and auto-filling related
  21317. ;; variables during `org-mode' initialization.
  21318. (defvar org-element-paragraph-separate) ; org-element.el
  21319. (defun org-setup-filling ()
  21320. (require 'org-element)
  21321. ;; Prevent auto-fill from inserting unwanted new items.
  21322. (when (boundp 'fill-nobreak-predicate)
  21323. (org-set-local
  21324. 'fill-nobreak-predicate
  21325. (org-uniquify
  21326. (append fill-nobreak-predicate
  21327. '(org-fill-line-break-nobreak-p
  21328. org-fill-paragraph-with-timestamp-nobreak-p)))))
  21329. (let ((paragraph-ending (substring org-element-paragraph-separate 1)))
  21330. (org-set-local 'paragraph-start paragraph-ending)
  21331. (org-set-local 'paragraph-separate paragraph-ending))
  21332. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  21333. (org-set-local 'auto-fill-inhibit-regexp nil)
  21334. (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
  21335. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  21336. (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
  21337. (defun org-fill-line-break-nobreak-p ()
  21338. "Non-nil when a new line at point would create an Org line break."
  21339. (save-excursion
  21340. (skip-chars-backward "[ \t]")
  21341. (skip-chars-backward "\\\\")
  21342. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  21343. (defun org-fill-paragraph-with-timestamp-nobreak-p ()
  21344. "Non-nil when a new line at point would split a timestamp."
  21345. (and (org-at-timestamp-p t)
  21346. (not (looking-at org-ts-regexp-both))))
  21347. (declare-function message-in-body-p "message" ())
  21348. (defvar orgtbl-line-start-regexp) ; From org-table.el
  21349. (defun org-adaptive-fill-function ()
  21350. "Compute a fill prefix for the current line.
  21351. Return fill prefix, as a string, or nil if current line isn't
  21352. meant to be filled. For convenience, if `adaptive-fill-regexp'
  21353. matches in paragraphs or comments, use it."
  21354. (catch 'exit
  21355. (when (derived-mode-p 'message-mode)
  21356. (save-excursion
  21357. (beginning-of-line)
  21358. (cond ((not (message-in-body-p)) (throw 'exit nil))
  21359. ((org-looking-at-p org-table-line-regexp) (throw 'exit nil))
  21360. ((looking-at message-cite-prefix-regexp)
  21361. (throw 'exit (match-string-no-properties 0)))
  21362. ((looking-at org-outline-regexp)
  21363. (throw 'exit (make-string (length (match-string 0)) ?\s))))))
  21364. (org-with-wide-buffer
  21365. (unless (org-at-heading-p)
  21366. (let* ((p (line-beginning-position))
  21367. (element (save-excursion
  21368. (beginning-of-line)
  21369. (org-element-at-point)))
  21370. (type (org-element-type element))
  21371. (post-affiliated (org-element-property :post-affiliated element)))
  21372. (unless (< p post-affiliated)
  21373. (case type
  21374. (comment
  21375. (save-excursion
  21376. (beginning-of-line)
  21377. (looking-at "[ \t]*")
  21378. (concat (match-string 0) "# ")))
  21379. (footnote-definition "")
  21380. ((item plain-list)
  21381. (make-string (org-list-item-body-column post-affiliated) ?\s))
  21382. (paragraph
  21383. ;; Fill prefix is usually the same as the current line,
  21384. ;; unless the paragraph is at the beginning of an item.
  21385. (let ((parent (org-element-property :parent element)))
  21386. (save-excursion
  21387. (beginning-of-line)
  21388. (cond ((eq (org-element-type parent) 'item)
  21389. (make-string (org-list-item-body-column
  21390. (org-element-property :begin parent))
  21391. ?\s))
  21392. ((and adaptive-fill-regexp
  21393. ;; Locally disable
  21394. ;; `adaptive-fill-function' to let
  21395. ;; `fill-context-prefix' handle
  21396. ;; `adaptive-fill-regexp' variable.
  21397. (let (adaptive-fill-function)
  21398. (fill-context-prefix
  21399. post-affiliated
  21400. (org-element-property :end element)))))
  21401. ((looking-at "[ \t]+") (match-string 0))
  21402. (t "")))))
  21403. (comment-block
  21404. ;; Only fill contents if P is within block boundaries.
  21405. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  21406. (forward-line)
  21407. (point)))
  21408. (cend (save-excursion
  21409. (goto-char (org-element-property :end element))
  21410. (skip-chars-backward " \r\t\n")
  21411. (line-beginning-position))))
  21412. (when (and (>= p cbeg) (< p cend))
  21413. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  21414. (match-string 0)
  21415. "")))))))))))
  21416. (declare-function message-goto-body "message" ())
  21417. (defvar message-cite-prefix-regexp) ; From message.el
  21418. (defun org-fill-paragraph (&optional justify)
  21419. "Fill element at point, when applicable.
  21420. This function only applies to comment blocks, comments, example
  21421. blocks and paragraphs. Also, as a special case, re-align table
  21422. when point is at one.
  21423. If JUSTIFY is non-nil (interactively, with prefix argument),
  21424. justify as well. If `sentence-end-double-space' is non-nil, then
  21425. period followed by one space does not end a sentence, so don't
  21426. break a line there. The variable `fill-column' controls the
  21427. width for filling.
  21428. For convenience, when point is at a plain list, an item or
  21429. a footnote definition, try to fill the first paragraph within."
  21430. (interactive)
  21431. (if (and (derived-mode-p 'message-mode)
  21432. (or (not (message-in-body-p))
  21433. (save-excursion (move-beginning-of-line 1)
  21434. (looking-at message-cite-prefix-regexp))))
  21435. ;; First ensure filling is correct in message-mode.
  21436. (let ((fill-paragraph-function
  21437. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  21438. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  21439. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  21440. (paragraph-separate
  21441. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  21442. (fill-paragraph nil))
  21443. (with-syntax-table org-mode-transpose-word-syntax-table
  21444. ;; Move to end of line in order to get the first paragraph
  21445. ;; within a plain list or a footnote definition.
  21446. (let ((element (save-excursion
  21447. (end-of-line)
  21448. (or (ignore-errors (org-element-at-point))
  21449. (user-error "An element cannot be parsed line %d"
  21450. (line-number-at-pos (point)))))))
  21451. ;; First check if point is in a blank line at the beginning of
  21452. ;; the buffer. In that case, ignore filling.
  21453. (case (org-element-type element)
  21454. ;; Use major mode filling function is src blocks.
  21455. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  21456. ;; Align Org tables, leave table.el tables as-is.
  21457. (table-row (org-table-align) t)
  21458. (table
  21459. (when (eq (org-element-property :type element) 'org)
  21460. (save-excursion
  21461. (goto-char (org-element-property :post-affiliated element))
  21462. (org-table-align)))
  21463. t)
  21464. (paragraph
  21465. ;; Paragraphs may contain `line-break' type objects.
  21466. (let ((beg (max (point-min)
  21467. (org-element-property :contents-begin element)))
  21468. (end (min (point-max)
  21469. (org-element-property :contents-end element))))
  21470. ;; Do nothing if point is at an affiliated keyword.
  21471. (if (< (line-end-position) beg) t
  21472. (when (derived-mode-p 'message-mode)
  21473. ;; In `message-mode', do not fill following citation
  21474. ;; in current paragraph nor text before message body.
  21475. (let ((body-start (save-excursion (message-goto-body))))
  21476. (when body-start (setq beg (max body-start beg))))
  21477. (when (save-excursion
  21478. (re-search-forward
  21479. (concat "^" message-cite-prefix-regexp) end t))
  21480. (setq end (match-beginning 0))))
  21481. ;; Fill paragraph, taking line breaks into account.
  21482. (save-excursion
  21483. (goto-char beg)
  21484. (let ((cuts (list beg)))
  21485. (while (re-search-forward "\\\\\\\\[ \t]*\n" end t)
  21486. (when (eq 'line-break
  21487. (org-element-type
  21488. (save-excursion (backward-char)
  21489. (org-element-context))))
  21490. (push (point) cuts)))
  21491. (dolist (c (delq end cuts))
  21492. (fill-region-as-paragraph c end justify)
  21493. (setq end c))))
  21494. t)))
  21495. ;; Contents of `comment-block' type elements should be
  21496. ;; filled as plain text, but only if point is within block
  21497. ;; markers.
  21498. (comment-block
  21499. (let* ((case-fold-search t)
  21500. (beg (save-excursion
  21501. (goto-char (org-element-property :begin element))
  21502. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  21503. (forward-line)
  21504. (point)))
  21505. (end (save-excursion
  21506. (goto-char (org-element-property :end element))
  21507. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  21508. (line-beginning-position))))
  21509. (if (or (< (point) beg) (> (point) end)) t
  21510. (fill-region-as-paragraph
  21511. (save-excursion (end-of-line)
  21512. (re-search-backward "^[ \t]*$" beg 'move)
  21513. (line-beginning-position))
  21514. (save-excursion (beginning-of-line)
  21515. (re-search-forward "^[ \t]*$" end 'move)
  21516. (line-beginning-position))
  21517. justify))))
  21518. ;; Fill comments.
  21519. (comment
  21520. (let ((begin (org-element-property :post-affiliated element))
  21521. (end (org-element-property :end element)))
  21522. (when (and (>= (point) begin) (<= (point) end))
  21523. (let ((begin (save-excursion
  21524. (end-of-line)
  21525. (if (re-search-backward "^[ \t]*#[ \t]*$" begin t)
  21526. (progn (forward-line) (point))
  21527. begin)))
  21528. (end (save-excursion
  21529. (end-of-line)
  21530. (if (re-search-forward "^[ \t]*#[ \t]*$" end 'move)
  21531. (1- (line-beginning-position))
  21532. (skip-chars-backward " \r\t\n")
  21533. (line-end-position)))))
  21534. ;; Do not fill comments when at a blank line.
  21535. (when (> end begin)
  21536. (let ((fill-prefix
  21537. (save-excursion
  21538. (beginning-of-line)
  21539. (looking-at "[ \t]*#")
  21540. (let ((comment-prefix (match-string 0)))
  21541. (goto-char (match-end 0))
  21542. (if (looking-at adaptive-fill-regexp)
  21543. (concat comment-prefix (match-string 0))
  21544. (concat comment-prefix " "))))))
  21545. (save-excursion
  21546. (fill-region-as-paragraph begin end justify))))))
  21547. t))
  21548. ;; Ignore every other element.
  21549. (otherwise t))))))
  21550. (defun org-auto-fill-function ()
  21551. "Auto-fill function."
  21552. ;; Check if auto-filling is meaningful.
  21553. (let ((fc (current-fill-column)))
  21554. (when (and fc (> (current-column) fc))
  21555. (let* ((fill-prefix (org-adaptive-fill-function))
  21556. ;; Enforce empty fill prefix, if required. Otherwise, it
  21557. ;; will be computed again.
  21558. (adaptive-fill-mode (not (equal fill-prefix ""))))
  21559. (when fill-prefix (do-auto-fill))))))
  21560. (defun org-comment-line-break-function (&optional soft)
  21561. "Break line at point and indent, continuing comment if within one.
  21562. The inserted newline is marked hard if variable
  21563. `use-hard-newlines' is true, unless optional argument SOFT is
  21564. non-nil."
  21565. (if soft (insert-and-inherit ?\n) (newline 1))
  21566. (save-excursion (forward-char -1) (delete-horizontal-space))
  21567. (delete-horizontal-space)
  21568. (indent-to-left-margin)
  21569. (insert-before-markers-and-inherit fill-prefix))
  21570. ;;; Fixed Width Areas
  21571. (defun org-toggle-fixed-width ()
  21572. "Toggle fixed-width markup.
  21573. Add or remove fixed-width markup on current line, whenever it
  21574. makes sense. Return an error otherwise.
  21575. If a region is active and if it contains only fixed-width areas
  21576. or blank lines, remove all fixed-width markup in it. If the
  21577. region contains anything else, convert all non-fixed-width lines
  21578. to fixed-width ones.
  21579. Blank lines at the end of the region are ignored unless the
  21580. region only contains such lines."
  21581. (interactive)
  21582. (if (not (org-region-active-p))
  21583. ;; No region:
  21584. ;;
  21585. ;; Remove fixed width marker only in a fixed-with element.
  21586. ;;
  21587. ;; Add fixed width maker in paragraphs, in blank lines after
  21588. ;; elements or at the beginning of a headline or an inlinetask,
  21589. ;; and before any one-line elements (e.g., a clock).
  21590. (progn
  21591. (beginning-of-line)
  21592. (let* ((element (org-element-at-point))
  21593. (type (org-element-type element)))
  21594. (cond
  21595. ((and (eq type 'fixed-width)
  21596. (looking-at "[ \t]*\\(:\\(?: \\|$\\)\\)"))
  21597. (replace-match
  21598. "" nil nil nil (if (= (line-end-position) (match-end 0)) 0 1)))
  21599. ((and (memq type '(babel-call clock comment diary-sexp headline
  21600. horizontal-rule keyword paragraph
  21601. planning))
  21602. (<= (org-element-property :post-affiliated element) (point)))
  21603. (skip-chars-forward " \t")
  21604. (insert ": "))
  21605. ((and (org-looking-at-p "[ \t]*$")
  21606. (or (eq type 'inlinetask)
  21607. (save-excursion
  21608. (skip-chars-forward " \r\t\n")
  21609. (<= (org-element-property :end element) (point)))))
  21610. (delete-region (point) (line-end-position))
  21611. (org-indent-line)
  21612. (insert ": "))
  21613. (t (user-error "Cannot insert a fixed-width line here")))))
  21614. ;; Region active.
  21615. (let* ((begin (save-excursion
  21616. (goto-char (region-beginning))
  21617. (line-beginning-position)))
  21618. (end (copy-marker
  21619. (save-excursion
  21620. (goto-char (region-end))
  21621. (unless (eolp) (beginning-of-line))
  21622. (if (save-excursion (re-search-backward "\\S-" begin t))
  21623. (progn (skip-chars-backward " \r\t\n") (point))
  21624. (point)))))
  21625. (all-fixed-width-p
  21626. (catch 'not-all-p
  21627. (save-excursion
  21628. (goto-char begin)
  21629. (skip-chars-forward " \r\t\n")
  21630. (when (eobp) (throw 'not-all-p nil))
  21631. (while (< (point) end)
  21632. (let ((element (org-element-at-point)))
  21633. (if (eq (org-element-type element) 'fixed-width)
  21634. (goto-char (org-element-property :end element))
  21635. (throw 'not-all-p nil))))
  21636. t))))
  21637. (if all-fixed-width-p
  21638. (save-excursion
  21639. (goto-char begin)
  21640. (while (< (point) end)
  21641. (when (looking-at "[ \t]*\\(:\\(?: \\|$\\)\\)")
  21642. (replace-match
  21643. "" nil nil nil
  21644. (if (= (line-end-position) (match-end 0)) 0 1)))
  21645. (forward-line)))
  21646. (let ((min-ind (point-max)))
  21647. ;; Find minimum indentation across all lines.
  21648. (save-excursion
  21649. (goto-char begin)
  21650. (if (not (save-excursion (re-search-forward "\\S-" end t)))
  21651. (setq min-ind 0)
  21652. (catch 'zerop
  21653. (while (< (point) end)
  21654. (unless (org-looking-at-p "[ \t]*$")
  21655. (let ((ind (org-get-indentation)))
  21656. (setq min-ind (min min-ind ind))
  21657. (when (zerop ind) (throw 'zerop t))))
  21658. (forward-line)))))
  21659. ;; Loop over all lines and add fixed-width markup everywhere
  21660. ;; but in fixed-width lines.
  21661. (save-excursion
  21662. (goto-char begin)
  21663. (while (< (point) end)
  21664. (cond
  21665. ((org-at-heading-p)
  21666. (insert ": ")
  21667. (forward-line)
  21668. (while (and (< (point) end) (org-looking-at-p "[ \t]*$"))
  21669. (insert ":")
  21670. (forward-line)))
  21671. ((org-looking-at-p "[ \t]*:\\( \\|$\\)")
  21672. (let* ((element (org-element-at-point))
  21673. (element-end (org-element-property :end element)))
  21674. (if (eq (org-element-type element) 'fixed-width)
  21675. (progn (goto-char element-end)
  21676. (skip-chars-backward " \r\t\n")
  21677. (forward-line))
  21678. (let ((limit (min end element-end)))
  21679. (while (< (point) limit)
  21680. (org-move-to-column min-ind t)
  21681. (insert ": ")
  21682. (forward-line))))))
  21683. (t
  21684. (org-move-to-column min-ind t)
  21685. (insert ": ")
  21686. (forward-line)))))))
  21687. (set-marker end nil))))
  21688. ;;; Comments
  21689. ;; Org comments syntax is quite complex. It requires the entire line
  21690. ;; to be just a comment. Also, even with the right syntax at the
  21691. ;; beginning of line, some some elements (i.e. verse-block or
  21692. ;; example-block) don't accept comments. Usual Emacs comment commands
  21693. ;; cannot cope with those requirements. Therefore, Org replaces them.
  21694. ;; Org still relies on `comment-dwim', but cannot trust
  21695. ;; `comment-only-p'. So, `comment-region-function' and
  21696. ;; `uncomment-region-function' both point
  21697. ;; to`org-comment-or-uncomment-region'. Eventually,
  21698. ;; `org-insert-comment' takes care of insertion of comments at the
  21699. ;; beginning of line.
  21700. ;; `org-setup-comments-handling' install comments related variables
  21701. ;; during `org-mode' initialization.
  21702. (defun org-setup-comments-handling ()
  21703. (interactive)
  21704. (org-set-local 'comment-use-syntax nil)
  21705. (org-set-local 'comment-start "# ")
  21706. (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  21707. (org-set-local 'comment-insert-comment-function 'org-insert-comment)
  21708. (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
  21709. (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
  21710. (defun org-insert-comment ()
  21711. "Insert an empty comment above current line.
  21712. If the line is empty, insert comment at its beginning. When
  21713. point is within a source block, comment according to the related
  21714. major mode."
  21715. (if (let ((element (org-element-at-point)))
  21716. (and (eq (org-element-type element) 'src-block)
  21717. (< (save-excursion
  21718. (goto-char (org-element-property :post-affiliated element))
  21719. (line-end-position))
  21720. (point))
  21721. (> (save-excursion
  21722. (goto-char (org-element-property :end element))
  21723. (skip-chars-backward " \r\t\n")
  21724. (line-beginning-position))
  21725. (point))))
  21726. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21727. (beginning-of-line)
  21728. (if (looking-at "\\s-*$") (delete-region (point) (point-at-eol))
  21729. (open-line 1))
  21730. (org-indent-line)
  21731. (insert "# ")))
  21732. (defvar comment-empty-lines) ; From newcomment.el.
  21733. (defun org-comment-or-uncomment-region (beg end &rest _)
  21734. "Comment or uncomment each non-blank line in the region.
  21735. Uncomment each non-blank line between BEG and END if it only
  21736. contains commented lines. Otherwise, comment them. If region is
  21737. strictly within a source block, use appropriate comment syntax."
  21738. (if (let ((element (org-element-at-point)))
  21739. (and (eq (org-element-type element) 'src-block)
  21740. (< (save-excursion
  21741. (goto-char (org-element-property :post-affiliated element))
  21742. (line-end-position))
  21743. beg)
  21744. (>= (save-excursion
  21745. (goto-char (org-element-property :end element))
  21746. (skip-chars-backward " \r\t\n")
  21747. (line-beginning-position))
  21748. end)))
  21749. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21750. (save-restriction
  21751. ;; Restrict region
  21752. (narrow-to-region (save-excursion (goto-char beg)
  21753. (skip-chars-forward " \r\t\n" end)
  21754. (line-beginning-position))
  21755. (save-excursion (goto-char end)
  21756. (skip-chars-backward " \r\t\n" beg)
  21757. (line-end-position)))
  21758. (let ((uncommentp
  21759. ;; UNCOMMENTP is non-nil when every non blank line between
  21760. ;; BEG and END is a comment.
  21761. (save-excursion
  21762. (goto-char (point-min))
  21763. (while (and (not (eobp))
  21764. (let ((element (org-element-at-point)))
  21765. (and (eq (org-element-type element) 'comment)
  21766. (goto-char (min (point-max)
  21767. (org-element-property
  21768. :end element)))))))
  21769. (eobp))))
  21770. (if uncommentp
  21771. ;; Only blank lines and comments in region: uncomment it.
  21772. (save-excursion
  21773. (goto-char (point-min))
  21774. (while (not (eobp))
  21775. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  21776. (replace-match "" nil nil nil 1))
  21777. (forward-line)))
  21778. ;; Comment each line in region.
  21779. (let ((min-indent (point-max)))
  21780. ;; First find the minimum indentation across all lines.
  21781. (save-excursion
  21782. (goto-char (point-min))
  21783. (while (and (not (eobp)) (not (zerop min-indent)))
  21784. (unless (looking-at "[ \t]*$")
  21785. (setq min-indent (min min-indent (current-indentation))))
  21786. (forward-line)))
  21787. ;; Then loop over all lines.
  21788. (save-excursion
  21789. (goto-char (point-min))
  21790. (while (not (eobp))
  21791. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  21792. ;; Don't get fooled by invisible text (e.g. link path)
  21793. ;; when moving to column MIN-INDENT.
  21794. (let ((buffer-invisibility-spec nil))
  21795. (org-move-to-column min-indent t))
  21796. (insert comment-start))
  21797. (forward-line)))))))))
  21798. (defun org-comment-dwim (arg)
  21799. "Call `comment-dwim' within a source edit buffer if needed."
  21800. (interactive "*P")
  21801. (if (org-in-src-block-p)
  21802. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21803. (call-interactively 'comment-dwim)))
  21804. ;;; Timestamps API
  21805. ;; This section contains tools to operate on timestamp objects, as
  21806. ;; returned by, e.g. `org-element-context'.
  21807. (defun org-timestamp--to-internal-time (timestamp &optional end)
  21808. "Encode TIMESTAMP object into Emacs internal time.
  21809. Use end of date range or time range when END is non-nil."
  21810. (apply #'encode-time
  21811. (cons 0
  21812. (mapcar
  21813. (lambda (prop) (or (org-element-property prop timestamp) 0))
  21814. (if end '(:minute-end :hour-end :day-end :month-end :year-end)
  21815. '(:minute-start :hour-start :day-start :month-start
  21816. :year-start))))))
  21817. (defun org-timestamp-has-time-p (timestamp)
  21818. "Non-nil when TIMESTAMP has a time specified."
  21819. (org-element-property :hour-start timestamp))
  21820. (defun org-timestamp-format (timestamp format &optional end utc)
  21821. "Format a TIMESTAMP object into a string.
  21822. FORMAT is a format specifier to be passed to
  21823. `format-time-string'.
  21824. When optional argument END is non-nil, use end of date-range or
  21825. time-range, if possible.
  21826. When optional argument UTC is non-nil, time will be expressed as
  21827. Universal Time."
  21828. (format-time-string
  21829. format (org-timestamp--to-internal-time timestamp end) utc))
  21830. (defun org-timestamp-split-range (timestamp &optional end)
  21831. "Extract a TIMESTAMP object from a date or time range.
  21832. END, when non-nil, means extract the end of the range.
  21833. Otherwise, extract its start.
  21834. Return a new timestamp object."
  21835. (let ((type (org-element-property :type timestamp)))
  21836. (if (memq type '(active inactive diary)) timestamp
  21837. (let ((split-ts (org-element-copy timestamp)))
  21838. ;; Set new type.
  21839. (org-element-put-property
  21840. split-ts :type (if (eq type 'active-range) 'active 'inactive))
  21841. ;; Copy start properties over end properties if END is
  21842. ;; non-nil. Otherwise, copy end properties over `start' ones.
  21843. (let ((p-alist '((:minute-start . :minute-end)
  21844. (:hour-start . :hour-end)
  21845. (:day-start . :day-end)
  21846. (:month-start . :month-end)
  21847. (:year-start . :year-end))))
  21848. (dolist (p-cell p-alist)
  21849. (org-element-put-property
  21850. split-ts
  21851. (funcall (if end #'car #'cdr) p-cell)
  21852. (org-element-property
  21853. (funcall (if end #'cdr #'car) p-cell) split-ts)))
  21854. ;; Eventually refresh `:raw-value'.
  21855. (org-element-put-property split-ts :raw-value nil)
  21856. (org-element-put-property
  21857. split-ts :raw-value (org-element-interpret-data split-ts)))))))
  21858. (defun org-timestamp-translate (timestamp &optional boundary)
  21859. "Translate TIMESTAMP object to custom format.
  21860. Format string is defined in `org-time-stamp-custom-formats',
  21861. which see.
  21862. When optional argument BOUNDARY is non-nil, it is either the
  21863. symbol `start' or `end'. In this case, only translate the
  21864. starting or ending part of TIMESTAMP if it is a date or time
  21865. range. Otherwise, translate both parts.
  21866. Return timestamp as-is if `org-display-custom-times' is nil or if
  21867. it has a `diary' type."
  21868. (let ((type (org-element-property :type timestamp)))
  21869. (if (or (not org-display-custom-times) (eq type 'diary))
  21870. (org-element-interpret-data timestamp)
  21871. (let ((fmt (funcall (if (org-timestamp-has-time-p timestamp) #'cdr #'car)
  21872. org-time-stamp-custom-formats)))
  21873. (if (and (not boundary) (memq type '(active-range inactive-range)))
  21874. (concat (org-timestamp-format timestamp fmt)
  21875. "--"
  21876. (org-timestamp-format timestamp fmt t))
  21877. (org-timestamp-format timestamp fmt (eq boundary 'end)))))))
  21878. ;;; Other stuff.
  21879. (defvar reftex-docstruct-symbol)
  21880. (defvar reftex-cite-format)
  21881. (defvar org--rds)
  21882. (defun org-reftex-citation ()
  21883. "Use reftex-citation to insert a citation into the buffer.
  21884. This looks for a line like
  21885. #+BIBLIOGRAPHY: foo plain option:-d
  21886. and derives from it that foo.bib is the bibliography file relevant
  21887. for this document. It then installs the necessary environment for RefTeX
  21888. to work in this buffer and calls `reftex-citation' to insert a citation
  21889. into the buffer.
  21890. Export of such citations to both LaTeX and HTML is handled by the contributed
  21891. package ox-bibtex by Taru Karttunen."
  21892. (interactive)
  21893. (let ((reftex-docstruct-symbol 'org--rds)
  21894. (reftex-cite-format "\\cite{%l}")
  21895. org--rds bib)
  21896. (save-excursion
  21897. (save-restriction
  21898. (widen)
  21899. (let ((case-fold-search t)
  21900. (re "^[ \t]*#\\+BIBLIOGRAPHY:[ \t]+\\([^ \t\n]+\\)"))
  21901. (if (not (save-excursion
  21902. (or (re-search-forward re nil t)
  21903. (re-search-backward re nil t))))
  21904. (user-error "No bibliography defined in file")
  21905. (setq bib (concat (match-string 1) ".bib")
  21906. org--rds (list (list 'bib bib)))))))
  21907. (call-interactively 'reftex-citation)))
  21908. ;;;; Functions extending outline functionality
  21909. (defun org-beginning-of-line (&optional arg)
  21910. "Go to the beginning of the current line. If that is invisible, continue
  21911. to a visible line beginning. This makes the function of C-a more intuitive.
  21912. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  21913. first attempt, and only move to after the tags when the cursor is already
  21914. beyond the end of the headline."
  21915. (interactive "P")
  21916. (let ((pos (point))
  21917. (special (if (consp org-special-ctrl-a/e)
  21918. (car org-special-ctrl-a/e)
  21919. org-special-ctrl-a/e))
  21920. deactivate-mark refpos)
  21921. (if (org-bound-and-true-p visual-line-mode)
  21922. (beginning-of-visual-line 1)
  21923. (beginning-of-line 1))
  21924. (if (and arg (fboundp 'move-beginning-of-line))
  21925. (call-interactively 'move-beginning-of-line)
  21926. (if (bobp)
  21927. nil
  21928. (backward-char 1)
  21929. (if (org-truely-invisible-p)
  21930. (while (and (not (bobp)) (org-truely-invisible-p))
  21931. (backward-char 1)
  21932. (beginning-of-line 1))
  21933. (forward-char 1))))
  21934. (when special
  21935. (cond
  21936. ((and (looking-at org-complex-heading-regexp)
  21937. (eq (char-after (match-end 1)) ?\s))
  21938. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  21939. (point-at-eol)))
  21940. (goto-char
  21941. (if (eq special t)
  21942. (cond ((> pos refpos) refpos)
  21943. ((= pos (point)) refpos)
  21944. (t (point)))
  21945. (cond ((> pos (point)) (point))
  21946. ((not (eq last-command this-command)) (point))
  21947. (t refpos)))))
  21948. ((org-at-item-p)
  21949. ;; Being at an item and not looking at an the item means point
  21950. ;; was previously moved to beginning of a visual line, which
  21951. ;; doesn't contain the item. Therefore, do nothing special,
  21952. ;; just stay here.
  21953. (when (looking-at org-list-full-item-re)
  21954. ;; Set special position at first white space character after
  21955. ;; bullet, and check-box, if any.
  21956. (let ((after-bullet
  21957. (let ((box (match-end 3)))
  21958. (if (not box) (match-end 1)
  21959. (let ((after (char-after box)))
  21960. (if (and after (= after ? )) (1+ box) box))))))
  21961. ;; Special case: Move point to special position when
  21962. ;; currently after it or at beginning of line.
  21963. (if (eq special t)
  21964. (when (or (> pos after-bullet) (= (point) pos))
  21965. (goto-char after-bullet))
  21966. ;; Reversed case: Move point to special position when
  21967. ;; point was already at beginning of line and command is
  21968. ;; repeated.
  21969. (when (and (= (point) pos) (eq last-command this-command))
  21970. (goto-char after-bullet))))))))
  21971. (org-no-warnings
  21972. (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  21973. (setq disable-point-adjustment
  21974. (or (not (invisible-p (point)))
  21975. (not (invisible-p (max (point-min) (1- (point))))))))
  21976. (defun org-end-of-line (&optional arg)
  21977. "Go to the end of the line.
  21978. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  21979. tags on the first attempt, and only move to after the tags when
  21980. the cursor is already beyond the end of the headline."
  21981. (interactive "P")
  21982. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  21983. org-special-ctrl-a/e))
  21984. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  21985. 'end-of-visual-line)
  21986. ((fboundp 'move-end-of-line) 'move-end-of-line)
  21987. (t 'end-of-line)))
  21988. deactivate-mark)
  21989. (if (or (not special) arg) (call-interactively move-fun)
  21990. (let* ((element (save-excursion (beginning-of-line)
  21991. (org-element-at-point)))
  21992. (type (org-element-type element)))
  21993. (cond
  21994. ((memq type '(headline inlinetask))
  21995. (let ((pos (point)))
  21996. (beginning-of-line 1)
  21997. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  21998. (if (eq special t)
  21999. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  22000. (goto-char (match-beginning 1))
  22001. (goto-char (match-end 0)))
  22002. (if (or (< pos (match-end 0))
  22003. (not (eq this-command last-command)))
  22004. (goto-char (match-end 0))
  22005. (goto-char (match-beginning 1))))
  22006. (call-interactively move-fun))))
  22007. ((outline-invisible-p (line-end-position))
  22008. ;; If element is hidden, `move-end-of-line' would put point
  22009. ;; after it. Use `end-of-line' to stay on current line.
  22010. (call-interactively 'end-of-line))
  22011. (t (call-interactively move-fun)))))
  22012. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  22013. (setq disable-point-adjustment
  22014. (or (not (invisible-p (point)))
  22015. (not (invisible-p (max (point-min) (1- (point))))))))
  22016. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  22017. (define-key org-mode-map "\C-e" 'org-end-of-line)
  22018. (defun org-backward-sentence (&optional _arg)
  22019. "Go to beginning of sentence, or beginning of table field.
  22020. This will call `backward-sentence' or `org-table-beginning-of-field',
  22021. depending on context."
  22022. (interactive)
  22023. (let* ((element (org-element-at-point))
  22024. (contents-begin (org-element-property :contents-begin element))
  22025. (table (org-element-lineage element '(table) t)))
  22026. (if (and table
  22027. (> (point) contents-begin)
  22028. (<= (point) (org-element-property :contents-end table)))
  22029. (call-interactively #'org-table-beginning-of-field)
  22030. (save-restriction
  22031. (when (and contents-begin
  22032. (< (point-min) contents-begin)
  22033. (> (point) contents-begin))
  22034. (narrow-to-region contents-begin
  22035. (org-element-property :contents-end element)))
  22036. (call-interactively #'backward-sentence)))))
  22037. (defun org-forward-sentence (&optional _arg)
  22038. "Go to end of sentence, or end of table field.
  22039. This will call `forward-sentence' or `org-table-end-of-field',
  22040. depending on context."
  22041. (interactive)
  22042. (let* ((element (org-element-at-point))
  22043. (contents-end (org-element-property :contents-end element))
  22044. (table (org-element-lineage element '(table) t)))
  22045. (if (and table
  22046. (>= (point) (org-element-property :contents-begin table))
  22047. (< (point) contents-end))
  22048. (call-interactively #'org-table-end-of-field)
  22049. (save-restriction
  22050. (when (and contents-end
  22051. (> (point-max) contents-end)
  22052. ;; Skip blank lines between elements.
  22053. (< (org-element-property :end element)
  22054. (save-excursion (goto-char contents-end)
  22055. (skip-chars-forward " \r\t\n"))))
  22056. (narrow-to-region (org-element-property :contents-begin element)
  22057. contents-end))
  22058. (call-interactively #'forward-sentence)))))
  22059. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  22060. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  22061. (defun org-kill-line (&optional _arg)
  22062. "Kill line, to tags or end of line."
  22063. (interactive)
  22064. (cond
  22065. ((or (not org-special-ctrl-k)
  22066. (bolp)
  22067. (not (org-at-heading-p)))
  22068. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  22069. org-ctrl-k-protect-subtree)
  22070. (if (or (eq org-ctrl-k-protect-subtree 'error)
  22071. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  22072. (user-error "C-k aborted as it would kill a hidden subtree")))
  22073. (call-interactively
  22074. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  22075. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  22076. (kill-region (point) (match-beginning 1))
  22077. (org-set-tags nil t))
  22078. (t (kill-region (point) (point-at-eol)))))
  22079. (define-key org-mode-map "\C-k" 'org-kill-line)
  22080. (defun org-yank (&optional arg)
  22081. "Yank. If the kill is a subtree, treat it specially.
  22082. This command will look at the current kill and check if is a single
  22083. subtree, or a series of subtrees[1]. If it passes the test, and if the
  22084. cursor is at the beginning of a line or after the stars of a currently
  22085. empty headline, then the yank is handled specially. How exactly depends
  22086. on the value of the following variables, both set by default.
  22087. `org-yank-folded-subtrees'
  22088. When set, the subtree(s) will be folded after insertion, but only
  22089. if doing so would now swallow text after the yanked text.
  22090. `org-yank-adjusted-subtrees'
  22091. When set, the subtree will be promoted or demoted in order to
  22092. fit into the local outline tree structure, which means that the
  22093. level will be adjusted so that it becomes the smaller one of the
  22094. two *visible* surrounding headings.
  22095. Any prefix to this command will cause `yank' to be called directly with
  22096. no special treatment. In particular, a simple \\[universal-argument] prefix \
  22097. will just
  22098. plainly yank the text as it is.
  22099. \[1] The test checks if the first non-white line is a heading
  22100. and if there are no other headings with fewer stars."
  22101. (interactive "P")
  22102. (org-yank-generic 'yank arg))
  22103. (defun org-yank-generic (command arg)
  22104. "Perform some yank-like command.
  22105. This function implements the behavior described in the `org-yank'
  22106. documentation. However, it has been generalized to work for any
  22107. interactive command with similar behavior."
  22108. ;; pretend to be command COMMAND
  22109. (setq this-command command)
  22110. (if arg
  22111. (call-interactively command)
  22112. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  22113. (and (org-kill-is-subtree-p)
  22114. (or (bolp)
  22115. (and (looking-at "[ \t]*$")
  22116. (string-match
  22117. "\\`\\*+\\'"
  22118. (buffer-substring (point-at-bol) (point)))))))
  22119. swallowp)
  22120. (cond
  22121. ((and subtreep org-yank-folded-subtrees)
  22122. (let ((beg (point))
  22123. end)
  22124. (if (and subtreep org-yank-adjusted-subtrees)
  22125. (org-paste-subtree nil nil 'for-yank)
  22126. (call-interactively command))
  22127. (setq end (point))
  22128. (goto-char beg)
  22129. (when (and (bolp) subtreep
  22130. (not (setq swallowp
  22131. (org-yank-folding-would-swallow-text beg end))))
  22132. (org-with-limited-levels
  22133. (or (looking-at org-outline-regexp)
  22134. (re-search-forward org-outline-regexp-bol end t))
  22135. (while (and (< (point) end) (looking-at org-outline-regexp))
  22136. (hide-subtree)
  22137. (org-cycle-show-empty-lines 'folded)
  22138. (condition-case nil
  22139. (outline-forward-same-level 1)
  22140. (error (goto-char end))))))
  22141. (when swallowp
  22142. (message
  22143. "Inserted text not folded because that would swallow text"))
  22144. (goto-char end)
  22145. (skip-chars-forward " \t\n\r")
  22146. (beginning-of-line 1)
  22147. (push-mark beg 'nomsg)))
  22148. ((and subtreep org-yank-adjusted-subtrees)
  22149. (let ((beg (point-at-bol)))
  22150. (org-paste-subtree nil nil 'for-yank)
  22151. (push-mark beg 'nomsg)))
  22152. (t
  22153. (call-interactively command))))))
  22154. (defun org-yank-folding-would-swallow-text (beg end)
  22155. "Would hide-subtree at BEG swallow any text after END?"
  22156. (let (level)
  22157. (org-with-limited-levels
  22158. (save-excursion
  22159. (goto-char beg)
  22160. (when (or (looking-at org-outline-regexp)
  22161. (re-search-forward org-outline-regexp-bol end t))
  22162. (setq level (org-outline-level)))
  22163. (goto-char end)
  22164. (skip-chars-forward " \t\r\n\v\f")
  22165. (if (or (eobp)
  22166. (and (bolp) (looking-at org-outline-regexp)
  22167. (<= (org-outline-level) level)))
  22168. nil ; Nothing would be swallowed
  22169. t))))) ; something would swallow
  22170. (define-key org-mode-map "\C-y" 'org-yank)
  22171. (defun org-truely-invisible-p ()
  22172. "Check if point is at a character currently not visible.
  22173. This version does not only check the character property, but also
  22174. `visible-mode'."
  22175. ;; Early versions of noutline don't have `outline-invisible-p'.
  22176. (if (org-bound-and-true-p visible-mode)
  22177. nil
  22178. (outline-invisible-p)))
  22179. (defun org-invisible-p2 ()
  22180. "Check if point is at a character currently not visible."
  22181. (save-excursion
  22182. (if (and (eolp) (not (bobp))) (backward-char 1))
  22183. ;; Early versions of noutline don't have `outline-invisible-p'.
  22184. (outline-invisible-p)))
  22185. (defun org-back-to-heading (&optional invisible-ok)
  22186. "Call `outline-back-to-heading', but provide a better error message."
  22187. (condition-case nil
  22188. (outline-back-to-heading invisible-ok)
  22189. (error (error "Before first headline at position %d in buffer %s"
  22190. (point) (current-buffer)))))
  22191. (defun org-before-first-heading-p ()
  22192. "Before first heading?"
  22193. (save-excursion
  22194. (end-of-line)
  22195. (null (re-search-backward org-outline-regexp-bol nil t))))
  22196. (defun org-at-heading-p (&optional ignored)
  22197. (outline-on-heading-p t))
  22198. ;; Compatibility alias with Org versions < 7.8.03
  22199. (defalias 'org-on-heading-p 'org-at-heading-p)
  22200. (defun org-in-commented-heading-p (&optional no-inheritance)
  22201. "Non-nil if point is under a commented heading.
  22202. This function also checks ancestors of the current headline,
  22203. unless optional argument NO-INHERITANCE is non-nil."
  22204. (cond
  22205. ((org-before-first-heading-p) nil)
  22206. ((let ((headline (nth 4 (org-heading-components))))
  22207. (and headline
  22208. (let ((case-fold-search nil))
  22209. (org-string-match-p (concat "^" org-comment-string "\\(?: \\|$\\)")
  22210. headline)))))
  22211. (no-inheritance nil)
  22212. (t
  22213. (save-excursion (and (org-up-heading-safe) (org-in-commented-heading-p))))))
  22214. (defun org-at-comment-p nil
  22215. "Is cursor in a commented line?"
  22216. (save-excursion
  22217. (save-match-data
  22218. (beginning-of-line)
  22219. (looking-at "^[ \t]*# "))))
  22220. (defun org-at-drawer-p nil
  22221. "Is cursor at a drawer keyword?"
  22222. (save-excursion
  22223. (move-beginning-of-line 1)
  22224. (looking-at org-drawer-regexp)))
  22225. (defun org-at-block-p nil
  22226. "Is cursor at a block keyword?"
  22227. (save-excursion
  22228. (move-beginning-of-line 1)
  22229. (looking-at org-block-regexp)))
  22230. (defun org-point-at-end-of-empty-headline ()
  22231. "If point is at the end of an empty headline, return t, else nil.
  22232. If the heading only contains a TODO keyword, it is still still considered
  22233. empty."
  22234. (and (looking-at "[ \t]*$")
  22235. (when org-todo-line-regexp
  22236. (save-excursion
  22237. (beginning-of-line 1)
  22238. (let ((case-fold-search nil))
  22239. (looking-at org-todo-line-regexp)
  22240. (string= (match-string 3) ""))))))
  22241. (defun org-at-heading-or-item-p ()
  22242. (or (org-at-heading-p) (org-at-item-p)))
  22243. (defun org-at-target-p ()
  22244. (or (org-in-regexp org-radio-target-regexp)
  22245. (org-in-regexp org-target-regexp)))
  22246. ;; Compatibility alias with Org versions < 7.8.03
  22247. (defalias 'org-on-target-p 'org-at-target-p)
  22248. (defun org-up-heading-all (arg)
  22249. "Move to the heading line of which the present line is a subheading.
  22250. This function considers both visible and invisible heading lines.
  22251. With argument, move up ARG levels."
  22252. (if (fboundp 'outline-up-heading-all)
  22253. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  22254. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  22255. (defun org-up-heading-safe ()
  22256. "Move to the heading line of which the present line is a subheading.
  22257. This version will not throw an error. It will return the level of the
  22258. headline found, or nil if no higher level is found.
  22259. Also, this function will be a lot faster than `outline-up-heading',
  22260. because it relies on stars being the outline starters. This can really
  22261. make a significant difference in outlines with very many siblings."
  22262. (when (ignore-errors (org-back-to-heading t))
  22263. (let ((level-up (1- (funcall outline-level))))
  22264. (and (> level-up 0)
  22265. (re-search-backward (format "^\\*\\{1,%d\\} " level-up) nil t)
  22266. (funcall outline-level)))))
  22267. (defun org-first-sibling-p ()
  22268. "Is this heading the first child of its parents?"
  22269. (interactive)
  22270. (let ((re org-outline-regexp-bol)
  22271. level l)
  22272. (unless (org-at-heading-p t)
  22273. (user-error "Not at a heading"))
  22274. (setq level (funcall outline-level))
  22275. (save-excursion
  22276. (if (not (re-search-backward re nil t))
  22277. t
  22278. (setq l (funcall outline-level))
  22279. (< l level)))))
  22280. (defun org-goto-sibling (&optional previous)
  22281. "Goto the next sibling, even if it is invisible.
  22282. When PREVIOUS is set, go to the previous sibling instead. Returns t
  22283. when a sibling was found. When none is found, return nil and don't
  22284. move point."
  22285. (let ((fun (if previous 're-search-backward 're-search-forward))
  22286. (pos (point))
  22287. (re org-outline-regexp-bol)
  22288. level l)
  22289. (when (ignore-errors (org-back-to-heading t))
  22290. (setq level (funcall outline-level))
  22291. (catch 'exit
  22292. (or previous (forward-char 1))
  22293. (while (funcall fun re nil t)
  22294. (setq l (funcall outline-level))
  22295. (when (< l level) (goto-char pos) (throw 'exit nil))
  22296. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  22297. (goto-char pos)
  22298. nil))))
  22299. (defun org-show-siblings ()
  22300. "Show all siblings of the current headline."
  22301. (save-excursion
  22302. (while (org-goto-sibling) (org-flag-heading nil)))
  22303. (save-excursion
  22304. (while (org-goto-sibling 'previous)
  22305. (org-flag-heading nil))))
  22306. (defun org-goto-first-child ()
  22307. "Goto the first child, even if it is invisible.
  22308. Return t when a child was found. Otherwise don't move point and
  22309. return nil."
  22310. (let (level (pos (point)) (re org-outline-regexp-bol))
  22311. (when (ignore-errors (org-back-to-heading t))
  22312. (setq level (outline-level))
  22313. (forward-char 1)
  22314. (if (and (re-search-forward re nil t) (> (outline-level) level))
  22315. (progn (goto-char (match-beginning 0)) t)
  22316. (goto-char pos) nil))))
  22317. (defun org-show-hidden-entry ()
  22318. "Show an entry where even the heading is hidden."
  22319. (save-excursion
  22320. (org-show-entry)))
  22321. (defun org-flag-heading (flag &optional entry)
  22322. "Flag the current heading. FLAG non-nil means make invisible.
  22323. When ENTRY is non-nil, show the entire entry."
  22324. (save-excursion
  22325. (org-back-to-heading t)
  22326. ;; Check if we should show the entire entry
  22327. (if entry
  22328. (progn
  22329. (org-show-entry)
  22330. (save-excursion
  22331. (and (outline-next-heading)
  22332. (org-flag-heading nil))))
  22333. (outline-flag-region (max (point-min) (1- (point)))
  22334. (save-excursion (outline-end-of-heading) (point))
  22335. flag))))
  22336. (defun org-get-next-sibling ()
  22337. "Move to next heading of the same level, and return point.
  22338. If there is no such heading, return nil.
  22339. This is like outline-next-sibling, but invisible headings are ok."
  22340. (let ((level (funcall outline-level)))
  22341. (outline-next-heading)
  22342. (while (and (not (eobp)) (> (funcall outline-level) level))
  22343. (outline-next-heading))
  22344. (if (or (eobp) (< (funcall outline-level) level))
  22345. nil
  22346. (point))))
  22347. (defun org-get-last-sibling ()
  22348. "Move to previous heading of the same level, and return point.
  22349. If there is no such heading, return nil."
  22350. (let ((opoint (point))
  22351. (level (funcall outline-level)))
  22352. (outline-previous-heading)
  22353. (when (and (/= (point) opoint) (outline-on-heading-p t))
  22354. (while (and (> (funcall outline-level) level)
  22355. (not (bobp)))
  22356. (outline-previous-heading))
  22357. (if (< (funcall outline-level) level)
  22358. nil
  22359. (point)))))
  22360. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  22361. "Goto to the end of a subtree."
  22362. ;; This contains an exact copy of the original function, but it uses
  22363. ;; `org-back-to-heading', to make it work also in invisible
  22364. ;; trees. And is uses an invisible-ok argument.
  22365. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  22366. ;; Furthermore, when used inside Org, finding the end of a large subtree
  22367. ;; with many children and grandchildren etc, this can be much faster
  22368. ;; than the outline version.
  22369. (org-back-to-heading invisible-ok)
  22370. (let ((first t)
  22371. (level (funcall outline-level)))
  22372. (if (and (derived-mode-p 'org-mode) (< level 1000))
  22373. ;; A true heading (not a plain list item), in Org-mode
  22374. ;; This means we can easily find the end by looking
  22375. ;; only for the right number of stars. Using a regexp to do
  22376. ;; this is so much faster than using a Lisp loop.
  22377. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  22378. (forward-char 1)
  22379. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  22380. ;; something else, do it the slow way
  22381. (while (and (not (eobp))
  22382. (or first (> (funcall outline-level) level)))
  22383. (setq first nil)
  22384. (outline-next-heading)))
  22385. (unless to-heading
  22386. (if (memq (preceding-char) '(?\n ?\^M))
  22387. (progn
  22388. ;; Go to end of line before heading
  22389. (forward-char -1)
  22390. (if (memq (preceding-char) '(?\n ?\^M))
  22391. ;; leave blank line before heading
  22392. (forward-char -1))))))
  22393. (point))
  22394. (defun org-end-of-meta-data (&optional full)
  22395. "Skip planning line and properties drawer in current entry.
  22396. When optional argument FULL is non-nil, also skip empty lines,
  22397. clocking lines and regular drawers at the beginning of the
  22398. entry."
  22399. (org-back-to-heading t)
  22400. (forward-line)
  22401. (when (org-looking-at-p org-planning-line-re) (forward-line))
  22402. (when (looking-at org-property-drawer-re)
  22403. (goto-char (match-end 0))
  22404. (forward-line))
  22405. (when (and full (not (org-at-heading-p)))
  22406. (catch 'exit
  22407. (let ((end (save-excursion (outline-next-heading) (point)))
  22408. (re (concat "[ \t]*$" "\\|" org-clock-line-re)))
  22409. (while (not (eobp))
  22410. (cond ((org-looking-at-p org-drawer-regexp)
  22411. (if (re-search-forward "^[ \t]*:END:[ \t]*$" end t)
  22412. (forward-line)
  22413. (throw 'exit t)))
  22414. ((org-looking-at-p re) (forward-line))
  22415. (t (throw 'exit t))))))))
  22416. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  22417. "Move forward to the ARG'th subheading at same level as this one.
  22418. Stop at the first and last subheadings of a superior heading.
  22419. Normally this only looks at visible headings, but when INVISIBLE-OK is
  22420. non-nil it will also look at invisible ones."
  22421. (interactive "p")
  22422. (if (not (ignore-errors (org-back-to-heading invisible-ok)))
  22423. (if (and arg (< arg 0))
  22424. (goto-char (point-min))
  22425. (outline-next-heading))
  22426. (org-at-heading-p)
  22427. (let ((level (- (match-end 0) (match-beginning 0) 1))
  22428. (f (if (and arg (< arg 0))
  22429. 're-search-backward
  22430. 're-search-forward))
  22431. (count (if arg (abs arg) 1))
  22432. (result (point)))
  22433. (while (and (prog1 (> count 0)
  22434. (forward-char (if (and arg (< arg 0)) -1 1)))
  22435. (funcall f org-outline-regexp-bol nil 'move))
  22436. (let ((l (- (match-end 0) (match-beginning 0) 1)))
  22437. (cond ((< l level) (setq count 0))
  22438. ((and (= l level)
  22439. (or invisible-ok
  22440. (progn
  22441. (goto-char (line-beginning-position))
  22442. (not (outline-invisible-p)))))
  22443. (setq count (1- count))
  22444. (when (eq l level)
  22445. (setq result (point)))))))
  22446. (goto-char result))
  22447. (beginning-of-line 1)))
  22448. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  22449. "Move backward to the ARG'th subheading at same level as this one.
  22450. Stop at the first and last subheadings of a superior heading."
  22451. (interactive "p")
  22452. (org-forward-heading-same-level (if arg (- arg) -1) invisible-ok))
  22453. (defun org-next-visible-heading (arg)
  22454. "Move to the next visible heading.
  22455. This function wraps `outline-next-visible-heading' with
  22456. `org-with-limited-levels' in order to skip over inline tasks and
  22457. respect customization of `org-odd-levels-only'."
  22458. (interactive "p")
  22459. (org-with-limited-levels
  22460. (outline-next-visible-heading arg)))
  22461. (defun org-previous-visible-heading (arg)
  22462. "Move to the next visible heading.
  22463. This function wraps `outline-previous-visible-heading' with
  22464. `org-with-limited-levels' in order to skip over inline tasks and
  22465. respect customization of `org-odd-levels-only'."
  22466. (interactive "p")
  22467. (org-with-limited-levels
  22468. (outline-previous-visible-heading arg)))
  22469. (defun org-next-block (arg &optional backward block-regexp)
  22470. "Jump to the next block.
  22471. With a prefix argument ARG, jump forward ARG many blocks.
  22472. When BACKWARD is non-nil, jump to the previous block.
  22473. When BLOCK-REGEXP is non-nil, use this regexp to find blocks.
  22474. Match data is set according to this regexp when the function
  22475. returns.
  22476. Return point at beginning of the opening line of found block.
  22477. Throw an error if no block is found."
  22478. (interactive "p")
  22479. (let ((re (or block-regexp "^[ \t]*#\\+BEGIN"))
  22480. (case-fold-search t)
  22481. (search-fn (if backward #'re-search-backward #'re-search-forward))
  22482. (count (or arg 1))
  22483. (origin (point))
  22484. last-element)
  22485. (if backward (beginning-of-line) (end-of-line))
  22486. (while (and (> count 0) (funcall search-fn re nil t))
  22487. (let ((element (save-excursion
  22488. (goto-char (match-beginning 0))
  22489. (save-match-data (org-element-at-point)))))
  22490. (when (and (memq (org-element-type element)
  22491. '(center-block comment-block dynamic-block
  22492. example-block export-block quote-block
  22493. special-block src-block verse-block))
  22494. (<= (match-beginning 0)
  22495. (org-element-property :post-affiliated element)))
  22496. (setq last-element element)
  22497. (decf count))))
  22498. (if (= count 0)
  22499. (prog1 (goto-char (org-element-property :post-affiliated last-element))
  22500. (save-match-data (org-show-context)))
  22501. (goto-char origin)
  22502. (user-error "No %s code blocks" (if backward "previous" "further")))))
  22503. (defun org-previous-block (arg &optional block-regexp)
  22504. "Jump to the previous block.
  22505. With a prefix argument ARG, jump backward ARG many source blocks.
  22506. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  22507. (interactive "p")
  22508. (org-next-block arg t block-regexp))
  22509. (defun org-forward-paragraph ()
  22510. "Move forward to beginning of next paragraph or equivalent.
  22511. The function moves point to the beginning of the next visible
  22512. structural element, which can be a paragraph, a table, a list
  22513. item, etc. It also provides some special moves for convenience:
  22514. - On an affiliated keyword, jump to the beginning of the
  22515. relative element.
  22516. - On an item or a footnote definition, move to the second
  22517. element inside, if any.
  22518. - On a table or a property drawer, jump after it.
  22519. - On a verse or source block, stop after blank lines."
  22520. (interactive)
  22521. (when (eobp) (user-error "Cannot move further down"))
  22522. (let* ((deactivate-mark nil)
  22523. (element (org-element-at-point))
  22524. (type (org-element-type element))
  22525. (post-affiliated (org-element-property :post-affiliated element))
  22526. (contents-begin (org-element-property :contents-begin element))
  22527. (contents-end (org-element-property :contents-end element))
  22528. (end (let ((end (org-element-property :end element)) (parent element))
  22529. (while (and (setq parent (org-element-property :parent parent))
  22530. (= (org-element-property :contents-end parent) end))
  22531. (setq end (org-element-property :end parent)))
  22532. end)))
  22533. (cond ((not element)
  22534. (skip-chars-forward " \r\t\n")
  22535. (or (eobp) (beginning-of-line)))
  22536. ;; On affiliated keywords, move to element's beginning.
  22537. ((< (point) post-affiliated)
  22538. (goto-char post-affiliated))
  22539. ;; At a table row, move to the end of the table. Similarly,
  22540. ;; at a node property, move to the end of the property
  22541. ;; drawer.
  22542. ((memq type '(node-property table-row))
  22543. (goto-char (org-element-property
  22544. :end (org-element-property :parent element))))
  22545. ((memq type '(property-drawer table)) (goto-char end))
  22546. ;; Consider blank lines as separators in verse and source
  22547. ;; blocks to ease editing.
  22548. ((memq type '(src-block verse-block))
  22549. (when (eq type 'src-block)
  22550. (setq contents-end
  22551. (save-excursion (goto-char end)
  22552. (skip-chars-backward " \r\t\n")
  22553. (line-beginning-position))))
  22554. (beginning-of-line)
  22555. (when (looking-at "[ \t]*$") (skip-chars-forward " \r\t\n"))
  22556. (if (not (re-search-forward "^[ \t]*$" contents-end t))
  22557. (goto-char end)
  22558. (skip-chars-forward " \r\t\n")
  22559. (if (= (point) contents-end) (goto-char end)
  22560. (beginning-of-line))))
  22561. ;; With no contents, just skip element.
  22562. ((not contents-begin) (goto-char end))
  22563. ;; If contents are invisible, skip the element altogether.
  22564. ((outline-invisible-p (line-end-position))
  22565. (case type
  22566. (headline
  22567. (org-with-limited-levels (outline-next-visible-heading 1)))
  22568. ;; At a plain list, make sure we move to the next item
  22569. ;; instead of skipping the whole list.
  22570. (plain-list (forward-char)
  22571. (org-forward-paragraph))
  22572. (otherwise (goto-char end))))
  22573. ((>= (point) contents-end) (goto-char end))
  22574. ((>= (point) contents-begin)
  22575. ;; This can only happen on paragraphs and plain lists.
  22576. (case type
  22577. (paragraph (goto-char end))
  22578. ;; At a plain list, try to move to second element in
  22579. ;; first item, if possible.
  22580. (plain-list (end-of-line)
  22581. (org-forward-paragraph))))
  22582. ;; When contents start on the middle of a line (e.g. in
  22583. ;; items and footnote definitions), try to reach first
  22584. ;; element starting after current line.
  22585. ((> (line-end-position) contents-begin)
  22586. (end-of-line)
  22587. (org-forward-paragraph))
  22588. (t (goto-char contents-begin)))))
  22589. (defun org-backward-paragraph ()
  22590. "Move backward to start of previous paragraph or equivalent.
  22591. The function moves point to the beginning of the current
  22592. structural element, which can be a paragraph, a table, a list
  22593. item, etc., or to the beginning of the previous visible one if
  22594. point is already there. It also provides some special moves for
  22595. convenience:
  22596. - On an affiliated keyword, jump to the first one.
  22597. - On a table or a property drawer, move to its beginning.
  22598. - On a verse or source block, stop before blank lines."
  22599. (interactive)
  22600. (when (bobp) (user-error "Cannot move further up"))
  22601. (let* ((deactivate-mark nil)
  22602. (element (org-element-at-point))
  22603. (type (org-element-type element))
  22604. (contents-begin (org-element-property :contents-begin element))
  22605. (contents-end (org-element-property :contents-end element))
  22606. (post-affiliated (org-element-property :post-affiliated element))
  22607. (begin (org-element-property :begin element)))
  22608. (cond
  22609. ((not element) (goto-char (point-min)))
  22610. ((= (point) begin)
  22611. (backward-char)
  22612. (org-backward-paragraph))
  22613. ((<= (point) post-affiliated) (goto-char begin))
  22614. ((memq type '(node-property table-row))
  22615. (goto-char (org-element-property
  22616. :post-affiliated (org-element-property :parent element))))
  22617. ((memq type '(property-drawer table)) (goto-char begin))
  22618. ((memq type '(src-block verse-block))
  22619. (when (eq type 'src-block)
  22620. (setq contents-begin
  22621. (save-excursion (goto-char begin) (forward-line) (point))))
  22622. (if (= (point) contents-begin) (goto-char post-affiliated)
  22623. ;; Inside a verse block, see blank lines as paragraph
  22624. ;; separators.
  22625. (let ((origin (point)))
  22626. (skip-chars-backward " \r\t\n" contents-begin)
  22627. (when (re-search-backward "^[ \t]*$" contents-begin 'move)
  22628. (skip-chars-forward " \r\t\n" origin)
  22629. (if (= (point) origin) (goto-char contents-begin)
  22630. (beginning-of-line))))))
  22631. ((not contents-begin) (goto-char (or post-affiliated begin)))
  22632. ((eq type 'paragraph)
  22633. (goto-char contents-begin)
  22634. ;; When at first paragraph in an item or a footnote definition,
  22635. ;; move directly to beginning of line.
  22636. (let ((parent-contents
  22637. (org-element-property
  22638. :contents-begin (org-element-property :parent element))))
  22639. (when (and parent-contents (= parent-contents contents-begin))
  22640. (beginning-of-line))))
  22641. ;; At the end of a greater element, move to the beginning of the
  22642. ;; last element within.
  22643. ((>= (point) contents-end)
  22644. (goto-char (1- contents-end))
  22645. (org-backward-paragraph))
  22646. (t (goto-char (or post-affiliated begin))))
  22647. ;; Ensure we never leave point invisible.
  22648. (when (outline-invisible-p (point)) (beginning-of-visual-line))))
  22649. (defun org-forward-element ()
  22650. "Move forward by one element.
  22651. Move to the next element at the same level, when possible."
  22652. (interactive)
  22653. (cond ((eobp) (user-error "Cannot move further down"))
  22654. ((org-with-limited-levels (org-at-heading-p))
  22655. (let ((origin (point)))
  22656. (goto-char (org-end-of-subtree nil t))
  22657. (unless (org-with-limited-levels (org-at-heading-p))
  22658. (goto-char origin)
  22659. (user-error "Cannot move further down"))))
  22660. (t
  22661. (let* ((elem (org-element-at-point))
  22662. (end (org-element-property :end elem))
  22663. (parent (org-element-property :parent elem)))
  22664. (cond ((and parent (= (org-element-property :contents-end parent) end))
  22665. (goto-char (org-element-property :end parent)))
  22666. ((integer-or-marker-p end) (goto-char end))
  22667. (t (message "No element at point")))))))
  22668. (defun org-backward-element ()
  22669. "Move backward by one element.
  22670. Move to the previous element at the same level, when possible."
  22671. (interactive)
  22672. (cond ((bobp) (user-error "Cannot move further up"))
  22673. ((org-with-limited-levels (org-at-heading-p))
  22674. ;; At a headline, move to the previous one, if any, or stay
  22675. ;; here.
  22676. (let ((origin (point)))
  22677. (org-with-limited-levels (org-backward-heading-same-level 1))
  22678. ;; When current headline has no sibling above, move to its
  22679. ;; parent.
  22680. (when (= (point) origin)
  22681. (or (org-with-limited-levels (org-up-heading-safe))
  22682. (progn (goto-char origin)
  22683. (user-error "Cannot move further up"))))))
  22684. (t
  22685. (let* ((elem (org-element-at-point))
  22686. (beg (org-element-property :begin elem)))
  22687. (cond
  22688. ;; Move to beginning of current element if point isn't
  22689. ;; there already.
  22690. ((null beg) (message "No element at point"))
  22691. ((/= (point) beg) (goto-char beg))
  22692. (t (goto-char beg)
  22693. (skip-chars-backward " \r\t\n")
  22694. (unless (bobp)
  22695. (let ((prev (org-element-at-point)))
  22696. (goto-char (org-element-property :begin prev))
  22697. (while (and (setq prev (org-element-property :parent prev))
  22698. (<= (org-element-property :end prev) beg))
  22699. (goto-char (org-element-property :begin prev)))))))))))
  22700. (defun org-up-element ()
  22701. "Move to upper element."
  22702. (interactive)
  22703. (if (org-with-limited-levels (org-at-heading-p))
  22704. (unless (org-up-heading-safe) (user-error "No surrounding element"))
  22705. (let* ((elem (org-element-at-point))
  22706. (parent (org-element-property :parent elem)))
  22707. (if parent (goto-char (org-element-property :begin parent))
  22708. (if (org-with-limited-levels (org-before-first-heading-p))
  22709. (user-error "No surrounding element")
  22710. (org-with-limited-levels (org-back-to-heading)))))))
  22711. (defvar org-element-greater-elements)
  22712. (defun org-down-element ()
  22713. "Move to inner element."
  22714. (interactive)
  22715. (let ((element (org-element-at-point)))
  22716. (cond
  22717. ((memq (org-element-type element) '(plain-list table))
  22718. (goto-char (org-element-property :contents-begin element))
  22719. (forward-char))
  22720. ((memq (org-element-type element) org-element-greater-elements)
  22721. ;; If contents are hidden, first disclose them.
  22722. (when (outline-invisible-p (line-end-position)) (org-cycle))
  22723. (goto-char (or (org-element-property :contents-begin element)
  22724. (user-error "No content for this element"))))
  22725. (t (user-error "No inner element")))))
  22726. (defun org-drag-element-backward ()
  22727. "Move backward element at point."
  22728. (interactive)
  22729. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  22730. (let* ((elem (org-element-at-point))
  22731. (prev-elem
  22732. (save-excursion
  22733. (goto-char (org-element-property :begin elem))
  22734. (skip-chars-backward " \r\t\n")
  22735. (unless (bobp)
  22736. (let* ((beg (org-element-property :begin elem))
  22737. (prev (org-element-at-point))
  22738. (up prev))
  22739. (while (and (setq up (org-element-property :parent up))
  22740. (<= (org-element-property :end up) beg))
  22741. (setq prev up))
  22742. prev)))))
  22743. ;; Error out if no previous element or previous element is
  22744. ;; a parent of the current one.
  22745. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  22746. (user-error "Cannot drag element backward")
  22747. (let ((pos (point)))
  22748. (org-element-swap-A-B prev-elem elem)
  22749. (goto-char (+ (org-element-property :begin prev-elem)
  22750. (- pos (org-element-property :begin elem)))))))))
  22751. (defun org-drag-element-forward ()
  22752. "Move forward element at point."
  22753. (interactive)
  22754. (let* ((pos (point))
  22755. (elem (org-element-at-point)))
  22756. (when (= (point-max) (org-element-property :end elem))
  22757. (user-error "Cannot drag element forward"))
  22758. (goto-char (org-element-property :end elem))
  22759. (let ((next-elem (org-element-at-point)))
  22760. (when (or (org-element-nested-p elem next-elem)
  22761. (and (eq (org-element-type next-elem) 'headline)
  22762. (not (eq (org-element-type elem) 'headline))))
  22763. (goto-char pos)
  22764. (user-error "Cannot drag element forward"))
  22765. ;; Compute new position of point: it's shifted by NEXT-ELEM
  22766. ;; body's length (without final blanks) and by the length of
  22767. ;; blanks between ELEM and NEXT-ELEM.
  22768. (let ((size-next (- (save-excursion
  22769. (goto-char (org-element-property :end next-elem))
  22770. (skip-chars-backward " \r\t\n")
  22771. (forward-line)
  22772. ;; Small correction if buffer doesn't end
  22773. ;; with a newline character.
  22774. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  22775. (org-element-property :begin next-elem)))
  22776. (size-blank (- (org-element-property :end elem)
  22777. (save-excursion
  22778. (goto-char (org-element-property :end elem))
  22779. (skip-chars-backward " \r\t\n")
  22780. (forward-line)
  22781. (point)))))
  22782. (org-element-swap-A-B elem next-elem)
  22783. (goto-char (+ pos size-next size-blank))))))
  22784. (defun org-drag-line-forward (arg)
  22785. "Drag the line at point ARG lines forward."
  22786. (interactive "p")
  22787. (dotimes (n (abs arg))
  22788. (let ((c (current-column)))
  22789. (if (< 0 arg)
  22790. (progn
  22791. (beginning-of-line 2)
  22792. (transpose-lines 1)
  22793. (beginning-of-line 0))
  22794. (transpose-lines 1)
  22795. (beginning-of-line -1))
  22796. (org-move-to-column c))))
  22797. (defun org-drag-line-backward (arg)
  22798. "Drag the line at point ARG lines backward."
  22799. (interactive "p")
  22800. (org-drag-line-forward (- arg)))
  22801. (defun org-mark-element ()
  22802. "Put point at beginning of this element, mark at end.
  22803. Interactively, if this command is repeated or (in Transient Mark
  22804. mode) if the mark is active, it marks the next element after the
  22805. ones already marked."
  22806. (interactive)
  22807. (let (deactivate-mark)
  22808. (if (and (org-called-interactively-p 'any)
  22809. (or (and (eq last-command this-command) (mark t))
  22810. (and transient-mark-mode mark-active)))
  22811. (set-mark
  22812. (save-excursion
  22813. (goto-char (mark))
  22814. (goto-char (org-element-property :end (org-element-at-point)))))
  22815. (let ((element (org-element-at-point)))
  22816. (end-of-line)
  22817. (push-mark (org-element-property :end element) t t)
  22818. (goto-char (org-element-property :begin element))))))
  22819. (defun org-narrow-to-element ()
  22820. "Narrow buffer to current element."
  22821. (interactive)
  22822. (let ((elem (org-element-at-point)))
  22823. (cond
  22824. ((eq (car elem) 'headline)
  22825. (narrow-to-region
  22826. (org-element-property :begin elem)
  22827. (org-element-property :end elem)))
  22828. ((memq (car elem) org-element-greater-elements)
  22829. (narrow-to-region
  22830. (org-element-property :contents-begin elem)
  22831. (org-element-property :contents-end elem)))
  22832. (t
  22833. (narrow-to-region
  22834. (org-element-property :begin elem)
  22835. (org-element-property :end elem))))))
  22836. (defun org-transpose-element ()
  22837. "Transpose current and previous elements, keeping blank lines between.
  22838. Point is moved after both elements."
  22839. (interactive)
  22840. (org-skip-whitespace)
  22841. (let ((end (org-element-property :end (org-element-at-point))))
  22842. (org-drag-element-backward)
  22843. (goto-char end)))
  22844. (defun org-unindent-buffer ()
  22845. "Un-indent the visible part of the buffer.
  22846. Relative indentation (between items, inside blocks, etc.) isn't
  22847. modified."
  22848. (interactive)
  22849. (unless (eq major-mode 'org-mode)
  22850. (user-error "Cannot un-indent a buffer not in Org mode"))
  22851. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  22852. unindent-tree ; For byte-compiler.
  22853. (unindent-tree
  22854. (function
  22855. (lambda (contents)
  22856. (mapc
  22857. (lambda (element)
  22858. (if (memq (org-element-type element) '(headline section))
  22859. (funcall unindent-tree (org-element-contents element))
  22860. (save-excursion
  22861. (save-restriction
  22862. (narrow-to-region
  22863. (org-element-property :begin element)
  22864. (org-element-property :end element))
  22865. (org-do-remove-indentation)))))
  22866. (reverse contents))))))
  22867. (funcall unindent-tree (org-element-contents parse-tree))))
  22868. (defun org-show-children (&optional level)
  22869. "Show all direct subheadings of this heading.
  22870. Prefix arg LEVEL is how many levels below the current level should be shown.
  22871. Default is enough to cause the following heading to appear."
  22872. (save-excursion
  22873. (org-back-to-heading t)
  22874. (let* ((current-level (funcall outline-level))
  22875. (max-level (org-get-valid-level
  22876. current-level (if level (prefix-numeric-value level) 1)))
  22877. (end (save-excursion (org-end-of-subtree t t)))
  22878. (regexp-fmt "^\\*\\{%d,%s\\}\\(?: \\|$\\)")
  22879. (past-first-child nil)
  22880. ;; Make sure to skip inlinetasks.
  22881. (re (format regexp-fmt
  22882. current-level
  22883. (cond
  22884. ((not (featurep 'org-inlinetask)) "")
  22885. (org-odd-levels-only (- (* 2 org-inlinetask-min-level)
  22886. 3))
  22887. (t (1- org-inlinetask-min-level))))))
  22888. ;; Display parent heading.
  22889. (outline-flag-region (line-end-position 0) (line-end-position) nil)
  22890. (forward-line)
  22891. ;; Display children. First child may be deeper than expected
  22892. ;; MAX-LEVEL. Since we want to display it anyway, adjust
  22893. ;; MAX-LEVEL accordingly.
  22894. (while (re-search-forward re end t)
  22895. (unless past-first-child
  22896. (setq re (format regexp-fmt
  22897. current-level
  22898. (max (funcall outline-level) max-level)))
  22899. (setq past-first-child t))
  22900. (outline-flag-region (line-end-position 0) (line-end-position) nil)))))
  22901. (defun org-show-subtree ()
  22902. "Show everything after this heading at deeper levels."
  22903. (interactive)
  22904. (outline-flag-region
  22905. (point)
  22906. (save-excursion
  22907. (org-end-of-subtree t t))
  22908. nil))
  22909. (defun org-show-entry ()
  22910. "Show the body directly following this heading.
  22911. Show the heading too, if it is currently invisible."
  22912. (interactive)
  22913. (save-excursion
  22914. (ignore-errors
  22915. (org-back-to-heading t)
  22916. (outline-flag-region
  22917. (max (point-min) (1- (point)))
  22918. (save-excursion
  22919. (if (re-search-forward
  22920. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  22921. (match-beginning 1)
  22922. (point-max)))
  22923. nil)
  22924. (org-cycle-hide-drawers 'children))))
  22925. (defun org-make-options-regexp (kwds &optional extra)
  22926. "Make a regular expression for keyword lines.
  22927. KWDS is a list of keywords, as strings. Optional argument EXTRA,
  22928. when non-nil, is a regexp matching keywords names."
  22929. (concat "^[ \t]*#\\+\\("
  22930. (regexp-opt kwds)
  22931. (and extra (concat (and kwds "\\|") extra))
  22932. "\\):[ \t]*\\(.*\\)"))
  22933. ;; Make isearch reveal the necessary context
  22934. (defun org-isearch-end ()
  22935. "Reveal context after isearch exits."
  22936. (when isearch-success ; only if search was successful
  22937. (if (featurep 'xemacs)
  22938. ;; Under XEmacs, the hook is run in the correct place,
  22939. ;; we directly show the context.
  22940. (org-show-context 'isearch)
  22941. ;; In Emacs the hook runs *before* restoring the overlays.
  22942. ;; So we have to use a one-time post-command-hook to do this.
  22943. ;; (Emacs 22 has a special variable, see function `org-mode')
  22944. (unless (and (boundp 'isearch-mode-end-hook-quit)
  22945. isearch-mode-end-hook-quit)
  22946. ;; Only when the isearch was not quitted.
  22947. (org-add-hook 'post-command-hook 'org-isearch-post-command
  22948. 'append 'local)))))
  22949. (defun org-isearch-post-command ()
  22950. "Remove self from hook, and show context."
  22951. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  22952. (org-show-context 'isearch))
  22953. ;;;; Integration with and fixes for other packages
  22954. ;;; Imenu support
  22955. (defvar org-imenu-markers nil
  22956. "All markers currently used by Imenu.")
  22957. (make-variable-buffer-local 'org-imenu-markers)
  22958. (defun org-imenu-new-marker (&optional pos)
  22959. "Return a new marker for use by Imenu, and remember the marker."
  22960. (let ((m (make-marker)))
  22961. (move-marker m (or pos (point)))
  22962. (push m org-imenu-markers)
  22963. m))
  22964. (defun org-imenu-get-tree ()
  22965. "Produce the index for Imenu."
  22966. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  22967. (setq org-imenu-markers nil)
  22968. (let* ((n org-imenu-depth)
  22969. (re (concat "^" (org-get-limited-outline-regexp)))
  22970. (subs (make-vector (1+ n) nil))
  22971. (last-level 0)
  22972. m level head0 head)
  22973. (save-excursion
  22974. (save-restriction
  22975. (widen)
  22976. (goto-char (point-max))
  22977. (while (re-search-backward re nil t)
  22978. (setq level (org-reduced-level (funcall outline-level)))
  22979. (when (and (<= level n)
  22980. (looking-at org-complex-heading-regexp)
  22981. (setq head0 (org-match-string-no-properties 4)))
  22982. (setq head (org-link-display-format head0)
  22983. m (org-imenu-new-marker))
  22984. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  22985. (if (>= level last-level)
  22986. (push (cons head m) (aref subs level))
  22987. (push (cons head (aref subs (1+ level))) (aref subs level))
  22988. (loop for i from (1+ level) to n do (aset subs i nil)))
  22989. (setq last-level level)))))
  22990. (aref subs 1)))
  22991. (eval-after-load "imenu"
  22992. '(progn
  22993. (add-hook 'imenu-after-jump-hook
  22994. (lambda ()
  22995. (if (derived-mode-p 'org-mode)
  22996. (org-show-context 'org-goto))))))
  22997. (defun org-link-display-format (link)
  22998. "Replace a link with its the description.
  22999. If there is no description, use the link target."
  23000. (save-match-data
  23001. (if (string-match org-bracket-link-analytic-regexp link)
  23002. (replace-match (if (match-end 5)
  23003. (match-string 5 link)
  23004. (concat (match-string 1 link)
  23005. (match-string 3 link)))
  23006. nil t link)
  23007. link)))
  23008. (defun org-toggle-link-display ()
  23009. "Toggle the literal or descriptive display of links."
  23010. (interactive)
  23011. (if org-descriptive-links
  23012. (progn (org-remove-from-invisibility-spec '(org-link))
  23013. (org-restart-font-lock)
  23014. (setq org-descriptive-links nil))
  23015. (progn (add-to-invisibility-spec '(org-link))
  23016. (org-restart-font-lock)
  23017. (setq org-descriptive-links t))))
  23018. ;; Speedbar support
  23019. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  23020. "Overlay marking the agenda restriction line in speedbar.")
  23021. (overlay-put org-speedbar-restriction-lock-overlay
  23022. 'face 'org-agenda-restriction-lock)
  23023. (overlay-put org-speedbar-restriction-lock-overlay
  23024. 'help-echo "Agendas are currently limited to this item.")
  23025. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  23026. (defun org-speedbar-set-agenda-restriction ()
  23027. "Restrict future agenda commands to the location at point in speedbar.
  23028. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  23029. (interactive)
  23030. (require 'org-agenda)
  23031. (let (p m tp np dir txt)
  23032. (cond
  23033. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  23034. 'org-imenu t))
  23035. (setq m (get-text-property p 'org-imenu-marker))
  23036. (with-current-buffer (marker-buffer m)
  23037. (goto-char m)
  23038. (org-agenda-set-restriction-lock 'subtree)))
  23039. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  23040. 'speedbar-function 'speedbar-find-file))
  23041. (setq tp (previous-single-property-change
  23042. (1+ p) 'speedbar-function)
  23043. np (next-single-property-change
  23044. tp 'speedbar-function)
  23045. dir (speedbar-line-directory)
  23046. txt (buffer-substring-no-properties (or tp (point-min))
  23047. (or np (point-max))))
  23048. (with-current-buffer (find-file-noselect
  23049. (let ((default-directory dir))
  23050. (expand-file-name txt)))
  23051. (unless (derived-mode-p 'org-mode)
  23052. (user-error "Cannot restrict to non-Org-mode file"))
  23053. (org-agenda-set-restriction-lock 'file)))
  23054. (t (user-error "Don't know how to restrict Org-mode's agenda")))
  23055. (move-overlay org-speedbar-restriction-lock-overlay
  23056. (point-at-bol) (point-at-eol))
  23057. (setq current-prefix-arg nil)
  23058. (org-agenda-maybe-redo)))
  23059. (defvar speedbar-file-key-map)
  23060. (declare-function speedbar-add-supported-extension "speedbar" (extension))
  23061. (eval-after-load "speedbar"
  23062. '(progn
  23063. (speedbar-add-supported-extension ".org")
  23064. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  23065. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  23066. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  23067. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  23068. (add-hook 'speedbar-visiting-tag-hook
  23069. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  23070. ;;; Fixes and Hacks for problems with other packages
  23071. (defun org--flyspell-object-check-p (element)
  23072. "Non-nil when Flyspell can check object at point.
  23073. ELEMENT is the element at point."
  23074. (let ((object (save-excursion
  23075. (when (org-looking-at-p "\\>") (backward-char))
  23076. (org-element-context element))))
  23077. (case (org-element-type object)
  23078. ;; Prevent checks in links due to keybinding conflict with
  23079. ;; Flyspell.
  23080. ((code entity export-snippet inline-babel-call
  23081. inline-src-block line-break latex-fragment link macro
  23082. statistics-cookie target timestamp verbatim)
  23083. nil)
  23084. (footnote-reference
  23085. ;; Only in inline footnotes, within the definition.
  23086. (and (eq (org-element-property :type object) 'inline)
  23087. (< (save-excursion
  23088. (goto-char (org-element-property :begin object))
  23089. (search-forward ":" nil t 2))
  23090. (point))))
  23091. (otherwise t))))
  23092. (defun org-mode-flyspell-verify ()
  23093. "Function used for `flyspell-generic-check-word-predicate'."
  23094. (if (org-at-heading-p)
  23095. ;; At a headline or an inlinetask, check title only. This is
  23096. ;; faster than relying on `org-element-at-point'.
  23097. (and (save-excursion (beginning-of-line)
  23098. (and (let ((case-fold-search t))
  23099. (not (looking-at "\\*+ END[ \t]*$")))
  23100. (looking-at org-complex-heading-regexp)))
  23101. (match-beginning 4)
  23102. (>= (point) (match-beginning 4))
  23103. (or (not (match-beginning 5))
  23104. (< (point) (match-beginning 5))))
  23105. (let* ((element (org-element-at-point))
  23106. (post-affiliated (org-element-property :post-affiliated element)))
  23107. (cond
  23108. ;; Ignore checks in all affiliated keywords but captions.
  23109. ((< (point) post-affiliated)
  23110. (and (save-excursion
  23111. (beginning-of-line)
  23112. (let ((case-fold-search t)) (looking-at "[ \t]*#\\+CAPTION:")))
  23113. (> (point) (match-end 0))
  23114. (org--flyspell-object-check-p element)))
  23115. ;; Ignore checks in LOGBOOK (or equivalent) drawer.
  23116. ((let ((log (org-log-into-drawer)))
  23117. (and log
  23118. (let ((drawer (org-element-lineage element '(drawer))))
  23119. (and drawer
  23120. (eq (compare-strings
  23121. log nil nil
  23122. (org-element-property :drawer-name drawer) nil nil t)
  23123. t)))))
  23124. nil)
  23125. (t
  23126. (case (org-element-type element)
  23127. ((comment quote-section) t)
  23128. (comment-block
  23129. ;; Allow checks between block markers, not on them.
  23130. (and (> (line-beginning-position) post-affiliated)
  23131. (save-excursion
  23132. (end-of-line)
  23133. (skip-chars-forward " \r\t\n")
  23134. (< (point) (org-element-property :end element)))))
  23135. ;; Arbitrary list of keywords where checks are meaningful.
  23136. ;; Make sure point is on the value part of the element.
  23137. (keyword
  23138. (and (member (org-element-property :key element)
  23139. '("DESCRIPTION" "TITLE"))
  23140. (save-excursion
  23141. (search-backward ":" (line-beginning-position) t))))
  23142. ;; Check is globally allowed in paragraphs verse blocks and
  23143. ;; table rows (after affiliated keywords) but some objects
  23144. ;; must not be affected.
  23145. ((paragraph table-row verse-block)
  23146. (let ((cbeg (org-element-property :contents-begin element))
  23147. (cend (org-element-property :contents-end element)))
  23148. (and cbeg (>= (point) cbeg) (< (point) cend)
  23149. (org--flyspell-object-check-p element))))))))))
  23150. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  23151. (defun org-remove-flyspell-overlays-in (beg end)
  23152. "Remove flyspell overlays in region."
  23153. (and (org-bound-and-true-p flyspell-mode)
  23154. (fboundp 'flyspell-delete-region-overlays)
  23155. (flyspell-delete-region-overlays beg end)))
  23156. (defvar flyspell-delayed-commands)
  23157. (eval-after-load "flyspell"
  23158. '(add-to-list 'flyspell-delayed-commands 'org-self-insert-command))
  23159. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  23160. (eval-after-load "bookmark"
  23161. '(if (boundp 'bookmark-after-jump-hook)
  23162. ;; We can use the hook
  23163. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  23164. ;; Hook not available, use advice
  23165. (defadvice bookmark-jump (after org-make-visible activate)
  23166. "Make the position visible."
  23167. (org-bookmark-jump-unhide))))
  23168. ;; Make sure saveplace shows the location if it was hidden
  23169. (eval-after-load "saveplace"
  23170. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  23171. "Make the position visible."
  23172. (org-bookmark-jump-unhide)))
  23173. ;; Make sure ecb shows the location if it was hidden
  23174. (eval-after-load "ecb"
  23175. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  23176. "Make hierarchy visible when jumping into location from ECB tree buffer."
  23177. (if (derived-mode-p 'org-mode)
  23178. (org-show-context))))
  23179. (defun org-bookmark-jump-unhide ()
  23180. "Unhide the current position, to show the bookmark location."
  23181. (and (derived-mode-p 'org-mode)
  23182. (or (outline-invisible-p)
  23183. (save-excursion (goto-char (max (point-min) (1- (point))))
  23184. (outline-invisible-p)))
  23185. (org-show-context 'bookmark-jump)))
  23186. (defun org-mark-jump-unhide ()
  23187. "Make the point visible with `org-show-context' after jumping to the mark."
  23188. (when (and (derived-mode-p 'org-mode)
  23189. (outline-invisible-p))
  23190. (org-show-context 'mark-goto)))
  23191. (eval-after-load "simple"
  23192. '(defadvice pop-to-mark-command (after org-make-visible activate)
  23193. "Make the point visible with `org-show-context'."
  23194. (org-mark-jump-unhide)))
  23195. (eval-after-load "simple"
  23196. '(defadvice exchange-point-and-mark (after org-make-visible activate)
  23197. "Make the point visible with `org-show-context'."
  23198. (org-mark-jump-unhide)))
  23199. (eval-after-load "simple"
  23200. '(defadvice pop-global-mark (after org-make-visible activate)
  23201. "Make the point visible with `org-show-context'."
  23202. (org-mark-jump-unhide)))
  23203. ;; Make session.el ignore our circular variable
  23204. (defvar session-globals-exclude)
  23205. (eval-after-load "session"
  23206. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  23207. ;;;; Finish up
  23208. (provide 'org)
  23209. (run-hooks 'org-load-hook)
  23210. ;;; org.el ends here