org-element.el 195 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524
  1. ;;; org-element.el --- Parser And Applications for Org syntax
  2. ;; Copyright (C) 2012-2013 Free Software Foundation, Inc.
  3. ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
  4. ;; Keywords: outlines, hypermedia, calendar, wp
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;;
  18. ;; Org syntax can be divided into three categories: "Greater
  19. ;; elements", "Elements" and "Objects".
  20. ;;
  21. ;; Elements are related to the structure of the document. Indeed, all
  22. ;; elements are a cover for the document: each position within belongs
  23. ;; to at least one element.
  24. ;;
  25. ;; An element always starts and ends at the beginning of a line. With
  26. ;; a few exceptions (`clock', `headline', `inlinetask', `item',
  27. ;; `planning', `node-property', `quote-section' `section' and
  28. ;; `table-row' types), it can also accept a fixed set of keywords as
  29. ;; attributes. Those are called "affiliated keywords" to distinguish
  30. ;; them from other keywords, which are full-fledged elements. Almost
  31. ;; all affiliated keywords are referenced in
  32. ;; `org-element-affiliated-keywords'; the others are export attributes
  33. ;; and start with "ATTR_" prefix.
  34. ;;
  35. ;; Element containing other elements (and only elements) are called
  36. ;; greater elements. Concerned types are: `center-block', `drawer',
  37. ;; `dynamic-block', `footnote-definition', `headline', `inlinetask',
  38. ;; `item', `plain-list', `property-drawer', `quote-block', `section'
  39. ;; and `special-block'.
  40. ;;
  41. ;; Other element types are: `babel-call', `clock', `comment',
  42. ;; `comment-block', `diary-sexp', `example-block', `export-block',
  43. ;; `fixed-width', `horizontal-rule', `keyword', `latex-environment',
  44. ;; `node-property', `paragraph', `planning', `quote-section',
  45. ;; `src-block', `table', `table-row' and `verse-block'. Among them,
  46. ;; `paragraph' and `verse-block' types can contain Org objects and
  47. ;; plain text.
  48. ;;
  49. ;; Objects are related to document's contents. Some of them are
  50. ;; recursive. Associated types are of the following: `bold', `code',
  51. ;; `entity', `export-snippet', `footnote-reference',
  52. ;; `inline-babel-call', `inline-src-block', `italic',
  53. ;; `latex-fragment', `line-break', `link', `macro', `radio-target',
  54. ;; `statistics-cookie', `strike-through', `subscript', `superscript',
  55. ;; `table-cell', `target', `timestamp', `underline' and `verbatim'.
  56. ;;
  57. ;; Some elements also have special properties whose value can hold
  58. ;; objects themselves (i.e. an item tag or a headline name). Such
  59. ;; values are called "secondary strings". Any object belongs to
  60. ;; either an element or a secondary string.
  61. ;;
  62. ;; Notwithstanding affiliated keywords, each greater element, element
  63. ;; and object has a fixed set of properties attached to it. Among
  64. ;; them, four are shared by all types: `:begin' and `:end', which
  65. ;; refer to the beginning and ending buffer positions of the
  66. ;; considered element or object, `:post-blank', which holds the number
  67. ;; of blank lines, or white spaces, at its end and `:parent' which
  68. ;; refers to the element or object containing it. Greater elements,
  69. ;; elements and objects containing objects will also have
  70. ;; `:contents-begin' and `:contents-end' properties to delimit
  71. ;; contents. Eventually, greater elements and elements accepting
  72. ;; affiliated keywords will have a `:post-affiliated' property,
  73. ;; referring to the buffer position after all such keywords.
  74. ;;
  75. ;; At the lowest level, a `:parent' property is also attached to any
  76. ;; string, as a text property.
  77. ;;
  78. ;; Lisp-wise, an element or an object can be represented as a list.
  79. ;; It follows the pattern (TYPE PROPERTIES CONTENTS), where:
  80. ;; TYPE is a symbol describing the Org element or object.
  81. ;; PROPERTIES is the property list attached to it. See docstring of
  82. ;; appropriate parsing function to get an exhaustive
  83. ;; list.
  84. ;; CONTENTS is a list of elements, objects or raw strings contained
  85. ;; in the current element or object, when applicable.
  86. ;;
  87. ;; An Org buffer is a nested list of such elements and objects, whose
  88. ;; type is `org-data' and properties is nil.
  89. ;;
  90. ;; The first part of this file defines Org syntax, while the second
  91. ;; one provide accessors and setters functions.
  92. ;;
  93. ;; The next part implements a parser and an interpreter for each
  94. ;; element and object type in Org syntax.
  95. ;;
  96. ;; The following part creates a fully recursive buffer parser. It
  97. ;; also provides a tool to map a function to elements or objects
  98. ;; matching some criteria in the parse tree. Functions of interest
  99. ;; are `org-element-parse-buffer', `org-element-map' and, to a lesser
  100. ;; extent, `org-element-parse-secondary-string'.
  101. ;;
  102. ;; The penultimate part is the cradle of an interpreter for the
  103. ;; obtained parse tree: `org-element-interpret-data'.
  104. ;;
  105. ;; The library ends by furnishing `org-element-at-point' function, and
  106. ;; a way to give information about document structure around point
  107. ;; with `org-element-context'. A simple cache mechanism is also
  108. ;; provided for these functions.
  109. ;;; Code:
  110. (eval-when-compile (require 'cl))
  111. (require 'org)
  112. ;;; Definitions And Rules
  113. ;;
  114. ;; Define elements, greater elements and specify recursive objects,
  115. ;; along with the affiliated keywords recognized. Also set up
  116. ;; restrictions on recursive objects combinations.
  117. ;;
  118. ;; These variables really act as a control center for the parsing
  119. ;; process.
  120. (defconst org-element-paragraph-separate
  121. (concat "^\\(?:"
  122. ;; Headlines, inlinetasks.
  123. org-outline-regexp "\\|"
  124. ;; Footnote definitions.
  125. "\\[\\(?:[0-9]+\\|fn:[-_[:word:]]+\\)\\]" "\\|"
  126. ;; Diary sexps.
  127. "%%(" "\\|"
  128. "[ \t]*\\(?:"
  129. ;; Empty lines.
  130. "$" "\\|"
  131. ;; Tables (any type).
  132. "\\(?:|\\|\\+-[-+]\\)" "\\|"
  133. ;; Blocks (any type), Babel calls and keywords. Note: this
  134. ;; is only an indication and need some thorough check.
  135. "#\\(?:[+ ]\\|$\\)" "\\|"
  136. ;; Drawers (any type) and fixed-width areas. This is also
  137. ;; only an indication.
  138. ":" "\\|"
  139. ;; Horizontal rules.
  140. "-\\{5,\\}[ \t]*$" "\\|"
  141. ;; LaTeX environments.
  142. "\\\\begin{\\([A-Za-z0-9]+\\*?\\)}" "\\|"
  143. ;; Planning and Clock lines.
  144. (regexp-opt (list org-scheduled-string
  145. org-deadline-string
  146. org-closed-string
  147. org-clock-string))
  148. "\\|"
  149. ;; Lists.
  150. (let ((term (case org-plain-list-ordered-item-terminator
  151. (?\) ")") (?. "\\.") (otherwise "[.)]")))
  152. (alpha (and org-list-allow-alphabetical "\\|[A-Za-z]")))
  153. (concat "\\(?:[-+*]\\|\\(?:[0-9]+" alpha "\\)" term "\\)"
  154. "\\(?:[ \t]\\|$\\)"))
  155. "\\)\\)")
  156. "Regexp to separate paragraphs in an Org buffer.
  157. In the case of lines starting with \"#\" and \":\", this regexp
  158. is not sufficient to know if point is at a paragraph ending. See
  159. `org-element-paragraph-parser' for more information.")
  160. (defconst org-element-all-elements
  161. '(babel-call center-block clock comment comment-block diary-sexp drawer
  162. dynamic-block example-block export-block fixed-width
  163. footnote-definition headline horizontal-rule inlinetask item
  164. keyword latex-environment node-property paragraph plain-list
  165. planning property-drawer quote-block quote-section section
  166. special-block src-block table table-row verse-block)
  167. "Complete list of element types.")
  168. (defconst org-element-greater-elements
  169. '(center-block drawer dynamic-block footnote-definition headline inlinetask
  170. item plain-list property-drawer quote-block section
  171. special-block table)
  172. "List of recursive element types aka Greater Elements.")
  173. (defconst org-element-all-successors
  174. '(export-snippet footnote-reference inline-babel-call inline-src-block
  175. latex-or-entity line-break link macro plain-link radio-target
  176. statistics-cookie sub/superscript table-cell target
  177. text-markup timestamp)
  178. "Complete list of successors.")
  179. (defconst org-element-object-successor-alist
  180. '((subscript . sub/superscript) (superscript . sub/superscript)
  181. (bold . text-markup) (code . text-markup) (italic . text-markup)
  182. (strike-through . text-markup) (underline . text-markup)
  183. (verbatim . text-markup) (entity . latex-or-entity)
  184. (latex-fragment . latex-or-entity))
  185. "Alist of translations between object type and successor name.
  186. Sharing the same successor comes handy when, for example, the
  187. regexp matching one object can also match the other object.")
  188. (defconst org-element-all-objects
  189. '(bold code entity export-snippet footnote-reference inline-babel-call
  190. inline-src-block italic line-break latex-fragment link macro
  191. radio-target statistics-cookie strike-through subscript superscript
  192. table-cell target timestamp underline verbatim)
  193. "Complete list of object types.")
  194. (defconst org-element-recursive-objects
  195. '(bold italic link subscript radio-target strike-through superscript
  196. table-cell underline)
  197. "List of recursive object types.")
  198. (defvar org-element-block-name-alist
  199. '(("CENTER" . org-element-center-block-parser)
  200. ("COMMENT" . org-element-comment-block-parser)
  201. ("EXAMPLE" . org-element-example-block-parser)
  202. ("QUOTE" . org-element-quote-block-parser)
  203. ("SRC" . org-element-src-block-parser)
  204. ("VERSE" . org-element-verse-block-parser))
  205. "Alist between block names and the associated parsing function.
  206. Names must be uppercase. Any block whose name has no association
  207. is parsed with `org-element-special-block-parser'.")
  208. (defconst org-element-link-type-is-file
  209. '("file" "file+emacs" "file+sys" "docview")
  210. "List of link types equivalent to \"file\".
  211. Only these types can accept search options and an explicit
  212. application to open them.")
  213. (defconst org-element-affiliated-keywords
  214. '("CAPTION" "DATA" "HEADER" "HEADERS" "LABEL" "NAME" "PLOT" "RESNAME" "RESULT"
  215. "RESULTS" "SOURCE" "SRCNAME" "TBLNAME")
  216. "List of affiliated keywords as strings.
  217. By default, all keywords setting attributes (i.e. \"ATTR_LATEX\")
  218. are affiliated keywords and need not to be in this list.")
  219. (defconst org-element--affiliated-re
  220. (format "[ \t]*#\\+%s:"
  221. ;; Regular affiliated keywords.
  222. (format "\\(%s\\|ATTR_[-_A-Za-z0-9]+\\)\\(?:\\[\\(.*\\)\\]\\)?"
  223. (regexp-opt org-element-affiliated-keywords)))
  224. "Regexp matching any affiliated keyword.
  225. Keyword name is put in match group 1. Moreover, if keyword
  226. belongs to `org-element-dual-keywords', put the dual value in
  227. match group 2.
  228. Don't modify it, set `org-element-affiliated-keywords' instead.")
  229. (defconst org-element-keyword-translation-alist
  230. '(("DATA" . "NAME") ("LABEL" . "NAME") ("RESNAME" . "NAME")
  231. ("SOURCE" . "NAME") ("SRCNAME" . "NAME") ("TBLNAME" . "NAME")
  232. ("RESULT" . "RESULTS") ("HEADERS" . "HEADER"))
  233. "Alist of usual translations for keywords.
  234. The key is the old name and the value the new one. The property
  235. holding their value will be named after the translated name.")
  236. (defconst org-element-multiple-keywords '("CAPTION" "HEADER")
  237. "List of affiliated keywords that can occur more than once in an element.
  238. Their value will be consed into a list of strings, which will be
  239. returned as the value of the property.
  240. This list is checked after translations have been applied. See
  241. `org-element-keyword-translation-alist'.
  242. By default, all keywords setting attributes (i.e. \"ATTR_LATEX\")
  243. allow multiple occurrences and need not to be in this list.")
  244. (defconst org-element-parsed-keywords '("CAPTION")
  245. "List of affiliated keywords whose value can be parsed.
  246. Their value will be stored as a secondary string: a list of
  247. strings and objects.
  248. This list is checked after translations have been applied. See
  249. `org-element-keyword-translation-alist'.")
  250. (defconst org-element-dual-keywords '("CAPTION" "RESULTS")
  251. "List of affiliated keywords which can have a secondary value.
  252. In Org syntax, they can be written with optional square brackets
  253. before the colons. For example, RESULTS keyword can be
  254. associated to a hash value with the following:
  255. #+RESULTS[hash-string]: some-source
  256. This list is checked after translations have been applied. See
  257. `org-element-keyword-translation-alist'.")
  258. (defconst org-element-document-properties '("AUTHOR" "DATE" "TITLE")
  259. "List of properties associated to the whole document.
  260. Any keyword in this list will have its value parsed and stored as
  261. a secondary string.")
  262. (defconst org-element-object-restrictions
  263. (let* ((standard-set
  264. (remq 'plain-link (remq 'table-cell org-element-all-successors)))
  265. (standard-set-no-line-break (remq 'line-break standard-set)))
  266. `((bold ,@standard-set)
  267. (footnote-reference ,@standard-set)
  268. (headline ,@standard-set-no-line-break)
  269. (inlinetask ,@standard-set-no-line-break)
  270. (italic ,@standard-set)
  271. (item ,@standard-set-no-line-break)
  272. (keyword ,@standard-set)
  273. ;; Ignore all links excepted plain links in a link description.
  274. ;; Also ignore radio-targets and line breaks.
  275. (link export-snippet inline-babel-call inline-src-block latex-or-entity
  276. macro plain-link statistics-cookie sub/superscript text-markup)
  277. (paragraph ,@standard-set)
  278. ;; Remove any variable object from radio target as it would
  279. ;; prevent it from being properly recognized.
  280. (radio-target latex-or-entity sub/superscript)
  281. (strike-through ,@standard-set)
  282. (subscript ,@standard-set)
  283. (superscript ,@standard-set)
  284. ;; Ignore inline babel call and inline src block as formulas are
  285. ;; possible. Also ignore line breaks and statistics cookies.
  286. (table-cell export-snippet footnote-reference latex-or-entity link macro
  287. radio-target sub/superscript target text-markup timestamp)
  288. (table-row table-cell)
  289. (underline ,@standard-set)
  290. (verse-block ,@standard-set)))
  291. "Alist of objects restrictions.
  292. CAR is an element or object type containing objects and CDR is
  293. a list of successors that will be called within an element or
  294. object of such type.
  295. For example, in a `radio-target' object, one can only find
  296. entities, latex-fragments, subscript and superscript.
  297. This alist also applies to secondary string. For example, an
  298. `headline' type element doesn't directly contain objects, but
  299. still has an entry since one of its properties (`:title') does.")
  300. (defconst org-element-secondary-value-alist
  301. '((headline . :title)
  302. (inlinetask . :title)
  303. (item . :tag)
  304. (footnote-reference . :inline-definition))
  305. "Alist between element types and location of secondary value.")
  306. (defconst org-element-object-variables '(org-link-abbrev-alist-local)
  307. "List of buffer-local variables used when parsing objects.
  308. These variables are copied to the temporary buffer created by
  309. `org-export-secondary-string'.")
  310. ;;; Accessors and Setters
  311. ;;
  312. ;; Provide four accessors: `org-element-type', `org-element-property'
  313. ;; `org-element-contents' and `org-element-restriction'.
  314. ;;
  315. ;; Setter functions allow to modify elements by side effect. There is
  316. ;; `org-element-put-property', `org-element-set-contents',
  317. ;; `org-element-set-element' and `org-element-adopt-element'. Note
  318. ;; that `org-element-set-element' and `org-element-adopt-elements' are
  319. ;; higher level functions since also update `:parent' property.
  320. (defsubst org-element-type (element)
  321. "Return type of ELEMENT.
  322. The function returns the type of the element or object provided.
  323. It can also return the following special value:
  324. `plain-text' for a string
  325. `org-data' for a complete document
  326. nil in any other case."
  327. (cond
  328. ((not (consp element)) (and (stringp element) 'plain-text))
  329. ((symbolp (car element)) (car element))))
  330. (defsubst org-element-property (property element)
  331. "Extract the value from the PROPERTY of an ELEMENT."
  332. (if (stringp element) (get-text-property 0 property element)
  333. (plist-get (nth 1 element) property)))
  334. (defsubst org-element-contents (element)
  335. "Extract contents from an ELEMENT."
  336. (cond ((not (consp element)) nil)
  337. ((symbolp (car element)) (nthcdr 2 element))
  338. (t element)))
  339. (defsubst org-element-restriction (element)
  340. "Return restriction associated to ELEMENT.
  341. ELEMENT can be an element, an object or a symbol representing an
  342. element or object type."
  343. (cdr (assq (if (symbolp element) element (org-element-type element))
  344. org-element-object-restrictions)))
  345. (defsubst org-element-put-property (element property value)
  346. "In ELEMENT set PROPERTY to VALUE.
  347. Return modified element."
  348. (if (stringp element) (org-add-props element nil property value)
  349. (setcar (cdr element) (plist-put (nth 1 element) property value))
  350. element))
  351. (defsubst org-element-set-contents (element &rest contents)
  352. "Set ELEMENT contents to CONTENTS.
  353. Return modified element."
  354. (cond ((not element) (list contents))
  355. ((not (symbolp (car element))) contents)
  356. ((cdr element) (setcdr (cdr element) contents))
  357. (t (nconc element contents))))
  358. (defsubst org-element-set-element (old new)
  359. "Replace element or object OLD with element or object NEW.
  360. The function takes care of setting `:parent' property for NEW."
  361. ;; Since OLD is going to be changed into NEW by side-effect, first
  362. ;; make sure that every element or object within NEW has OLD as
  363. ;; parent.
  364. (mapc (lambda (blob) (org-element-put-property blob :parent old))
  365. (org-element-contents new))
  366. ;; Transfer contents.
  367. (apply 'org-element-set-contents old (org-element-contents new))
  368. ;; Ensure NEW has same parent as OLD, then overwrite OLD properties
  369. ;; with NEW's.
  370. (org-element-put-property new :parent (org-element-property :parent old))
  371. (setcar (cdr old) (nth 1 new))
  372. ;; Transfer type.
  373. (setcar old (car new)))
  374. (defsubst org-element-adopt-elements (parent &rest children)
  375. "Append elements to the contents of another element.
  376. PARENT is an element or object. CHILDREN can be elements,
  377. objects, or a strings.
  378. The function takes care of setting `:parent' property for CHILD.
  379. Return parent element."
  380. ;; Link every child to PARENT. If PARENT is nil, it is a secondary
  381. ;; string: parent is the list itself.
  382. (mapc (lambda (child)
  383. (org-element-put-property child :parent (or parent children)))
  384. children)
  385. ;; Add CHILDREN at the end of PARENT contents.
  386. (when parent
  387. (apply 'org-element-set-contents
  388. parent
  389. (nconc (org-element-contents parent) children)))
  390. ;; Return modified PARENT element.
  391. (or parent children))
  392. ;;; Greater elements
  393. ;;
  394. ;; For each greater element type, we define a parser and an
  395. ;; interpreter.
  396. ;;
  397. ;; A parser returns the element or object as the list described above.
  398. ;; Most of them accepts no argument. Though, exceptions exist. Hence
  399. ;; every element containing a secondary string (see
  400. ;; `org-element-secondary-value-alist') will accept an optional
  401. ;; argument to toggle parsing of that secondary string. Moreover,
  402. ;; `item' parser requires current list's structure as its first
  403. ;; element.
  404. ;;
  405. ;; An interpreter accepts two arguments: the list representation of
  406. ;; the element or object, and its contents. The latter may be nil,
  407. ;; depending on the element or object considered. It returns the
  408. ;; appropriate Org syntax, as a string.
  409. ;;
  410. ;; Parsing functions must follow the naming convention:
  411. ;; org-element-TYPE-parser, where TYPE is greater element's type, as
  412. ;; defined in `org-element-greater-elements'.
  413. ;;
  414. ;; Similarly, interpreting functions must follow the naming
  415. ;; convention: org-element-TYPE-interpreter.
  416. ;;
  417. ;; With the exception of `headline' and `item' types, greater elements
  418. ;; cannot contain other greater elements of their own type.
  419. ;;
  420. ;; Beside implementing a parser and an interpreter, adding a new
  421. ;; greater element requires to tweak `org-element--current-element'.
  422. ;; Moreover, the newly defined type must be added to both
  423. ;; `org-element-all-elements' and `org-element-greater-elements'.
  424. ;;;; Center Block
  425. (defun org-element-center-block-parser (limit affiliated)
  426. "Parse a center block.
  427. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  428. the buffer position at the beginning of the first affiliated
  429. keyword and CDR is a plist of affiliated keywords along with
  430. their value.
  431. Return a list whose CAR is `center-block' and CDR is a plist
  432. containing `:begin', `:end', `:contents-begin', `:contents-end',
  433. `:post-blank' and `:post-affiliated' keywords.
  434. Assume point is at the beginning of the block."
  435. (let ((case-fold-search t))
  436. (if (not (save-excursion
  437. (re-search-forward "^[ \t]*#\\+END_CENTER[ \t]*$" limit t)))
  438. ;; Incomplete block: parse it as a paragraph.
  439. (org-element-paragraph-parser limit affiliated)
  440. (let ((block-end-line (match-beginning 0)))
  441. (let* ((begin (car affiliated))
  442. (post-affiliated (point))
  443. ;; Empty blocks have no contents.
  444. (contents-begin (progn (forward-line)
  445. (and (< (point) block-end-line)
  446. (point))))
  447. (contents-end (and contents-begin block-end-line))
  448. (pos-before-blank (progn (goto-char block-end-line)
  449. (forward-line)
  450. (point)))
  451. (end (save-excursion
  452. (skip-chars-forward " \r\t\n" limit)
  453. (if (eobp) (point) (line-beginning-position)))))
  454. (list 'center-block
  455. (nconc
  456. (list :begin begin
  457. :end end
  458. :contents-begin contents-begin
  459. :contents-end contents-end
  460. :post-blank (count-lines pos-before-blank end)
  461. :post-affiliated post-affiliated)
  462. (cdr affiliated))))))))
  463. (defun org-element-center-block-interpreter (center-block contents)
  464. "Interpret CENTER-BLOCK element as Org syntax.
  465. CONTENTS is the contents of the element."
  466. (format "#+BEGIN_CENTER\n%s#+END_CENTER" contents))
  467. ;;;; Drawer
  468. (defun org-element-drawer-parser (limit affiliated)
  469. "Parse a drawer.
  470. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  471. the buffer position at the beginning of the first affiliated
  472. keyword and CDR is a plist of affiliated keywords along with
  473. their value.
  474. Return a list whose CAR is `drawer' and CDR is a plist containing
  475. `:drawer-name', `:begin', `:end', `:contents-begin',
  476. `:contents-end', `:post-blank' and `:post-affiliated' keywords.
  477. Assume point is at beginning of drawer."
  478. (let ((case-fold-search t))
  479. (if (not (save-excursion (re-search-forward "^[ \t]*:END:[ \t]*$" limit t)))
  480. ;; Incomplete drawer: parse it as a paragraph.
  481. (org-element-paragraph-parser limit affiliated)
  482. (save-excursion
  483. (let* ((drawer-end-line (match-beginning 0))
  484. (name (progn (looking-at org-drawer-regexp)
  485. (org-match-string-no-properties 1)))
  486. (begin (car affiliated))
  487. (post-affiliated (point))
  488. ;; Empty drawers have no contents.
  489. (contents-begin (progn (forward-line)
  490. (and (< (point) drawer-end-line)
  491. (point))))
  492. (contents-end (and contents-begin drawer-end-line))
  493. (pos-before-blank (progn (goto-char drawer-end-line)
  494. (forward-line)
  495. (point)))
  496. (end (progn (skip-chars-forward " \r\t\n" limit)
  497. (if (eobp) (point) (line-beginning-position)))))
  498. (list 'drawer
  499. (nconc
  500. (list :begin begin
  501. :end end
  502. :drawer-name name
  503. :contents-begin contents-begin
  504. :contents-end contents-end
  505. :post-blank (count-lines pos-before-blank end)
  506. :post-affiliated post-affiliated)
  507. (cdr affiliated))))))))
  508. (defun org-element-drawer-interpreter (drawer contents)
  509. "Interpret DRAWER element as Org syntax.
  510. CONTENTS is the contents of the element."
  511. (format ":%s:\n%s:END:"
  512. (org-element-property :drawer-name drawer)
  513. contents))
  514. ;;;; Dynamic Block
  515. (defun org-element-dynamic-block-parser (limit affiliated)
  516. "Parse a dynamic block.
  517. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  518. the buffer position at the beginning of the first affiliated
  519. keyword and CDR is a plist of affiliated keywords along with
  520. their value.
  521. Return a list whose CAR is `dynamic-block' and CDR is a plist
  522. containing `:block-name', `:begin', `:end', `:contents-begin',
  523. `:contents-end', `:arguments', `:post-blank' and
  524. `:post-affiliated' keywords.
  525. Assume point is at beginning of dynamic block."
  526. (let ((case-fold-search t))
  527. (if (not (save-excursion
  528. (re-search-forward "^[ \t]*#\\+END:?[ \t]*$" limit t)))
  529. ;; Incomplete block: parse it as a paragraph.
  530. (org-element-paragraph-parser limit affiliated)
  531. (let ((block-end-line (match-beginning 0)))
  532. (save-excursion
  533. (let* ((name (progn (looking-at org-dblock-start-re)
  534. (org-match-string-no-properties 1)))
  535. (arguments (org-match-string-no-properties 3))
  536. (begin (car affiliated))
  537. (post-affiliated (point))
  538. ;; Empty blocks have no contents.
  539. (contents-begin (progn (forward-line)
  540. (and (< (point) block-end-line)
  541. (point))))
  542. (contents-end (and contents-begin block-end-line))
  543. (pos-before-blank (progn (goto-char block-end-line)
  544. (forward-line)
  545. (point)))
  546. (end (progn (skip-chars-forward " \r\t\n" limit)
  547. (if (eobp) (point) (line-beginning-position)))))
  548. (list 'dynamic-block
  549. (nconc
  550. (list :begin begin
  551. :end end
  552. :block-name name
  553. :arguments arguments
  554. :contents-begin contents-begin
  555. :contents-end contents-end
  556. :post-blank (count-lines pos-before-blank end)
  557. :post-affiliated post-affiliated)
  558. (cdr affiliated)))))))))
  559. (defun org-element-dynamic-block-interpreter (dynamic-block contents)
  560. "Interpret DYNAMIC-BLOCK element as Org syntax.
  561. CONTENTS is the contents of the element."
  562. (format "#+BEGIN: %s%s\n%s#+END:"
  563. (org-element-property :block-name dynamic-block)
  564. (let ((args (org-element-property :arguments dynamic-block)))
  565. (and args (concat " " args)))
  566. contents))
  567. ;;;; Footnote Definition
  568. (defun org-element-footnote-definition-parser (limit affiliated)
  569. "Parse a footnote definition.
  570. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  571. the buffer position at the beginning of the first affiliated
  572. keyword and CDR is a plist of affiliated keywords along with
  573. their value.
  574. Return a list whose CAR is `footnote-definition' and CDR is
  575. a plist containing `:label', `:begin' `:end', `:contents-begin',
  576. `:contents-end', `:post-blank' and `:post-affiliated' keywords.
  577. Assume point is at the beginning of the footnote definition."
  578. (save-excursion
  579. (let* ((label (progn (looking-at org-footnote-definition-re)
  580. (org-match-string-no-properties 1)))
  581. (begin (car affiliated))
  582. (post-affiliated (point))
  583. (ending (save-excursion
  584. (if (progn
  585. (end-of-line)
  586. (re-search-forward
  587. (concat org-outline-regexp-bol "\\|"
  588. org-footnote-definition-re "\\|"
  589. "^\\([ \t]*\n\\)\\{2,\\}") limit 'move))
  590. (match-beginning 0)
  591. (point))))
  592. (contents-begin (progn
  593. (search-forward "]")
  594. (skip-chars-forward " \r\t\n" ending)
  595. (cond ((= (point) ending) nil)
  596. ((= (line-beginning-position) begin) (point))
  597. (t (line-beginning-position)))))
  598. (contents-end (and contents-begin ending))
  599. (end (progn (goto-char ending)
  600. (skip-chars-forward " \r\t\n" limit)
  601. (if (eobp) (point) (line-beginning-position)))))
  602. (list 'footnote-definition
  603. (nconc
  604. (list :label label
  605. :begin begin
  606. :end end
  607. :contents-begin contents-begin
  608. :contents-end contents-end
  609. :post-blank (count-lines ending end)
  610. :post-affiliated post-affiliated)
  611. (cdr affiliated))))))
  612. (defun org-element-footnote-definition-interpreter (footnote-definition contents)
  613. "Interpret FOOTNOTE-DEFINITION element as Org syntax.
  614. CONTENTS is the contents of the footnote-definition."
  615. (concat (format "[%s]" (org-element-property :label footnote-definition))
  616. " "
  617. contents))
  618. ;;;; Headline
  619. (defun org-element-headline-parser (limit &optional raw-secondary-p)
  620. "Parse a headline.
  621. Return a list whose CAR is `headline' and CDR is a plist
  622. containing `:raw-value', `:title', `:alt-title', `:begin',
  623. `:end', `:pre-blank', `:contents-begin' and `:contents-end',
  624. `:level', `:priority', `:tags', `:todo-keyword',`:todo-type',
  625. `:scheduled', `:deadline', `:closed', `:quotedp', `:archivedp',
  626. `:commentedp' and `:footnote-section-p' keywords.
  627. The plist also contains any property set in the property drawer,
  628. with its name in upper cases and colons added at the
  629. beginning (i.e. `:CUSTOM_ID').
  630. When RAW-SECONDARY-P is non-nil, headline's title will not be
  631. parsed as a secondary string, but as a plain string instead.
  632. Assume point is at beginning of the headline."
  633. (save-excursion
  634. (let* ((components (org-heading-components))
  635. (level (nth 1 components))
  636. (todo (nth 2 components))
  637. (todo-type
  638. (and todo (if (member todo org-done-keywords) 'done 'todo)))
  639. (tags (let ((raw-tags (nth 5 components)))
  640. (and raw-tags (org-split-string raw-tags ":"))))
  641. (raw-value (or (nth 4 components) ""))
  642. (quotedp
  643. (let ((case-fold-search nil))
  644. (string-match (format "^%s\\( \\|$\\)" org-quote-string)
  645. raw-value)))
  646. (commentedp
  647. (let ((case-fold-search nil))
  648. (string-match (format "^%s\\( \\|$\\)" org-comment-string)
  649. raw-value)))
  650. (archivedp (member org-archive-tag tags))
  651. (footnote-section-p (and org-footnote-section
  652. (string= org-footnote-section raw-value)))
  653. ;; Upcase property names. It avoids confusion between
  654. ;; properties obtained through property drawer and default
  655. ;; properties from the parser (e.g. `:end' and :END:)
  656. (standard-props
  657. (let (plist)
  658. (mapc
  659. (lambda (p)
  660. (setq plist
  661. (plist-put plist
  662. (intern (concat ":" (upcase (car p))))
  663. (cdr p))))
  664. (org-entry-properties nil 'standard))
  665. plist))
  666. (time-props
  667. ;; Read time properties on the line below the headline.
  668. (save-excursion
  669. (when (progn (forward-line)
  670. (looking-at org-planning-or-clock-line-re))
  671. (let ((end (line-end-position)) plist)
  672. (while (re-search-forward
  673. org-keyword-time-not-clock-regexp end t)
  674. (goto-char (match-end 1))
  675. (skip-chars-forward " \t")
  676. (let ((keyword (match-string 1))
  677. (time (org-element-timestamp-parser)))
  678. (cond ((equal keyword org-scheduled-string)
  679. (setq plist (plist-put plist :scheduled time)))
  680. ((equal keyword org-deadline-string)
  681. (setq plist (plist-put plist :deadline time)))
  682. (t (setq plist (plist-put plist :closed time))))))
  683. plist))))
  684. (begin (point))
  685. (end (save-excursion (goto-char (org-end-of-subtree t t))))
  686. (pos-after-head (progn (forward-line) (point)))
  687. (contents-begin (save-excursion
  688. (skip-chars-forward " \r\t\n" end)
  689. (and (/= (point) end) (line-beginning-position))))
  690. (contents-end (and contents-begin
  691. (progn (goto-char end)
  692. (skip-chars-backward " \r\t\n")
  693. (forward-line)
  694. (point)))))
  695. ;; Clean RAW-VALUE from any quote or comment string.
  696. (when (or quotedp commentedp)
  697. (let ((case-fold-search nil))
  698. (setq raw-value
  699. (replace-regexp-in-string
  700. (concat
  701. (regexp-opt (list org-quote-string org-comment-string))
  702. "\\(?: \\|$\\)")
  703. ""
  704. raw-value))))
  705. ;; Clean TAGS from archive tag, if any.
  706. (when archivedp (setq tags (delete org-archive-tag tags)))
  707. (let ((headline
  708. (list 'headline
  709. (nconc
  710. (list :raw-value raw-value
  711. :begin begin
  712. :end end
  713. :pre-blank
  714. (if (not contents-begin) 0
  715. (count-lines pos-after-head contents-begin))
  716. :contents-begin contents-begin
  717. :contents-end contents-end
  718. :level level
  719. :priority (nth 3 components)
  720. :tags tags
  721. :todo-keyword todo
  722. :todo-type todo-type
  723. :post-blank (count-lines
  724. (if (not contents-end) pos-after-head
  725. (goto-char contents-end)
  726. (forward-line)
  727. (point))
  728. end)
  729. :footnote-section-p footnote-section-p
  730. :archivedp archivedp
  731. :commentedp commentedp
  732. :quotedp quotedp)
  733. time-props
  734. standard-props))))
  735. (let ((alt-title (org-element-property :ALT_TITLE headline)))
  736. (when alt-title
  737. (org-element-put-property
  738. headline :alt-title
  739. (if raw-secondary-p alt-title
  740. (org-element-parse-secondary-string
  741. alt-title (org-element-restriction 'headline) headline)))))
  742. (org-element-put-property
  743. headline :title
  744. (if raw-secondary-p raw-value
  745. (org-element-parse-secondary-string
  746. raw-value (org-element-restriction 'headline) headline)))))))
  747. (defun org-element-headline-interpreter (headline contents)
  748. "Interpret HEADLINE element as Org syntax.
  749. CONTENTS is the contents of the element."
  750. (let* ((level (org-element-property :level headline))
  751. (todo (org-element-property :todo-keyword headline))
  752. (priority (org-element-property :priority headline))
  753. (title (org-element-interpret-data
  754. (org-element-property :title headline)))
  755. (tags (let ((tag-list (if (org-element-property :archivedp headline)
  756. (cons org-archive-tag
  757. (org-element-property :tags headline))
  758. (org-element-property :tags headline))))
  759. (and tag-list
  760. (format ":%s:" (mapconcat 'identity tag-list ":")))))
  761. (commentedp (org-element-property :commentedp headline))
  762. (quotedp (org-element-property :quotedp headline))
  763. (pre-blank (or (org-element-property :pre-blank headline) 0))
  764. (heading (concat (make-string (org-reduced-level level) ?*)
  765. (and todo (concat " " todo))
  766. (and quotedp (concat " " org-quote-string))
  767. (and commentedp (concat " " org-comment-string))
  768. (and priority
  769. (format " [#%s]" (char-to-string priority)))
  770. (cond ((and org-footnote-section
  771. (org-element-property
  772. :footnote-section-p headline))
  773. (concat " " org-footnote-section))
  774. (title (concat " " title))))))
  775. (concat heading
  776. ;; Align tags.
  777. (when tags
  778. (cond
  779. ((zerop org-tags-column) (format " %s" tags))
  780. ((< org-tags-column 0)
  781. (concat
  782. (make-string
  783. (max (- (+ org-tags-column (length heading) (length tags))) 1)
  784. ? )
  785. tags))
  786. (t
  787. (concat
  788. (make-string (max (- org-tags-column (length heading)) 1) ? )
  789. tags))))
  790. (make-string (1+ pre-blank) 10)
  791. contents)))
  792. ;;;; Inlinetask
  793. (defun org-element-inlinetask-parser (limit &optional raw-secondary-p)
  794. "Parse an inline task.
  795. Return a list whose CAR is `inlinetask' and CDR is a plist
  796. containing `:title', `:begin', `:end', `:contents-begin' and
  797. `:contents-end', `:level', `:priority', `:raw-value', `:tags',
  798. `:todo-keyword', `:todo-type', `:scheduled', `:deadline',
  799. `:closed' and `:post-blank' keywords.
  800. The plist also contains any property set in the property drawer,
  801. with its name in upper cases and colons added at the
  802. beginning (i.e. `:CUSTOM_ID').
  803. When optional argument RAW-SECONDARY-P is non-nil, inline-task's
  804. title will not be parsed as a secondary string, but as a plain
  805. string instead.
  806. Assume point is at beginning of the inline task."
  807. (save-excursion
  808. (let* ((begin (point))
  809. (components (org-heading-components))
  810. (todo (nth 2 components))
  811. (todo-type (and todo
  812. (if (member todo org-done-keywords) 'done 'todo)))
  813. (tags (let ((raw-tags (nth 5 components)))
  814. (and raw-tags (org-split-string raw-tags ":"))))
  815. (raw-value (or (nth 4 components) ""))
  816. ;; Upcase property names. It avoids confusion between
  817. ;; properties obtained through property drawer and default
  818. ;; properties from the parser (e.g. `:end' and :END:)
  819. (standard-props
  820. (let (plist)
  821. (mapc
  822. (lambda (p)
  823. (setq plist
  824. (plist-put plist
  825. (intern (concat ":" (upcase (car p))))
  826. (cdr p))))
  827. (org-entry-properties nil 'standard))
  828. plist))
  829. (time-props
  830. ;; Read time properties on the line below the inlinetask
  831. ;; opening string.
  832. (save-excursion
  833. (when (progn (forward-line)
  834. (looking-at org-planning-or-clock-line-re))
  835. (let ((end (line-end-position)) plist)
  836. (while (re-search-forward
  837. org-keyword-time-not-clock-regexp end t)
  838. (goto-char (match-end 1))
  839. (skip-chars-forward " \t")
  840. (let ((keyword (match-string 1))
  841. (time (org-element-timestamp-parser)))
  842. (cond ((equal keyword org-scheduled-string)
  843. (setq plist (plist-put plist :scheduled time)))
  844. ((equal keyword org-deadline-string)
  845. (setq plist (plist-put plist :deadline time)))
  846. (t (setq plist (plist-put plist :closed time))))))
  847. plist))))
  848. (task-end (save-excursion
  849. (end-of-line)
  850. (and (re-search-forward "^\\*+ END" limit t)
  851. (match-beginning 0))))
  852. (contents-begin (progn (forward-line)
  853. (and task-end (< (point) task-end) (point))))
  854. (contents-end (and contents-begin task-end))
  855. (before-blank (if (not task-end) (point)
  856. (goto-char task-end)
  857. (forward-line)
  858. (point)))
  859. (end (progn (skip-chars-forward " \r\t\n" limit)
  860. (if (eobp) (point) (line-beginning-position))))
  861. (inlinetask
  862. (list 'inlinetask
  863. (nconc
  864. (list :raw-value raw-value
  865. :begin begin
  866. :end end
  867. :contents-begin contents-begin
  868. :contents-end contents-end
  869. :level (nth 1 components)
  870. :priority (nth 3 components)
  871. :tags tags
  872. :todo-keyword todo
  873. :todo-type todo-type
  874. :post-blank (count-lines before-blank end))
  875. time-props
  876. standard-props))))
  877. (org-element-put-property
  878. inlinetask :title
  879. (if raw-secondary-p raw-value
  880. (org-element-parse-secondary-string
  881. raw-value
  882. (org-element-restriction 'inlinetask)
  883. inlinetask))))))
  884. (defun org-element-inlinetask-interpreter (inlinetask contents)
  885. "Interpret INLINETASK element as Org syntax.
  886. CONTENTS is the contents of inlinetask."
  887. (let* ((level (org-element-property :level inlinetask))
  888. (todo (org-element-property :todo-keyword inlinetask))
  889. (priority (org-element-property :priority inlinetask))
  890. (title (org-element-interpret-data
  891. (org-element-property :title inlinetask)))
  892. (tags (let ((tag-list (org-element-property :tags inlinetask)))
  893. (and tag-list
  894. (format ":%s:" (mapconcat 'identity tag-list ":")))))
  895. (task (concat (make-string level ?*)
  896. (and todo (concat " " todo))
  897. (and priority
  898. (format " [#%s]" (char-to-string priority)))
  899. (and title (concat " " title)))))
  900. (concat task
  901. ;; Align tags.
  902. (when tags
  903. (cond
  904. ((zerop org-tags-column) (format " %s" tags))
  905. ((< org-tags-column 0)
  906. (concat
  907. (make-string
  908. (max (- (+ org-tags-column (length task) (length tags))) 1)
  909. ? )
  910. tags))
  911. (t
  912. (concat
  913. (make-string (max (- org-tags-column (length task)) 1) ? )
  914. tags))))
  915. ;; Prefer degenerate inlinetasks when there are no
  916. ;; contents.
  917. (when contents
  918. (concat "\n"
  919. contents
  920. (make-string level ?*) " END")))))
  921. ;;;; Item
  922. (defun org-element-item-parser (limit struct &optional raw-secondary-p)
  923. "Parse an item.
  924. STRUCT is the structure of the plain list.
  925. Return a list whose CAR is `item' and CDR is a plist containing
  926. `:bullet', `:begin', `:end', `:contents-begin', `:contents-end',
  927. `:checkbox', `:counter', `:tag', `:structure' and `:post-blank'
  928. keywords.
  929. When optional argument RAW-SECONDARY-P is non-nil, item's tag, if
  930. any, will not be parsed as a secondary string, but as a plain
  931. string instead.
  932. Assume point is at the beginning of the item."
  933. (save-excursion
  934. (beginning-of-line)
  935. (looking-at org-list-full-item-re)
  936. (let* ((begin (point))
  937. (bullet (org-match-string-no-properties 1))
  938. (checkbox (let ((box (org-match-string-no-properties 3)))
  939. (cond ((equal "[ ]" box) 'off)
  940. ((equal "[X]" box) 'on)
  941. ((equal "[-]" box) 'trans))))
  942. (counter (let ((c (org-match-string-no-properties 2)))
  943. (save-match-data
  944. (cond
  945. ((not c) nil)
  946. ((string-match "[A-Za-z]" c)
  947. (- (string-to-char (upcase (match-string 0 c)))
  948. 64))
  949. ((string-match "[0-9]+" c)
  950. (string-to-number (match-string 0 c)))))))
  951. (end (progn (goto-char (nth 6 (assq (point) struct)))
  952. (unless (bolp) (forward-line))
  953. (point)))
  954. (contents-begin
  955. (progn (goto-char
  956. ;; Ignore tags in un-ordered lists: they are just
  957. ;; a part of item's body.
  958. (if (and (match-beginning 4)
  959. (save-match-data (string-match "[.)]" bullet)))
  960. (match-beginning 4)
  961. (match-end 0)))
  962. (skip-chars-forward " \r\t\n" limit)
  963. ;; If first line isn't empty, contents really start
  964. ;; at the text after item's meta-data.
  965. (if (= (point-at-bol) begin) (point) (point-at-bol))))
  966. (contents-end (progn (goto-char end)
  967. (skip-chars-backward " \r\t\n")
  968. (forward-line)
  969. (point)))
  970. (item
  971. (list 'item
  972. (list :bullet bullet
  973. :begin begin
  974. :end end
  975. ;; CONTENTS-BEGIN and CONTENTS-END may be
  976. ;; mixed up in the case of an empty item
  977. ;; separated from the next by a blank line.
  978. ;; Thus ensure the former is always the
  979. ;; smallest.
  980. :contents-begin (min contents-begin contents-end)
  981. :contents-end (max contents-begin contents-end)
  982. :checkbox checkbox
  983. :counter counter
  984. :structure struct
  985. :post-blank (count-lines contents-end end)))))
  986. (org-element-put-property
  987. item :tag
  988. (let ((raw-tag (org-list-get-tag begin struct)))
  989. (and raw-tag
  990. (if raw-secondary-p raw-tag
  991. (org-element-parse-secondary-string
  992. raw-tag (org-element-restriction 'item) item))))))))
  993. (defun org-element-item-interpreter (item contents)
  994. "Interpret ITEM element as Org syntax.
  995. CONTENTS is the contents of the element."
  996. (let* ((bullet (let ((bullet (org-element-property :bullet item)))
  997. (org-list-bullet-string
  998. (cond ((not (string-match "[0-9a-zA-Z]" bullet)) "- ")
  999. ((eq org-plain-list-ordered-item-terminator ?\)) "1)")
  1000. (t "1.")))))
  1001. (checkbox (org-element-property :checkbox item))
  1002. (counter (org-element-property :counter item))
  1003. (tag (let ((tag (org-element-property :tag item)))
  1004. (and tag (org-element-interpret-data tag))))
  1005. ;; Compute indentation.
  1006. (ind (make-string (length bullet) 32))
  1007. (item-starts-with-par-p
  1008. (eq (org-element-type (car (org-element-contents item)))
  1009. 'paragraph)))
  1010. ;; Indent contents.
  1011. (concat
  1012. bullet
  1013. (and counter (format "[@%d] " counter))
  1014. (case checkbox
  1015. (on "[X] ")
  1016. (off "[ ] ")
  1017. (trans "[-] "))
  1018. (and tag (format "%s :: " tag))
  1019. (when contents
  1020. (let ((contents (replace-regexp-in-string
  1021. "\\(^\\)[ \t]*\\S-" ind contents nil nil 1)))
  1022. (if item-starts-with-par-p (org-trim contents)
  1023. (concat "\n" contents)))))))
  1024. ;;;; Plain List
  1025. (defun org-element--list-struct (limit)
  1026. ;; Return structure of list at point. Internal function. See
  1027. ;; `org-list-struct' for details.
  1028. (let ((case-fold-search t)
  1029. (top-ind limit)
  1030. (item-re (org-item-re))
  1031. (inlinetask-re (and (featurep 'org-inlinetask) "^\\*+ "))
  1032. items struct)
  1033. (save-excursion
  1034. (catch 'exit
  1035. (while t
  1036. (cond
  1037. ;; At limit: end all items.
  1038. ((>= (point) limit)
  1039. (throw 'exit
  1040. (let ((end (progn (skip-chars-backward " \r\t\n")
  1041. (forward-line)
  1042. (point))))
  1043. (dolist (item items (sort (nconc items struct)
  1044. 'car-less-than-car))
  1045. (setcar (nthcdr 6 item) end)))))
  1046. ;; At list end: end all items.
  1047. ((looking-at org-list-end-re)
  1048. (throw 'exit (dolist (item items (sort (nconc items struct)
  1049. 'car-less-than-car))
  1050. (setcar (nthcdr 6 item) (point)))))
  1051. ;; At a new item: end previous sibling.
  1052. ((looking-at item-re)
  1053. (let ((ind (save-excursion (skip-chars-forward " \t")
  1054. (current-column))))
  1055. (setq top-ind (min top-ind ind))
  1056. (while (and items (<= ind (nth 1 (car items))))
  1057. (let ((item (pop items)))
  1058. (setcar (nthcdr 6 item) (point))
  1059. (push item struct)))
  1060. (push (progn (looking-at org-list-full-item-re)
  1061. (let ((bullet (match-string-no-properties 1)))
  1062. (list (point)
  1063. ind
  1064. bullet
  1065. (match-string-no-properties 2) ; counter
  1066. (match-string-no-properties 3) ; checkbox
  1067. ;; Description tag.
  1068. (and (save-match-data
  1069. (string-match "[-+*]" bullet))
  1070. (match-string-no-properties 4))
  1071. ;; Ending position, unknown so far.
  1072. nil)))
  1073. items))
  1074. (forward-line 1))
  1075. ;; Skip empty lines.
  1076. ((looking-at "^[ \t]*$") (forward-line))
  1077. ;; Skip inline tasks and blank lines along the way.
  1078. ((and inlinetask-re (looking-at inlinetask-re))
  1079. (forward-line)
  1080. (let ((origin (point)))
  1081. (when (re-search-forward inlinetask-re limit t)
  1082. (if (looking-at "^\\*+ END[ \t]*$") (forward-line)
  1083. (goto-char origin)))))
  1084. ;; At some text line. Check if it ends any previous item.
  1085. (t
  1086. (let ((ind (progn (skip-chars-forward " \t") (current-column))))
  1087. (when (<= ind top-ind)
  1088. (skip-chars-backward " \r\t\n")
  1089. (forward-line))
  1090. (while (<= ind (nth 1 (car items)))
  1091. (let ((item (pop items)))
  1092. (setcar (nthcdr 6 item) (line-beginning-position))
  1093. (push item struct)
  1094. (unless items
  1095. (throw 'exit (sort struct 'car-less-than-car))))))
  1096. ;; Skip blocks (any type) and drawers contents.
  1097. (cond
  1098. ((and (looking-at "#\\+BEGIN\\(:\\|_\\S-+\\)")
  1099. (re-search-forward
  1100. (format "^[ \t]*#\\+END%s[ \t]*$"
  1101. (org-match-string-no-properties 1))
  1102. limit t)))
  1103. ((and (looking-at org-drawer-regexp)
  1104. (re-search-forward "^[ \t]*:END:[ \t]*$" limit t))))
  1105. (forward-line))))))))
  1106. (defun org-element-plain-list-parser (limit affiliated structure)
  1107. "Parse a plain list.
  1108. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  1109. the buffer position at the beginning of the first affiliated
  1110. keyword and CDR is a plist of affiliated keywords along with
  1111. their value. STRUCTURE is the structure of the plain list being
  1112. parsed.
  1113. Return a list whose CAR is `plain-list' and CDR is a plist
  1114. containing `:type', `:begin', `:end', `:contents-begin' and
  1115. `:contents-end', `:structure', `:post-blank' and
  1116. `:post-affiliated' keywords.
  1117. Assume point is at the beginning of the list."
  1118. (save-excursion
  1119. (let* ((struct (or structure (org-element--list-struct limit)))
  1120. (type (cond ((org-looking-at-p "[ \t]*[A-Za-z0-9]") 'ordered)
  1121. ((nth 5 (assq (point) struct)) 'descriptive)
  1122. (t 'unordered)))
  1123. (contents-begin (point))
  1124. (begin (car affiliated))
  1125. (contents-end (let* ((item (assq contents-begin struct))
  1126. (ind (nth 1 item))
  1127. (pos (nth 6 item)))
  1128. (while (and (setq item (assq pos struct))
  1129. (= (nth 1 item) ind))
  1130. (setq pos (nth 6 item)))
  1131. pos))
  1132. (end (progn (goto-char contents-end)
  1133. (skip-chars-forward " \r\t\n" limit)
  1134. (if (= (point) limit) limit (line-beginning-position)))))
  1135. ;; Return value.
  1136. (list 'plain-list
  1137. (nconc
  1138. (list :type type
  1139. :begin begin
  1140. :end end
  1141. :contents-begin contents-begin
  1142. :contents-end contents-end
  1143. :structure struct
  1144. :post-blank (count-lines contents-end end)
  1145. :post-affiliated contents-begin)
  1146. (cdr affiliated))))))
  1147. (defun org-element-plain-list-interpreter (plain-list contents)
  1148. "Interpret PLAIN-LIST element as Org syntax.
  1149. CONTENTS is the contents of the element."
  1150. (with-temp-buffer
  1151. (insert contents)
  1152. (goto-char (point-min))
  1153. (org-list-repair)
  1154. (buffer-string)))
  1155. ;;;; Property Drawer
  1156. (defun org-element-property-drawer-parser (limit affiliated)
  1157. "Parse a property drawer.
  1158. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  1159. the buffer position at the beginning of the first affiliated
  1160. keyword and CDR is a plist of affiliated keywords along with
  1161. their value.
  1162. Return a list whose CAR is `property-drawer' and CDR is a plist
  1163. containing `:begin', `:end', `:contents-begin', `:contents-end',
  1164. `:post-blank' and `:post-affiliated' keywords.
  1165. Assume point is at the beginning of the property drawer."
  1166. (save-excursion
  1167. (let ((case-fold-search t))
  1168. (if (not (save-excursion
  1169. (re-search-forward "^[ \t]*:END:[ \t]*$" limit t)))
  1170. ;; Incomplete drawer: parse it as a paragraph.
  1171. (org-element-paragraph-parser limit affiliated)
  1172. (save-excursion
  1173. (let* ((drawer-end-line (match-beginning 0))
  1174. (begin (car affiliated))
  1175. (post-affiliated (point))
  1176. (contents-begin (progn (forward-line)
  1177. (and (< (point) drawer-end-line)
  1178. (point))))
  1179. (contents-end (and contents-begin drawer-end-line))
  1180. (pos-before-blank (progn (goto-char drawer-end-line)
  1181. (forward-line)
  1182. (point)))
  1183. (end (progn (skip-chars-forward " \r\t\n" limit)
  1184. (if (eobp) (point) (line-beginning-position)))))
  1185. (list 'property-drawer
  1186. (nconc
  1187. (list :begin begin
  1188. :end end
  1189. :contents-begin contents-begin
  1190. :contents-end contents-end
  1191. :post-blank (count-lines pos-before-blank end)
  1192. :post-affiliated post-affiliated)
  1193. (cdr affiliated)))))))))
  1194. (defun org-element-property-drawer-interpreter (property-drawer contents)
  1195. "Interpret PROPERTY-DRAWER element as Org syntax.
  1196. CONTENTS is the properties within the drawer."
  1197. (format ":PROPERTIES:\n%s:END:" contents))
  1198. ;;;; Quote Block
  1199. (defun org-element-quote-block-parser (limit affiliated)
  1200. "Parse a quote block.
  1201. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  1202. the buffer position at the beginning of the first affiliated
  1203. keyword and CDR is a plist of affiliated keywords along with
  1204. their value.
  1205. Return a list whose CAR is `quote-block' and CDR is a plist
  1206. containing `:begin', `:end', `:contents-begin', `:contents-end',
  1207. `:post-blank' and `:post-affiliated' keywords.
  1208. Assume point is at the beginning of the block."
  1209. (let ((case-fold-search t))
  1210. (if (not (save-excursion
  1211. (re-search-forward "^[ \t]*#\\+END_QUOTE[ \t]*$" limit t)))
  1212. ;; Incomplete block: parse it as a paragraph.
  1213. (org-element-paragraph-parser limit affiliated)
  1214. (let ((block-end-line (match-beginning 0)))
  1215. (save-excursion
  1216. (let* ((begin (car affiliated))
  1217. (post-affiliated (point))
  1218. ;; Empty blocks have no contents.
  1219. (contents-begin (progn (forward-line)
  1220. (and (< (point) block-end-line)
  1221. (point))))
  1222. (contents-end (and contents-begin block-end-line))
  1223. (pos-before-blank (progn (goto-char block-end-line)
  1224. (forward-line)
  1225. (point)))
  1226. (end (progn (skip-chars-forward " \r\t\n" limit)
  1227. (if (eobp) (point) (line-beginning-position)))))
  1228. (list 'quote-block
  1229. (nconc
  1230. (list :begin begin
  1231. :end end
  1232. :contents-begin contents-begin
  1233. :contents-end contents-end
  1234. :post-blank (count-lines pos-before-blank end)
  1235. :post-affiliated post-affiliated)
  1236. (cdr affiliated)))))))))
  1237. (defun org-element-quote-block-interpreter (quote-block contents)
  1238. "Interpret QUOTE-BLOCK element as Org syntax.
  1239. CONTENTS is the contents of the element."
  1240. (format "#+BEGIN_QUOTE\n%s#+END_QUOTE" contents))
  1241. ;;;; Section
  1242. (defun org-element-section-parser (limit)
  1243. "Parse a section.
  1244. LIMIT bounds the search.
  1245. Return a list whose CAR is `section' and CDR is a plist
  1246. containing `:begin', `:end', `:contents-begin', `contents-end'
  1247. and `:post-blank' keywords."
  1248. (save-excursion
  1249. ;; Beginning of section is the beginning of the first non-blank
  1250. ;; line after previous headline.
  1251. (let ((begin (point))
  1252. (end (progn (org-with-limited-levels (outline-next-heading))
  1253. (point)))
  1254. (pos-before-blank (progn (skip-chars-backward " \r\t\n")
  1255. (forward-line)
  1256. (point))))
  1257. (list 'section
  1258. (list :begin begin
  1259. :end end
  1260. :contents-begin begin
  1261. :contents-end pos-before-blank
  1262. :post-blank (count-lines pos-before-blank end))))))
  1263. (defun org-element-section-interpreter (section contents)
  1264. "Interpret SECTION element as Org syntax.
  1265. CONTENTS is the contents of the element."
  1266. contents)
  1267. ;;;; Special Block
  1268. (defun org-element-special-block-parser (limit affiliated)
  1269. "Parse a special block.
  1270. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  1271. the buffer position at the beginning of the first affiliated
  1272. keyword and CDR is a plist of affiliated keywords along with
  1273. their value.
  1274. Return a list whose CAR is `special-block' and CDR is a plist
  1275. containing `:type', `:begin', `:end', `:contents-begin',
  1276. `:contents-end', `:post-blank' and `:post-affiliated' keywords.
  1277. Assume point is at the beginning of the block."
  1278. (let* ((case-fold-search t)
  1279. (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
  1280. (upcase (match-string-no-properties 1)))))
  1281. (if (not (save-excursion
  1282. (re-search-forward
  1283. (format "^[ \t]*#\\+END_%s[ \t]*$" (regexp-quote type))
  1284. limit t)))
  1285. ;; Incomplete block: parse it as a paragraph.
  1286. (org-element-paragraph-parser limit affiliated)
  1287. (let ((block-end-line (match-beginning 0)))
  1288. (save-excursion
  1289. (let* ((begin (car affiliated))
  1290. (post-affiliated (point))
  1291. ;; Empty blocks have no contents.
  1292. (contents-begin (progn (forward-line)
  1293. (and (< (point) block-end-line)
  1294. (point))))
  1295. (contents-end (and contents-begin block-end-line))
  1296. (pos-before-blank (progn (goto-char block-end-line)
  1297. (forward-line)
  1298. (point)))
  1299. (end (progn (skip-chars-forward " \r\t\n" limit)
  1300. (if (eobp) (point) (line-beginning-position)))))
  1301. (list 'special-block
  1302. (nconc
  1303. (list :type type
  1304. :begin begin
  1305. :end end
  1306. :contents-begin contents-begin
  1307. :contents-end contents-end
  1308. :post-blank (count-lines pos-before-blank end)
  1309. :post-affiliated post-affiliated)
  1310. (cdr affiliated)))))))))
  1311. (defun org-element-special-block-interpreter (special-block contents)
  1312. "Interpret SPECIAL-BLOCK element as Org syntax.
  1313. CONTENTS is the contents of the element."
  1314. (let ((block-type (org-element-property :type special-block)))
  1315. (format "#+BEGIN_%s\n%s#+END_%s" block-type contents block-type)))
  1316. ;;; Elements
  1317. ;;
  1318. ;; For each element, a parser and an interpreter are also defined.
  1319. ;; Both follow the same naming convention used for greater elements.
  1320. ;;
  1321. ;; Also, as for greater elements, adding a new element type is done
  1322. ;; through the following steps: implement a parser and an interpreter,
  1323. ;; tweak `org-element--current-element' so that it recognizes the new
  1324. ;; type and add that new type to `org-element-all-elements'.
  1325. ;;
  1326. ;; As a special case, when the newly defined type is a block type,
  1327. ;; `org-element-block-name-alist' has to be modified accordingly.
  1328. ;;;; Babel Call
  1329. (defun org-element-babel-call-parser (limit affiliated)
  1330. "Parse a babel call.
  1331. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  1332. the buffer position at the beginning of the first affiliated
  1333. keyword and CDR is a plist of affiliated keywords along with
  1334. their value.
  1335. Return a list whose CAR is `babel-call' and CDR is a plist
  1336. containing `:begin', `:end', `:value', `:post-blank' and
  1337. `:post-affiliated' as keywords."
  1338. (save-excursion
  1339. (let ((begin (car affiliated))
  1340. (post-affiliated (point))
  1341. (value (progn (let ((case-fold-search t))
  1342. (re-search-forward "call:[ \t]*" nil t))
  1343. (buffer-substring-no-properties (point)
  1344. (line-end-position))))
  1345. (pos-before-blank (progn (forward-line) (point)))
  1346. (end (progn (skip-chars-forward " \r\t\n" limit)
  1347. (if (eobp) (point) (line-beginning-position)))))
  1348. (list 'babel-call
  1349. (nconc
  1350. (list :begin begin
  1351. :end end
  1352. :value value
  1353. :post-blank (count-lines pos-before-blank end)
  1354. :post-affiliated post-affiliated)
  1355. (cdr affiliated))))))
  1356. (defun org-element-babel-call-interpreter (babel-call contents)
  1357. "Interpret BABEL-CALL element as Org syntax.
  1358. CONTENTS is nil."
  1359. (concat "#+CALL: " (org-element-property :value babel-call)))
  1360. ;;;; Clock
  1361. (defun org-element-clock-parser (limit)
  1362. "Parse a clock.
  1363. LIMIT bounds the search.
  1364. Return a list whose CAR is `clock' and CDR is a plist containing
  1365. `:status', `:value', `:time', `:begin', `:end' and `:post-blank'
  1366. as keywords."
  1367. (save-excursion
  1368. (let* ((case-fold-search nil)
  1369. (begin (point))
  1370. (value (progn (search-forward org-clock-string (line-end-position) t)
  1371. (skip-chars-forward " \t")
  1372. (org-element-timestamp-parser)))
  1373. (duration (and (search-forward " => " (line-end-position) t)
  1374. (progn (skip-chars-forward " \t")
  1375. (looking-at "\\(\\S-+\\)[ \t]*$"))
  1376. (org-match-string-no-properties 1)))
  1377. (status (if duration 'closed 'running))
  1378. (post-blank (let ((before-blank (progn (forward-line) (point))))
  1379. (skip-chars-forward " \r\t\n" limit)
  1380. (skip-chars-backward " \t")
  1381. (unless (bolp) (end-of-line))
  1382. (count-lines before-blank (point))))
  1383. (end (point)))
  1384. (list 'clock
  1385. (list :status status
  1386. :value value
  1387. :duration duration
  1388. :begin begin
  1389. :end end
  1390. :post-blank post-blank)))))
  1391. (defun org-element-clock-interpreter (clock contents)
  1392. "Interpret CLOCK element as Org syntax.
  1393. CONTENTS is nil."
  1394. (concat org-clock-string " "
  1395. (org-element-timestamp-interpreter
  1396. (org-element-property :value clock) nil)
  1397. (let ((duration (org-element-property :duration clock)))
  1398. (and duration
  1399. (concat " => "
  1400. (apply 'format
  1401. "%2s:%02s"
  1402. (org-split-string duration ":")))))))
  1403. ;;;; Comment
  1404. (defun org-element-comment-parser (limit affiliated)
  1405. "Parse a comment.
  1406. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  1407. the buffer position at the beginning of the first affiliated
  1408. keyword and CDR is a plist of affiliated keywords along with
  1409. their value.
  1410. Return a list whose CAR is `comment' and CDR is a plist
  1411. containing `:begin', `:end', `:value', `:post-blank',
  1412. `:post-affiliated' keywords.
  1413. Assume point is at comment beginning."
  1414. (save-excursion
  1415. (let* ((begin (car affiliated))
  1416. (post-affiliated (point))
  1417. (value (prog2 (looking-at "[ \t]*# ?")
  1418. (buffer-substring-no-properties
  1419. (match-end 0) (line-end-position))
  1420. (forward-line)))
  1421. (com-end
  1422. ;; Get comments ending.
  1423. (progn
  1424. (while (and (< (point) limit) (looking-at "[ \t]*#\\( \\|$\\)"))
  1425. ;; Accumulate lines without leading hash and first
  1426. ;; whitespace.
  1427. (setq value
  1428. (concat value
  1429. "\n"
  1430. (buffer-substring-no-properties
  1431. (match-end 0) (line-end-position))))
  1432. (forward-line))
  1433. (point)))
  1434. (end (progn (goto-char com-end)
  1435. (skip-chars-forward " \r\t\n" limit)
  1436. (if (eobp) (point) (line-beginning-position)))))
  1437. (list 'comment
  1438. (nconc
  1439. (list :begin begin
  1440. :end end
  1441. :value value
  1442. :post-blank (count-lines com-end end)
  1443. :post-affiliated post-affiliated)
  1444. (cdr affiliated))))))
  1445. (defun org-element-comment-interpreter (comment contents)
  1446. "Interpret COMMENT element as Org syntax.
  1447. CONTENTS is nil."
  1448. (replace-regexp-in-string "^" "# " (org-element-property :value comment)))
  1449. ;;;; Comment Block
  1450. (defun org-element-comment-block-parser (limit affiliated)
  1451. "Parse an export block.
  1452. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  1453. the buffer position at the beginning of the first affiliated
  1454. keyword and CDR is a plist of affiliated keywords along with
  1455. their value.
  1456. Return a list whose CAR is `comment-block' and CDR is a plist
  1457. containing `:begin', `:end', `:value', `:post-blank' and
  1458. `:post-affiliated' keywords.
  1459. Assume point is at comment block beginning."
  1460. (let ((case-fold-search t))
  1461. (if (not (save-excursion
  1462. (re-search-forward "^[ \t]*#\\+END_COMMENT[ \t]*$" limit t)))
  1463. ;; Incomplete block: parse it as a paragraph.
  1464. (org-element-paragraph-parser limit affiliated)
  1465. (let ((contents-end (match-beginning 0)))
  1466. (save-excursion
  1467. (let* ((begin (car affiliated))
  1468. (post-affiliated (point))
  1469. (contents-begin (progn (forward-line) (point)))
  1470. (pos-before-blank (progn (goto-char contents-end)
  1471. (forward-line)
  1472. (point)))
  1473. (end (progn (skip-chars-forward " \r\t\n" limit)
  1474. (if (eobp) (point) (line-beginning-position))))
  1475. (value (buffer-substring-no-properties
  1476. contents-begin contents-end)))
  1477. (list 'comment-block
  1478. (nconc
  1479. (list :begin begin
  1480. :end end
  1481. :value value
  1482. :post-blank (count-lines pos-before-blank end)
  1483. :post-affiliated post-affiliated)
  1484. (cdr affiliated)))))))))
  1485. (defun org-element-comment-block-interpreter (comment-block contents)
  1486. "Interpret COMMENT-BLOCK element as Org syntax.
  1487. CONTENTS is nil."
  1488. (format "#+BEGIN_COMMENT\n%s#+END_COMMENT"
  1489. (org-remove-indentation (org-element-property :value comment-block))))
  1490. ;;;; Diary Sexp
  1491. (defun org-element-diary-sexp-parser (limit affiliated)
  1492. "Parse a diary sexp.
  1493. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  1494. the buffer position at the beginning of the first affiliated
  1495. keyword and CDR is a plist of affiliated keywords along with
  1496. their value.
  1497. Return a list whose CAR is `diary-sexp' and CDR is a plist
  1498. containing `:begin', `:end', `:value', `:post-blank' and
  1499. `:post-affiliated' keywords."
  1500. (save-excursion
  1501. (let ((begin (car affiliated))
  1502. (post-affiliated (point))
  1503. (value (progn (looking-at "\\(%%(.*\\)[ \t]*$")
  1504. (org-match-string-no-properties 1)))
  1505. (pos-before-blank (progn (forward-line) (point)))
  1506. (end (progn (skip-chars-forward " \r\t\n" limit)
  1507. (if (eobp) (point) (line-beginning-position)))))
  1508. (list 'diary-sexp
  1509. (nconc
  1510. (list :value value
  1511. :begin begin
  1512. :end end
  1513. :post-blank (count-lines pos-before-blank end)
  1514. :post-affiliated post-affiliated)
  1515. (cdr affiliated))))))
  1516. (defun org-element-diary-sexp-interpreter (diary-sexp contents)
  1517. "Interpret DIARY-SEXP as Org syntax.
  1518. CONTENTS is nil."
  1519. (org-element-property :value diary-sexp))
  1520. ;;;; Example Block
  1521. (defun org-element-example-block-parser (limit affiliated)
  1522. "Parse an example block.
  1523. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  1524. the buffer position at the beginning of the first affiliated
  1525. keyword and CDR is a plist of affiliated keywords along with
  1526. their value.
  1527. Return a list whose CAR is `example-block' and CDR is a plist
  1528. containing `:begin', `:end', `:number-lines', `:preserve-indent',
  1529. `:retain-labels', `:use-labels', `:label-fmt', `:switches',
  1530. `:value', `:post-blank' and `:post-affiliated' keywords."
  1531. (let ((case-fold-search t))
  1532. (if (not (save-excursion
  1533. (re-search-forward "^[ \t]*#\\+END_EXAMPLE[ \t]*$" limit t)))
  1534. ;; Incomplete block: parse it as a paragraph.
  1535. (org-element-paragraph-parser limit affiliated)
  1536. (let ((contents-end (match-beginning 0)))
  1537. (save-excursion
  1538. (let* ((switches
  1539. (progn
  1540. (looking-at "^[ \t]*#\\+BEGIN_EXAMPLE\\(?: +\\(.*\\)\\)?")
  1541. (org-match-string-no-properties 1)))
  1542. ;; Switches analysis
  1543. (number-lines
  1544. (cond ((not switches) nil)
  1545. ((string-match "-n\\>" switches) 'new)
  1546. ((string-match "+n\\>" switches) 'continued)))
  1547. (preserve-indent
  1548. (and switches (string-match "-i\\>" switches)))
  1549. ;; Should labels be retained in (or stripped from) example
  1550. ;; blocks?
  1551. (retain-labels
  1552. (or (not switches)
  1553. (not (string-match "-r\\>" switches))
  1554. (and number-lines (string-match "-k\\>" switches))))
  1555. ;; What should code-references use - labels or
  1556. ;; line-numbers?
  1557. (use-labels
  1558. (or (not switches)
  1559. (and retain-labels
  1560. (not (string-match "-k\\>" switches)))))
  1561. (label-fmt
  1562. (and switches
  1563. (string-match "-l +\"\\([^\"\n]+\\)\"" switches)
  1564. (match-string 1 switches)))
  1565. ;; Standard block parsing.
  1566. (begin (car affiliated))
  1567. (post-affiliated (point))
  1568. (block-ind (progn (skip-chars-forward " \t") (current-column)))
  1569. (contents-begin (progn (forward-line) (point)))
  1570. (value (org-element-remove-indentation
  1571. (org-unescape-code-in-string
  1572. (buffer-substring-no-properties
  1573. contents-begin contents-end))
  1574. block-ind))
  1575. (pos-before-blank (progn (goto-char contents-end)
  1576. (forward-line)
  1577. (point)))
  1578. (end (progn (skip-chars-forward " \r\t\n" limit)
  1579. (if (eobp) (point) (line-beginning-position)))))
  1580. (list 'example-block
  1581. (nconc
  1582. (list :begin begin
  1583. :end end
  1584. :value value
  1585. :switches switches
  1586. :number-lines number-lines
  1587. :preserve-indent preserve-indent
  1588. :retain-labels retain-labels
  1589. :use-labels use-labels
  1590. :label-fmt label-fmt
  1591. :post-blank (count-lines pos-before-blank end)
  1592. :post-affiliated post-affiliated)
  1593. (cdr affiliated)))))))))
  1594. (defun org-element-example-block-interpreter (example-block contents)
  1595. "Interpret EXAMPLE-BLOCK element as Org syntax.
  1596. CONTENTS is nil."
  1597. (let ((switches (org-element-property :switches example-block))
  1598. (value (org-element-property :value example-block)))
  1599. (concat "#+BEGIN_EXAMPLE" (and switches (concat " " switches)) "\n"
  1600. (org-escape-code-in-string
  1601. (if (or org-src-preserve-indentation
  1602. (org-element-property :preserve-indent example-block))
  1603. value
  1604. (org-element-remove-indentation value)))
  1605. "#+END_EXAMPLE")))
  1606. ;;;; Export Block
  1607. (defun org-element-export-block-parser (limit affiliated)
  1608. "Parse an export block.
  1609. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  1610. the buffer position at the beginning of the first affiliated
  1611. keyword and CDR is a plist of affiliated keywords along with
  1612. their value.
  1613. Return a list whose CAR is `export-block' and CDR is a plist
  1614. containing `:begin', `:end', `:type', `:value', `:post-blank' and
  1615. `:post-affiliated' keywords.
  1616. Assume point is at export-block beginning."
  1617. (let* ((case-fold-search t)
  1618. (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
  1619. (upcase (org-match-string-no-properties 1)))))
  1620. (if (not (save-excursion
  1621. (re-search-forward
  1622. (format "^[ \t]*#\\+END_%s[ \t]*$" type) limit t)))
  1623. ;; Incomplete block: parse it as a paragraph.
  1624. (org-element-paragraph-parser limit affiliated)
  1625. (let ((contents-end (match-beginning 0)))
  1626. (save-excursion
  1627. (let* ((begin (car affiliated))
  1628. (post-affiliated (point))
  1629. (contents-begin (progn (forward-line) (point)))
  1630. (pos-before-blank (progn (goto-char contents-end)
  1631. (forward-line)
  1632. (point)))
  1633. (end (progn (skip-chars-forward " \r\t\n" limit)
  1634. (if (eobp) (point) (line-beginning-position))))
  1635. (value (buffer-substring-no-properties contents-begin
  1636. contents-end)))
  1637. (list 'export-block
  1638. (nconc
  1639. (list :begin begin
  1640. :end end
  1641. :type type
  1642. :value value
  1643. :post-blank (count-lines pos-before-blank end)
  1644. :post-affiliated post-affiliated)
  1645. (cdr affiliated)))))))))
  1646. (defun org-element-export-block-interpreter (export-block contents)
  1647. "Interpret EXPORT-BLOCK element as Org syntax.
  1648. CONTENTS is nil."
  1649. (let ((type (org-element-property :type export-block)))
  1650. (concat (format "#+BEGIN_%s\n" type)
  1651. (org-element-property :value export-block)
  1652. (format "#+END_%s" type))))
  1653. ;;;; Fixed-width
  1654. (defun org-element-fixed-width-parser (limit affiliated)
  1655. "Parse a fixed-width section.
  1656. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  1657. the buffer position at the beginning of the first affiliated
  1658. keyword and CDR is a plist of affiliated keywords along with
  1659. their value.
  1660. Return a list whose CAR is `fixed-width' and CDR is a plist
  1661. containing `:begin', `:end', `:value', `:post-blank' and
  1662. `:post-affiliated' keywords.
  1663. Assume point is at the beginning of the fixed-width area."
  1664. (save-excursion
  1665. (let* ((begin (car affiliated))
  1666. (post-affiliated (point))
  1667. value
  1668. (end-area
  1669. (progn
  1670. (while (and (< (point) limit)
  1671. (looking-at "[ \t]*:\\( \\|$\\)"))
  1672. ;; Accumulate text without starting colons.
  1673. (setq value
  1674. (concat value
  1675. (buffer-substring-no-properties
  1676. (match-end 0) (point-at-eol))
  1677. "\n"))
  1678. (forward-line))
  1679. (point)))
  1680. (end (progn (skip-chars-forward " \r\t\n" limit)
  1681. (if (eobp) (point) (line-beginning-position)))))
  1682. (list 'fixed-width
  1683. (nconc
  1684. (list :begin begin
  1685. :end end
  1686. :value value
  1687. :post-blank (count-lines end-area end)
  1688. :post-affiliated post-affiliated)
  1689. (cdr affiliated))))))
  1690. (defun org-element-fixed-width-interpreter (fixed-width contents)
  1691. "Interpret FIXED-WIDTH element as Org syntax.
  1692. CONTENTS is nil."
  1693. (let ((value (org-element-property :value fixed-width)))
  1694. (and value
  1695. (replace-regexp-in-string
  1696. "^" ": "
  1697. (if (string-match "\n\\'" value) (substring value 0 -1) value)))))
  1698. ;;;; Horizontal Rule
  1699. (defun org-element-horizontal-rule-parser (limit affiliated)
  1700. "Parse an horizontal rule.
  1701. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  1702. the buffer position at the beginning of the first affiliated
  1703. keyword and CDR is a plist of affiliated keywords along with
  1704. their value.
  1705. Return a list whose CAR is `horizontal-rule' and CDR is a plist
  1706. containing `:begin', `:end', `:post-blank' and `:post-affiliated'
  1707. keywords."
  1708. (save-excursion
  1709. (let ((begin (car affiliated))
  1710. (post-affiliated (point))
  1711. (post-hr (progn (forward-line) (point)))
  1712. (end (progn (skip-chars-forward " \r\t\n" limit)
  1713. (if (eobp) (point) (line-beginning-position)))))
  1714. (list 'horizontal-rule
  1715. (nconc
  1716. (list :begin begin
  1717. :end end
  1718. :post-blank (count-lines post-hr end)
  1719. :post-affiliated post-affiliated)
  1720. (cdr affiliated))))))
  1721. (defun org-element-horizontal-rule-interpreter (horizontal-rule contents)
  1722. "Interpret HORIZONTAL-RULE element as Org syntax.
  1723. CONTENTS is nil."
  1724. "-----")
  1725. ;;;; Keyword
  1726. (defun org-element-keyword-parser (limit affiliated)
  1727. "Parse a keyword at point.
  1728. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  1729. the buffer position at the beginning of the first affiliated
  1730. keyword and CDR is a plist of affiliated keywords along with
  1731. their value.
  1732. Return a list whose CAR is `keyword' and CDR is a plist
  1733. containing `:key', `:value', `:begin', `:end', `:post-blank' and
  1734. `:post-affiliated' keywords."
  1735. (save-excursion
  1736. (let ((begin (car affiliated))
  1737. (post-affiliated (point))
  1738. (key (progn (looking-at "[ \t]*#\\+\\(\\S-+*\\):")
  1739. (upcase (org-match-string-no-properties 1))))
  1740. (value (org-trim (buffer-substring-no-properties
  1741. (match-end 0) (point-at-eol))))
  1742. (pos-before-blank (progn (forward-line) (point)))
  1743. (end (progn (skip-chars-forward " \r\t\n" limit)
  1744. (if (eobp) (point) (line-beginning-position)))))
  1745. (list 'keyword
  1746. (nconc
  1747. (list :key key
  1748. :value value
  1749. :begin begin
  1750. :end end
  1751. :post-blank (count-lines pos-before-blank end)
  1752. :post-affiliated post-affiliated)
  1753. (cdr affiliated))))))
  1754. (defun org-element-keyword-interpreter (keyword contents)
  1755. "Interpret KEYWORD element as Org syntax.
  1756. CONTENTS is nil."
  1757. (format "#+%s: %s"
  1758. (org-element-property :key keyword)
  1759. (org-element-property :value keyword)))
  1760. ;;;; Latex Environment
  1761. (defun org-element-latex-environment-parser (limit affiliated)
  1762. "Parse a LaTeX environment.
  1763. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  1764. the buffer position at the beginning of the first affiliated
  1765. keyword and CDR is a plist of affiliated keywords along with
  1766. their value.
  1767. Return a list whose CAR is `latex-environment' and CDR is a plist
  1768. containing `:begin', `:end', `:value', `:post-blank' and
  1769. `:post-affiliated' keywords.
  1770. Assume point is at the beginning of the latex environment."
  1771. (save-excursion
  1772. (let ((case-fold-search t)
  1773. (code-begin (point)))
  1774. (looking-at "[ \t]*\\\\begin{\\([A-Za-z0-9]+\\*?\\)}")
  1775. (if (not (re-search-forward (format "^[ \t]*\\\\end{%s}[ \t]*$"
  1776. (regexp-quote (match-string 1)))
  1777. limit t))
  1778. ;; Incomplete latex environment: parse it as a paragraph.
  1779. (org-element-paragraph-parser limit affiliated)
  1780. (let* ((code-end (progn (forward-line) (point)))
  1781. (begin (car affiliated))
  1782. (value (buffer-substring-no-properties code-begin code-end))
  1783. (end (progn (skip-chars-forward " \r\t\n" limit)
  1784. (if (eobp) (point) (line-beginning-position)))))
  1785. (list 'latex-environment
  1786. (nconc
  1787. (list :begin begin
  1788. :end end
  1789. :value value
  1790. :post-blank (count-lines code-end end)
  1791. :post-affiliated code-begin)
  1792. (cdr affiliated))))))))
  1793. (defun org-element-latex-environment-interpreter (latex-environment contents)
  1794. "Interpret LATEX-ENVIRONMENT element as Org syntax.
  1795. CONTENTS is nil."
  1796. (org-element-property :value latex-environment))
  1797. ;;;; Node Property
  1798. (defun org-element-node-property-parser (limit)
  1799. "Parse a node-property at point.
  1800. LIMIT bounds the search.
  1801. Return a list whose CAR is `node-property' and CDR is a plist
  1802. containing `:key', `:value', `:begin', `:end' and `:post-blank'
  1803. keywords."
  1804. (save-excursion
  1805. (looking-at org-property-re)
  1806. (let ((case-fold-search t)
  1807. (begin (point))
  1808. (key (org-match-string-no-properties 2))
  1809. (value (org-match-string-no-properties 3))
  1810. (pos-before-blank (progn (forward-line) (point)))
  1811. (end (progn (skip-chars-forward " \r\t\n" limit)
  1812. (if (eobp) (point) (point-at-bol)))))
  1813. (list 'node-property
  1814. (list :key key
  1815. :value value
  1816. :begin begin
  1817. :end end
  1818. :post-blank (count-lines pos-before-blank end))))))
  1819. (defun org-element-node-property-interpreter (node-property contents)
  1820. "Interpret NODE-PROPERTY element as Org syntax.
  1821. CONTENTS is nil."
  1822. (format org-property-format
  1823. (format ":%s:" (org-element-property :key node-property))
  1824. (org-element-property :value node-property)))
  1825. ;;;; Paragraph
  1826. (defun org-element-paragraph-parser (limit affiliated)
  1827. "Parse a paragraph.
  1828. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  1829. the buffer position at the beginning of the first affiliated
  1830. keyword and CDR is a plist of affiliated keywords along with
  1831. their value.
  1832. Return a list whose CAR is `paragraph' and CDR is a plist
  1833. containing `:begin', `:end', `:contents-begin' and
  1834. `:contents-end', `:post-blank' and `:post-affiliated' keywords.
  1835. Assume point is at the beginning of the paragraph."
  1836. (save-excursion
  1837. (let* ((begin (car affiliated))
  1838. (contents-begin (point))
  1839. (before-blank
  1840. (let ((case-fold-search t))
  1841. (end-of-line)
  1842. (if (not (re-search-forward
  1843. org-element-paragraph-separate limit 'm))
  1844. limit
  1845. ;; A matching `org-element-paragraph-separate' is not
  1846. ;; necessarily the end of the paragraph. In
  1847. ;; particular, lines starting with # or : as a first
  1848. ;; non-space character are ambiguous. We have check
  1849. ;; if they are valid Org syntax (i.e. not an
  1850. ;; incomplete keyword).
  1851. (beginning-of-line)
  1852. (while (not
  1853. (or
  1854. ;; There's no ambiguity for other symbols or
  1855. ;; empty lines: stop here.
  1856. (looking-at "[ \t]*\\(?:[^:#]\\|$\\)")
  1857. ;; Stop at valid fixed-width areas.
  1858. (looking-at "[ \t]*:\\(?: \\|$\\)")
  1859. ;; Stop at drawers.
  1860. (and (looking-at org-drawer-regexp)
  1861. (save-excursion
  1862. (re-search-forward
  1863. "^[ \t]*:END:[ \t]*$" limit t)))
  1864. ;; Stop at valid comments.
  1865. (looking-at "[ \t]*#\\(?: \\|$\\)")
  1866. ;; Stop at valid dynamic blocks.
  1867. (and (looking-at org-dblock-start-re)
  1868. (save-excursion
  1869. (re-search-forward
  1870. "^[ \t]*#\\+END:?[ \t]*$" limit t)))
  1871. ;; Stop at valid blocks.
  1872. (and (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
  1873. (save-excursion
  1874. (re-search-forward
  1875. (format "^[ \t]*#\\+END_%s[ \t]*$"
  1876. (regexp-quote
  1877. (org-match-string-no-properties 1)))
  1878. limit t)))
  1879. ;; Stop at valid latex environments.
  1880. (and (looking-at
  1881. "[ \t]*\\\\begin{\\([A-Za-z0-9]+\\*?\\)}")
  1882. (save-excursion
  1883. (re-search-forward
  1884. (format "^[ \t]*\\\\end{%s}[ \t]*$"
  1885. (regexp-quote
  1886. (org-match-string-no-properties 1)))
  1887. limit t)))
  1888. ;; Stop at valid keywords.
  1889. (looking-at "[ \t]*#\\+\\S-+:")
  1890. ;; Skip everything else.
  1891. (not
  1892. (progn
  1893. (end-of-line)
  1894. (re-search-forward org-element-paragraph-separate
  1895. limit 'm)))))
  1896. (beginning-of-line)))
  1897. (if (= (point) limit) limit
  1898. (goto-char (line-beginning-position)))))
  1899. (contents-end (progn (skip-chars-backward " \r\t\n" contents-begin)
  1900. (forward-line)
  1901. (point)))
  1902. (end (progn (skip-chars-forward " \r\t\n" limit)
  1903. (if (eobp) (point) (line-beginning-position)))))
  1904. (list 'paragraph
  1905. (nconc
  1906. (list :begin begin
  1907. :end end
  1908. :contents-begin contents-begin
  1909. :contents-end contents-end
  1910. :post-blank (count-lines before-blank end)
  1911. :post-affiliated contents-begin)
  1912. (cdr affiliated))))))
  1913. (defun org-element-paragraph-interpreter (paragraph contents)
  1914. "Interpret PARAGRAPH element as Org syntax.
  1915. CONTENTS is the contents of the element."
  1916. contents)
  1917. ;;;; Planning
  1918. (defun org-element-planning-parser (limit)
  1919. "Parse a planning.
  1920. LIMIT bounds the search.
  1921. Return a list whose CAR is `planning' and CDR is a plist
  1922. containing `:closed', `:deadline', `:scheduled', `:begin', `:end'
  1923. and `:post-blank' keywords."
  1924. (save-excursion
  1925. (let* ((case-fold-search nil)
  1926. (begin (point))
  1927. (post-blank (let ((before-blank (progn (forward-line) (point))))
  1928. (skip-chars-forward " \r\t\n" limit)
  1929. (skip-chars-backward " \t")
  1930. (unless (bolp) (end-of-line))
  1931. (count-lines before-blank (point))))
  1932. (end (point))
  1933. closed deadline scheduled)
  1934. (goto-char begin)
  1935. (while (re-search-forward org-keyword-time-not-clock-regexp end t)
  1936. (goto-char (match-end 1))
  1937. (skip-chars-forward " \t" end)
  1938. (let ((keyword (match-string 1))
  1939. (time (org-element-timestamp-parser)))
  1940. (cond ((equal keyword org-closed-string) (setq closed time))
  1941. ((equal keyword org-deadline-string) (setq deadline time))
  1942. (t (setq scheduled time)))))
  1943. (list 'planning
  1944. (list :closed closed
  1945. :deadline deadline
  1946. :scheduled scheduled
  1947. :begin begin
  1948. :end end
  1949. :post-blank post-blank)))))
  1950. (defun org-element-planning-interpreter (planning contents)
  1951. "Interpret PLANNING element as Org syntax.
  1952. CONTENTS is nil."
  1953. (mapconcat
  1954. 'identity
  1955. (delq nil
  1956. (list (let ((deadline (org-element-property :deadline planning)))
  1957. (when deadline
  1958. (concat org-deadline-string " "
  1959. (org-element-timestamp-interpreter deadline nil))))
  1960. (let ((scheduled (org-element-property :scheduled planning)))
  1961. (when scheduled
  1962. (concat org-scheduled-string " "
  1963. (org-element-timestamp-interpreter scheduled nil))))
  1964. (let ((closed (org-element-property :closed planning)))
  1965. (when closed
  1966. (concat org-closed-string " "
  1967. (org-element-timestamp-interpreter closed nil))))))
  1968. " "))
  1969. ;;;; Quote Section
  1970. (defun org-element-quote-section-parser (limit)
  1971. "Parse a quote section.
  1972. LIMIT bounds the search.
  1973. Return a list whose CAR is `quote-section' and CDR is a plist
  1974. containing `:begin', `:end', `:value' and `:post-blank' keywords.
  1975. Assume point is at beginning of the section."
  1976. (save-excursion
  1977. (let* ((begin (point))
  1978. (end (progn (org-with-limited-levels (outline-next-heading))
  1979. (point)))
  1980. (pos-before-blank (progn (skip-chars-backward " \r\t\n")
  1981. (forward-line)
  1982. (point)))
  1983. (value (buffer-substring-no-properties begin pos-before-blank)))
  1984. (list 'quote-section
  1985. (list :begin begin
  1986. :end end
  1987. :value value
  1988. :post-blank (count-lines pos-before-blank end))))))
  1989. (defun org-element-quote-section-interpreter (quote-section contents)
  1990. "Interpret QUOTE-SECTION element as Org syntax.
  1991. CONTENTS is nil."
  1992. (org-element-property :value quote-section))
  1993. ;;;; Src Block
  1994. (defun org-element-src-block-parser (limit affiliated)
  1995. "Parse a src block.
  1996. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  1997. the buffer position at the beginning of the first affiliated
  1998. keyword and CDR is a plist of affiliated keywords along with
  1999. their value.
  2000. Return a list whose CAR is `src-block' and CDR is a plist
  2001. containing `:language', `:switches', `:parameters', `:begin',
  2002. `:end', `:number-lines', `:retain-labels', `:use-labels',
  2003. `:label-fmt', `:preserve-indent', `:value', `:post-blank' and
  2004. `:post-affiliated' keywords.
  2005. Assume point is at the beginning of the block."
  2006. (let ((case-fold-search t))
  2007. (if (not (save-excursion (re-search-forward "^[ \t]*#\\+END_SRC[ \t]*$"
  2008. limit t)))
  2009. ;; Incomplete block: parse it as a paragraph.
  2010. (org-element-paragraph-parser limit affiliated)
  2011. (let ((contents-end (match-beginning 0)))
  2012. (save-excursion
  2013. (let* ((begin (car affiliated))
  2014. (post-affiliated (point))
  2015. ;; Get language as a string.
  2016. (language
  2017. (progn
  2018. (looking-at
  2019. (concat "^[ \t]*#\\+BEGIN_SRC"
  2020. "\\(?: +\\(\\S-+\\)\\)?"
  2021. "\\(\\(?: +\\(?:-l \".*?\"\\|[-+][A-Za-z]\\)\\)+\\)?"
  2022. "\\(.*\\)[ \t]*$"))
  2023. (org-match-string-no-properties 1)))
  2024. ;; Get switches.
  2025. (switches (org-match-string-no-properties 2))
  2026. ;; Get parameters.
  2027. (parameters (org-match-string-no-properties 3))
  2028. ;; Switches analysis
  2029. (number-lines
  2030. (cond ((not switches) nil)
  2031. ((string-match "-n\\>" switches) 'new)
  2032. ((string-match "+n\\>" switches) 'continued)))
  2033. (preserve-indent (and switches
  2034. (string-match "-i\\>" switches)))
  2035. (label-fmt
  2036. (and switches
  2037. (string-match "-l +\"\\([^\"\n]+\\)\"" switches)
  2038. (match-string 1 switches)))
  2039. ;; Should labels be retained in (or stripped from)
  2040. ;; src blocks?
  2041. (retain-labels
  2042. (or (not switches)
  2043. (not (string-match "-r\\>" switches))
  2044. (and number-lines (string-match "-k\\>" switches))))
  2045. ;; What should code-references use - labels or
  2046. ;; line-numbers?
  2047. (use-labels
  2048. (or (not switches)
  2049. (and retain-labels
  2050. (not (string-match "-k\\>" switches)))))
  2051. ;; Indentation.
  2052. (block-ind (progn (skip-chars-forward " \t") (current-column)))
  2053. ;; Retrieve code.
  2054. (value (org-element-remove-indentation
  2055. (org-unescape-code-in-string
  2056. (buffer-substring-no-properties
  2057. (progn (forward-line) (point)) contents-end))
  2058. block-ind))
  2059. (pos-before-blank (progn (goto-char contents-end)
  2060. (forward-line)
  2061. (point)))
  2062. ;; Get position after ending blank lines.
  2063. (end (progn (skip-chars-forward " \r\t\n" limit)
  2064. (if (eobp) (point) (line-beginning-position)))))
  2065. (list 'src-block
  2066. (nconc
  2067. (list :language language
  2068. :switches (and (org-string-nw-p switches)
  2069. (org-trim switches))
  2070. :parameters (and (org-string-nw-p parameters)
  2071. (org-trim parameters))
  2072. :begin begin
  2073. :end end
  2074. :number-lines number-lines
  2075. :preserve-indent preserve-indent
  2076. :retain-labels retain-labels
  2077. :use-labels use-labels
  2078. :label-fmt label-fmt
  2079. :value value
  2080. :post-blank (count-lines pos-before-blank end)
  2081. :post-affiliated post-affiliated)
  2082. (cdr affiliated)))))))))
  2083. (defun org-element-src-block-interpreter (src-block contents)
  2084. "Interpret SRC-BLOCK element as Org syntax.
  2085. CONTENTS is nil."
  2086. (let ((lang (org-element-property :language src-block))
  2087. (switches (org-element-property :switches src-block))
  2088. (params (org-element-property :parameters src-block))
  2089. (value
  2090. (let ((val (org-element-property :value src-block)))
  2091. (cond
  2092. ((or org-src-preserve-indentation
  2093. (org-element-property :preserve-indent src-block))
  2094. val)
  2095. ((zerop org-edit-src-content-indentation) val)
  2096. (t
  2097. (let ((ind (make-string org-edit-src-content-indentation ?\s)))
  2098. (replace-regexp-in-string
  2099. "\\(^\\)[ \t]*\\S-" ind val nil nil 1)))))))
  2100. (concat (format "#+BEGIN_SRC%s\n"
  2101. (concat (and lang (concat " " lang))
  2102. (and switches (concat " " switches))
  2103. (and params (concat " " params))))
  2104. (org-escape-code-in-string value)
  2105. "#+END_SRC")))
  2106. ;;;; Table
  2107. (defun org-element-table-parser (limit affiliated)
  2108. "Parse a table at point.
  2109. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  2110. the buffer position at the beginning of the first affiliated
  2111. keyword and CDR is a plist of affiliated keywords along with
  2112. their value.
  2113. Return a list whose CAR is `table' and CDR is a plist containing
  2114. `:begin', `:end', `:tblfm', `:type', `:contents-begin',
  2115. `:contents-end', `:value', `:post-blank' and `:post-affiliated'
  2116. keywords.
  2117. Assume point is at the beginning of the table."
  2118. (save-excursion
  2119. (let* ((case-fold-search t)
  2120. (table-begin (point))
  2121. (type (if (org-at-table.el-p) 'table.el 'org))
  2122. (begin (car affiliated))
  2123. (table-end
  2124. (if (re-search-forward org-table-any-border-regexp limit 'm)
  2125. (goto-char (match-beginning 0))
  2126. (point)))
  2127. (tblfm (let (acc)
  2128. (while (looking-at "[ \t]*#\\+TBLFM: +\\(.*\\)[ \t]*$")
  2129. (push (org-match-string-no-properties 1) acc)
  2130. (forward-line))
  2131. acc))
  2132. (pos-before-blank (point))
  2133. (end (progn (skip-chars-forward " \r\t\n" limit)
  2134. (if (eobp) (point) (line-beginning-position)))))
  2135. (list 'table
  2136. (nconc
  2137. (list :begin begin
  2138. :end end
  2139. :type type
  2140. :tblfm tblfm
  2141. ;; Only `org' tables have contents. `table.el' tables
  2142. ;; use a `:value' property to store raw table as
  2143. ;; a string.
  2144. :contents-begin (and (eq type 'org) table-begin)
  2145. :contents-end (and (eq type 'org) table-end)
  2146. :value (and (eq type 'table.el)
  2147. (buffer-substring-no-properties
  2148. table-begin table-end))
  2149. :post-blank (count-lines pos-before-blank end)
  2150. :post-affiliated table-begin)
  2151. (cdr affiliated))))))
  2152. (defun org-element-table-interpreter (table contents)
  2153. "Interpret TABLE element as Org syntax.
  2154. CONTENTS is nil."
  2155. (if (eq (org-element-property :type table) 'table.el)
  2156. (org-remove-indentation (org-element-property :value table))
  2157. (concat (with-temp-buffer (insert contents)
  2158. (org-table-align)
  2159. (buffer-string))
  2160. (mapconcat (lambda (fm) (concat "#+TBLFM: " fm))
  2161. (reverse (org-element-property :tblfm table))
  2162. "\n"))))
  2163. ;;;; Table Row
  2164. (defun org-element-table-row-parser (limit)
  2165. "Parse table row at point.
  2166. LIMIT bounds the search.
  2167. Return a list whose CAR is `table-row' and CDR is a plist
  2168. containing `:begin', `:end', `:contents-begin', `:contents-end',
  2169. `:type' and `:post-blank' keywords."
  2170. (save-excursion
  2171. (let* ((type (if (looking-at "^[ \t]*|-") 'rule 'standard))
  2172. (begin (point))
  2173. ;; A table rule has no contents. In that case, ensure
  2174. ;; CONTENTS-BEGIN matches CONTENTS-END.
  2175. (contents-begin (and (eq type 'standard)
  2176. (search-forward "|")
  2177. (point)))
  2178. (contents-end (and (eq type 'standard)
  2179. (progn
  2180. (end-of-line)
  2181. (skip-chars-backward " \t")
  2182. (point))))
  2183. (end (progn (forward-line) (point))))
  2184. (list 'table-row
  2185. (list :type type
  2186. :begin begin
  2187. :end end
  2188. :contents-begin contents-begin
  2189. :contents-end contents-end
  2190. :post-blank 0)))))
  2191. (defun org-element-table-row-interpreter (table-row contents)
  2192. "Interpret TABLE-ROW element as Org syntax.
  2193. CONTENTS is the contents of the table row."
  2194. (if (eq (org-element-property :type table-row) 'rule) "|-"
  2195. (concat "| " contents)))
  2196. ;;;; Verse Block
  2197. (defun org-element-verse-block-parser (limit affiliated)
  2198. "Parse a verse block.
  2199. LIMIT bounds the search. AFFILIATED is a list of which CAR is
  2200. the buffer position at the beginning of the first affiliated
  2201. keyword and CDR is a plist of affiliated keywords along with
  2202. their value.
  2203. Return a list whose CAR is `verse-block' and CDR is a plist
  2204. containing `:begin', `:end', `:contents-begin', `:contents-end',
  2205. `:post-blank' and `:post-affiliated' keywords.
  2206. Assume point is at beginning of the block."
  2207. (let ((case-fold-search t))
  2208. (if (not (save-excursion
  2209. (re-search-forward "^[ \t]*#\\+END_VERSE[ \t]*$" limit t)))
  2210. ;; Incomplete block: parse it as a paragraph.
  2211. (org-element-paragraph-parser limit affiliated)
  2212. (let ((contents-end (match-beginning 0)))
  2213. (save-excursion
  2214. (let* ((begin (car affiliated))
  2215. (post-affiliated (point))
  2216. (contents-begin (progn (forward-line) (point)))
  2217. (pos-before-blank (progn (goto-char contents-end)
  2218. (forward-line)
  2219. (point)))
  2220. (end (progn (skip-chars-forward " \r\t\n" limit)
  2221. (if (eobp) (point) (line-beginning-position)))))
  2222. (list 'verse-block
  2223. (nconc
  2224. (list :begin begin
  2225. :end end
  2226. :contents-begin contents-begin
  2227. :contents-end contents-end
  2228. :post-blank (count-lines pos-before-blank end)
  2229. :post-affiliated post-affiliated)
  2230. (cdr affiliated)))))))))
  2231. (defun org-element-verse-block-interpreter (verse-block contents)
  2232. "Interpret VERSE-BLOCK element as Org syntax.
  2233. CONTENTS is verse block contents."
  2234. (format "#+BEGIN_VERSE\n%s#+END_VERSE" contents))
  2235. ;;; Objects
  2236. ;;
  2237. ;; Unlike to elements, interstices can be found between objects.
  2238. ;; That's why, along with the parser, successor functions are provided
  2239. ;; for each object. Some objects share the same successor (i.e. `code'
  2240. ;; and `verbatim' objects).
  2241. ;;
  2242. ;; A successor must accept a single argument bounding the search. It
  2243. ;; will return either a cons cell whose CAR is the object's type, as
  2244. ;; a symbol, and CDR the position of its next occurrence, or nil.
  2245. ;;
  2246. ;; Successors follow the naming convention:
  2247. ;; org-element-NAME-successor, where NAME is the name of the
  2248. ;; successor, as defined in `org-element-all-successors'.
  2249. ;;
  2250. ;; Some object types (i.e. `italic') are recursive. Restrictions on
  2251. ;; object types they can contain will be specified in
  2252. ;; `org-element-object-restrictions'.
  2253. ;;
  2254. ;; Adding a new type of object is simple. Implement a successor,
  2255. ;; a parser, and an interpreter for it, all following the naming
  2256. ;; convention. Register type in `org-element-all-objects' and
  2257. ;; successor in `org-element-all-successors'. Maybe tweak
  2258. ;; restrictions about it, and that's it.
  2259. ;;;; Bold
  2260. (defun org-element-bold-parser ()
  2261. "Parse bold object at point.
  2262. Return a list whose CAR is `bold' and CDR is a plist with
  2263. `:begin', `:end', `:contents-begin' and `:contents-end' and
  2264. `:post-blank' keywords.
  2265. Assume point is at the first star marker."
  2266. (save-excursion
  2267. (unless (bolp) (backward-char 1))
  2268. (looking-at org-emph-re)
  2269. (let ((begin (match-beginning 2))
  2270. (contents-begin (match-beginning 4))
  2271. (contents-end (match-end 4))
  2272. (post-blank (progn (goto-char (match-end 2))
  2273. (skip-chars-forward " \t")))
  2274. (end (point)))
  2275. (list 'bold
  2276. (list :begin begin
  2277. :end end
  2278. :contents-begin contents-begin
  2279. :contents-end contents-end
  2280. :post-blank post-blank)))))
  2281. (defun org-element-bold-interpreter (bold contents)
  2282. "Interpret BOLD object as Org syntax.
  2283. CONTENTS is the contents of the object."
  2284. (format "*%s*" contents))
  2285. (defun org-element-text-markup-successor ()
  2286. "Search for the next text-markup object.
  2287. Return value is a cons cell whose CAR is a symbol among `bold',
  2288. `italic', `underline', `strike-through', `code' and `verbatim'
  2289. and CDR is beginning position."
  2290. (save-excursion
  2291. (unless (bolp) (backward-char))
  2292. (when (re-search-forward org-emph-re nil t)
  2293. (let ((marker (match-string 3)))
  2294. (cons (cond
  2295. ((equal marker "*") 'bold)
  2296. ((equal marker "/") 'italic)
  2297. ((equal marker "_") 'underline)
  2298. ((equal marker "+") 'strike-through)
  2299. ((equal marker "~") 'code)
  2300. ((equal marker "=") 'verbatim)
  2301. (t (error "Unknown marker at %d" (match-beginning 3))))
  2302. (match-beginning 2))))))
  2303. ;;;; Code
  2304. (defun org-element-code-parser ()
  2305. "Parse code object at point.
  2306. Return a list whose CAR is `code' and CDR is a plist with
  2307. `:value', `:begin', `:end' and `:post-blank' keywords.
  2308. Assume point is at the first tilde marker."
  2309. (save-excursion
  2310. (unless (bolp) (backward-char 1))
  2311. (looking-at org-emph-re)
  2312. (let ((begin (match-beginning 2))
  2313. (value (org-match-string-no-properties 4))
  2314. (post-blank (progn (goto-char (match-end 2))
  2315. (skip-chars-forward " \t")))
  2316. (end (point)))
  2317. (list 'code
  2318. (list :value value
  2319. :begin begin
  2320. :end end
  2321. :post-blank post-blank)))))
  2322. (defun org-element-code-interpreter (code contents)
  2323. "Interpret CODE object as Org syntax.
  2324. CONTENTS is nil."
  2325. (format "~%s~" (org-element-property :value code)))
  2326. ;;;; Entity
  2327. (defun org-element-entity-parser ()
  2328. "Parse entity at point.
  2329. Return a list whose CAR is `entity' and CDR a plist with
  2330. `:begin', `:end', `:latex', `:latex-math-p', `:html', `:latin1',
  2331. `:utf-8', `:ascii', `:use-brackets-p' and `:post-blank' as
  2332. keywords.
  2333. Assume point is at the beginning of the entity."
  2334. (save-excursion
  2335. (looking-at "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)")
  2336. (let* ((value (org-entity-get (match-string 1)))
  2337. (begin (match-beginning 0))
  2338. (bracketsp (string= (match-string 2) "{}"))
  2339. (post-blank (progn (goto-char (match-end 1))
  2340. (when bracketsp (forward-char 2))
  2341. (skip-chars-forward " \t")))
  2342. (end (point)))
  2343. (list 'entity
  2344. (list :name (car value)
  2345. :latex (nth 1 value)
  2346. :latex-math-p (nth 2 value)
  2347. :html (nth 3 value)
  2348. :ascii (nth 4 value)
  2349. :latin1 (nth 5 value)
  2350. :utf-8 (nth 6 value)
  2351. :begin begin
  2352. :end end
  2353. :use-brackets-p bracketsp
  2354. :post-blank post-blank)))))
  2355. (defun org-element-entity-interpreter (entity contents)
  2356. "Interpret ENTITY object as Org syntax.
  2357. CONTENTS is nil."
  2358. (concat "\\"
  2359. (org-element-property :name entity)
  2360. (when (org-element-property :use-brackets-p entity) "{}")))
  2361. (defun org-element-latex-or-entity-successor ()
  2362. "Search for the next latex-fragment or entity object.
  2363. Return value is a cons cell whose CAR is `entity' or
  2364. `latex-fragment' and CDR is beginning position."
  2365. (save-excursion
  2366. (unless (bolp) (backward-char))
  2367. (let ((matchers (cdr org-latex-regexps))
  2368. ;; ENTITY-RE matches both LaTeX commands and Org entities.
  2369. (entity-re
  2370. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))
  2371. (when (re-search-forward
  2372. (concat (mapconcat #'cadr matchers "\\|") "\\|" entity-re) nil t)
  2373. (goto-char (match-beginning 0))
  2374. (if (looking-at entity-re)
  2375. ;; Determine if it's a real entity or a LaTeX command.
  2376. (cons (if (org-entity-get (match-string 1)) 'entity 'latex-fragment)
  2377. (match-beginning 0))
  2378. ;; No entity nor command: point is at a LaTeX fragment.
  2379. ;; Determine its type to get the correct beginning position.
  2380. (cons 'latex-fragment
  2381. (catch 'return
  2382. (dolist (e matchers)
  2383. (when (looking-at (nth 1 e))
  2384. (throw 'return (match-beginning (nth 2 e)))))
  2385. (point))))))))
  2386. ;;;; Export Snippet
  2387. (defun org-element-export-snippet-parser ()
  2388. "Parse export snippet at point.
  2389. Return a list whose CAR is `export-snippet' and CDR a plist with
  2390. `:begin', `:end', `:back-end', `:value' and `:post-blank' as
  2391. keywords.
  2392. Assume point is at the beginning of the snippet."
  2393. (save-excursion
  2394. (re-search-forward "@@\\([-A-Za-z0-9]+\\):" nil t)
  2395. (let* ((begin (match-beginning 0))
  2396. (back-end (org-match-string-no-properties 1))
  2397. (value (buffer-substring-no-properties
  2398. (point)
  2399. (progn (re-search-forward "@@" nil t) (match-beginning 0))))
  2400. (post-blank (skip-chars-forward " \t"))
  2401. (end (point)))
  2402. (list 'export-snippet
  2403. (list :back-end back-end
  2404. :value value
  2405. :begin begin
  2406. :end end
  2407. :post-blank post-blank)))))
  2408. (defun org-element-export-snippet-interpreter (export-snippet contents)
  2409. "Interpret EXPORT-SNIPPET object as Org syntax.
  2410. CONTENTS is nil."
  2411. (format "@@%s:%s@@"
  2412. (org-element-property :back-end export-snippet)
  2413. (org-element-property :value export-snippet)))
  2414. (defun org-element-export-snippet-successor ()
  2415. "Search for the next export-snippet object.
  2416. Return value is a cons cell whose CAR is `export-snippet' and CDR
  2417. its beginning position."
  2418. (save-excursion
  2419. (let (beg)
  2420. (when (and (re-search-forward "@@[-A-Za-z0-9]+:" nil t)
  2421. (setq beg (match-beginning 0))
  2422. (search-forward "@@" nil t))
  2423. (cons 'export-snippet beg)))))
  2424. ;;;; Footnote Reference
  2425. (defun org-element-footnote-reference-parser ()
  2426. "Parse footnote reference at point.
  2427. Return a list whose CAR is `footnote-reference' and CDR a plist
  2428. with `:label', `:type', `:inline-definition', `:begin', `:end'
  2429. and `:post-blank' as keywords."
  2430. (save-excursion
  2431. (looking-at org-footnote-re)
  2432. (let* ((begin (point))
  2433. (label (or (org-match-string-no-properties 2)
  2434. (org-match-string-no-properties 3)
  2435. (and (match-string 1)
  2436. (concat "fn:" (org-match-string-no-properties 1)))))
  2437. (type (if (or (not label) (match-string 1)) 'inline 'standard))
  2438. (inner-begin (match-end 0))
  2439. (inner-end
  2440. (let ((count 1))
  2441. (forward-char)
  2442. (while (and (> count 0) (re-search-forward "[][]" nil t))
  2443. (if (equal (match-string 0) "[") (incf count) (decf count)))
  2444. (1- (point))))
  2445. (post-blank (progn (goto-char (1+ inner-end))
  2446. (skip-chars-forward " \t")))
  2447. (end (point))
  2448. (footnote-reference
  2449. (list 'footnote-reference
  2450. (list :label label
  2451. :type type
  2452. :begin begin
  2453. :end end
  2454. :post-blank post-blank))))
  2455. (org-element-put-property
  2456. footnote-reference :inline-definition
  2457. (and (eq type 'inline)
  2458. (org-element-parse-secondary-string
  2459. (buffer-substring inner-begin inner-end)
  2460. (org-element-restriction 'footnote-reference)
  2461. footnote-reference))))))
  2462. (defun org-element-footnote-reference-interpreter (footnote-reference contents)
  2463. "Interpret FOOTNOTE-REFERENCE object as Org syntax.
  2464. CONTENTS is nil."
  2465. (let ((label (or (org-element-property :label footnote-reference) "fn:"))
  2466. (def
  2467. (let ((inline-def
  2468. (org-element-property :inline-definition footnote-reference)))
  2469. (if (not inline-def) ""
  2470. (concat ":" (org-element-interpret-data inline-def))))))
  2471. (format "[%s]" (concat label def))))
  2472. (defun org-element-footnote-reference-successor ()
  2473. "Search for the next footnote-reference object.
  2474. Return value is a cons cell whose CAR is `footnote-reference' and
  2475. CDR is beginning position."
  2476. (save-excursion
  2477. (catch 'exit
  2478. (while (re-search-forward org-footnote-re nil t)
  2479. (save-excursion
  2480. (let ((beg (match-beginning 0))
  2481. (count 1))
  2482. (backward-char)
  2483. (while (re-search-forward "[][]" nil t)
  2484. (if (equal (match-string 0) "[") (incf count) (decf count))
  2485. (when (zerop count)
  2486. (throw 'exit (cons 'footnote-reference beg))))))))))
  2487. ;;;; Inline Babel Call
  2488. (defun org-element-inline-babel-call-parser ()
  2489. "Parse inline babel call at point.
  2490. Return a list whose CAR is `inline-babel-call' and CDR a plist
  2491. with `:begin', `:end', `:value' and `:post-blank' as keywords.
  2492. Assume point is at the beginning of the babel call."
  2493. (save-excursion
  2494. (unless (bolp) (backward-char))
  2495. (let ((case-fold-search t))
  2496. (looking-at org-babel-inline-lob-one-liner-regexp))
  2497. (let ((begin (match-end 1))
  2498. (value (buffer-substring-no-properties (match-end 1) (match-end 0)))
  2499. (post-blank (progn (goto-char (match-end 0))
  2500. (skip-chars-forward " \t")))
  2501. (end (point)))
  2502. (list 'inline-babel-call
  2503. (list :begin begin
  2504. :end end
  2505. :value value
  2506. :post-blank post-blank)))))
  2507. (defun org-element-inline-babel-call-interpreter (inline-babel-call contents)
  2508. "Interpret INLINE-BABEL-CALL object as Org syntax.
  2509. CONTENTS is nil."
  2510. (org-element-property :value inline-babel-call))
  2511. (defun org-element-inline-babel-call-successor ()
  2512. "Search for the next inline-babel-call object.
  2513. Return value is a cons cell whose CAR is `inline-babel-call' and
  2514. CDR is beginning position."
  2515. (save-excursion
  2516. ;; Use a simplified version of
  2517. ;; `org-babel-inline-lob-one-liner-regexp'.
  2518. (when (re-search-forward
  2519. "call_\\([^()\n]+?\\)\\(?:\\[.*?\\]\\)?([^\n]*?)\\(\\[.*?\\]\\)?"
  2520. nil t)
  2521. (cons 'inline-babel-call (match-beginning 0)))))
  2522. ;;;; Inline Src Block
  2523. (defun org-element-inline-src-block-parser ()
  2524. "Parse inline source block at point.
  2525. Return a list whose CAR is `inline-src-block' and CDR a plist
  2526. with `:begin', `:end', `:language', `:value', `:parameters' and
  2527. `:post-blank' as keywords.
  2528. Assume point is at the beginning of the inline src block."
  2529. (save-excursion
  2530. (unless (bolp) (backward-char))
  2531. (looking-at org-babel-inline-src-block-regexp)
  2532. (let ((begin (match-beginning 1))
  2533. (language (org-match-string-no-properties 2))
  2534. (parameters (org-match-string-no-properties 4))
  2535. (value (org-match-string-no-properties 5))
  2536. (post-blank (progn (goto-char (match-end 0))
  2537. (skip-chars-forward " \t")))
  2538. (end (point)))
  2539. (list 'inline-src-block
  2540. (list :language language
  2541. :value value
  2542. :parameters parameters
  2543. :begin begin
  2544. :end end
  2545. :post-blank post-blank)))))
  2546. (defun org-element-inline-src-block-interpreter (inline-src-block contents)
  2547. "Interpret INLINE-SRC-BLOCK object as Org syntax.
  2548. CONTENTS is nil."
  2549. (let ((language (org-element-property :language inline-src-block))
  2550. (arguments (org-element-property :parameters inline-src-block))
  2551. (body (org-element-property :value inline-src-block)))
  2552. (format "src_%s%s{%s}"
  2553. language
  2554. (if arguments (format "[%s]" arguments) "")
  2555. body)))
  2556. (defun org-element-inline-src-block-successor ()
  2557. "Search for the next inline-babel-call element.
  2558. Return value is a cons cell whose CAR is `inline-babel-call' and
  2559. CDR is beginning position."
  2560. (save-excursion
  2561. (unless (bolp) (backward-char))
  2562. (when (re-search-forward org-babel-inline-src-block-regexp nil t)
  2563. (cons 'inline-src-block (match-beginning 1)))))
  2564. ;;;; Italic
  2565. (defun org-element-italic-parser ()
  2566. "Parse italic object at point.
  2567. Return a list whose CAR is `italic' and CDR is a plist with
  2568. `:begin', `:end', `:contents-begin' and `:contents-end' and
  2569. `:post-blank' keywords.
  2570. Assume point is at the first slash marker."
  2571. (save-excursion
  2572. (unless (bolp) (backward-char 1))
  2573. (looking-at org-emph-re)
  2574. (let ((begin (match-beginning 2))
  2575. (contents-begin (match-beginning 4))
  2576. (contents-end (match-end 4))
  2577. (post-blank (progn (goto-char (match-end 2))
  2578. (skip-chars-forward " \t")))
  2579. (end (point)))
  2580. (list 'italic
  2581. (list :begin begin
  2582. :end end
  2583. :contents-begin contents-begin
  2584. :contents-end contents-end
  2585. :post-blank post-blank)))))
  2586. (defun org-element-italic-interpreter (italic contents)
  2587. "Interpret ITALIC object as Org syntax.
  2588. CONTENTS is the contents of the object."
  2589. (format "/%s/" contents))
  2590. ;;;; Latex Fragment
  2591. (defun org-element-latex-fragment-parser ()
  2592. "Parse LaTeX fragment at point.
  2593. Return a list whose CAR is `latex-fragment' and CDR a plist with
  2594. `:value', `:begin', `:end', and `:post-blank' as keywords.
  2595. Assume point is at the beginning of the LaTeX fragment."
  2596. (save-excursion
  2597. (let* ((begin (point))
  2598. (substring-match
  2599. (catch 'exit
  2600. (dolist (e (cdr org-latex-regexps))
  2601. (let ((latex-regexp (nth 1 e)))
  2602. (when (or (looking-at latex-regexp)
  2603. (and (not (bobp))
  2604. (save-excursion
  2605. (backward-char)
  2606. (looking-at latex-regexp))))
  2607. (throw 'exit (nth 2 e)))))
  2608. ;; None found: it's a macro.
  2609. (looking-at "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")
  2610. 0))
  2611. (value (org-match-string-no-properties substring-match))
  2612. (post-blank (progn (goto-char (match-end substring-match))
  2613. (skip-chars-forward " \t")))
  2614. (end (point)))
  2615. (list 'latex-fragment
  2616. (list :value value
  2617. :begin begin
  2618. :end end
  2619. :post-blank post-blank)))))
  2620. (defun org-element-latex-fragment-interpreter (latex-fragment contents)
  2621. "Interpret LATEX-FRAGMENT object as Org syntax.
  2622. CONTENTS is nil."
  2623. (org-element-property :value latex-fragment))
  2624. ;;;; Line Break
  2625. (defun org-element-line-break-parser ()
  2626. "Parse line break at point.
  2627. Return a list whose CAR is `line-break', and CDR a plist with
  2628. `:begin', `:end' and `:post-blank' keywords.
  2629. Assume point is at the beginning of the line break."
  2630. (list 'line-break
  2631. (list :begin (point)
  2632. :end (progn (forward-line) (point))
  2633. :post-blank 0)))
  2634. (defun org-element-line-break-interpreter (line-break contents)
  2635. "Interpret LINE-BREAK object as Org syntax.
  2636. CONTENTS is nil."
  2637. "\\\\\n")
  2638. (defun org-element-line-break-successor ()
  2639. "Search for the next line-break object.
  2640. Return value is a cons cell whose CAR is `line-break' and CDR is
  2641. beginning position."
  2642. (save-excursion
  2643. (let ((beg (and (re-search-forward "[^\\\\]\\(\\\\\\\\\\)[ \t]*$" nil t)
  2644. (goto-char (match-beginning 1)))))
  2645. ;; A line break can only happen on a non-empty line.
  2646. (when (and beg (re-search-backward "\\S-" (point-at-bol) t))
  2647. (cons 'line-break beg)))))
  2648. ;;;; Link
  2649. (defun org-element-link-parser ()
  2650. "Parse link at point.
  2651. Return a list whose CAR is `link' and CDR a plist with `:type',
  2652. `:path', `:raw-link', `:application', `:search-option', `:begin',
  2653. `:end', `:contents-begin', `:contents-end' and `:post-blank' as
  2654. keywords.
  2655. Assume point is at the beginning of the link."
  2656. (save-excursion
  2657. (let ((begin (point))
  2658. end contents-begin contents-end link-end post-blank path type
  2659. raw-link link search-option application)
  2660. (cond
  2661. ;; Type 1: Text targeted from a radio target.
  2662. ((and org-target-link-regexp (looking-at org-target-link-regexp))
  2663. (setq type "radio"
  2664. link-end (match-end 0)
  2665. path (org-match-string-no-properties 0)))
  2666. ;; Type 2: Standard link, i.e. [[http://orgmode.org][homepage]]
  2667. ((looking-at org-bracket-link-regexp)
  2668. (setq contents-begin (match-beginning 3)
  2669. contents-end (match-end 3)
  2670. link-end (match-end 0)
  2671. ;; RAW-LINK is the original link. Expand any
  2672. ;; abbreviation in it.
  2673. raw-link (org-translate-link
  2674. (org-link-expand-abbrev
  2675. (org-match-string-no-properties 1))))
  2676. ;; Determine TYPE of link and set PATH accordingly.
  2677. (cond
  2678. ;; File type.
  2679. ((or (file-name-absolute-p raw-link)
  2680. (string-match "^\\.\\.?/" raw-link))
  2681. (setq type "file" path raw-link))
  2682. ;; Explicit type (http, irc, bbdb...). See `org-link-types'.
  2683. ((string-match org-link-re-with-space3 raw-link)
  2684. (setq type (match-string 1 raw-link) path (match-string 2 raw-link)))
  2685. ;; Id type: PATH is the id.
  2686. ((string-match "^id:\\([-a-f0-9]+\\)" raw-link)
  2687. (setq type "id" path (match-string 1 raw-link)))
  2688. ;; Code-ref type: PATH is the name of the reference.
  2689. ((string-match "^(\\(.*\\))$" raw-link)
  2690. (setq type "coderef" path (match-string 1 raw-link)))
  2691. ;; Custom-id type: PATH is the name of the custom id.
  2692. ((= (aref raw-link 0) ?#)
  2693. (setq type "custom-id" path (substring raw-link 1)))
  2694. ;; Fuzzy type: Internal link either matches a target, an
  2695. ;; headline name or nothing. PATH is the target or
  2696. ;; headline's name.
  2697. (t (setq type "fuzzy" path raw-link))))
  2698. ;; Type 3: Plain link, i.e. http://orgmode.org
  2699. ((looking-at org-plain-link-re)
  2700. (setq raw-link (org-match-string-no-properties 0)
  2701. type (org-match-string-no-properties 1)
  2702. link-end (match-end 0)
  2703. path (org-match-string-no-properties 2)))
  2704. ;; Type 4: Angular link, i.e. <http://orgmode.org>
  2705. ((looking-at org-angle-link-re)
  2706. (setq raw-link (buffer-substring-no-properties
  2707. (match-beginning 1) (match-end 2))
  2708. type (org-match-string-no-properties 1)
  2709. link-end (match-end 0)
  2710. path (org-match-string-no-properties 2))))
  2711. ;; In any case, deduce end point after trailing white space from
  2712. ;; LINK-END variable.
  2713. (setq post-blank (progn (goto-char link-end) (skip-chars-forward " \t"))
  2714. end (point))
  2715. ;; Extract search option and opening application out of
  2716. ;; "file"-type links.
  2717. (when (member type org-element-link-type-is-file)
  2718. ;; Application.
  2719. (cond ((string-match "^file\\+\\(.*\\)$" type)
  2720. (setq application (match-string 1 type)))
  2721. ((not (string-match "^file" type))
  2722. (setq application type)))
  2723. ;; Extract search option from PATH.
  2724. (when (string-match "::\\(.*\\)$" path)
  2725. (setq search-option (match-string 1 path)
  2726. path (replace-match "" nil nil path)))
  2727. ;; Make sure TYPE always reports "file".
  2728. (setq type "file"))
  2729. (list 'link
  2730. (list :type type
  2731. :path path
  2732. :raw-link (or raw-link path)
  2733. :application application
  2734. :search-option search-option
  2735. :begin begin
  2736. :end end
  2737. :contents-begin contents-begin
  2738. :contents-end contents-end
  2739. :post-blank post-blank)))))
  2740. (defun org-element-link-interpreter (link contents)
  2741. "Interpret LINK object as Org syntax.
  2742. CONTENTS is the contents of the object, or nil."
  2743. (let ((type (org-element-property :type link))
  2744. (raw-link (org-element-property :raw-link link)))
  2745. (if (string= type "radio") raw-link
  2746. (format "[[%s]%s]"
  2747. raw-link
  2748. (if contents (format "[%s]" contents) "")))))
  2749. (defun org-element-link-successor ()
  2750. "Search for the next link object.
  2751. Return value is a cons cell whose CAR is `link' and CDR is
  2752. beginning position."
  2753. (save-excursion
  2754. (let ((link-regexp
  2755. (if (not org-target-link-regexp) org-any-link-re
  2756. (concat org-any-link-re "\\|" org-target-link-regexp))))
  2757. (when (re-search-forward link-regexp nil t)
  2758. (cons 'link (match-beginning 0))))))
  2759. (defun org-element-plain-link-successor ()
  2760. "Search for the next plain link object.
  2761. Return value is a cons cell whose CAR is `link' and CDR is
  2762. beginning position."
  2763. (and (save-excursion (re-search-forward org-plain-link-re nil t))
  2764. (cons 'link (match-beginning 0))))
  2765. ;;;; Macro
  2766. (defun org-element-macro-parser ()
  2767. "Parse macro at point.
  2768. Return a list whose CAR is `macro' and CDR a plist with `:key',
  2769. `:args', `:begin', `:end', `:value' and `:post-blank' as
  2770. keywords.
  2771. Assume point is at the macro."
  2772. (save-excursion
  2773. (looking-at "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}")
  2774. (let ((begin (point))
  2775. (key (downcase (org-match-string-no-properties 1)))
  2776. (value (org-match-string-no-properties 0))
  2777. (post-blank (progn (goto-char (match-end 0))
  2778. (skip-chars-forward " \t")))
  2779. (end (point))
  2780. (args (let ((args (org-match-string-no-properties 3)))
  2781. (when args
  2782. ;; Do not use `org-split-string' since empty
  2783. ;; strings are meaningful here.
  2784. (split-string
  2785. (replace-regexp-in-string
  2786. "\\(\\\\*\\)\\(,\\)"
  2787. (lambda (str)
  2788. (let ((len (length (match-string 1 str))))
  2789. (concat (make-string (/ len 2) ?\\)
  2790. (if (zerop (mod len 2)) "\000" ","))))
  2791. args nil t)
  2792. "\000")))))
  2793. (list 'macro
  2794. (list :key key
  2795. :value value
  2796. :args args
  2797. :begin begin
  2798. :end end
  2799. :post-blank post-blank)))))
  2800. (defun org-element-macro-interpreter (macro contents)
  2801. "Interpret MACRO object as Org syntax.
  2802. CONTENTS is nil."
  2803. (org-element-property :value macro))
  2804. (defun org-element-macro-successor ()
  2805. "Search for the next macro object.
  2806. Return value is cons cell whose CAR is `macro' and CDR is
  2807. beginning position."
  2808. (save-excursion
  2809. (when (re-search-forward
  2810. "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}"
  2811. nil t)
  2812. (cons 'macro (match-beginning 0)))))
  2813. ;;;; Radio-target
  2814. (defun org-element-radio-target-parser ()
  2815. "Parse radio target at point.
  2816. Return a list whose CAR is `radio-target' and CDR a plist with
  2817. `:begin', `:end', `:contents-begin', `:contents-end', `:value'
  2818. and `:post-blank' as keywords.
  2819. Assume point is at the radio target."
  2820. (save-excursion
  2821. (looking-at org-radio-target-regexp)
  2822. (let ((begin (point))
  2823. (contents-begin (match-beginning 1))
  2824. (contents-end (match-end 1))
  2825. (value (org-match-string-no-properties 1))
  2826. (post-blank (progn (goto-char (match-end 0))
  2827. (skip-chars-forward " \t")))
  2828. (end (point)))
  2829. (list 'radio-target
  2830. (list :begin begin
  2831. :end end
  2832. :contents-begin contents-begin
  2833. :contents-end contents-end
  2834. :post-blank post-blank
  2835. :value value)))))
  2836. (defun org-element-radio-target-interpreter (target contents)
  2837. "Interpret TARGET object as Org syntax.
  2838. CONTENTS is the contents of the object."
  2839. (concat "<<<" contents ">>>"))
  2840. (defun org-element-radio-target-successor ()
  2841. "Search for the next radio-target object.
  2842. Return value is a cons cell whose CAR is `radio-target' and CDR
  2843. is beginning position."
  2844. (save-excursion
  2845. (when (re-search-forward org-radio-target-regexp nil t)
  2846. (cons 'radio-target (match-beginning 0)))))
  2847. ;;;; Statistics Cookie
  2848. (defun org-element-statistics-cookie-parser ()
  2849. "Parse statistics cookie at point.
  2850. Return a list whose CAR is `statistics-cookie', and CDR a plist
  2851. with `:begin', `:end', `:value' and `:post-blank' keywords.
  2852. Assume point is at the beginning of the statistics-cookie."
  2853. (save-excursion
  2854. (looking-at "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]")
  2855. (let* ((begin (point))
  2856. (value (buffer-substring-no-properties
  2857. (match-beginning 0) (match-end 0)))
  2858. (post-blank (progn (goto-char (match-end 0))
  2859. (skip-chars-forward " \t")))
  2860. (end (point)))
  2861. (list 'statistics-cookie
  2862. (list :begin begin
  2863. :end end
  2864. :value value
  2865. :post-blank post-blank)))))
  2866. (defun org-element-statistics-cookie-interpreter (statistics-cookie contents)
  2867. "Interpret STATISTICS-COOKIE object as Org syntax.
  2868. CONTENTS is nil."
  2869. (org-element-property :value statistics-cookie))
  2870. (defun org-element-statistics-cookie-successor ()
  2871. "Search for the next statistics cookie object.
  2872. Return value is a cons cell whose CAR is `statistics-cookie' and
  2873. CDR is beginning position."
  2874. (save-excursion
  2875. (when (re-search-forward "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]" nil t)
  2876. (cons 'statistics-cookie (match-beginning 0)))))
  2877. ;;;; Strike-Through
  2878. (defun org-element-strike-through-parser ()
  2879. "Parse strike-through object at point.
  2880. Return a list whose CAR is `strike-through' and CDR is a plist
  2881. with `:begin', `:end', `:contents-begin' and `:contents-end' and
  2882. `:post-blank' keywords.
  2883. Assume point is at the first plus sign marker."
  2884. (save-excursion
  2885. (unless (bolp) (backward-char 1))
  2886. (looking-at org-emph-re)
  2887. (let ((begin (match-beginning 2))
  2888. (contents-begin (match-beginning 4))
  2889. (contents-end (match-end 4))
  2890. (post-blank (progn (goto-char (match-end 2))
  2891. (skip-chars-forward " \t")))
  2892. (end (point)))
  2893. (list 'strike-through
  2894. (list :begin begin
  2895. :end end
  2896. :contents-begin contents-begin
  2897. :contents-end contents-end
  2898. :post-blank post-blank)))))
  2899. (defun org-element-strike-through-interpreter (strike-through contents)
  2900. "Interpret STRIKE-THROUGH object as Org syntax.
  2901. CONTENTS is the contents of the object."
  2902. (format "+%s+" contents))
  2903. ;;;; Subscript
  2904. (defun org-element-subscript-parser ()
  2905. "Parse subscript at point.
  2906. Return a list whose CAR is `subscript' and CDR a plist with
  2907. `:begin', `:end', `:contents-begin', `:contents-end',
  2908. `:use-brackets-p' and `:post-blank' as keywords.
  2909. Assume point is at the underscore."
  2910. (save-excursion
  2911. (unless (bolp) (backward-char))
  2912. (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp)
  2913. t
  2914. (not (looking-at org-match-substring-regexp))))
  2915. (begin (match-beginning 2))
  2916. (contents-begin (or (match-beginning 5)
  2917. (match-beginning 3)))
  2918. (contents-end (or (match-end 5) (match-end 3)))
  2919. (post-blank (progn (goto-char (match-end 0))
  2920. (skip-chars-forward " \t")))
  2921. (end (point)))
  2922. (list 'subscript
  2923. (list :begin begin
  2924. :end end
  2925. :use-brackets-p bracketsp
  2926. :contents-begin contents-begin
  2927. :contents-end contents-end
  2928. :post-blank post-blank)))))
  2929. (defun org-element-subscript-interpreter (subscript contents)
  2930. "Interpret SUBSCRIPT object as Org syntax.
  2931. CONTENTS is the contents of the object."
  2932. (format
  2933. (if (org-element-property :use-brackets-p subscript) "_{%s}" "_%s")
  2934. contents))
  2935. (defun org-element-sub/superscript-successor ()
  2936. "Search for the next sub/superscript object.
  2937. Return value is a cons cell whose CAR is either `subscript' or
  2938. `superscript' and CDR is beginning position."
  2939. (save-excursion
  2940. (unless (bolp) (backward-char))
  2941. (when (re-search-forward org-match-substring-regexp nil t)
  2942. (cons (if (string= (match-string 2) "_") 'subscript 'superscript)
  2943. (match-beginning 2)))))
  2944. ;;;; Superscript
  2945. (defun org-element-superscript-parser ()
  2946. "Parse superscript at point.
  2947. Return a list whose CAR is `superscript' and CDR a plist with
  2948. `:begin', `:end', `:contents-begin', `:contents-end',
  2949. `:use-brackets-p' and `:post-blank' as keywords.
  2950. Assume point is at the caret."
  2951. (save-excursion
  2952. (unless (bolp) (backward-char))
  2953. (let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp) t
  2954. (not (looking-at org-match-substring-regexp))))
  2955. (begin (match-beginning 2))
  2956. (contents-begin (or (match-beginning 5)
  2957. (match-beginning 3)))
  2958. (contents-end (or (match-end 5) (match-end 3)))
  2959. (post-blank (progn (goto-char (match-end 0))
  2960. (skip-chars-forward " \t")))
  2961. (end (point)))
  2962. (list 'superscript
  2963. (list :begin begin
  2964. :end end
  2965. :use-brackets-p bracketsp
  2966. :contents-begin contents-begin
  2967. :contents-end contents-end
  2968. :post-blank post-blank)))))
  2969. (defun org-element-superscript-interpreter (superscript contents)
  2970. "Interpret SUPERSCRIPT object as Org syntax.
  2971. CONTENTS is the contents of the object."
  2972. (format
  2973. (if (org-element-property :use-brackets-p superscript) "^{%s}" "^%s")
  2974. contents))
  2975. ;;;; Table Cell
  2976. (defun org-element-table-cell-parser ()
  2977. "Parse table cell at point.
  2978. Return a list whose CAR is `table-cell' and CDR is a plist
  2979. containing `:begin', `:end', `:contents-begin', `:contents-end'
  2980. and `:post-blank' keywords."
  2981. (looking-at "[ \t]*\\(.*?\\)[ \t]*|")
  2982. (let* ((begin (match-beginning 0))
  2983. (end (match-end 0))
  2984. (contents-begin (match-beginning 1))
  2985. (contents-end (match-end 1)))
  2986. (list 'table-cell
  2987. (list :begin begin
  2988. :end end
  2989. :contents-begin contents-begin
  2990. :contents-end contents-end
  2991. :post-blank 0))))
  2992. (defun org-element-table-cell-interpreter (table-cell contents)
  2993. "Interpret TABLE-CELL element as Org syntax.
  2994. CONTENTS is the contents of the cell, or nil."
  2995. (concat " " contents " |"))
  2996. (defun org-element-table-cell-successor ()
  2997. "Search for the next table-cell object.
  2998. Return value is a cons cell whose CAR is `table-cell' and CDR is
  2999. beginning position."
  3000. (when (looking-at "[ \t]*.*?[ \t]*|") (cons 'table-cell (point))))
  3001. ;;;; Target
  3002. (defun org-element-target-parser ()
  3003. "Parse target at point.
  3004. Return a list whose CAR is `target' and CDR a plist with
  3005. `:begin', `:end', `:value' and `:post-blank' as keywords.
  3006. Assume point is at the target."
  3007. (save-excursion
  3008. (looking-at org-target-regexp)
  3009. (let ((begin (point))
  3010. (value (org-match-string-no-properties 1))
  3011. (post-blank (progn (goto-char (match-end 0))
  3012. (skip-chars-forward " \t")))
  3013. (end (point)))
  3014. (list 'target
  3015. (list :begin begin
  3016. :end end
  3017. :value value
  3018. :post-blank post-blank)))))
  3019. (defun org-element-target-interpreter (target contents)
  3020. "Interpret TARGET object as Org syntax.
  3021. CONTENTS is nil."
  3022. (format "<<%s>>" (org-element-property :value target)))
  3023. (defun org-element-target-successor ()
  3024. "Search for the next target object.
  3025. Return value is a cons cell whose CAR is `target' and CDR is
  3026. beginning position."
  3027. (save-excursion
  3028. (when (re-search-forward org-target-regexp nil t)
  3029. (cons 'target (match-beginning 0)))))
  3030. ;;;; Timestamp
  3031. (defun org-element-timestamp-parser ()
  3032. "Parse time stamp at point.
  3033. Return a list whose CAR is `timestamp', and CDR a plist with
  3034. `:type', `:raw-value', `:year-start', `:month-start',
  3035. `:day-start', `:hour-start', `:minute-start', `:year-end',
  3036. `:month-end', `:day-end', `:hour-end', `:minute-end',
  3037. `:repeater-type', `:repeater-value', `:repeater-unit',
  3038. `:warning-type', `:warning-value', `:warning-unit', `:begin',
  3039. `:end', `:value' and `:post-blank' keywords.
  3040. Assume point is at the beginning of the timestamp."
  3041. (save-excursion
  3042. (let* ((begin (point))
  3043. (activep (eq (char-after) ?<))
  3044. (raw-value
  3045. (progn
  3046. (looking-at "\\([<[]\\(%%\\)?.*?\\)[]>]\\(?:--\\([<[].*?[]>]\\)\\)?")
  3047. (match-string-no-properties 0)))
  3048. (date-start (match-string-no-properties 1))
  3049. (date-end (match-string 3))
  3050. (diaryp (match-beginning 2))
  3051. (post-blank (progn (goto-char (match-end 0))
  3052. (skip-chars-forward " \t")))
  3053. (end (point))
  3054. (time-range
  3055. (and (not diaryp)
  3056. (string-match
  3057. "[012]?[0-9]:[0-5][0-9]\\(-\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)"
  3058. date-start)
  3059. (cons (string-to-number (match-string 2 date-start))
  3060. (string-to-number (match-string 3 date-start)))))
  3061. (type (cond (diaryp 'diary)
  3062. ((and activep (or date-end time-range)) 'active-range)
  3063. (activep 'active)
  3064. ((or date-end time-range) 'inactive-range)
  3065. (t 'inactive)))
  3066. (repeater-props
  3067. (and (not diaryp)
  3068. (string-match "\\([.+]?\\+\\)\\([0-9]+\\)\\([hdwmy]\\)"
  3069. raw-value)
  3070. (list
  3071. :repeater-type
  3072. (let ((type (match-string 1 raw-value)))
  3073. (cond ((equal "++" type) 'catch-up)
  3074. ((equal ".+" type) 'restart)
  3075. (t 'cumulate)))
  3076. :repeater-value (string-to-number (match-string 2 raw-value))
  3077. :repeater-unit
  3078. (case (string-to-char (match-string 3 raw-value))
  3079. (?h 'hour) (?d 'day) (?w 'week) (?m 'month) (t 'year)))))
  3080. (warning-props
  3081. (and (not diaryp)
  3082. (string-match "\\(-\\)?-\\([0-9]+\\)\\([hdwmy]\\)" raw-value)
  3083. (list
  3084. :warning-type (if (match-string 1 raw-value) 'first 'all)
  3085. :warning-value (string-to-number (match-string 2 raw-value))
  3086. :warning-unit
  3087. (case (string-to-char (match-string 3 raw-value))
  3088. (?h 'hour) (?d 'day) (?w 'week) (?m 'month) (t 'year)))))
  3089. year-start month-start day-start hour-start minute-start year-end
  3090. month-end day-end hour-end minute-end)
  3091. ;; Parse date-start.
  3092. (unless diaryp
  3093. (let ((date (org-parse-time-string date-start t)))
  3094. (setq year-start (nth 5 date)
  3095. month-start (nth 4 date)
  3096. day-start (nth 3 date)
  3097. hour-start (nth 2 date)
  3098. minute-start (nth 1 date))))
  3099. ;; Compute date-end. It can be provided directly in time-stamp,
  3100. ;; or extracted from time range. Otherwise, it defaults to the
  3101. ;; same values as date-start.
  3102. (unless diaryp
  3103. (let ((date (and date-end (org-parse-time-string date-end t))))
  3104. (setq year-end (or (nth 5 date) year-start)
  3105. month-end (or (nth 4 date) month-start)
  3106. day-end (or (nth 3 date) day-start)
  3107. hour-end (or (nth 2 date) (car time-range) hour-start)
  3108. minute-end (or (nth 1 date) (cdr time-range) minute-start))))
  3109. (list 'timestamp
  3110. (nconc (list :type type
  3111. :raw-value raw-value
  3112. :year-start year-start
  3113. :month-start month-start
  3114. :day-start day-start
  3115. :hour-start hour-start
  3116. :minute-start minute-start
  3117. :year-end year-end
  3118. :month-end month-end
  3119. :day-end day-end
  3120. :hour-end hour-end
  3121. :minute-end minute-end
  3122. :begin begin
  3123. :end end
  3124. :post-blank post-blank)
  3125. repeater-props
  3126. warning-props)))))
  3127. (defun org-element-timestamp-interpreter (timestamp contents)
  3128. "Interpret TIMESTAMP object as Org syntax.
  3129. CONTENTS is nil."
  3130. (let* ((repeat-string
  3131. (concat
  3132. (case (org-element-property :repeater-type timestamp)
  3133. (cumulate "+") (catch-up "++") (restart ".+"))
  3134. (let ((val (org-element-property :repeater-value timestamp)))
  3135. (and val (number-to-string val)))
  3136. (case (org-element-property :repeater-unit timestamp)
  3137. (hour "h") (day "d") (week "w") (month "m") (year "y"))))
  3138. (warning-string
  3139. (concat
  3140. (case (org-element-property :warning-type timestamp)
  3141. (first "--")
  3142. (all "-"))
  3143. (let ((val (org-element-property :warning-value timestamp)))
  3144. (and val (number-to-string val)))
  3145. (case (org-element-property :warning-unit timestamp)
  3146. (hour "h") (day "d") (week "w") (month "m") (year "y"))))
  3147. (build-ts-string
  3148. ;; Build an Org timestamp string from TIME. ACTIVEP is
  3149. ;; non-nil when time stamp is active. If WITH-TIME-P is
  3150. ;; non-nil, add a time part. HOUR-END and MINUTE-END
  3151. ;; specify a time range in the timestamp. REPEAT-STRING is
  3152. ;; the repeater string, if any.
  3153. (lambda (time activep &optional with-time-p hour-end minute-end)
  3154. (let ((ts (format-time-string
  3155. (funcall (if with-time-p 'cdr 'car)
  3156. org-time-stamp-formats)
  3157. time)))
  3158. (when (and hour-end minute-end)
  3159. (string-match "[012]?[0-9]:[0-5][0-9]" ts)
  3160. (setq ts
  3161. (replace-match
  3162. (format "\\&-%02d:%02d" hour-end minute-end)
  3163. nil nil ts)))
  3164. (unless activep (setq ts (format "[%s]" (substring ts 1 -1))))
  3165. (dolist (s (list repeat-string warning-string))
  3166. (when (org-string-nw-p s)
  3167. (setq ts (concat (substring ts 0 -1)
  3168. " "
  3169. s
  3170. (substring ts -1)))))
  3171. ;; Return value.
  3172. ts)))
  3173. (type (org-element-property :type timestamp)))
  3174. (case type
  3175. ((active inactive)
  3176. (let* ((minute-start (org-element-property :minute-start timestamp))
  3177. (minute-end (org-element-property :minute-end timestamp))
  3178. (hour-start (org-element-property :hour-start timestamp))
  3179. (hour-end (org-element-property :hour-end timestamp))
  3180. (time-range-p (and hour-start hour-end minute-start minute-end
  3181. (or (/= hour-start hour-end)
  3182. (/= minute-start minute-end)))))
  3183. (funcall
  3184. build-ts-string
  3185. (encode-time 0
  3186. (or minute-start 0)
  3187. (or hour-start 0)
  3188. (org-element-property :day-start timestamp)
  3189. (org-element-property :month-start timestamp)
  3190. (org-element-property :year-start timestamp))
  3191. (eq type 'active)
  3192. (and hour-start minute-start)
  3193. (and time-range-p hour-end)
  3194. (and time-range-p minute-end))))
  3195. ((active-range inactive-range)
  3196. (let ((minute-start (org-element-property :minute-start timestamp))
  3197. (minute-end (org-element-property :minute-end timestamp))
  3198. (hour-start (org-element-property :hour-start timestamp))
  3199. (hour-end (org-element-property :hour-end timestamp)))
  3200. (concat
  3201. (funcall
  3202. build-ts-string (encode-time
  3203. 0
  3204. (or minute-start 0)
  3205. (or hour-start 0)
  3206. (org-element-property :day-start timestamp)
  3207. (org-element-property :month-start timestamp)
  3208. (org-element-property :year-start timestamp))
  3209. (eq type 'active-range)
  3210. (and hour-start minute-start))
  3211. "--"
  3212. (funcall build-ts-string
  3213. (encode-time 0
  3214. (or minute-end 0)
  3215. (or hour-end 0)
  3216. (org-element-property :day-end timestamp)
  3217. (org-element-property :month-end timestamp)
  3218. (org-element-property :year-end timestamp))
  3219. (eq type 'active-range)
  3220. (and hour-end minute-end))))))))
  3221. (defun org-element-timestamp-successor ()
  3222. "Search for the next timestamp object.
  3223. Return value is a cons cell whose CAR is `timestamp' and CDR is
  3224. beginning position."
  3225. (save-excursion
  3226. (when (re-search-forward
  3227. (concat org-ts-regexp-both
  3228. "\\|"
  3229. "\\(?:<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
  3230. "\\|"
  3231. "\\(?:<%%\\(?:([^>\n]+)\\)>\\)")
  3232. nil t)
  3233. (cons 'timestamp (match-beginning 0)))))
  3234. ;;;; Underline
  3235. (defun org-element-underline-parser ()
  3236. "Parse underline object at point.
  3237. Return a list whose CAR is `underline' and CDR is a plist with
  3238. `:begin', `:end', `:contents-begin' and `:contents-end' and
  3239. `:post-blank' keywords.
  3240. Assume point is at the first underscore marker."
  3241. (save-excursion
  3242. (unless (bolp) (backward-char 1))
  3243. (looking-at org-emph-re)
  3244. (let ((begin (match-beginning 2))
  3245. (contents-begin (match-beginning 4))
  3246. (contents-end (match-end 4))
  3247. (post-blank (progn (goto-char (match-end 2))
  3248. (skip-chars-forward " \t")))
  3249. (end (point)))
  3250. (list 'underline
  3251. (list :begin begin
  3252. :end end
  3253. :contents-begin contents-begin
  3254. :contents-end contents-end
  3255. :post-blank post-blank)))))
  3256. (defun org-element-underline-interpreter (underline contents)
  3257. "Interpret UNDERLINE object as Org syntax.
  3258. CONTENTS is the contents of the object."
  3259. (format "_%s_" contents))
  3260. ;;;; Verbatim
  3261. (defun org-element-verbatim-parser ()
  3262. "Parse verbatim object at point.
  3263. Return a list whose CAR is `verbatim' and CDR is a plist with
  3264. `:value', `:begin', `:end' and `:post-blank' keywords.
  3265. Assume point is at the first equal sign marker."
  3266. (save-excursion
  3267. (unless (bolp) (backward-char 1))
  3268. (looking-at org-emph-re)
  3269. (let ((begin (match-beginning 2))
  3270. (value (org-match-string-no-properties 4))
  3271. (post-blank (progn (goto-char (match-end 2))
  3272. (skip-chars-forward " \t")))
  3273. (end (point)))
  3274. (list 'verbatim
  3275. (list :value value
  3276. :begin begin
  3277. :end end
  3278. :post-blank post-blank)))))
  3279. (defun org-element-verbatim-interpreter (verbatim contents)
  3280. "Interpret VERBATIM object as Org syntax.
  3281. CONTENTS is nil."
  3282. (format "=%s=" (org-element-property :value verbatim)))
  3283. ;;; Parsing Element Starting At Point
  3284. ;;
  3285. ;; `org-element--current-element' is the core function of this section.
  3286. ;; It returns the Lisp representation of the element starting at
  3287. ;; point.
  3288. ;;
  3289. ;; `org-element--current-element' makes use of special modes. They
  3290. ;; are activated for fixed element chaining (i.e. `plain-list' >
  3291. ;; `item') or fixed conditional element chaining (i.e. `headline' >
  3292. ;; `section'). Special modes are: `first-section', `item',
  3293. ;; `node-property', `quote-section', `section' and `table-row'.
  3294. (defun org-element--current-element
  3295. (limit &optional granularity special structure)
  3296. "Parse the element starting at point.
  3297. Return value is a list like (TYPE PROPS) where TYPE is the type
  3298. of the element and PROPS a plist of properties associated to the
  3299. element.
  3300. Possible types are defined in `org-element-all-elements'.
  3301. LIMIT bounds the search.
  3302. Optional argument GRANULARITY determines the depth of the
  3303. recursion. Allowed values are `headline', `greater-element',
  3304. `element', `object' or nil. When it is broader than `object' (or
  3305. nil), secondary values will not be parsed, since they only
  3306. contain objects.
  3307. Optional argument SPECIAL, when non-nil, can be either
  3308. `first-section', `item', `node-property', `quote-section',
  3309. `section', and `table-row'.
  3310. If STRUCTURE isn't provided but SPECIAL is set to `item', it will
  3311. be computed.
  3312. This function assumes point is always at the beginning of the
  3313. element it has to parse."
  3314. (save-excursion
  3315. (let ((case-fold-search t)
  3316. ;; Determine if parsing depth allows for secondary strings
  3317. ;; parsing. It only applies to elements referenced in
  3318. ;; `org-element-secondary-value-alist'.
  3319. (raw-secondary-p (and granularity (not (eq granularity 'object)))))
  3320. (cond
  3321. ;; Item.
  3322. ((eq special 'item)
  3323. (org-element-item-parser limit structure raw-secondary-p))
  3324. ;; Table Row.
  3325. ((eq special 'table-row) (org-element-table-row-parser limit))
  3326. ;; Node Property.
  3327. ((eq special 'node-property) (org-element-node-property-parser limit))
  3328. ;; Headline.
  3329. ((org-with-limited-levels (org-at-heading-p))
  3330. (org-element-headline-parser limit raw-secondary-p))
  3331. ;; Sections (must be checked after headline).
  3332. ((eq special 'section) (org-element-section-parser limit))
  3333. ((eq special 'quote-section) (org-element-quote-section-parser limit))
  3334. ((eq special 'first-section)
  3335. (org-element-section-parser
  3336. (or (save-excursion (org-with-limited-levels (outline-next-heading)))
  3337. limit)))
  3338. ;; When not at bol, point is at the beginning of an item or
  3339. ;; a footnote definition: next item is always a paragraph.
  3340. ((not (bolp)) (org-element-paragraph-parser limit (list (point))))
  3341. ;; Planning and Clock.
  3342. ((looking-at org-planning-or-clock-line-re)
  3343. (if (equal (match-string 1) org-clock-string)
  3344. (org-element-clock-parser limit)
  3345. (org-element-planning-parser limit)))
  3346. ;; Inlinetask.
  3347. ((org-at-heading-p)
  3348. (org-element-inlinetask-parser limit raw-secondary-p))
  3349. ;; From there, elements can have affiliated keywords.
  3350. (t (let ((affiliated (org-element--collect-affiliated-keywords limit)))
  3351. (cond
  3352. ;; Jumping over affiliated keywords put point off-limits.
  3353. ;; Parse them as regular keywords.
  3354. ((and (cdr affiliated) (>= (point) limit))
  3355. (goto-char (car affiliated))
  3356. (org-element-keyword-parser limit nil))
  3357. ;; LaTeX Environment.
  3358. ((looking-at
  3359. "[ \t]*\\\\begin{[A-Za-z0-9*]+}\\(\\[.*?\\]\\|{.*?}\\)*[ \t]*$")
  3360. (org-element-latex-environment-parser limit affiliated))
  3361. ;; Drawer and Property Drawer.
  3362. ((looking-at org-drawer-regexp)
  3363. (if (equal (match-string 1) "PROPERTIES")
  3364. (org-element-property-drawer-parser limit affiliated)
  3365. (org-element-drawer-parser limit affiliated)))
  3366. ;; Fixed Width
  3367. ((looking-at "[ \t]*:\\( \\|$\\)")
  3368. (org-element-fixed-width-parser limit affiliated))
  3369. ;; Inline Comments, Blocks, Babel Calls, Dynamic Blocks and
  3370. ;; Keywords.
  3371. ((looking-at "[ \t]*#")
  3372. (goto-char (match-end 0))
  3373. (cond ((looking-at "\\(?: \\|$\\)")
  3374. (beginning-of-line)
  3375. (org-element-comment-parser limit affiliated))
  3376. ((looking-at "\\+BEGIN_\\(\\S-+\\)")
  3377. (beginning-of-line)
  3378. (let ((parser (assoc (upcase (match-string 1))
  3379. org-element-block-name-alist)))
  3380. (if parser (funcall (cdr parser) limit affiliated)
  3381. (org-element-special-block-parser limit affiliated))))
  3382. ((looking-at "\\+CALL:")
  3383. (beginning-of-line)
  3384. (org-element-babel-call-parser limit affiliated))
  3385. ((looking-at "\\+BEGIN:? ")
  3386. (beginning-of-line)
  3387. (org-element-dynamic-block-parser limit affiliated))
  3388. ((looking-at "\\+\\S-+:")
  3389. (beginning-of-line)
  3390. (org-element-keyword-parser limit affiliated))
  3391. (t
  3392. (beginning-of-line)
  3393. (org-element-paragraph-parser limit affiliated))))
  3394. ;; Footnote Definition.
  3395. ((looking-at org-footnote-definition-re)
  3396. (org-element-footnote-definition-parser limit affiliated))
  3397. ;; Horizontal Rule.
  3398. ((looking-at "[ \t]*-\\{5,\\}[ \t]*$")
  3399. (org-element-horizontal-rule-parser limit affiliated))
  3400. ;; Diary Sexp.
  3401. ((looking-at "%%(")
  3402. (org-element-diary-sexp-parser limit affiliated))
  3403. ;; Table.
  3404. ((org-at-table-p t) (org-element-table-parser limit affiliated))
  3405. ;; List.
  3406. ((looking-at (org-item-re))
  3407. (org-element-plain-list-parser
  3408. limit affiliated
  3409. (or structure (org-element--list-struct limit))))
  3410. ;; Default element: Paragraph.
  3411. (t (org-element-paragraph-parser limit affiliated)))))))))
  3412. ;; Most elements can have affiliated keywords. When looking for an
  3413. ;; element beginning, we want to move before them, as they belong to
  3414. ;; that element, and, in the meantime, collect information they give
  3415. ;; into appropriate properties. Hence the following function.
  3416. (defun org-element--collect-affiliated-keywords (limit)
  3417. "Collect affiliated keywords from point down to LIMIT.
  3418. Return a list whose CAR is the position at the first of them and
  3419. CDR a plist of keywords and values and move point to the
  3420. beginning of the first line after them.
  3421. As a special case, if element doesn't start at the beginning of
  3422. the line (i.e. a paragraph starting an item), CAR is current
  3423. position of point and CDR is nil."
  3424. (if (not (bolp)) (list (point))
  3425. (let ((case-fold-search t)
  3426. (origin (point))
  3427. ;; RESTRICT is the list of objects allowed in parsed
  3428. ;; keywords value.
  3429. (restrict (org-element-restriction 'keyword))
  3430. output)
  3431. (while (and (< (point) limit) (looking-at org-element--affiliated-re))
  3432. (let* ((raw-kwd (upcase (match-string 1)))
  3433. ;; Apply translation to RAW-KWD. From there, KWD is
  3434. ;; the official keyword.
  3435. (kwd (or (cdr (assoc raw-kwd
  3436. org-element-keyword-translation-alist))
  3437. raw-kwd))
  3438. ;; Find main value for any keyword.
  3439. (value
  3440. (save-match-data
  3441. (org-trim
  3442. (buffer-substring-no-properties
  3443. (match-end 0) (point-at-eol)))))
  3444. ;; PARSEDP is non-nil when keyword should have its
  3445. ;; value parsed.
  3446. (parsedp (member kwd org-element-parsed-keywords))
  3447. ;; If KWD is a dual keyword, find its secondary
  3448. ;; value. Maybe parse it.
  3449. (dualp (member kwd org-element-dual-keywords))
  3450. (dual-value
  3451. (and dualp
  3452. (let ((sec (org-match-string-no-properties 2)))
  3453. (if (or (not sec) (not parsedp)) sec
  3454. (org-element-parse-secondary-string sec restrict)))))
  3455. ;; Attribute a property name to KWD.
  3456. (kwd-sym (and kwd (intern (concat ":" (downcase kwd))))))
  3457. ;; Now set final shape for VALUE.
  3458. (when parsedp
  3459. (setq value (org-element-parse-secondary-string value restrict)))
  3460. (when dualp
  3461. (setq value (and (or value dual-value) (cons value dual-value))))
  3462. (when (or (member kwd org-element-multiple-keywords)
  3463. ;; Attributes can always appear on multiple lines.
  3464. (string-match "^ATTR_" kwd))
  3465. (setq value (cons value (plist-get output kwd-sym))))
  3466. ;; Eventually store the new value in OUTPUT.
  3467. (setq output (plist-put output kwd-sym value))
  3468. ;; Move to next keyword.
  3469. (forward-line)))
  3470. ;; If affiliated keywords are orphaned: move back to first one.
  3471. ;; They will be parsed as a paragraph.
  3472. (when (looking-at "[ \t]*$") (goto-char origin) (setq output nil))
  3473. ;; Return value.
  3474. (cons origin output))))
  3475. ;;; The Org Parser
  3476. ;;
  3477. ;; The two major functions here are `org-element-parse-buffer', which
  3478. ;; parses Org syntax inside the current buffer, taking into account
  3479. ;; region, narrowing, or even visibility if specified, and
  3480. ;; `org-element-parse-secondary-string', which parses objects within
  3481. ;; a given string.
  3482. ;;
  3483. ;; The (almost) almighty `org-element-map' allows to apply a function
  3484. ;; on elements or objects matching some type, and accumulate the
  3485. ;; resulting values. In an export situation, it also skips unneeded
  3486. ;; parts of the parse tree.
  3487. (defun org-element-parse-buffer (&optional granularity visible-only)
  3488. "Recursively parse the buffer and return structure.
  3489. If narrowing is in effect, only parse the visible part of the
  3490. buffer.
  3491. Optional argument GRANULARITY determines the depth of the
  3492. recursion. It can be set to the following symbols:
  3493. `headline' Only parse headlines.
  3494. `greater-element' Don't recurse into greater elements excepted
  3495. headlines and sections. Thus, elements
  3496. parsed are the top-level ones.
  3497. `element' Parse everything but objects and plain text.
  3498. `object' Parse the complete buffer (default).
  3499. When VISIBLE-ONLY is non-nil, don't parse contents of hidden
  3500. elements.
  3501. An element or an objects is represented as a list with the
  3502. pattern (TYPE PROPERTIES CONTENTS), where :
  3503. TYPE is a symbol describing the element or object. See
  3504. `org-element-all-elements' and `org-element-all-objects' for an
  3505. exhaustive list of such symbols. One can retrieve it with
  3506. `org-element-type' function.
  3507. PROPERTIES is the list of attributes attached to the element or
  3508. object, as a plist. Although most of them are specific to the
  3509. element or object type, all types share `:begin', `:end',
  3510. `:post-blank' and `:parent' properties, which respectively
  3511. refer to buffer position where the element or object starts,
  3512. ends, the number of white spaces or blank lines after it, and
  3513. the element or object containing it. Properties values can be
  3514. obtained by using `org-element-property' function.
  3515. CONTENTS is a list of elements, objects or raw strings
  3516. contained in the current element or object, when applicable.
  3517. One can access them with `org-element-contents' function.
  3518. The Org buffer has `org-data' as type and nil as properties.
  3519. `org-element-map' function can be used to find specific elements
  3520. or objects within the parse tree.
  3521. This function assumes that current major mode is `org-mode'."
  3522. (save-excursion
  3523. (goto-char (point-min))
  3524. (org-skip-whitespace)
  3525. (org-element--parse-elements
  3526. (point-at-bol) (point-max)
  3527. ;; Start in `first-section' mode so text before the first
  3528. ;; headline belongs to a section.
  3529. 'first-section nil granularity visible-only (list 'org-data nil))))
  3530. (defun org-element-parse-secondary-string (string restriction &optional parent)
  3531. "Recursively parse objects in STRING and return structure.
  3532. RESTRICTION is a symbol limiting the object types that will be
  3533. looked after.
  3534. Optional argument PARENT, when non-nil, is the element or object
  3535. containing the secondary string. It is used to set correctly
  3536. `:parent' property within the string."
  3537. ;; Copy buffer-local variables listed in
  3538. ;; `org-element-object-variables' into temporary buffer. This is
  3539. ;; required since object parsing is dependent on these variables.
  3540. (let ((pairs (delq nil (mapcar (lambda (var)
  3541. (when (boundp var)
  3542. (cons var (symbol-value var))))
  3543. org-element-object-variables))))
  3544. (with-temp-buffer
  3545. (mapc (lambda (pair) (org-set-local (car pair) (cdr pair))) pairs)
  3546. (insert string)
  3547. (let ((secondary (org-element--parse-objects
  3548. (point-min) (point-max) nil restriction)))
  3549. (when parent
  3550. (mapc (lambda (obj) (org-element-put-property obj :parent parent))
  3551. secondary))
  3552. secondary))))
  3553. (defun org-element-map
  3554. (data types fun &optional info first-match no-recursion with-affiliated)
  3555. "Map a function on selected elements or objects.
  3556. DATA is a parse tree, an element, an object, a string, or a list
  3557. of such constructs. TYPES is a symbol or list of symbols of
  3558. elements or objects types (see `org-element-all-elements' and
  3559. `org-element-all-objects' for a complete list of types). FUN is
  3560. the function called on the matching element or object. It has to
  3561. accept one argument: the element or object itself.
  3562. When optional argument INFO is non-nil, it should be a plist
  3563. holding export options. In that case, parts of the parse tree
  3564. not exportable according to that property list will be skipped.
  3565. When optional argument FIRST-MATCH is non-nil, stop at the first
  3566. match for which FUN doesn't return nil, and return that value.
  3567. Optional argument NO-RECURSION is a symbol or a list of symbols
  3568. representing elements or objects types. `org-element-map' won't
  3569. enter any recursive element or object whose type belongs to that
  3570. list. Though, FUN can still be applied on them.
  3571. When optional argument WITH-AFFILIATED is non-nil, FUN will also
  3572. apply to matching objects within parsed affiliated keywords (see
  3573. `org-element-parsed-keywords').
  3574. Nil values returned from FUN do not appear in the results.
  3575. Examples:
  3576. ---------
  3577. Assuming TREE is a variable containing an Org buffer parse tree,
  3578. the following example will return a flat list of all `src-block'
  3579. and `example-block' elements in it:
  3580. \(org-element-map tree '(example-block src-block) 'identity)
  3581. The following snippet will find the first headline with a level
  3582. of 1 and a \"phone\" tag, and will return its beginning position:
  3583. \(org-element-map tree 'headline
  3584. \(lambda (hl)
  3585. \(and (= (org-element-property :level hl) 1)
  3586. \(member \"phone\" (org-element-property :tags hl))
  3587. \(org-element-property :begin hl)))
  3588. nil t)
  3589. The next example will return a flat list of all `plain-list' type
  3590. elements in TREE that are not a sub-list themselves:
  3591. \(org-element-map tree 'plain-list 'identity nil nil 'plain-list)
  3592. Eventually, this example will return a flat list of all `bold'
  3593. type objects containing a `latex-snippet' type object, even
  3594. looking into captions:
  3595. \(org-element-map tree 'bold
  3596. \(lambda (b)
  3597. \(and (org-element-map b 'latex-snippet 'identity nil t) b))
  3598. nil nil nil t)"
  3599. ;; Ensure TYPES and NO-RECURSION are a list, even of one element.
  3600. (unless (listp types) (setq types (list types)))
  3601. (unless (listp no-recursion) (setq no-recursion (list no-recursion)))
  3602. ;; Recursion depth is determined by --CATEGORY.
  3603. (let* ((--category
  3604. (catch 'found
  3605. (let ((category 'greater-elements))
  3606. (mapc (lambda (type)
  3607. (cond ((or (memq type org-element-all-objects)
  3608. (eq type 'plain-text))
  3609. ;; If one object is found, the function
  3610. ;; has to recurse into every object.
  3611. (throw 'found 'objects))
  3612. ((not (memq type org-element-greater-elements))
  3613. ;; If one regular element is found, the
  3614. ;; function has to recurse, at least,
  3615. ;; into every element it encounters.
  3616. (and (not (eq category 'elements))
  3617. (setq category 'elements)))))
  3618. types)
  3619. category)))
  3620. ;; Compute properties for affiliated keywords if necessary.
  3621. (--affiliated-alist
  3622. (and with-affiliated
  3623. (mapcar (lambda (kwd)
  3624. (cons kwd (intern (concat ":" (downcase kwd)))))
  3625. org-element-affiliated-keywords)))
  3626. --acc
  3627. --walk-tree
  3628. (--walk-tree
  3629. (function
  3630. (lambda (--data)
  3631. ;; Recursively walk DATA. INFO, if non-nil, is a plist
  3632. ;; holding contextual information.
  3633. (let ((--type (org-element-type --data)))
  3634. (cond
  3635. ((not --data))
  3636. ;; Ignored element in an export context.
  3637. ((and info (memq --data (plist-get info :ignore-list))))
  3638. ;; List of elements or objects.
  3639. ((not --type) (mapc --walk-tree --data))
  3640. ;; Unconditionally enter parse trees.
  3641. ((eq --type 'org-data)
  3642. (mapc --walk-tree (org-element-contents --data)))
  3643. (t
  3644. ;; Check if TYPE is matching among TYPES. If so,
  3645. ;; apply FUN to --DATA and accumulate return value
  3646. ;; into --ACC (or exit if FIRST-MATCH is non-nil).
  3647. (when (memq --type types)
  3648. (let ((result (funcall fun --data)))
  3649. (cond ((not result))
  3650. (first-match (throw '--map-first-match result))
  3651. (t (push result --acc)))))
  3652. ;; If --DATA has a secondary string that can contain
  3653. ;; objects with their type among TYPES, look into it.
  3654. (when (and (eq --category 'objects) (not (stringp --data)))
  3655. (let ((sec-prop
  3656. (assq --type org-element-secondary-value-alist)))
  3657. (when sec-prop
  3658. (funcall --walk-tree
  3659. (org-element-property (cdr sec-prop) --data)))))
  3660. ;; If --DATA has any affiliated keywords and
  3661. ;; WITH-AFFILIATED is non-nil, look for objects in
  3662. ;; them.
  3663. (when (and with-affiliated
  3664. (eq --category 'objects)
  3665. (memq --type org-element-all-elements))
  3666. (mapc (lambda (kwd-pair)
  3667. (let ((kwd (car kwd-pair))
  3668. (value (org-element-property
  3669. (cdr kwd-pair) --data)))
  3670. ;; Pay attention to the type of value.
  3671. ;; Preserve order for multiple keywords.
  3672. (cond
  3673. ((not value))
  3674. ((and (member kwd org-element-multiple-keywords)
  3675. (member kwd org-element-dual-keywords))
  3676. (mapc (lambda (line)
  3677. (funcall --walk-tree (cdr line))
  3678. (funcall --walk-tree (car line)))
  3679. (reverse value)))
  3680. ((member kwd org-element-multiple-keywords)
  3681. (mapc (lambda (line) (funcall --walk-tree line))
  3682. (reverse value)))
  3683. ((member kwd org-element-dual-keywords)
  3684. (funcall --walk-tree (cdr value))
  3685. (funcall --walk-tree (car value)))
  3686. (t (funcall --walk-tree value)))))
  3687. --affiliated-alist))
  3688. ;; Determine if a recursion into --DATA is possible.
  3689. (cond
  3690. ;; --TYPE is explicitly removed from recursion.
  3691. ((memq --type no-recursion))
  3692. ;; --DATA has no contents.
  3693. ((not (org-element-contents --data)))
  3694. ;; Looking for greater elements but --DATA is simply
  3695. ;; an element or an object.
  3696. ((and (eq --category 'greater-elements)
  3697. (not (memq --type org-element-greater-elements))))
  3698. ;; Looking for elements but --DATA is an object.
  3699. ((and (eq --category 'elements)
  3700. (memq --type org-element-all-objects)))
  3701. ;; In any other case, map contents.
  3702. (t (mapc --walk-tree (org-element-contents --data)))))))))))
  3703. (catch '--map-first-match
  3704. (funcall --walk-tree data)
  3705. ;; Return value in a proper order.
  3706. (nreverse --acc))))
  3707. (put 'org-element-map 'lisp-indent-function 2)
  3708. ;; The following functions are internal parts of the parser.
  3709. ;;
  3710. ;; The first one, `org-element--parse-elements' acts at the element's
  3711. ;; level.
  3712. ;;
  3713. ;; The second one, `org-element--parse-objects' applies on all objects
  3714. ;; of a paragraph or a secondary string. It uses
  3715. ;; `org-element--get-next-object-candidates' to optimize the search of
  3716. ;; the next object in the buffer.
  3717. ;;
  3718. ;; More precisely, that function looks for every allowed object type
  3719. ;; first. Then, it discards failed searches, keeps further matches,
  3720. ;; and searches again types matched behind point, for subsequent
  3721. ;; calls. Thus, searching for a given type fails only once, and every
  3722. ;; object is searched only once at top level (but sometimes more for
  3723. ;; nested types).
  3724. (defun org-element--parse-elements
  3725. (beg end special structure granularity visible-only acc)
  3726. "Parse elements between BEG and END positions.
  3727. SPECIAL prioritize some elements over the others. It can be set
  3728. to `first-section', `quote-section', `section' `item' or
  3729. `table-row'.
  3730. When value is `item', STRUCTURE will be used as the current list
  3731. structure.
  3732. GRANULARITY determines the depth of the recursion. See
  3733. `org-element-parse-buffer' for more information.
  3734. When VISIBLE-ONLY is non-nil, don't parse contents of hidden
  3735. elements.
  3736. Elements are accumulated into ACC."
  3737. (save-excursion
  3738. (goto-char beg)
  3739. ;; Visible only: skip invisible parts at the beginning of the
  3740. ;; element.
  3741. (when (and visible-only (org-invisible-p2))
  3742. (goto-char (min (1+ (org-find-visible)) end)))
  3743. ;; When parsing only headlines, skip any text before first one.
  3744. (when (and (eq granularity 'headline) (not (org-at-heading-p)))
  3745. (org-with-limited-levels (outline-next-heading)))
  3746. ;; Main loop start.
  3747. (while (< (point) end)
  3748. ;; Find current element's type and parse it accordingly to
  3749. ;; its category.
  3750. (let* ((element (org-element--current-element
  3751. end granularity special structure))
  3752. (type (org-element-type element))
  3753. (cbeg (org-element-property :contents-begin element)))
  3754. (goto-char (org-element-property :end element))
  3755. ;; Visible only: skip invisible parts between siblings.
  3756. (when (and visible-only (org-invisible-p2))
  3757. (goto-char (min (1+ (org-find-visible)) end)))
  3758. ;; Fill ELEMENT contents by side-effect.
  3759. (cond
  3760. ;; If element has no contents, don't modify it.
  3761. ((not cbeg))
  3762. ;; Greater element: parse it between `contents-begin' and
  3763. ;; `contents-end'. Make sure GRANULARITY allows the
  3764. ;; recursion, or ELEMENT is a headline, in which case going
  3765. ;; inside is mandatory, in order to get sub-level headings.
  3766. ((and (memq type org-element-greater-elements)
  3767. (or (memq granularity '(element object nil))
  3768. (and (eq granularity 'greater-element)
  3769. (eq type 'section))
  3770. (eq type 'headline)))
  3771. (org-element--parse-elements
  3772. cbeg (org-element-property :contents-end element)
  3773. ;; Possibly switch to a special mode.
  3774. (case type
  3775. (headline
  3776. (if (org-element-property :quotedp element) 'quote-section
  3777. 'section))
  3778. (plain-list 'item)
  3779. (property-drawer 'node-property)
  3780. (table 'table-row))
  3781. (and (memq type '(item plain-list))
  3782. (org-element-property :structure element))
  3783. granularity visible-only element))
  3784. ;; ELEMENT has contents. Parse objects inside, if
  3785. ;; GRANULARITY allows it.
  3786. ((memq granularity '(object nil))
  3787. (org-element--parse-objects
  3788. cbeg (org-element-property :contents-end element) element
  3789. (org-element-restriction type))))
  3790. (org-element-adopt-elements acc element)))
  3791. ;; Return result.
  3792. acc))
  3793. (defun org-element--parse-objects (beg end acc restriction)
  3794. "Parse objects between BEG and END and return recursive structure.
  3795. Objects are accumulated in ACC.
  3796. RESTRICTION is a list of object successors which are allowed in
  3797. the current object."
  3798. (let ((candidates 'initial))
  3799. (save-excursion
  3800. (save-restriction
  3801. (narrow-to-region beg end)
  3802. (goto-char (point-min))
  3803. (while (and (not (eobp))
  3804. (setq candidates
  3805. (org-element--get-next-object-candidates
  3806. restriction candidates)))
  3807. (let ((next-object
  3808. (let ((pos (apply 'min (mapcar 'cdr candidates))))
  3809. (save-excursion
  3810. (goto-char pos)
  3811. (funcall (intern (format "org-element-%s-parser"
  3812. (car (rassq pos candidates)))))))))
  3813. ;; 1. Text before any object. Untabify it.
  3814. (let ((obj-beg (org-element-property :begin next-object)))
  3815. (unless (= (point) obj-beg)
  3816. (setq acc
  3817. (org-element-adopt-elements
  3818. acc
  3819. (replace-regexp-in-string
  3820. "\t" (make-string tab-width ? )
  3821. (buffer-substring-no-properties (point) obj-beg))))))
  3822. ;; 2. Object...
  3823. (let ((obj-end (org-element-property :end next-object))
  3824. (cont-beg (org-element-property :contents-begin next-object)))
  3825. ;; Fill contents of NEXT-OBJECT by side-effect, if it has
  3826. ;; a recursive type.
  3827. (when (and cont-beg
  3828. (memq (car next-object) org-element-recursive-objects))
  3829. (org-element--parse-objects
  3830. cont-beg (org-element-property :contents-end next-object)
  3831. next-object (org-element-restriction next-object)))
  3832. (setq acc (org-element-adopt-elements acc next-object))
  3833. (goto-char obj-end))))
  3834. ;; 3. Text after last object. Untabify it.
  3835. (unless (eobp)
  3836. (setq acc
  3837. (org-element-adopt-elements
  3838. acc
  3839. (replace-regexp-in-string
  3840. "\t" (make-string tab-width ? )
  3841. (buffer-substring-no-properties (point) end)))))
  3842. ;; Result.
  3843. acc))))
  3844. (defun org-element--get-next-object-candidates (restriction objects)
  3845. "Return an alist of candidates for the next object.
  3846. RESTRICTION is a list of object types, as symbols. Only
  3847. candidates with such types are looked after.
  3848. OBJECTS is the previous candidates alist. If it is set to
  3849. `initial', no search has been done before, and all symbols in
  3850. RESTRICTION should be looked after.
  3851. Return value is an alist whose CAR is the object type and CDR its
  3852. beginning position."
  3853. (delq
  3854. nil
  3855. (if (eq objects 'initial)
  3856. ;; When searching for the first time, look for every successor
  3857. ;; allowed in RESTRICTION.
  3858. (mapcar
  3859. (lambda (res)
  3860. (funcall (intern (format "org-element-%s-successor" res))))
  3861. restriction)
  3862. ;; Focus on objects returned during last search. Keep those
  3863. ;; still after point. Search again objects before it.
  3864. (mapcar
  3865. (lambda (obj)
  3866. (if (>= (cdr obj) (point)) obj
  3867. (let* ((type (car obj))
  3868. (succ (or (cdr (assq type org-element-object-successor-alist))
  3869. type)))
  3870. (and succ
  3871. (funcall (intern (format "org-element-%s-successor" succ)))))))
  3872. objects))))
  3873. ;;; Towards A Bijective Process
  3874. ;;
  3875. ;; The parse tree obtained with `org-element-parse-buffer' is really
  3876. ;; a snapshot of the corresponding Org buffer. Therefore, it can be
  3877. ;; interpreted and expanded into a string with canonical Org syntax.
  3878. ;; Hence `org-element-interpret-data'.
  3879. ;;
  3880. ;; The function relies internally on
  3881. ;; `org-element--interpret-affiliated-keywords'.
  3882. ;;;###autoload
  3883. (defun org-element-interpret-data (data &optional pseudo-objects)
  3884. "Interpret DATA as Org syntax.
  3885. DATA is a parse tree, an element, an object or a secondary string
  3886. to interpret.
  3887. Optional argument PSEUDO-OBJECTS is a list of symbols defining
  3888. new types that should be treated as objects. An unknown type not
  3889. belonging to this list is seen as a pseudo-element instead. Both
  3890. pseudo-objects and pseudo-elements are transparent entities, i.e.
  3891. only their contents are interpreted.
  3892. Return Org syntax as a string."
  3893. (org-element--interpret-data-1 data nil pseudo-objects))
  3894. (defun org-element--interpret-data-1 (data parent pseudo-objects)
  3895. "Interpret DATA as Org syntax.
  3896. DATA is a parse tree, an element, an object or a secondary string
  3897. to interpret. PARENT is used for recursive calls. It contains
  3898. the element or object containing data, or nil. PSEUDO-OBJECTS
  3899. are list of symbols defining new element or object types.
  3900. Unknown types that don't belong to this list are treated as
  3901. pseudo-elements instead.
  3902. Return Org syntax as a string."
  3903. (let* ((type (org-element-type data))
  3904. ;; Find interpreter for current object or element. If it
  3905. ;; doesn't exist (e.g. this is a pseudo object or element),
  3906. ;; return contents, if any.
  3907. (interpret
  3908. (let ((fun (intern (format "org-element-%s-interpreter" type))))
  3909. (if (fboundp fun) fun (lambda (data contents) contents))))
  3910. (results
  3911. (cond
  3912. ;; Secondary string.
  3913. ((not type)
  3914. (mapconcat
  3915. (lambda (obj)
  3916. (org-element--interpret-data-1 obj parent pseudo-objects))
  3917. data ""))
  3918. ;; Full Org document.
  3919. ((eq type 'org-data)
  3920. (mapconcat
  3921. (lambda (obj)
  3922. (org-element--interpret-data-1 obj parent pseudo-objects))
  3923. (org-element-contents data) ""))
  3924. ;; Plain text: remove `:parent' text property from output.
  3925. ((stringp data) (org-no-properties data))
  3926. ;; Element or object without contents.
  3927. ((not (org-element-contents data)) (funcall interpret data nil))
  3928. ;; Element or object with contents.
  3929. (t
  3930. (funcall interpret data
  3931. ;; Recursively interpret contents.
  3932. (mapconcat
  3933. (lambda (obj)
  3934. (org-element--interpret-data-1 obj data pseudo-objects))
  3935. (org-element-contents
  3936. (if (not (memq type '(paragraph verse-block)))
  3937. data
  3938. ;; Fix indentation of elements containing
  3939. ;; objects. We ignore `table-row' elements
  3940. ;; as they are one line long anyway.
  3941. (org-element-normalize-contents
  3942. data
  3943. ;; When normalizing first paragraph of an
  3944. ;; item or a footnote-definition, ignore
  3945. ;; first line's indentation.
  3946. (and (eq type 'paragraph)
  3947. (equal data (car (org-element-contents parent)))
  3948. (memq (org-element-type parent)
  3949. '(footnote-definition item))))))
  3950. ""))))))
  3951. (if (memq type '(org-data plain-text nil)) results
  3952. ;; Build white spaces. If no `:post-blank' property is
  3953. ;; specified, assume its value is 0.
  3954. (let ((post-blank (or (org-element-property :post-blank data) 0)))
  3955. (if (or (memq type org-element-all-objects)
  3956. (memq type pseudo-objects))
  3957. (concat results (make-string post-blank ?\s))
  3958. (concat
  3959. (org-element--interpret-affiliated-keywords data)
  3960. (org-element-normalize-string results)
  3961. (make-string post-blank ?\n)))))))
  3962. (defun org-element--interpret-affiliated-keywords (element)
  3963. "Return ELEMENT's affiliated keywords as Org syntax.
  3964. If there is no affiliated keyword, return the empty string."
  3965. (let ((keyword-to-org
  3966. (function
  3967. (lambda (key value)
  3968. (let (dual)
  3969. (when (member key org-element-dual-keywords)
  3970. (setq dual (cdr value) value (car value)))
  3971. (concat "#+" key
  3972. (and dual
  3973. (format "[%s]" (org-element-interpret-data dual)))
  3974. ": "
  3975. (if (member key org-element-parsed-keywords)
  3976. (org-element-interpret-data value)
  3977. value)
  3978. "\n"))))))
  3979. (mapconcat
  3980. (lambda (prop)
  3981. (let ((value (org-element-property prop element))
  3982. (keyword (upcase (substring (symbol-name prop) 1))))
  3983. (when value
  3984. (if (or (member keyword org-element-multiple-keywords)
  3985. ;; All attribute keywords can have multiple lines.
  3986. (string-match "^ATTR_" keyword))
  3987. (mapconcat (lambda (line) (funcall keyword-to-org keyword line))
  3988. (reverse value)
  3989. "")
  3990. (funcall keyword-to-org keyword value)))))
  3991. ;; List all ELEMENT's properties matching an attribute line or an
  3992. ;; affiliated keyword, but ignore translated keywords since they
  3993. ;; cannot belong to the property list.
  3994. (loop for prop in (nth 1 element) by 'cddr
  3995. when (let ((keyword (upcase (substring (symbol-name prop) 1))))
  3996. (or (string-match "^ATTR_" keyword)
  3997. (and
  3998. (member keyword org-element-affiliated-keywords)
  3999. (not (assoc keyword
  4000. org-element-keyword-translation-alist)))))
  4001. collect prop)
  4002. "")))
  4003. ;; Because interpretation of the parse tree must return the same
  4004. ;; number of blank lines between elements and the same number of white
  4005. ;; space after objects, some special care must be given to white
  4006. ;; spaces.
  4007. ;;
  4008. ;; The first function, `org-element-normalize-string', ensures any
  4009. ;; string different from the empty string will end with a single
  4010. ;; newline character.
  4011. ;;
  4012. ;; The second function, `org-element-normalize-contents', removes
  4013. ;; global indentation from the contents of the current element.
  4014. (defun org-element-normalize-string (s)
  4015. "Ensure string S ends with a single newline character.
  4016. If S isn't a string return it unchanged. If S is the empty
  4017. string, return it. Otherwise, return a new string with a single
  4018. newline character at its end."
  4019. (cond
  4020. ((not (stringp s)) s)
  4021. ((string= "" s) "")
  4022. (t (and (string-match "\\(\n[ \t]*\\)*\\'" s)
  4023. (replace-match "\n" nil nil s)))))
  4024. (defun org-element-normalize-contents (element &optional ignore-first)
  4025. "Normalize plain text in ELEMENT's contents.
  4026. ELEMENT must only contain plain text and objects.
  4027. If optional argument IGNORE-FIRST is non-nil, ignore first line's
  4028. indentation to compute maximal common indentation.
  4029. Return the normalized element that is element with global
  4030. indentation removed from its contents. The function assumes that
  4031. indentation is not done with TAB characters."
  4032. (let* (ind-list ; for byte-compiler
  4033. collect-inds ; for byte-compiler
  4034. (collect-inds
  4035. (function
  4036. ;; Return list of indentations within BLOB. This is done by
  4037. ;; walking recursively BLOB and updating IND-LIST along the
  4038. ;; way. FIRST-FLAG is non-nil when the first string hasn't
  4039. ;; been seen yet. It is required as this string is the only
  4040. ;; one whose indentation doesn't happen after a newline
  4041. ;; character.
  4042. (lambda (blob first-flag)
  4043. (mapc
  4044. (lambda (object)
  4045. (when (and first-flag (stringp object))
  4046. (setq first-flag nil)
  4047. (string-match "\\`\\( *\\)" object)
  4048. (let ((len (length (match-string 1 object))))
  4049. ;; An indentation of zero means no string will be
  4050. ;; modified. Quit the process.
  4051. (if (zerop len) (throw 'zero (setq ind-list nil))
  4052. (push len ind-list))))
  4053. (cond
  4054. ((stringp object)
  4055. (let ((start 0))
  4056. ;; Avoid matching blank or empty lines.
  4057. (while (and (string-match "\n\\( *\\)\\(.\\)" object start)
  4058. (not (equal (match-string 2 object) " ")))
  4059. (setq start (match-end 0))
  4060. (push (length (match-string 1 object)) ind-list))))
  4061. ((memq (org-element-type object) org-element-recursive-objects)
  4062. (funcall collect-inds object first-flag))))
  4063. (org-element-contents blob))))))
  4064. ;; Collect indentation list in ELEMENT. Possibly remove first
  4065. ;; value if IGNORE-FIRST is non-nil.
  4066. (catch 'zero (funcall collect-inds element (not ignore-first)))
  4067. (if (not ind-list) element
  4068. ;; Build ELEMENT back, replacing each string with the same
  4069. ;; string minus common indentation.
  4070. (let* (build ; For byte compiler.
  4071. (build
  4072. (function
  4073. (lambda (blob mci first-flag)
  4074. ;; Return BLOB with all its strings indentation
  4075. ;; shortened from MCI white spaces. FIRST-FLAG is
  4076. ;; non-nil when the first string hasn't been seen
  4077. ;; yet.
  4078. (setcdr (cdr blob)
  4079. (mapcar
  4080. (lambda (object)
  4081. (when (and first-flag (stringp object))
  4082. (setq first-flag nil)
  4083. (setq object
  4084. (replace-regexp-in-string
  4085. (format "\\` \\{%d\\}" mci) "" object)))
  4086. (cond
  4087. ((stringp object)
  4088. (replace-regexp-in-string
  4089. (format "\n \\{%d\\}" mci) "\n" object))
  4090. ((memq (org-element-type object)
  4091. org-element-recursive-objects)
  4092. (funcall build object mci first-flag))
  4093. (t object)))
  4094. (org-element-contents blob)))
  4095. blob))))
  4096. (funcall build element (apply 'min ind-list) (not ignore-first))))))
  4097. ;;; The Toolbox
  4098. ;;
  4099. ;; The first move is to implement a way to obtain the smallest element
  4100. ;; containing point. This is the job of `org-element-at-point'. It
  4101. ;; basically jumps back to the beginning of section containing point
  4102. ;; and proceed, one element after the other, with
  4103. ;; `org-element--current-element' until the container is found. Note:
  4104. ;; When using `org-element-at-point', secondary values are never
  4105. ;; parsed since the function focuses on elements, not on objects.
  4106. ;;
  4107. ;; At a deeper level, `org-element-context' lists all elements and
  4108. ;; objects containing point.
  4109. ;;
  4110. ;; Both functions benefit from a simple caching mechanism. It is
  4111. ;; enabled by default, but can be disabled globally with
  4112. ;; `org-element-use-cache'. Also `org-element-cache-reset' clears or
  4113. ;; initializes cache for current buffer. Values are retrieved and put
  4114. ;; into cache with respectively, `org-element-cache-get' and
  4115. ;; `org-element-cache-put'. `org-element--cache-sync-idle-time' and
  4116. ;; `org-element--cache-merge-changes-threshold' are used internally to
  4117. ;; control caching behaviour.
  4118. ;;
  4119. ;; Eventually `org-element-nested-p' and `org-element-swap-A-B' may be
  4120. ;; used internally by navigation and manipulation tools.
  4121. (defvar org-element-use-cache t
  4122. "Non nil when Org parser should cache its results.")
  4123. (defvar org-element--cache nil
  4124. "Hash table used as a cache for parser.
  4125. Key is a buffer position and value is a cons cell with the
  4126. pattern:
  4127. \(ELEMENT . OBJECTS-DATA)
  4128. where ELEMENT is the element starting at the key and OBJECTS-DATA
  4129. is an alist where each association is:
  4130. \(POS CANDIDATES . OBJECTS)
  4131. where POS is a buffer position, CANDIDATES is the last know list
  4132. of successors (see `org-element--get-next-object-candidates') in
  4133. container starting at POS and OBJECTS is a list of objects known
  4134. to live within that container, from farthest to closest.
  4135. In the following example, \\alpha, bold object and \\beta start
  4136. at, respectively, positions 1, 7 and 8,
  4137. \\alpha *\\beta*
  4138. If the paragraph is completely parsed, OBJECTS-DATA will be
  4139. \((1 nil BOLD-OBJECT ENTITY-OBJECT)
  4140. \(8 nil ENTITY-OBJECT))
  4141. whereas in a partially parsed paragraph, it could be
  4142. \((1 ((entity . 1) (bold . 7)) ENTITY-OBJECT))
  4143. This cache is used in both `org-element-at-point' and
  4144. `org-element-context'. The former uses ELEMENT only and the
  4145. latter OBJECTS-DATA only.")
  4146. (defvar org-element--cache-sync-idle-time 0.5
  4147. "Number of seconds of idle time wait before syncing buffer cache.
  4148. Syncing also happens when current modification is too distant
  4149. from the stored one (for more information, see
  4150. `org-element--cache-merge-changes-threshold').")
  4151. (defvar org-element--cache-merge-changes-threshold 200
  4152. "Number of characters triggering cache syncing.
  4153. The cache mechanism only stores one buffer modification at any
  4154. given time. When another change happens, it replaces it with
  4155. a change containing both the stored modification and the current
  4156. one. This is a trade-off, as merging them prevents another
  4157. syncing, but every element between them is then lost.
  4158. This variable determines the maximum size, in characters, we
  4159. accept to lose in order to avoid syncing the cache.")
  4160. (defvar org-element--cache-status nil
  4161. "Contains data about cache validity for current buffer.
  4162. Value is a vector of seven elements,
  4163. [ACTIVEP BEGIN END OFFSET TIMER PREVIOUS-STATE]
  4164. ACTIVEP is a boolean non-nil when changes described in the other
  4165. slots are valid for current buffer.
  4166. BEGIN and END are the beginning and ending position of the area
  4167. for which cache cannot be trusted.
  4168. OFFSET it an integer specifying the number to add to position of
  4169. elements after that area.
  4170. TIMER is a timer used to apply these changes to cache when Emacs
  4171. is idle.
  4172. PREVIOUS-STATE is a symbol referring to the state of the buffer
  4173. before a change happens. It is used to know if sensitive
  4174. areas (block boundaries, headlines) were modified. It can be set
  4175. to nil, `headline' or `other'.")
  4176. ;;;###autoload
  4177. (defun org-element-cache-reset (&optional all)
  4178. "Reset cache in current buffer.
  4179. When optional argument ALL is non-nil, reset cache in all Org
  4180. buffers. This function will do nothing if
  4181. `org-element-use-cache' is nil."
  4182. (interactive "P")
  4183. (when org-element-use-cache
  4184. (dolist (buffer (if all (buffer-list) (list (current-buffer))))
  4185. (with-current-buffer buffer
  4186. (when (derived-mode-p 'org-mode)
  4187. (if (org-bound-and-true-p org-element--cache)
  4188. (clrhash org-element--cache)
  4189. (org-set-local 'org-element--cache
  4190. (make-hash-table :size 5003 :test 'eq)))
  4191. (org-set-local 'org-element--cache-status (make-vector 6 nil))
  4192. (add-hook 'before-change-functions
  4193. 'org-element--cache-before-change nil t)
  4194. (add-hook 'after-change-functions
  4195. 'org-element--cache-record-change nil t))))))
  4196. (defsubst org-element--cache-pending-changes-p ()
  4197. "Non-nil when changes are not integrated in cache yet."
  4198. (and org-element--cache-status
  4199. (aref org-element--cache-status 0)))
  4200. (defsubst org-element--cache-push-change (beg end offset)
  4201. "Push change to current buffer staging area.
  4202. BEG and END and the beginning and ending position of the
  4203. modification area. OFFSET is the size of the change, as an
  4204. integer."
  4205. (aset org-element--cache-status 1 beg)
  4206. (aset org-element--cache-status 2 end)
  4207. (aset org-element--cache-status 3 offset)
  4208. (let ((timer (aref org-element--cache-status 4)))
  4209. (if timer (timer-activate-when-idle timer t)
  4210. (aset org-element--cache-status 4
  4211. (run-with-idle-timer org-element--cache-sync-idle-time
  4212. nil
  4213. #'org-element--cache-sync
  4214. (current-buffer)))))
  4215. (aset org-element--cache-status 0 t))
  4216. (defsubst org-element--cache-cancel-changes ()
  4217. "Remove any cache change set for current buffer."
  4218. (let ((timer (aref org-element--cache-status 4)))
  4219. (and timer (cancel-timer timer)))
  4220. (aset org-element--cache-status 0 nil))
  4221. (defsubst org-element--cache-get-key (element)
  4222. "Return expected key for ELEMENT in cache."
  4223. (let ((begin (org-element-property :begin element)))
  4224. (if (and (memq (org-element-type element) '(item table-row))
  4225. (= (org-element-property :contents-begin
  4226. (org-element-property :parent element))
  4227. begin))
  4228. ;; Special key for first item (resp. table-row) in a plain
  4229. ;; list (resp. table).
  4230. (1+ begin)
  4231. begin)))
  4232. (defsubst org-element-cache-get (pos &optional type)
  4233. "Return data stored at key POS in current buffer cache.
  4234. When optional argument TYPE is `element', retrieve the element
  4235. starting at POS. When it is `objects', return the list of object
  4236. types along with their beginning position within that element.
  4237. Otherwise, return the full data. In any case, return nil if no
  4238. data is found, or if caching is not allowed."
  4239. (when (and org-element-use-cache org-element--cache)
  4240. ;; If there are pending changes, first sync them.
  4241. (when (org-element--cache-pending-changes-p)
  4242. (org-element--cache-sync (current-buffer)))
  4243. (let ((data (gethash pos org-element--cache)))
  4244. (case type
  4245. (element (car data))
  4246. (objects (cdr data))
  4247. (otherwise data)))))
  4248. (defsubst org-element-cache-put (pos data)
  4249. "Store data in current buffer's cache, if allowed.
  4250. POS is a buffer position, which will be used as a key. DATA is
  4251. the value to store. Nothing will be stored if
  4252. `org-element-use-cache' is nil. Return DATA in any case."
  4253. (if (not org-element-use-cache) data
  4254. (unless org-element--cache (org-element-cache-reset))
  4255. (puthash pos data org-element--cache)))
  4256. (defsubst org-element--cache-shift-positions (element offset)
  4257. "Shift ELEMENT properties relative to buffer positions by OFFSET.
  4258. Properties containing buffer positions are `:begin', `:end',
  4259. `:contents-begin', `:contents-end' and `:structure'. They are
  4260. modified by side-effect. Return modified element."
  4261. (let ((properties (nth 1 element)))
  4262. ;; Shift :structure property for the first plain list only: it is
  4263. ;; the only one that really matters and it prevents from shifting
  4264. ;; it more than once.
  4265. (when (and (eq (org-element-type element) 'plain-list)
  4266. (not (eq (org-element-type (plist-get properties :parent))
  4267. 'item)))
  4268. (dolist (item (plist-get properties :structure))
  4269. (incf (car item) offset)
  4270. (incf (nth 6 item) offset)))
  4271. (plist-put properties :begin (+ (plist-get properties :begin) offset))
  4272. (plist-put properties :end (+ (plist-get properties :end) offset))
  4273. (dolist (key '(:contents-begin :contents-end :post-affiliated))
  4274. (let ((value (plist-get properties key)))
  4275. (and value (plist-put properties key (+ offset value))))))
  4276. element)
  4277. (defconst org-element--cache-opening-line
  4278. (concat "^[ \t]*\\(?:"
  4279. "#\\+BEGIN[:_]" "\\|"
  4280. "\\\\begin{[A-Za-z0-9]+\\*?}" "\\|"
  4281. ":\\S-+:[ \t]*$"
  4282. "\\)")
  4283. "Regexp matching an element opening line.
  4284. When such a line is modified, modifications may propagate after
  4285. modified area. In that situation, every element between that
  4286. area and next section is removed from cache.")
  4287. (defconst org-element--cache-closing-line
  4288. (concat "^[ \t]*\\(?:"
  4289. "#\\+END\\(?:_\\|:?[ \t]*$\\)" "\\|"
  4290. "\\\\end{[A-Za-z0-9]+\\*?}[ \t]*$" "\\|"
  4291. ":END:[ \t]*$"
  4292. "\\)")
  4293. "Regexp matching an element closing line.
  4294. When such a line is modified, modifications may propagate before
  4295. modified area. In that situation, every element between that
  4296. area and previous section is removed from cache.")
  4297. (defun org-element--cache-before-change (beg end)
  4298. "Request extension of area going to be modified if needed.
  4299. BEG and END are the beginning and end of the range of changed
  4300. text. See `before-change-functions' for more information."
  4301. (let ((inhibit-quit t))
  4302. (org-with-wide-buffer
  4303. (goto-char beg)
  4304. (beginning-of-line)
  4305. (let ((top (point))
  4306. (bottom (save-excursion (goto-char end) (line-end-position)))
  4307. (sensitive-re
  4308. ;; A sensitive line is a headline or a block (or drawer,
  4309. ;; or latex-environment) boundary. Inserting one can
  4310. ;; modify buffer drastically both above and below that
  4311. ;; line, possibly making cache invalid. Therefore, we
  4312. ;; need to pay special attention to changes happening to
  4313. ;; them.
  4314. (concat
  4315. "\\(" (org-with-limited-levels org-outline-regexp-bol) "\\)" "\\|"
  4316. org-element--cache-closing-line "\\|"
  4317. org-element--cache-opening-line)))
  4318. (save-match-data
  4319. (aset org-element--cache-status 5
  4320. (cond ((not (re-search-forward sensitive-re bottom t)) nil)
  4321. ((and (match-beginning 1)
  4322. (progn (goto-char bottom)
  4323. (or (not (re-search-backward sensitive-re
  4324. (match-end 1) t))
  4325. (match-beginning 1))))
  4326. 'headline)
  4327. (t 'other))))))))
  4328. (defun org-element--cache-record-change (beg end pre)
  4329. "Update buffer modifications for current buffer.
  4330. BEG and END are the beginning and end of the range of changed
  4331. text, and the length in bytes of the pre-change text replaced by
  4332. that range. See `after-change-functions' for more information.
  4333. If there are already pending changes, try to merge them into
  4334. a bigger change record. If that's not possible, the function
  4335. will first synchronize cache with previous change and store the
  4336. new one."
  4337. (let ((inhibit-quit t))
  4338. (when (and org-element-use-cache org-element--cache)
  4339. (org-with-wide-buffer
  4340. (goto-char beg)
  4341. (beginning-of-line)
  4342. (let ((top (point))
  4343. (bottom (save-excursion (goto-char end) (line-end-position))))
  4344. (org-with-limited-levels
  4345. (save-match-data
  4346. ;; Determine if modified area needs to be extended,
  4347. ;; according to both previous and current state. We make
  4348. ;; a special case for headline editing: if a headline is
  4349. ;; modified but not removed, do not extend.
  4350. (when (let ((previous-state (aref org-element--cache-status 5))
  4351. (sensitive-re
  4352. (concat "\\(" org-outline-regexp-bol "\\)" "\\|"
  4353. org-element--cache-closing-line "\\|"
  4354. org-element--cache-opening-line)))
  4355. (cond ((eq previous-state 'other))
  4356. ((not (re-search-forward sensitive-re bottom t))
  4357. (eq previous-state 'headline))
  4358. ((match-beginning 1)
  4359. (or (not (eq previous-state 'headline))
  4360. (and (progn (goto-char bottom)
  4361. (re-search-backward
  4362. sensitive-re (match-end 1) t))
  4363. (not (match-beginning 1)))))
  4364. (t)))
  4365. ;; Effectively extend modified area.
  4366. (setq top (progn (goto-char top)
  4367. (outline-previous-heading)
  4368. ;; Headline above is inclusive.
  4369. (point)))
  4370. (setq bottom (progn (goto-char bottom)
  4371. (outline-next-heading)
  4372. ;; Headline below is exclusive.
  4373. (if (eobp) (point) (1- (point))))))))
  4374. ;; Store changes.
  4375. (let ((offset (- end beg pre)))
  4376. (if (not (org-element--cache-pending-changes-p))
  4377. ;; No pending changes. Store the new ones.
  4378. (org-element--cache-push-change top (- bottom offset) offset)
  4379. (let* ((current-start (aref org-element--cache-status 1))
  4380. (current-end (+ (aref org-element--cache-status 2)
  4381. (aref org-element--cache-status 3)))
  4382. (gap (max (- beg current-end) (- current-start end))))
  4383. (if (> gap org-element--cache-merge-changes-threshold)
  4384. ;; If we cannot merge two change sets (i.e. they
  4385. ;; modify distinct buffer parts) first apply current
  4386. ;; change set and store new one. This way, there is
  4387. ;; never more than one pending change set, which
  4388. ;; avoids handling costly merges.
  4389. (progn (org-element--cache-sync (current-buffer))
  4390. (org-element--cache-push-change
  4391. top (- bottom offset) offset))
  4392. ;; Change sets can be merged. We can expand the area
  4393. ;; that requires an update, and postpone the sync.
  4394. (timer-activate-when-idle (aref org-element--cache-status 4) t)
  4395. (aset org-element--cache-status 0 t)
  4396. (aset org-element--cache-status 1 (min top current-start))
  4397. (aset org-element--cache-status 2
  4398. (- (max current-end bottom) offset))
  4399. (incf (aref org-element--cache-status 3) offset))))))))))
  4400. (defun org-element--cache-sync (buffer)
  4401. "Synchronize cache with recent modification in BUFFER.
  4402. Elements ending before modification area are kept in cache.
  4403. Elements starting after modification area have their position
  4404. shifted by the size of the modification. Every other element is
  4405. removed from the cache."
  4406. (when (buffer-live-p buffer)
  4407. (with-current-buffer buffer
  4408. (when (org-element--cache-pending-changes-p)
  4409. (let ((inhibit-quit t)
  4410. (beg (aref org-element--cache-status 1))
  4411. (end (aref org-element--cache-status 2))
  4412. (offset (aref org-element--cache-status 3))
  4413. new-keys)
  4414. (maphash
  4415. #'(lambda (key value)
  4416. (cond
  4417. ((memq key new-keys))
  4418. ((> key end)
  4419. ;; Shift every element starting after END by OFFSET.
  4420. ;; We also need to shift keys, since they refer to
  4421. ;; buffer positions.
  4422. ;;
  4423. ;; Upon shifting a key a conflict can occur if the
  4424. ;; shifted key also refers to some element in the
  4425. ;; cache. In this case, we temporarily associate
  4426. ;; both elements, as a cons cell, to the shifted key,
  4427. ;; following the pattern (SHIFTED . CURRENT).
  4428. ;;
  4429. ;; Such a conflict can only occur if shifted key hash
  4430. ;; hasn't been processed by `maphash' yet.
  4431. (unless (zerop offset)
  4432. (let* ((conflictp (consp (caar value)))
  4433. (value-to-shift (if conflictp (cdr value) value)))
  4434. ;; Shift element part.
  4435. (org-element--cache-shift-positions (car value-to-shift) offset)
  4436. ;; Shift objects part.
  4437. (dolist (object-data (cdr value-to-shift))
  4438. (incf (car object-data) offset)
  4439. (dolist (successor (nth 1 object-data))
  4440. (incf (cdr successor) offset))
  4441. (dolist (object (cddr object-data))
  4442. (org-element--cache-shift-positions object offset)))
  4443. ;; Shift key-value pair.
  4444. (let* ((new-key (+ key offset))
  4445. (new-value (gethash new-key org-element--cache)))
  4446. ;; Put new value to shifted key.
  4447. ;;
  4448. ;; If one already exists, do not overwrite it:
  4449. ;; store it as the car of a cons cell instead,
  4450. ;; and handle it when `maphash' reaches
  4451. ;; NEW-KEY.
  4452. ;;
  4453. ;; If there is no element stored at NEW-KEY or
  4454. ;; if NEW-KEY is going to be removed anyway
  4455. ;; (i.e., it is before END), just store new
  4456. ;; value there and make sure it will not be
  4457. ;; processed again by storing NEW-KEY in
  4458. ;; NEW-KEYS.
  4459. (puthash new-key
  4460. (if (and new-value (> new-key end))
  4461. (cons value-to-shift new-value)
  4462. (push new-key new-keys)
  4463. value-to-shift)
  4464. org-element--cache)
  4465. ;; If current value contains two elements, car
  4466. ;; should be the new value, since cdr has been
  4467. ;; shifted already.
  4468. (if conflictp
  4469. (puthash key (car value) org-element--cache)
  4470. (remhash key org-element--cache))))))
  4471. ;; Remove every element between BEG and END, since
  4472. ;; this is where changes happened.
  4473. ((>= key beg) (remhash key org-element--cache))
  4474. ;; Preserve any element ending before BEG. If it
  4475. ;; overlaps the BEG-END area, remove it.
  4476. (t
  4477. (let ((element (car value)))
  4478. (if (>= (org-element-property :end element) beg)
  4479. (remhash key org-element--cache)
  4480. ;; Special case: footnote definitions and plain
  4481. ;; lists can end with blank lines. Modifying
  4482. ;; those can also alter last element inside. We
  4483. ;; must therefore remove them from cache.
  4484. (let ((parent (org-element-property :parent element)))
  4485. (when (and parent (eq (org-element-type parent) 'item))
  4486. (setq parent (org-element-property :parent parent)))
  4487. (when (and (memq (org-element-type parent)
  4488. '(footnote-definition plain-list))
  4489. (>= (org-element-property :end parent) beg)
  4490. (= (org-element-property :contents-end parent)
  4491. (org-element-property :end element)))
  4492. (remhash key org-element--cache))))))))
  4493. org-element--cache)
  4494. ;; Signal cache as up-to-date.
  4495. (org-element--cache-cancel-changes))))))
  4496. ;;;###autoload
  4497. (defun org-element-at-point ()
  4498. "Determine closest element around point.
  4499. Return value is a list like (TYPE PROPS) where TYPE is the type
  4500. of the element and PROPS a plist of properties associated to the
  4501. element.
  4502. Possible types are defined in `org-element-all-elements'.
  4503. Properties depend on element or object type, but always include
  4504. `:begin', `:end', `:parent' and `:post-blank' properties.
  4505. As a special case, if point is at the very beginning of a list or
  4506. sub-list, returned element will be that list instead of the first
  4507. item. In the same way, if point is at the beginning of the first
  4508. row of a table, returned element will be the table instead of the
  4509. first row."
  4510. (org-with-wide-buffer
  4511. (let ((origin (point)) element parent end)
  4512. (end-of-line)
  4513. (skip-chars-backward " \r\t\n")
  4514. (cond
  4515. ((bobp) nil)
  4516. ((org-with-limited-levels (org-at-heading-p))
  4517. (beginning-of-line)
  4518. (or (org-element-cache-get (point) 'element)
  4519. (car (org-element-cache-put
  4520. (point)
  4521. (list (org-element-headline-parser (point-max) t))))))
  4522. (t
  4523. (catch 'loop
  4524. (when org-element-use-cache
  4525. ;; Opportunistic shortcut. Instead of going back to
  4526. ;; headline above (or beginning of buffer) and descending
  4527. ;; again, first try to find a known element above current
  4528. ;; position. Give up after 3 tries or when we hit
  4529. ;; a headline (or beginning of buffer).
  4530. (beginning-of-line)
  4531. (skip-chars-backward " \r\t\n")
  4532. (dotimes (i 3)
  4533. (unless (re-search-backward org-element-paragraph-separate nil t)
  4534. (throw 'loop (goto-char (point-min))))
  4535. (cond ((not (org-string-match-p "\\S-" (match-string 0)))
  4536. (when (bobp) (throw 'loop nil))
  4537. ;; An element cannot start at a headline, so check
  4538. ;; first non-blank line below.
  4539. (skip-chars-forward " \r\t\n" origin)
  4540. (beginning-of-line))
  4541. ((org-looking-at-p org-element--affiliated-re)
  4542. ;; At an affiliated keyword, make sure to move to
  4543. ;; the first one.
  4544. (if (re-search-backward "^[ \t]*[^#]" nil t)
  4545. (forward-line)
  4546. (throw 'loop (goto-char (point-min)))))
  4547. ((org-looking-at-p "^[ \t]*:\\(?: \\|$\\)")
  4548. ;; At a fixed width area or a property drawer, reach
  4549. ;; the beginning of the element.
  4550. (if (re-search-backward "^[ \t]*[^:]" nil t)
  4551. (forward-line)
  4552. (throw 'loop (goto-char (point-min))))))
  4553. (when (org-with-limited-levels (org-at-heading-p))
  4554. ;; Tough luck: we're back at a headline above. Move to
  4555. ;; beginning of section.
  4556. (forward-line)
  4557. (skip-chars-forward " \r\t\n")
  4558. (beginning-of-line)
  4559. (throw 'loop nil))
  4560. (let ((cached (org-element-cache-get (point) 'element)))
  4561. ;; Search successful: we know an element before point
  4562. ;; which is not an headline. If it has a common
  4563. ;; ancestor with ORIGIN, set this ancestor as the
  4564. ;; current parent and the element as the one to check.
  4565. ;; Otherwise, move at top level and start parsing right
  4566. ;; after its broader ancestor.
  4567. (when cached
  4568. (let ((cache-end (org-element-property :end cached)))
  4569. (if (or (> cache-end origin)
  4570. (and (= cache-end origin) (= (point-max) origin)))
  4571. (setq element cached
  4572. parent (org-element-property :parent cached)
  4573. end cache-end)
  4574. (goto-char cache-end)
  4575. (let ((up cached))
  4576. (while (and (setq up (org-element-property :parent up))
  4577. (<= (org-element-property :end up) origin))
  4578. (goto-char (org-element-property :end up)))
  4579. (when up
  4580. (setq element up
  4581. parent (org-element-property :parent up)
  4582. end (org-element-property :end up))))))
  4583. (throw 'loop nil)))))
  4584. ;; Opportunistic search failed. Move back to beginning of
  4585. ;; section in current headline, if any, or to first non-empty
  4586. ;; line in buffer otherwise.
  4587. (org-with-limited-levels (outline-previous-heading))
  4588. (unless (bobp) (forward-line))
  4589. (skip-chars-forward " \r\t\n")
  4590. (beginning-of-line))
  4591. ;; Now we are at the beginning of an element, start parsing.
  4592. (unless end
  4593. (save-excursion (org-with-limited-levels (outline-next-heading))
  4594. (setq end (point))))
  4595. (let (type special-flag struct)
  4596. ;; Parse successively each element, skipping those ending
  4597. ;; before original position.
  4598. (catch 'exit
  4599. (while t
  4600. (unless element
  4601. (setq element
  4602. (let* ((pos (if (and (memq special-flag '(item table-row))
  4603. (memq type '(plain-list table)))
  4604. ;; First item (resp. row) in
  4605. ;; plain list (resp. table) gets
  4606. ;; a special key in cache.
  4607. (1+ (point))
  4608. (point)))
  4609. (cached (org-element-cache-get pos 'element)))
  4610. (cond
  4611. ((not cached)
  4612. (let ((element (org-element--current-element
  4613. end 'element special-flag struct)))
  4614. (when (derived-mode-p 'org-mode)
  4615. (org-element-cache-put pos (cons element nil)))
  4616. (org-element-put-property element :parent parent)))
  4617. ;; When changes happened in the middle of
  4618. ;; a list, its structure ends up being
  4619. ;; invalid. Therefore, we make sure to use
  4620. ;; a valid one.
  4621. ((and struct (memq (org-element-type cached)
  4622. '(item plain-list)))
  4623. (org-element-put-property cached :structure struct))
  4624. (t cached)))))
  4625. (setq type (org-element-type element))
  4626. (cond
  4627. ;; 1. Skip any element ending before point. Also skip
  4628. ;; element ending at point when we're sure that
  4629. ;; another element has started.
  4630. ((let ((elem-end (org-element-property :end element)))
  4631. (when (or (< elem-end origin)
  4632. (and (= elem-end origin) (/= elem-end end)))
  4633. (goto-char elem-end)))
  4634. (setq element nil))
  4635. ;; 2. An element containing point is always the element at
  4636. ;; point.
  4637. ((not (memq type org-element-greater-elements))
  4638. (throw 'exit element))
  4639. ;; 3. At any other greater element type, if point is
  4640. ;; within contents, move into it.
  4641. (t
  4642. (let ((cbeg (org-element-property :contents-begin element))
  4643. (cend (org-element-property :contents-end element)))
  4644. (if (or (not cbeg) (not cend) (> cbeg origin) (< cend origin)
  4645. ;; Create an anchor for tables and plain
  4646. ;; lists: when point is at the very beginning
  4647. ;; of these elements, ignoring affiliated
  4648. ;; keywords, target them instead of their
  4649. ;; contents.
  4650. (and (= cbeg origin) (memq type '(plain-list table)))
  4651. ;; When point is at contents end, do not move
  4652. ;; into elements with an explicit ending, but
  4653. ;; return that element instead.
  4654. (and (= cend origin)
  4655. (or (memq type
  4656. '(center-block
  4657. drawer dynamic-block inlinetask
  4658. property-drawer quote-block
  4659. special-block))
  4660. ;; Corner case: if a list ends at
  4661. ;; the end of a buffer without
  4662. ;; a final new line, return last
  4663. ;; element in last item instead.
  4664. (and (memq type '(item plain-list))
  4665. (progn (goto-char cend)
  4666. (or (bolp) (not (eobp))))))))
  4667. (throw 'exit element)
  4668. (case type
  4669. (plain-list
  4670. (setq special-flag 'item
  4671. struct (org-element-property :structure element)))
  4672. (item (setq special-flag nil))
  4673. (property-drawer
  4674. (setq special-flag 'node-property struct nil))
  4675. (table (setq special-flag 'table-row struct nil))
  4676. (otherwise (setq special-flag nil struct nil)))
  4677. (setq parent element element nil end cend)
  4678. (goto-char cbeg)))))))))))))
  4679. ;;;###autoload
  4680. (defun org-element-context (&optional element)
  4681. "Return closest element or object around point.
  4682. Return value is a list like (TYPE PROPS) where TYPE is the type
  4683. of the element or object and PROPS a plist of properties
  4684. associated to it.
  4685. Possible types are defined in `org-element-all-elements' and
  4686. `org-element-all-objects'. Properties depend on element or
  4687. object type, but always include `:begin', `:end', `:parent' and
  4688. `:post-blank'.
  4689. Optional argument ELEMENT, when non-nil, is the closest element
  4690. containing point, as returned by `org-element-at-point'.
  4691. Providing it allows for quicker computation."
  4692. (catch 'objects-forbidden
  4693. (org-with-wide-buffer
  4694. (let* ((origin (point))
  4695. (element (or element (org-element-at-point)))
  4696. (type (org-element-type element)))
  4697. ;; If point is inside an element containing objects or
  4698. ;; a secondary string, narrow buffer to the container and
  4699. ;; proceed with parsing. Otherwise, return ELEMENT.
  4700. (cond
  4701. ;; At a parsed affiliated keyword, check if we're inside main
  4702. ;; or dual value.
  4703. ((let ((post (org-element-property :post-affiliated element)))
  4704. (and post (< origin post)))
  4705. (beginning-of-line)
  4706. (let ((case-fold-search t)) (looking-at org-element--affiliated-re))
  4707. (cond
  4708. ((not (member-ignore-case (match-string 1)
  4709. org-element-parsed-keywords))
  4710. (throw 'objects-forbidden element))
  4711. ((< (match-end 0) origin)
  4712. (narrow-to-region (match-end 0) (line-end-position)))
  4713. ((and (match-beginning 2)
  4714. (>= origin (match-beginning 2))
  4715. (< origin (match-end 2)))
  4716. (narrow-to-region (match-beginning 2) (match-end 2)))
  4717. (t (throw 'objects-forbidden element)))
  4718. ;; Also change type to retrieve correct restrictions.
  4719. (setq type 'keyword))
  4720. ;; At an item, objects can only be located within tag, if any.
  4721. ((eq type 'item)
  4722. (let ((tag (org-element-property :tag element)))
  4723. (if (not tag) (throw 'objects-forbidden element)
  4724. (beginning-of-line)
  4725. (search-forward tag (line-end-position))
  4726. (goto-char (match-beginning 0))
  4727. (if (and (>= origin (point)) (< origin (match-end 0)))
  4728. (narrow-to-region (point) (match-end 0))
  4729. (throw 'objects-forbidden element)))))
  4730. ;; At an headline or inlinetask, objects are in title.
  4731. ((memq type '(headline inlinetask))
  4732. (goto-char (org-element-property :begin element))
  4733. (skip-chars-forward "* ")
  4734. (if (and (>= origin (point)) (< origin (line-end-position)))
  4735. (narrow-to-region (point) (line-end-position))
  4736. (throw 'objects-forbidden element)))
  4737. ;; At a paragraph, a table-row or a verse block, objects are
  4738. ;; located within their contents.
  4739. ((memq type '(paragraph table-row verse-block))
  4740. (let ((cbeg (org-element-property :contents-begin element))
  4741. (cend (org-element-property :contents-end element)))
  4742. ;; CBEG is nil for table rules.
  4743. (if (and cbeg cend (>= origin cbeg) (< origin cend))
  4744. (narrow-to-region cbeg cend)
  4745. (throw 'objects-forbidden element))))
  4746. ;; At a parsed keyword, objects are located within value.
  4747. ((eq type 'keyword)
  4748. (if (not (member (org-element-property :key element)
  4749. org-element-document-properties))
  4750. (throw 'objects-forbidden element)
  4751. (beginning-of-line)
  4752. (search-forward ":")
  4753. (if (and (>= origin (point)) (< origin (line-end-position)))
  4754. (narrow-to-region (point) (line-end-position))
  4755. (throw 'objects-forbidden element))))
  4756. ;; All other locations cannot contain objects: bail out.
  4757. (t (throw 'objects-forbidden element)))
  4758. (goto-char (point-min))
  4759. (let* ((restriction (org-element-restriction type))
  4760. (parent element)
  4761. (candidates 'initial)
  4762. (cache-key (org-element--cache-get-key element))
  4763. (cache (org-element-cache-get cache-key 'objects))
  4764. objects-data next update-cache-flag)
  4765. (prog1
  4766. (catch 'exit
  4767. (while t
  4768. ;; Get list of next object candidates in CANDIDATES.
  4769. ;; When entering for the first time PARENT, grab it
  4770. ;; from cache, if available, or compute it. Then,
  4771. ;; for each subsequent iteration in PARENT, always
  4772. ;; compute it since we're beyond cache anyway.
  4773. (when (and (not next) org-element-use-cache)
  4774. (let ((data (assq (point) cache)))
  4775. (if data (setq candidates (nth 1 (setq objects-data data)))
  4776. (push (setq objects-data (list (point) 'initial))
  4777. cache))))
  4778. (when (or next (eq 'initial candidates))
  4779. (setq candidates
  4780. (org-element--get-next-object-candidates
  4781. restriction candidates))
  4782. (when org-element-use-cache
  4783. (setcar (cdr objects-data) candidates)
  4784. (or update-cache-flag (setq update-cache-flag t))))
  4785. ;; Compare ORIGIN with next object starting position,
  4786. ;; if any.
  4787. ;;
  4788. ;; If ORIGIN is lesser or if there is no object
  4789. ;; following, look for a previous object that might
  4790. ;; contain it in cache. If there is no cache, we
  4791. ;; didn't miss any object so simply return PARENT.
  4792. ;;
  4793. ;; If ORIGIN is greater or equal, parse next
  4794. ;; candidate for further processing.
  4795. (let ((closest
  4796. (and candidates
  4797. (rassq (apply #'min (mapcar #'cdr candidates))
  4798. candidates))))
  4799. (if (or (not closest) (> (cdr closest) origin))
  4800. (catch 'found
  4801. (dolist (obj (cddr objects-data) (throw 'exit parent))
  4802. (when (<= (org-element-property :begin obj) origin)
  4803. (if (<= (org-element-property :end obj) origin)
  4804. ;; Object ends before ORIGIN and we
  4805. ;; know next one in cache starts
  4806. ;; after it: bail out.
  4807. (throw 'exit parent)
  4808. (throw 'found (setq next obj))))))
  4809. (goto-char (cdr closest))
  4810. (setq next
  4811. (funcall (intern (format "org-element-%s-parser"
  4812. (car closest)))))
  4813. (when org-element-use-cache
  4814. (push next (cddr objects-data))
  4815. (or update-cache-flag (setq update-cache-flag t)))))
  4816. ;; Process NEXT to know if we need to skip it, return
  4817. ;; it or move into it.
  4818. (let ((cbeg (org-element-property :contents-begin next))
  4819. (cend (org-element-property :contents-end next))
  4820. (obj-end (org-element-property :end next)))
  4821. (cond
  4822. ;; ORIGIN is after NEXT, so skip it.
  4823. ((<= obj-end origin) (goto-char obj-end))
  4824. ;; ORIGIN is within a non-recursive next or
  4825. ;; at an object boundaries: Return that object.
  4826. ((or (not cbeg) (< origin cbeg) (>= origin cend))
  4827. (throw 'exit
  4828. (org-element-put-property next :parent parent)))
  4829. ;; Otherwise, move into NEXT and reset flags as we
  4830. ;; shift parent.
  4831. (t (goto-char cbeg)
  4832. (narrow-to-region (point) cend)
  4833. (org-element-put-property next :parent parent)
  4834. (setq parent next
  4835. restriction (org-element-restriction next)
  4836. next nil
  4837. objects-data nil
  4838. candidates 'initial))))))
  4839. ;; Update cache if required.
  4840. (when (and update-cache-flag (derived-mode-p 'org-mode))
  4841. (org-element-cache-put cache-key (cons element cache)))))))))
  4842. (defun org-element-nested-p (elem-A elem-B)
  4843. "Non-nil when elements ELEM-A and ELEM-B are nested."
  4844. (let ((beg-A (org-element-property :begin elem-A))
  4845. (beg-B (org-element-property :begin elem-B))
  4846. (end-A (org-element-property :end elem-A))
  4847. (end-B (org-element-property :end elem-B)))
  4848. (or (and (>= beg-A beg-B) (<= end-A end-B))
  4849. (and (>= beg-B beg-A) (<= end-B end-A)))))
  4850. (defun org-element-swap-A-B (elem-A elem-B)
  4851. "Swap elements ELEM-A and ELEM-B.
  4852. Assume ELEM-B is after ELEM-A in the buffer. Leave point at the
  4853. end of ELEM-A."
  4854. (goto-char (org-element-property :begin elem-A))
  4855. ;; There are two special cases when an element doesn't start at bol:
  4856. ;; the first paragraph in an item or in a footnote definition.
  4857. (let ((specialp (not (bolp))))
  4858. ;; Only a paragraph without any affiliated keyword can be moved at
  4859. ;; ELEM-A position in such a situation. Note that the case of
  4860. ;; a footnote definition is impossible: it cannot contain two
  4861. ;; paragraphs in a row because it cannot contain a blank line.
  4862. (if (and specialp
  4863. (or (not (eq (org-element-type elem-B) 'paragraph))
  4864. (/= (org-element-property :begin elem-B)
  4865. (org-element-property :contents-begin elem-B))))
  4866. (error "Cannot swap elements"))
  4867. ;; In a special situation, ELEM-A will have no indentation. We'll
  4868. ;; give it ELEM-B's (which will in, in turn, have no indentation).
  4869. (let* ((ind-B (when specialp
  4870. (goto-char (org-element-property :begin elem-B))
  4871. (org-get-indentation)))
  4872. (beg-A (org-element-property :begin elem-A))
  4873. (end-A (save-excursion
  4874. (goto-char (org-element-property :end elem-A))
  4875. (skip-chars-backward " \r\t\n")
  4876. (point-at-eol)))
  4877. (beg-B (org-element-property :begin elem-B))
  4878. (end-B (save-excursion
  4879. (goto-char (org-element-property :end elem-B))
  4880. (skip-chars-backward " \r\t\n")
  4881. (point-at-eol)))
  4882. ;; Store overlays responsible for visibility status. We
  4883. ;; also need to store their boundaries as they will be
  4884. ;; removed from buffer.
  4885. (overlays
  4886. (cons
  4887. (mapcar (lambda (ov) (list ov (overlay-start ov) (overlay-end ov)))
  4888. (overlays-in beg-A end-A))
  4889. (mapcar (lambda (ov) (list ov (overlay-start ov) (overlay-end ov)))
  4890. (overlays-in beg-B end-B))))
  4891. ;; Get contents.
  4892. (body-A (buffer-substring beg-A end-A))
  4893. (body-B (delete-and-extract-region beg-B end-B)))
  4894. (goto-char beg-B)
  4895. (when specialp
  4896. (setq body-B (replace-regexp-in-string "\\`[ \t]*" "" body-B))
  4897. (org-indent-to-column ind-B))
  4898. (insert body-A)
  4899. ;; Restore ex ELEM-A overlays.
  4900. (let ((offset (- beg-B beg-A)))
  4901. (mapc (lambda (ov)
  4902. (move-overlay
  4903. (car ov) (+ (nth 1 ov) offset) (+ (nth 2 ov) offset)))
  4904. (car overlays))
  4905. (goto-char beg-A)
  4906. (delete-region beg-A end-A)
  4907. (insert body-B)
  4908. ;; Restore ex ELEM-B overlays.
  4909. (mapc (lambda (ov)
  4910. (move-overlay
  4911. (car ov) (- (nth 1 ov) offset) (- (nth 2 ov) offset)))
  4912. (cdr overlays)))
  4913. (goto-char (org-element-property :end elem-B)))))
  4914. (defun org-element-remove-indentation (s &optional n)
  4915. "Remove maximum common indentation in string S and return it.
  4916. When optional argument N is a positive integer, remove exactly
  4917. that much characters from indentation, if possible, or return
  4918. S as-is otherwise. Unlike to `org-remove-indentation', this
  4919. function doesn't call `untabify' on S."
  4920. (catch 'exit
  4921. (with-temp-buffer
  4922. (insert s)
  4923. (goto-char (point-min))
  4924. ;; Find maximum common indentation, if not specified.
  4925. (setq n (or n
  4926. (let ((min-ind (point-max)))
  4927. (save-excursion
  4928. (while (re-search-forward "^[ \t]*\\S-" nil t)
  4929. (let ((ind (1- (current-column))))
  4930. (if (zerop ind) (throw 'exit s)
  4931. (setq min-ind (min min-ind ind))))))
  4932. min-ind)))
  4933. (if (zerop n) s
  4934. ;; Remove exactly N indentation, but give up if not possible.
  4935. (while (not (eobp))
  4936. (let ((ind (progn (skip-chars-forward " \t") (current-column))))
  4937. (cond ((eolp) (delete-region (line-beginning-position) (point)))
  4938. ((< ind n) (throw 'exit s))
  4939. (t (org-indent-line-to (- ind n))))
  4940. (forward-line)))
  4941. (buffer-string)))))
  4942. (provide 'org-element)
  4943. ;; Local variables:
  4944. ;; generated-autoload-file: "org-loaddefs.el"
  4945. ;; End:
  4946. ;;; org-element.el ends here