org-element.el 193 KB

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