org-e-ascii.el 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780
  1. ;;; org-e-ascii.el --- ASCII Back-End For Org Export Engine
  2. ;; Copyright (C) 2012 Free Software Foundation, Inc.
  3. ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
  4. ;; Keywords: outlines, hypermedia, calendar, wp
  5. ;; This program is free software; you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; This program is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;; This library implements an ASCII back-end for Org generic exporter.
  17. ;; To test it, run
  18. ;;
  19. ;; M-: (org-export-to-buffer 'e-ascii "*Test e-ASCII*") RET
  20. ;;
  21. ;; in an Org mode buffer then switch to that buffer to see the ASCII
  22. ;; export. See contrib/lisp/org-export.el for more details on how
  23. ;; this exporter works.
  24. ;;; Code:
  25. (eval-when-compile (require 'cl))
  26. (require 'org-export)
  27. ;;; Define Back-End
  28. ;;
  29. ;; The following setting won't allow to modify preferred charset
  30. ;; through a buffer keyword or an option item, but, since the property
  31. ;; will appear in communication channel nonetheless, it allows to
  32. ;; override `org-e-ascii-charset' variable on the fly by the ext-plist
  33. ;; mechanism.
  34. ;;
  35. ;; We also install a filter for headlines and sections, in order to
  36. ;; control blank lines separating them in output string.
  37. (org-export-define-backend e-ascii
  38. ((bold . org-e-ascii-bold)
  39. (center-block . org-e-ascii-center-block)
  40. (clock . org-e-ascii-clock)
  41. (code . org-e-ascii-code)
  42. (drawer . org-e-ascii-drawer)
  43. (dynamic-block . org-e-ascii-dynamic-block)
  44. (entity . org-e-ascii-entity)
  45. (example-block . org-e-ascii-example-block)
  46. (export-block . org-e-ascii-export-block)
  47. (export-snippet . org-e-ascii-export-snippet)
  48. (fixed-width . org-e-ascii-fixed-width)
  49. (footnote-definition . org-e-ascii-footnote-definition)
  50. (footnote-reference . org-e-ascii-footnote-reference)
  51. (headline . org-e-ascii-headline)
  52. (horizontal-rule . org-e-ascii-horizontal-rule)
  53. (inline-src-block . org-e-ascii-inline-src-block)
  54. (inlinetask . org-e-ascii-inlinetask)
  55. (italic . org-e-ascii-italic)
  56. (item . org-e-ascii-item)
  57. (keyword . org-e-ascii-keyword)
  58. (latex-environment . org-e-ascii-latex-environment)
  59. (latex-fragment . org-e-ascii-latex-fragment)
  60. (line-break . org-e-ascii-line-break)
  61. (link . org-e-ascii-link)
  62. (macro . org-e-ascii-macro)
  63. (paragraph . org-e-ascii-paragraph)
  64. (plain-list . org-e-ascii-plain-list)
  65. (plain-text . org-e-ascii-plain-text)
  66. (planning . org-e-ascii-planning)
  67. (quote-block . org-e-ascii-quote-block)
  68. (quote-section . org-e-ascii-quote-section)
  69. (radio-target . org-e-ascii-radio-target)
  70. (section . org-e-ascii-section)
  71. (special-block . org-e-ascii-special-block)
  72. (src-block . org-e-ascii-src-block)
  73. (statistics-cookie . org-e-ascii-statistics-cookie)
  74. (strike-through . org-e-ascii-strike-through)
  75. (subscript . org-e-ascii-subscript)
  76. (superscript . org-e-ascii-superscript)
  77. (table . org-e-ascii-table)
  78. (table-cell . org-e-ascii-table-cell)
  79. (table-row . org-e-ascii-table-row)
  80. (target . org-e-ascii-target)
  81. (template . org-e-ascii-template)
  82. (timestamp . org-e-ascii-timestamp)
  83. (underline . org-e-ascii-underline)
  84. (verbatim . org-e-ascii-verbatim)
  85. (verse-block . org-e-ascii-verse-block))
  86. :export-block "ASCII"
  87. :filters-alist ((:filter-headline . org-e-ascii-filter-headline-blank-lines)
  88. (:filter-section . org-e-ascii-filter-headline-blank-lines))
  89. :options-alist ((:ascii-charset nil nil org-e-ascii-charset)))
  90. ;;; User Configurable Variables
  91. (defgroup org-export-e-ascii nil
  92. "Options for exporting Org mode files to ASCII."
  93. :tag "Org Export ASCII"
  94. :group 'org-export)
  95. (defcustom org-e-ascii-text-width 72
  96. "Maximum width of exported text.
  97. This number includes margin size, as set in
  98. `org-e-ascii-global-margin'."
  99. :group 'org-export-e-ascii
  100. :type 'integer)
  101. (defcustom org-e-ascii-global-margin 0
  102. "Width of the left margin, in number of characters."
  103. :group 'org-export-e-ascii
  104. :type 'integer)
  105. (defcustom org-e-ascii-inner-margin 2
  106. "Width of the inner margin, in number of characters.
  107. Inner margin is applied between each headline."
  108. :group 'org-export-e-ascii
  109. :type 'integer)
  110. (defcustom org-e-ascii-quote-margin 6
  111. "Width of margin used for quoting text, in characters.
  112. This margin is applied on both sides of the text."
  113. :group 'org-export-e-ascii
  114. :type 'integer)
  115. (defcustom org-e-ascii-inlinetask-width 30
  116. "Width of inline tasks, in number of characters.
  117. This number ignores any margin."
  118. :group 'org-export-e-ascii
  119. :type 'integer)
  120. (defcustom org-e-ascii-headline-spacing '(1 . 2)
  121. "Number of blank lines inserted around headlines.
  122. This variable can be set to a cons cell. In that case, its car
  123. represents the number of blank lines present before headline
  124. contents whereas its cdr reflects the number of blank lines after
  125. contents.
  126. A nil value replicates the number of blank lines found in the
  127. original Org buffer at the same place."
  128. :group 'org-export-e-ascii
  129. :type '(choice
  130. (const :tag "Replicate original spacing" nil)
  131. (cons :tag "Set an uniform spacing"
  132. (integer :tag "Number of blank lines before contents")
  133. (integer :tag "Number of blank lines after contents"))))
  134. (defcustom org-e-ascii-charset 'ascii
  135. "The charset allowed to represent various elements and objects.
  136. Possible values are:
  137. `ascii' Only use plain ASCII characters
  138. `latin1' Include Latin-1 characters
  139. `utf-8' Use all UTF-8 characters"
  140. :group 'org-export-e-ascii
  141. :type '(choice
  142. (const :tag "ASCII" ascii)
  143. (const :tag "Latin-1" latin1)
  144. (const :tag "UTF-8" utf-8)))
  145. (defcustom org-e-ascii-underline '((ascii ?= ?~ ?-)
  146. (latin1 ?= ?~ ?-)
  147. (utf-8 ?═ ?─ ?╌ ?┄ ?┈))
  148. "Characters for underlining headings in ASCII export.
  149. Alist whose key is a symbol among `ascii', `latin1' and `utf-8'
  150. and whose value is a list of characters.
  151. For each supported charset, this variable associates a sequence
  152. of underline characters. In a sequence, the characters will be
  153. used in order for headlines level 1, 2, ... If no character is
  154. available for a given level, the headline won't be underlined."
  155. :group 'org-export-e-ascii
  156. :type '(list
  157. (cons :tag "Underline characters sequence"
  158. (const :tag "ASCII charset" ascii)
  159. (repeat character))
  160. (cons :tag "Underline characters sequence"
  161. (const :tag "Latin-1 charset" latin1)
  162. (repeat character))
  163. (cons :tag "Underline characters sequence"
  164. (const :tag "UTF-8 charset" utf-8)
  165. (repeat character))))
  166. (defcustom org-e-ascii-bullets '((ascii ?* ?+ ?-)
  167. (latin1 ?§ ?¶)
  168. (utf-8 ?◊))
  169. "Bullet characters for headlines converted to lists in ASCII export.
  170. Alist whose key is a symbol among `ascii', `latin1' and `utf-8'
  171. and whose value is a list of characters.
  172. The first character is used for the first level considered as low
  173. level, and so on. If there are more levels than characters given
  174. here, the list will be repeated.
  175. Note that this variable doesn't affect plain lists
  176. representation."
  177. :group 'org-export-e-ascii
  178. :type '(list
  179. (cons :tag "Bullet characters for low level headlines"
  180. (const :tag "ASCII charset" ascii)
  181. (repeat character))
  182. (cons :tag "Bullet characters for low level headlines"
  183. (const :tag "Latin-1 charset" latin1)
  184. (repeat character))
  185. (cons :tag "Bullet characters for low level headlines"
  186. (const :tag "UTF-8 charset" utf-8)
  187. (repeat character))))
  188. (defcustom org-e-ascii-links-to-notes t
  189. "Non-nil means convert links to notes before the next headline.
  190. When nil, the link will be exported in place. If the line
  191. becomes long in this way, it will be wrapped."
  192. :group 'org-export-e-ascii
  193. :type 'boolean)
  194. (defcustom org-e-ascii-table-keep-all-vertical-lines nil
  195. "Non-nil means keep all vertical lines in ASCII tables.
  196. When nil, vertical lines will be removed except for those needed
  197. for column grouping."
  198. :group 'org-export-e-ascii
  199. :type 'boolean)
  200. (defcustom org-e-ascii-table-widen-columns t
  201. "Non-nil means widen narrowed columns for export.
  202. When nil, narrowed columns will look in ASCII export just like in
  203. Org mode, i.e. with \"=>\" as ellipsis."
  204. :group 'org-export-e-ascii
  205. :type 'boolean)
  206. (defcustom org-e-ascii-caption-above nil
  207. "When non-nil, place caption string before the element.
  208. Otherwise, place it right after it."
  209. :group 'org-export-e-ascii
  210. :type 'boolean)
  211. (defcustom org-e-ascii-verbatim-format "`%s'"
  212. "Format string used for verbatim text and inline code."
  213. :group 'org-export-e-ascii
  214. :type 'string)
  215. (defcustom org-e-ascii-format-drawer-function nil
  216. "Function called to format a drawer in ASCII.
  217. The function must accept two parameters:
  218. NAME the drawer name, like \"LOGBOOK\"
  219. CONTENTS the contents of the drawer.
  220. WIDTH the text width within the drawer.
  221. The function should return either the string to be exported or
  222. nil to ignore the drawer.
  223. For example, the variable could be set to the following function
  224. in order to mimic default behaviour:
  225. \(defun org-e-ascii-format-drawer-default \(name contents width\)
  226. \"Format a drawer element for ASCII export.\"
  227. contents\)"
  228. :group 'org-export-e-ascii
  229. :type 'function)
  230. (defcustom org-e-ascii-format-inlinetask-function nil
  231. "Function called to format an inlinetask in ASCII.
  232. The function must accept six parameters:
  233. TODO the todo keyword, as a string
  234. TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
  235. PRIORITY the inlinetask priority, as a string
  236. NAME the inlinetask name, as a string.
  237. TAGS the inlinetask tags, as a list of strings.
  238. CONTENTS the contents of the inlinetask, as a string.
  239. The function should return either the string to be exported or
  240. nil to ignore the inline task.
  241. For example, the variable could be set to the following function
  242. in order to mimic default behaviour:
  243. \(defun org-e-ascii-format-inlinetask-default
  244. \(todo type priority name tags contents\)
  245. \"Format an inline task element for ASCII export.\"
  246. \(let* \(\(utf8p \(eq \(plist-get info :ascii-charset\) 'utf-8\)\)
  247. \(width org-e-ascii-inlinetask-width\)
  248. \(org-e-ascii--indent-string
  249. \(concat
  250. ;; Top line, with an additional blank line if not in UTF-8.
  251. \(make-string width \(if utf8p ?━ ?_\)\) \"\\n\"
  252. \(unless utf8p \(concat \(make-string width ? \) \"\\n\"\)\)
  253. ;; Add title. Fill it if wider than inlinetask.
  254. \(let \(\(title \(org-e-ascii--build-title inlinetask info width\)\)\)
  255. \(if \(<= \(length title\) width\) title
  256. \(org-e-ascii--fill-string title width info\)\)\)
  257. \"\\n\"
  258. ;; If CONTENTS is not empty, insert it along with
  259. ;; a separator.
  260. \(when \(org-string-nw-p contents\)
  261. \(concat \(make-string width \(if utf8p ?─ ?-\)\) \"\\n\" contents\)\)
  262. ;; Bottom line.
  263. \(make-string width \(if utf8p ?━ ?_\)\)\)
  264. ;; Flush the inlinetask to the right.
  265. \(- \(plist-get info :ascii-width\)
  266. \(plist-get info :ascii-margin\)
  267. \(plist-get info :ascii-inner-margin\)
  268. \(org-e-ascii--current-text-width inlinetask info\)\)"
  269. :group 'org-export-e-ascii
  270. :type 'function)
  271. ;;; Internal Functions
  272. ;; Internal functions fall into three categories.
  273. ;; The first one is about text formatting. The core function is
  274. ;; `org-e-ascii--current-text-width', which determines the current
  275. ;; text width allowed to a given element. In other words, it helps
  276. ;; keeping each line width within maximum text width defined in
  277. ;; `org-e-ascii-text-width'. Once this information is known,
  278. ;; `org-e-ascii--fill-string', `org-e-ascii--justify-string',
  279. ;; `org-e-ascii--box-string' and `org-e-ascii--indent-string' can
  280. ;; operate on a given output string.
  281. ;; The second category contains functions handling elements listings,
  282. ;; triggered by "#+TOC:" keyword. As such, `org-e-ascii--build-toc'
  283. ;; returns a complete table of contents, `org-e-ascii--list-listings'
  284. ;; returns a list of referenceable src-block elements, and
  285. ;; `org-e-ascii--list-tables' does the same for table elements.
  286. ;; The third category includes general helper functions.
  287. ;; `org-e-ascii--build-title' creates the title for a given headline
  288. ;; or inlinetask element. `org-e-ascii--build-caption' returns the
  289. ;; caption string associated to a table or a src-block.
  290. ;; `org-e-ascii--describe-links' creates notes about links for
  291. ;; insertion at the end of a section. It uses
  292. ;; `org-e-ascii--unique-links' to get the list of links to describe.
  293. ;; Eventually, `org-e-ascii--translate' translates a string according
  294. ;; to language and charset specification.
  295. (defun org-e-ascii--fill-string (s text-width info &optional justify)
  296. "Fill a string with specified text-width and return it.
  297. S is the string being filled. TEXT-WIDTH is an integer
  298. specifying maximum length of a line. INFO is the plist used as
  299. a communication channel.
  300. Optional argument JUSTIFY can specify any type of justification
  301. among `left', `center', `right' or `full'. A nil value is
  302. equivalent to `left'. For a justification that doesn't also fill
  303. string, see `org-e-ascii--justify-string'.
  304. Return nil if S isn't a string."
  305. ;; Don't fill paragraph when break should be preserved.
  306. (cond ((not (stringp s)) nil)
  307. ((plist-get info :preserve-breaks) s)
  308. (t (with-temp-buffer
  309. (let ((fill-column text-width)
  310. (use-hard-newlines t))
  311. (insert s)
  312. (fill-region (point-min) (point-max) justify))
  313. (buffer-string)))))
  314. (defun org-e-ascii--justify-string (s text-width how)
  315. "Justify string S.
  316. TEXT-WIDTH is an integer specifying maximum length of a line.
  317. HOW determines the type of justification: it can be `left',
  318. `right', `full' or `center'."
  319. (with-temp-buffer
  320. (insert s)
  321. (goto-char (point-min))
  322. (let ((fill-column text-width))
  323. (while (< (point) (point-max))
  324. (justify-current-line how)
  325. (forward-line)))
  326. (buffer-string)))
  327. (defun org-e-ascii--indent-string (s width)
  328. "Indent string S by WIDTH white spaces.
  329. Empty lines are not indented."
  330. (when (stringp s)
  331. (replace-regexp-in-string
  332. "\\(^\\)\\(?:.*\\S-\\)" (make-string width ? ) s nil nil 1)))
  333. (defun org-e-ascii--box-string (s info)
  334. "Return string S with a partial box to its left.
  335. INFO is a plist used as a communicaton channel."
  336. (let ((utf8p (eq (plist-get info :ascii-charset) 'utf-8)))
  337. (format (if utf8p "╭────\n%s\n╰────" ",----\n%s\n`----")
  338. (replace-regexp-in-string
  339. "^" (if utf8p "│ " "| ")
  340. ;; Remove last newline character.
  341. (replace-regexp-in-string "\n[ \t]*\\'" "" s)))))
  342. (defun org-e-ascii--current-text-width (element info)
  343. "Return maximum text width for ELEMENT's contents.
  344. INFO is a plist used as a communication channel."
  345. (case (org-element-type element)
  346. ;; Elements with an absolute width: `headline' and `inlinetask'.
  347. (inlinetask org-e-ascii-inlinetask-width)
  348. ('headline
  349. (- org-e-ascii-text-width
  350. (let ((low-level-rank (org-export-low-level-p element info)))
  351. (if low-level-rank (* low-level-rank 2) org-e-ascii-global-margin))))
  352. ;; Elements with a relative width: store maximum text width in
  353. ;; TOTAL-WIDTH.
  354. (otherwise
  355. (let* ((genealogy (cons element (org-export-get-genealogy element)))
  356. ;; Total width is determined by the presence, or not, of an
  357. ;; inline task among ELEMENT parents.
  358. (total-width
  359. (if (loop for parent in genealogy
  360. thereis (eq (org-element-type parent) 'inlinetask))
  361. org-e-ascii-inlinetask-width
  362. ;; No inlinetask: Remove global margin from text width.
  363. (- org-e-ascii-text-width
  364. org-e-ascii-global-margin
  365. (let ((parent (org-export-get-parent-headline element)))
  366. ;; Inner margin doesn't apply to text before first
  367. ;; headline.
  368. (if (not parent) 0
  369. (let ((low-level-rank
  370. (org-export-low-level-p parent info)))
  371. ;; Inner margin doesn't apply to contents of
  372. ;; low level headlines, since they've got their
  373. ;; own indentation mechanism.
  374. (if low-level-rank (* low-level-rank 2)
  375. org-e-ascii-inner-margin))))))))
  376. (- total-width
  377. ;; Each `quote-block', `quote-section' and `verse-block' above
  378. ;; narrows text width by twice the standard margin size.
  379. (+ (* (loop for parent in genealogy
  380. when (memq (org-element-type parent)
  381. '(quote-block quote-section verse-block))
  382. count parent)
  383. 2 org-e-ascii-quote-margin)
  384. ;; Text width within a plain-list is restricted by
  385. ;; indentation of current item. If that's the case,
  386. ;; compute it with the help of `:structure' property from
  387. ;; parent item, if any.
  388. (let ((parent-item
  389. (if (eq (org-element-type element) 'item) element
  390. (loop for parent in genealogy
  391. when (eq (org-element-type parent) 'item)
  392. return parent))))
  393. (if (not parent-item) 0
  394. ;; Compute indentation offset of the current item,
  395. ;; that is the sum of the difference between its
  396. ;; indentation and the indentation of the top item in
  397. ;; the list and current item bullet's length. Also
  398. ;; remove checkbox length, and tag length (for
  399. ;; description lists) or bullet length.
  400. (let ((struct (org-element-property :structure parent-item))
  401. (beg-item (org-element-property :begin parent-item)))
  402. (+ (- (org-list-get-ind beg-item struct)
  403. (org-list-get-ind
  404. (org-list-get-top-point struct) struct))
  405. (length (org-e-ascii--checkbox parent-item info))
  406. (length
  407. (or (org-list-get-tag beg-item struct)
  408. (org-list-get-bullet beg-item struct)))))))))))))
  409. (defun org-e-ascii--build-title
  410. (element info text-width &optional underline notags)
  411. "Format ELEMENT title and return it.
  412. ELEMENT is either an `headline' or `inlinetask' element. INFO is
  413. a plist used as a communication channel. TEXT-WIDTH is an
  414. integer representing the maximum length of a line.
  415. When optional argument UNDERLINE is non-nil, underline title,
  416. without the tags, according to `org-e-ascii-underline'
  417. specifications.
  418. if optional argument NOTAGS is nil, no tags will be added to the
  419. title."
  420. (let* ((headlinep (eq (org-element-type element) 'headline))
  421. (numbers
  422. ;; Numbering is specific to headlines.
  423. (and headlinep (org-export-numbered-headline-p element info)
  424. ;; All tests passed: build numbering string.
  425. (concat
  426. (mapconcat
  427. 'number-to-string
  428. (org-export-get-headline-number element info) ".")
  429. " ")))
  430. (text (org-export-data (org-element-property :title element) info))
  431. (todo
  432. (and (plist-get info :with-todo-keywords)
  433. (let ((todo (org-element-property :todo-keyword element)))
  434. (and todo (concat (org-export-data todo info) " ")))))
  435. (tags (and (not notags)
  436. (plist-get info :with-tags)
  437. (let ((tag-list (org-export-get-tags element info)))
  438. (and tag-list
  439. (format ":%s:"
  440. (mapconcat 'identity tag-list ":"))))))
  441. (priority
  442. (and (plist-get info :with-priority)
  443. (concat (org-element-property :priority element) " ")))
  444. (first-part (concat numbers todo priority text)))
  445. (concat
  446. first-part
  447. ;; Align tags, if any.
  448. (when tags
  449. (format
  450. (format " %%%ds"
  451. (max (- text-width (1+ (length first-part))) (length tags)))
  452. tags))
  453. ;; Maybe underline text, if ELEMENT type is `headline' and an
  454. ;; underline character has been defined.
  455. (when (and underline headlinep)
  456. (let ((under-char
  457. (nth (1- (org-export-get-relative-level element info))
  458. (cdr (assq (plist-get info :ascii-charset)
  459. org-e-ascii-underline)))))
  460. (and under-char
  461. (concat "\n"
  462. (make-string (length first-part) under-char))))))))
  463. (defun org-e-ascii--has-caption-p (element info)
  464. "Non-nil when ELEMENT has a caption affiliated keyword.
  465. INFO is a plist used as a communication channel. This function
  466. is meant to be used as a predicate for `org-export-get-ordinal'."
  467. (org-element-property :caption element))
  468. (defun org-e-ascii--build-caption (element info)
  469. "Return caption string for ELEMENT, if applicable.
  470. INFO is a plist used as a communication channel.
  471. The caption string contains the sequence number of ELEMENT along
  472. with its real caption. Return nil when ELEMENT has no affiliated
  473. caption keyword."
  474. (let ((caption (org-element-property :caption element)))
  475. (when caption
  476. ;; Get sequence number of current src-block among every
  477. ;; src-block with a caption.
  478. (let ((reference
  479. (org-export-get-ordinal
  480. element info nil 'org-e-ascii--has-caption-p))
  481. (title-fmt (org-e-ascii--translate
  482. (case (org-element-type element)
  483. (table "Table %d: %s")
  484. (src-block "Listing %d: %s"))
  485. info)))
  486. (org-e-ascii--fill-string
  487. (format title-fmt reference (org-export-data (car caption) info))
  488. (org-e-ascii--current-text-width element info) info)))))
  489. (defun org-e-ascii--build-toc (info &optional n keyword)
  490. "Return a table of contents.
  491. INFO is a plist used as a communication channel.
  492. Optional argument N, when non-nil, is an integer specifying the
  493. depth of the table.
  494. Optional argument KEYWORD specifies the TOC keyword, if any, from
  495. which the table of contents generation has been initiated."
  496. (let ((title (org-e-ascii--translate "Table of Contents" info)))
  497. (concat
  498. title "\n"
  499. (make-string (length title)
  500. (if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))
  501. "\n\n"
  502. (let ((text-width
  503. (if keyword (org-e-ascii--current-text-width keyword info)
  504. (- org-e-ascii-text-width org-e-ascii-global-margin))))
  505. (mapconcat
  506. (lambda (headline)
  507. (let* ((level (org-export-get-relative-level headline info))
  508. (indent (* (1- level) 3)))
  509. (concat
  510. (unless (zerop indent) (concat (make-string (1- indent) ?.) " "))
  511. (org-e-ascii--build-title
  512. headline info (- text-width indent) nil
  513. (eq (plist-get info :with-tags) 'not-in-toc)))))
  514. (org-export-collect-headlines info n) "\n")))))
  515. (defun org-e-ascii--list-listings (keyword info)
  516. "Return a list of listings.
  517. KEYWORD is the keyword that initiated the list of listings
  518. generation. INFO is a plist used as a communication channel."
  519. (let ((title (org-e-ascii--translate "List of Listings" info)))
  520. (concat
  521. title "\n"
  522. (make-string (length title)
  523. (if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))
  524. "\n\n"
  525. (let ((text-width
  526. (if keyword (org-e-ascii--current-text-width keyword info)
  527. (- org-e-ascii-text-width org-e-ascii-global-margin)))
  528. ;; Use a counter instead of retreiving ordinal of each
  529. ;; src-block.
  530. (count 0))
  531. (mapconcat
  532. (lambda (src-block)
  533. ;; Store initial text so its length can be computed. This is
  534. ;; used to properly align caption right to it in case of
  535. ;; filling (like contents of a description list item).
  536. (let ((initial-text
  537. (format (org-e-ascii--translate "Listing %d:" info)
  538. (incf count))))
  539. (concat
  540. initial-text " "
  541. (org-trim
  542. (org-e-ascii--indent-string
  543. (org-e-ascii--fill-string
  544. (let ((caption (org-element-property :caption src-block)))
  545. ;; Use short name in priority, if available.
  546. (org-export-data (or (cdr caption) (car caption)) info))
  547. (- text-width (length initial-text)) info)
  548. (length initial-text))))))
  549. (org-export-collect-listings info) "\n")))))
  550. (defun org-e-ascii--list-tables (keyword info)
  551. "Return a list of listings.
  552. KEYWORD is the keyword that initiated the list of listings
  553. generation. INFO is a plist used as a communication channel."
  554. (let ((title (org-e-ascii--translate "List of Tables" info)))
  555. (concat
  556. title "\n"
  557. (make-string (length title)
  558. (if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))
  559. "\n\n"
  560. (let ((text-width
  561. (if keyword (org-e-ascii--current-text-width keyword info)
  562. (- org-e-ascii-text-width org-e-ascii-global-margin)))
  563. ;; Use a counter instead of retreiving ordinal of each
  564. ;; src-block.
  565. (count 0))
  566. (mapconcat
  567. (lambda (table)
  568. ;; Store initial text so its length can be computed. This is
  569. ;; used to properly align caption right to it in case of
  570. ;; filling (like contents of a description list item).
  571. (let ((initial-text
  572. (format (org-e-ascii--translate "Table %d:" info)
  573. (incf count))))
  574. (concat
  575. initial-text " "
  576. (org-trim
  577. (org-e-ascii--indent-string
  578. (org-e-ascii--fill-string
  579. (let ((caption (org-element-property :caption table)))
  580. ;; Use short name in priority, if available.
  581. (org-export-data (or (cdr caption) (car caption)) info))
  582. (- text-width (length initial-text)) info)
  583. (length initial-text))))))
  584. (org-export-collect-tables info) "\n")))))
  585. (defun org-e-ascii--unique-links (element info)
  586. "Return a list of unique link references in ELEMENT.
  587. ELEMENT is either an headline element or a section element. INFO
  588. is a plist used as a communication channel."
  589. (let* (seen
  590. (unique-link-p
  591. (function
  592. ;; Return LINK if it wasn't referenced so far, or nil.
  593. ;; Update SEEN links along the way.
  594. (lambda (link)
  595. (let ((footprint
  596. (cons (org-element-property :raw-link link)
  597. (org-element-contents link))))
  598. (unless (member footprint seen)
  599. (push footprint seen) link)))))
  600. ;; If at a section, find parent headline, if any, in order to
  601. ;; count links that might be in the title.
  602. (headline
  603. (if (eq (org-element-type element) 'headline) element
  604. (or (org-export-get-parent-headline element) element))))
  605. ;; Get all links in HEADLINE.
  606. (org-element-map
  607. headline 'link (lambda (link) (funcall unique-link-p link)) info)))
  608. (defun org-e-ascii--describe-links (links width info)
  609. "Return a string describing a list of links.
  610. LINKS is a list of link type objects, as returned by
  611. `org-e-ascii--unique-links'. WIDTH is the text width allowed for
  612. the output string. INFO is a plist used as a communication
  613. channel."
  614. (mapconcat
  615. (lambda (link)
  616. (let ((type (org-element-property :type link))
  617. (anchor (let ((desc (org-element-contents link)))
  618. (if (not desc) (org-element-property :raw-link link)
  619. (org-export-data desc info)))))
  620. (cond
  621. ;; Coderefs, radio links and fuzzy links are ignored.
  622. ((member type '("coderef" "radio" "fuzzy")) nil)
  623. ;; Id and custom-id links: Headlines refer to their numbering.
  624. ((member type '("custom-id" "id"))
  625. (let ((dest (org-export-resolve-id-link link info)))
  626. (concat
  627. (org-e-ascii--fill-string
  628. (format
  629. "[%s] %s"
  630. anchor
  631. (if (not dest) (org-e-ascii--translate "Unknown reference" info)
  632. (format
  633. (org-e-ascii--translate "See section %s" info)
  634. (mapconcat 'number-to-string
  635. (org-export-get-headline-number dest info) "."))))
  636. width info) "\n\n")))
  637. ;; Do not add a link that cannot be resolved and doesn't have
  638. ;; any description: destination is already visible in the
  639. ;; paragraph.
  640. ((not (org-element-contents link)) nil)
  641. (t
  642. (concat
  643. (org-e-ascii--fill-string
  644. (format "[%s] %s" anchor (org-element-property :raw-link link))
  645. width info)
  646. "\n\n")))))
  647. links ""))
  648. (defun org-e-ascii--checkbox (item info)
  649. "Return checkbox string for ITEM or nil.
  650. INFO is a plist used as a communication channel."
  651. (let ((utf8p (eq (plist-get info :ascii-charset) 'utf-8)))
  652. (case (org-element-property :checkbox item)
  653. (on (if utf8p "☑ " "[X] "))
  654. (off (if utf8p "☐ " "[ ] "))
  655. (trans (if utf8p "☒ " "[-] ")))))
  656. ;;; Template
  657. (defun org-e-ascii-template--document-title (info)
  658. "Return document title, as a string.
  659. INFO is a plist used as a communication channel."
  660. (let ((text-width org-e-ascii-text-width)
  661. (title (org-export-data (plist-get info :title) info))
  662. (author (and (plist-get info :with-author)
  663. (let ((auth (plist-get info :author)))
  664. (and auth (org-export-data auth info)))))
  665. (email (and (plist-get info :with-email)
  666. (org-export-data (plist-get info :email) info)))
  667. (date (org-export-data (plist-get info :date) info)))
  668. ;; There are two types of title blocks depending on the presence
  669. ;; of a title to display.
  670. (if (string= title "")
  671. ;; Title block without a title. DATE is positioned at the top
  672. ;; right of the document, AUTHOR to the top left and EMAIL
  673. ;; just below.
  674. (cond
  675. ((and (org-string-nw-p date) (org-string-nw-p author))
  676. (concat
  677. author
  678. (make-string (- text-width (length date) (length author)) ? )
  679. date
  680. (when (org-string-nw-p email) (concat "\n" email))
  681. "\n\n\n"))
  682. ((and (org-string-nw-p date) (org-string-nw-p email))
  683. (concat
  684. email
  685. (make-string (- text-width (length date) (length email)) ? )
  686. date "\n\n\n"))
  687. ((org-string-nw-p date)
  688. (concat
  689. (org-e-ascii--justify-string date text-width 'right)
  690. "\n\n\n"))
  691. ((and (org-string-nw-p author) (org-string-nw-p email))
  692. (concat author "\n" email "\n\n\n"))
  693. ((org-string-nw-p author) (concat author "\n\n\n"))
  694. ((org-string-nw-p email) (concat email "\n\n\n")))
  695. ;; Title block with a title. Document's TITLE, along with the
  696. ;; AUTHOR and its EMAIL are both overlined and an underlined,
  697. ;; centered. Date is just below, also centered.
  698. (let* ((utf8p (eq (plist-get info :ascii-charset) 'utf-8))
  699. ;; Format TITLE. It may be filled if it is too wide,
  700. ;; that is wider than the two thirds of the total width.
  701. (title-len (min (length title) (/ (* 2 text-width) 3)))
  702. (formatted-title (org-e-ascii--fill-string title title-len info))
  703. (line
  704. (make-string
  705. (min (+ (max title-len (length author) (length email)) 2)
  706. text-width) (if utf8p ?━ ?_))))
  707. (org-e-ascii--justify-string
  708. (concat line "\n"
  709. (unless utf8p "\n")
  710. (upcase formatted-title)
  711. (cond
  712. ((and (org-string-nw-p author) (org-string-nw-p email))
  713. (concat (if utf8p "\n\n\n" "\n\n") author "\n" email))
  714. ((org-string-nw-p author)
  715. (concat (if utf8p "\n\n\n" "\n\n") author))
  716. ((org-string-nw-p email)
  717. (concat (if utf8p "\n\n\n" "\n\n") email)))
  718. "\n" line
  719. (when (org-string-nw-p date) (concat "\n\n\n" date))
  720. "\n\n\n") text-width 'center)))))
  721. (defun org-e-ascii-template (contents info)
  722. "Return complete document string after ASCII conversion.
  723. CONTENTS is the transcoded contents string. INFO is a plist
  724. holding export options."
  725. (org-element-normalize-string
  726. (org-e-ascii--indent-string
  727. (let ((text-width (- org-e-ascii-text-width org-e-ascii-global-margin)))
  728. ;; 1. Build title block.
  729. (concat
  730. (org-e-ascii-template--document-title info)
  731. ;; 2. Table of contents.
  732. (let ((depth (plist-get info :with-toc)))
  733. (when depth
  734. (concat
  735. (org-e-ascii--build-toc info (and (wholenump depth) depth))
  736. "\n\n\n")))
  737. ;; 3. Document's body.
  738. contents
  739. ;; 4. Footnote definitions.
  740. (let ((definitions (org-export-collect-footnote-definitions
  741. (plist-get info :parse-tree) info))
  742. ;; Insert full links right inside the footnote definition
  743. ;; as they have no chance to be inserted later.
  744. (org-e-ascii-links-to-notes nil))
  745. (when definitions
  746. (concat
  747. "\n\n\n"
  748. (let ((title (org-e-ascii--translate "Footnotes" info)))
  749. (concat
  750. title "\n"
  751. (make-string
  752. (length title)
  753. (if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))))
  754. "\n\n"
  755. (mapconcat
  756. (lambda (ref)
  757. (let ((id (format "[%s] " (car ref))))
  758. ;; Distinguish between inline definitions and
  759. ;; full-fledged definitions.
  760. (org-trim
  761. (let ((def (nth 2 ref)))
  762. (if (eq (org-element-type def) 'org-data)
  763. ;; Full-fledged definition: footnote ID is
  764. ;; inserted inside the first parsed paragraph
  765. ;; (FIRST), if any, to be sure filling will
  766. ;; take it into consideration.
  767. (let ((first (car (org-element-contents def))))
  768. (if (not (eq (org-element-type first) 'paragraph))
  769. (concat id "\n" (org-export-data def info))
  770. (push id (nthcdr 2 first))
  771. (org-export-data def info)))
  772. ;; Fill paragraph once footnote ID is inserted in
  773. ;; order to have a correct length for first line.
  774. (org-e-ascii--fill-string
  775. (concat id (org-export-data def info))
  776. text-width info))))))
  777. definitions "\n\n"))))
  778. ;; 5. Creator. Ignore `comment' value as there are no comments in
  779. ;; ASCII. Justify it to the bottom right.
  780. (let ((creator-info (plist-get info :with-creator)))
  781. (unless (or (not creator-info) (eq creator-info 'comment))
  782. (concat
  783. "\n\n\n"
  784. (org-e-ascii--fill-string
  785. (plist-get info :creator) text-width info 'right))))))
  786. org-e-ascii-global-margin)))
  787. (defun org-e-ascii--translate (s info)
  788. "Translate string S according to specified language and charset.
  789. INFO is a plist used as a communication channel."
  790. (let ((charset (intern (format ":%s" (plist-get info :ascii-charset)))))
  791. (org-export-translate s charset info)))
  792. ;;; Transcode Functions
  793. ;;;; Babel Call
  794. ;; Babel Calls are ignored.
  795. ;;;; Bold
  796. (defun org-e-ascii-bold (bold contents info)
  797. "Transcode BOLD from Org to ASCII.
  798. CONTENTS is the text with bold markup. INFO is a plist holding
  799. contextual information."
  800. (format "*%s*" contents))
  801. ;;;; Center Block
  802. (defun org-e-ascii-center-block (center-block contents info)
  803. "Transcode a CENTER-BLOCK element from Org to ASCII.
  804. CONTENTS holds the contents of the block. INFO is a plist
  805. holding contextual information."
  806. (org-e-ascii--justify-string
  807. contents (org-e-ascii--current-text-width center-block info) 'center))
  808. ;;;; Clock
  809. (defun org-e-ascii-clock (clock contents info)
  810. "Transcode a CLOCK object from Org to ASCII.
  811. CONTENTS is nil. INFO is a plist holding contextual
  812. information."
  813. (concat org-clock-string " "
  814. (org-translate-time (org-element-property :value clock))
  815. (let ((time (org-element-property :time clock)))
  816. (and time
  817. (concat " => "
  818. (apply 'format
  819. "%2s:%02s"
  820. (org-split-string time ":")))))))
  821. ;;;; Code
  822. (defun org-e-ascii-code (code contents info)
  823. "Return a CODE object from Org to ASCII.
  824. CONTENTS is nil. INFO is a plist holding contextual
  825. information."
  826. (format org-e-ascii-verbatim-format (org-element-property :value code)))
  827. ;;;; Comment
  828. ;; Comments are ignored.
  829. ;;;; Comment Block
  830. ;; Comment Blocks are ignored.
  831. ;;;; Drawer
  832. (defun org-e-ascii-drawer (drawer contents info)
  833. "Transcode a DRAWER element from Org to ASCII.
  834. CONTENTS holds the contents of the block. INFO is a plist
  835. holding contextual information."
  836. (let ((name (org-element-property :drawer-name drawer))
  837. (width (org-e-ascii--current-text-width drawer info)))
  838. (if (functionp org-e-ascii-format-drawer-function)
  839. (funcall org-e-ascii-format-drawer-function name contents width)
  840. ;; If there's no user defined function: simply
  841. ;; display contents of the drawer.
  842. contents)))
  843. ;;;; Dynamic Block
  844. (defun org-e-ascii-dynamic-block (dynamic-block contents info)
  845. "Transcode a DYNAMIC-BLOCK element from Org to ASCII.
  846. CONTENTS holds the contents of the block. INFO is a plist
  847. holding contextual information."
  848. contents)
  849. ;;;; Entity
  850. (defun org-e-ascii-entity (entity contents info)
  851. "Transcode an ENTITY object from Org to ASCII.
  852. CONTENTS are the definition itself. INFO is a plist holding
  853. contextual information."
  854. (org-element-property
  855. (intern (concat ":" (symbol-name (plist-get info :ascii-charset))))
  856. entity))
  857. ;;;; Example Block
  858. (defun org-e-ascii-example-block (example-block contents info)
  859. "Transcode a EXAMPLE-BLOCK element from Org to ASCII.
  860. CONTENTS is nil. INFO is a plist holding contextual information."
  861. (org-e-ascii--box-string
  862. (org-export-format-code-default example-block info) info))
  863. ;;;; Export Snippet
  864. (defun org-e-ascii-export-snippet (export-snippet contents info)
  865. "Transcode a EXPORT-SNIPPET object from Org to ASCII.
  866. CONTENTS is nil. INFO is a plist holding contextual information."
  867. (when (eq (org-export-snippet-backend export-snippet) 'e-ascii)
  868. (org-element-property :value export-snippet)))
  869. ;;;; Export Block
  870. (defun org-e-ascii-export-block (export-block contents info)
  871. "Transcode a EXPORT-BLOCK element from Org to ASCII.
  872. CONTENTS is nil. INFO is a plist holding contextual information."
  873. (when (string= (org-element-property :type export-block) "ASCII")
  874. (org-remove-indentation (org-element-property :value export-block))))
  875. ;;;; Fixed Width
  876. (defun org-e-ascii-fixed-width (fixed-width contents info)
  877. "Transcode a FIXED-WIDTH element from Org to ASCII.
  878. CONTENTS is nil. INFO is a plist holding contextual information."
  879. (org-e-ascii--box-string
  880. (org-remove-indentation
  881. (org-element-property :value fixed-width)) info))
  882. ;;;; Footnote Definition
  883. ;; Footnote Definitions are ignored. They are compiled at the end of
  884. ;; the document, by `org-e-ascii-template'.
  885. ;;;; Footnote Reference
  886. (defun org-e-ascii-footnote-reference (footnote-reference contents info)
  887. "Transcode a FOOTNOTE-REFERENCE element from Org to ASCII.
  888. CONTENTS is nil. INFO is a plist holding contextual information."
  889. (format "[%s]" (org-export-get-footnote-number footnote-reference info)))
  890. ;;;; Headline
  891. (defun org-e-ascii-headline (headline contents info)
  892. "Transcode an HEADLINE element from Org to ASCII.
  893. CONTENTS holds the contents of the headline. INFO is a plist
  894. holding contextual information."
  895. ;; Don't export footnote section, which will be handled at the end
  896. ;; of the template.
  897. (unless (org-element-property :footnote-section-p headline)
  898. (let* ((low-level-rank (org-export-low-level-p headline info))
  899. (width (org-e-ascii--current-text-width headline info))
  900. ;; Blank lines between headline and its contents.
  901. ;; `org-e-ascii-headline-spacing', when set, overwrites
  902. ;; original buffer's spacing.
  903. (pre-blanks
  904. (make-string
  905. (if org-e-ascii-headline-spacing (car org-e-ascii-headline-spacing)
  906. (org-element-property :pre-blank headline)) ?\n))
  907. ;; Even if HEADLINE has no section, there might be some
  908. ;; links in its title that we shouldn't forget to describe.
  909. (links
  910. (unless (or (eq (caar (org-element-contents headline)) 'section))
  911. (let ((title (org-element-property :title headline)))
  912. (when (consp title)
  913. (org-e-ascii--describe-links
  914. (org-e-ascii--unique-links title info) width info))))))
  915. ;; Deep subtree: export it as a list item.
  916. (if low-level-rank
  917. (concat
  918. ;; Bullet.
  919. (let ((bullets (cdr (assq (plist-get info :ascii-charset)
  920. org-e-ascii-bullets))))
  921. (char-to-string
  922. (nth (mod (1- low-level-rank) (length bullets)) bullets)))
  923. " "
  924. ;; Title.
  925. (org-e-ascii--build-title headline info width) "\n"
  926. ;; Contents, indented by length of bullet.
  927. pre-blanks
  928. (org-e-ascii--indent-string
  929. (concat contents
  930. (when (org-string-nw-p links) (concat "\n\n" links)))
  931. 2))
  932. ;; Else: Standard headline.
  933. (concat
  934. (org-e-ascii--build-title headline info width 'underline)
  935. "\n" pre-blanks
  936. (concat (when (org-string-nw-p links) links) contents))))))
  937. ;;;; Horizontal Rule
  938. (defun org-e-ascii-horizontal-rule (horizontal-rule contents info)
  939. "Transcode an HORIZONTAL-RULE object from Org to ASCII.
  940. CONTENTS is nil. INFO is a plist holding contextual
  941. information."
  942. (let ((width (org-export-read-attribute :attr_ascii horizontal-rule :width)))
  943. (make-string (or (and (wholenump width) width)
  944. (org-e-ascii--current-text-width horizontal-rule info))
  945. (if (eq (plist-get info :ascii-charset) 'utf-8) ?― ?-))))
  946. ;;;; Inline Babel Call
  947. ;; Inline Babel Calls are ignored.
  948. ;;;; Inline Src Block
  949. (defun org-e-ascii-inline-src-block (inline-src-block contents info)
  950. "Transcode an INLINE-SRC-BLOCK element from Org to ASCII.
  951. CONTENTS holds the contents of the item. INFO is a plist holding
  952. contextual information."
  953. (format org-e-ascii-verbatim-format
  954. (org-element-property :value inline-src-block)))
  955. ;;;; Inlinetask
  956. (defun org-e-ascii-inlinetask (inlinetask contents info)
  957. "Transcode an INLINETASK element from Org to ASCII.
  958. CONTENTS holds the contents of the block. INFO is a plist
  959. holding contextual information."
  960. (let ((width (org-e-ascii--current-text-width inlinetask info)))
  961. ;; If `org-e-ascii-format-inlinetask-function' is provided, call it
  962. ;; with appropriate arguments.
  963. (if (functionp org-e-ascii-format-inlinetask-function)
  964. (funcall org-e-ascii-format-inlinetask-function
  965. ;; todo.
  966. (and (plist-get info :with-todo-keywords)
  967. (let ((todo (org-element-property
  968. :todo-keyword inlinetask)))
  969. (and todo (org-export-data todo info))))
  970. ;; todo-type
  971. (org-element-property :todo-type inlinetask)
  972. ;; priority
  973. (and (plist-get info :with-priority)
  974. (org-element-property :priority inlinetask))
  975. ;; title
  976. (org-export-data (org-element-property :title inlinetask) info)
  977. ;; tags
  978. (and (plist-get info :with-tags)
  979. (org-element-property :tags inlinetask))
  980. ;; contents and width
  981. contents width)
  982. ;; Otherwise, use a default template.
  983. (let* ((utf8p (eq (plist-get info :ascii-charset) 'utf-8)))
  984. (org-e-ascii--indent-string
  985. (concat
  986. ;; Top line, with an additional blank line if not in UTF-8.
  987. (make-string width (if utf8p ?━ ?_)) "\n"
  988. (unless utf8p (concat (make-string width ? ) "\n"))
  989. ;; Add title. Fill it if wider than inlinetask.
  990. (let ((title (org-e-ascii--build-title inlinetask info width)))
  991. (if (<= (length title) width) title
  992. (org-e-ascii--fill-string title width info)))
  993. "\n"
  994. ;; If CONTENTS is not empty, insert it along with
  995. ;; a separator.
  996. (when (org-string-nw-p contents)
  997. (concat (make-string width (if utf8p ?─ ?-)) "\n" contents))
  998. ;; Bottom line.
  999. (make-string width (if utf8p ?━ ?_)))
  1000. ;; Flush the inlinetask to the right.
  1001. (- org-e-ascii-text-width org-e-ascii-global-margin
  1002. (if (not (org-export-get-parent-headline inlinetask)) 0
  1003. org-e-ascii-inner-margin)
  1004. (org-e-ascii--current-text-width inlinetask info)))))))
  1005. ;;;; Italic
  1006. (defun org-e-ascii-italic (italic contents info)
  1007. "Transcode italic from Org to ASCII.
  1008. CONTENTS is the text with italic markup. INFO is a plist holding
  1009. contextual information."
  1010. (format "/%s/" contents))
  1011. ;;;; Item
  1012. (defun org-e-ascii-item (item contents info)
  1013. "Transcode an ITEM element from Org to ASCII.
  1014. CONTENTS holds the contents of the item. INFO is a plist holding
  1015. contextual information."
  1016. (let* ((utf8p (eq (plist-get info :ascii-charset) 'utf-8))
  1017. (checkbox (org-e-ascii--checkbox item info))
  1018. (list-type (org-element-property :type (org-export-get-parent item)))
  1019. (bullet
  1020. ;; First parent of ITEM is always the plain-list. Get
  1021. ;; `:type' property from it.
  1022. (org-list-bullet-string
  1023. (case list-type
  1024. (descriptive
  1025. (concat checkbox
  1026. (org-export-data (org-element-property :tag item) info)
  1027. ": "))
  1028. (ordered
  1029. ;; Return correct number for ITEM, paying attention to
  1030. ;; counters.
  1031. (let* ((struct (org-element-property :structure item))
  1032. (bul (org-element-property :bullet item))
  1033. (num (number-to-string
  1034. (car (last (org-list-get-item-number
  1035. (org-element-property :begin item)
  1036. struct
  1037. (org-list-prevs-alist struct)
  1038. (org-list-parents-alist struct)))))))
  1039. (replace-regexp-in-string "[0-9]+" num bul)))
  1040. (t (let ((bul (org-element-property :bullet item)))
  1041. ;; Change bullets into more visible form if UTF-8 is active.
  1042. (if (not utf8p) bul
  1043. (replace-regexp-in-string
  1044. "-" "•"
  1045. (replace-regexp-in-string
  1046. "+" "⁃"
  1047. (replace-regexp-in-string "*" "‣" bul))))))))))
  1048. (concat
  1049. bullet
  1050. (unless (eq list-type 'descriptive) checkbox)
  1051. ;; Contents: Pay attention to indentation. Note: check-boxes are
  1052. ;; already taken care of at the paragraph level so they don't
  1053. ;; interfere with indentation.
  1054. (let ((contents (org-e-ascii--indent-string contents (length bullet))))
  1055. (if (eq (org-element-type (car (org-element-contents item))) 'paragraph)
  1056. (org-trim contents)
  1057. (concat "\n" contents))))))
  1058. ;;;; Keyword
  1059. (defun org-e-ascii-keyword (keyword contents info)
  1060. "Transcode a KEYWORD element from Org to ASCII.
  1061. CONTENTS is nil. INFO is a plist holding contextual
  1062. information."
  1063. (let ((key (org-element-property :key keyword))
  1064. (value (org-element-property :value keyword)))
  1065. (cond
  1066. ((string= key "ASCII") value)
  1067. ((string= key "TOC")
  1068. (let ((value (downcase value)))
  1069. (cond
  1070. ((string-match "\\<headlines\\>" value)
  1071. (let ((depth (or (and (string-match "[0-9]+" value)
  1072. (string-to-number (match-string 0 value)))
  1073. (plist-get info :with-toc))))
  1074. (org-e-ascii--build-toc
  1075. info (and (wholenump depth) depth) keyword)))
  1076. ((string= "tables" value)
  1077. (org-e-ascii--list-tables keyword info))
  1078. ((string= "listings" value)
  1079. (org-e-ascii--list-listings keyword info))))))))
  1080. ;;;; Latex Environment
  1081. (defun org-e-ascii-latex-environment (latex-environment contents info)
  1082. "Transcode a LATEX-ENVIRONMENT element from Org to ASCII.
  1083. CONTENTS is nil. INFO is a plist holding contextual
  1084. information."
  1085. (org-remove-indentation (org-element-property :value latex-environment)))
  1086. ;;;; Latex Fragment
  1087. (defun org-e-ascii-latex-fragment (latex-fragment contents info)
  1088. "Transcode a LATEX-FRAGMENT object from Org to ASCII.
  1089. CONTENTS is nil. INFO is a plist holding contextual
  1090. information."
  1091. (org-element-property :value latex-fragment))
  1092. ;;;; Line Break
  1093. (defun org-e-ascii-line-break (line-break contents info)
  1094. "Transcode a LINE-BREAK object from Org to ASCII.
  1095. CONTENTS is nil. INFO is a plist holding contextual
  1096. information." hard-newline)
  1097. ;;;; Link
  1098. (defun org-e-ascii-link (link desc info)
  1099. "Transcode a LINK object from Org to ASCII.
  1100. DESC is the description part of the link, or the empty string.
  1101. INFO is a plist holding contextual information."
  1102. (let ((raw-link (org-element-property :raw-link link))
  1103. (type (org-element-property :type link)))
  1104. (cond
  1105. ((string= type "coderef")
  1106. (let ((ref (org-element-property :path link)))
  1107. (format (org-export-get-coderef-format ref desc)
  1108. (org-export-resolve-coderef ref info))))
  1109. ;; Do not apply a special syntax on radio links. Though, use
  1110. ;; transcoded target's contents as output.
  1111. ((string= type "radio")
  1112. (let ((destination (org-export-resolve-radio-link link info)))
  1113. (when destination
  1114. (org-export-data (org-element-contents destination) info))))
  1115. ;; Do not apply a special syntax on fuzzy links pointing to
  1116. ;; targets.
  1117. ((string= type "fuzzy")
  1118. (let ((destination (org-export-resolve-fuzzy-link link info)))
  1119. ;; Ignore invisible "#+TARGET: path".
  1120. (unless (eq (org-element-type destination) 'keyword)
  1121. (if (org-string-nw-p desc) desc
  1122. (when destination
  1123. (let ((number
  1124. (org-export-get-ordinal
  1125. destination info nil 'org-e-ascii--has-caption-p)))
  1126. (when number
  1127. (if (atom number) (number-to-string number)
  1128. (mapconcat 'number-to-string number ".")))))))))
  1129. (t
  1130. (if (not (org-string-nw-p desc)) (format "[%s]" raw-link)
  1131. (concat
  1132. (format "[%s]" desc)
  1133. (unless org-e-ascii-links-to-notes (format " (%s)" raw-link))))))))
  1134. ;;;; Macro
  1135. (defun org-e-ascii-macro (macro contents info)
  1136. "Transcode a MACRO element from Org to ASCII.
  1137. CONTENTS is nil. INFO is a plist holding contextual
  1138. information."
  1139. (org-export-expand-macro macro info))
  1140. ;;;; Paragraph
  1141. (defun org-e-ascii-paragraph (paragraph contents info)
  1142. "Transcode a PARAGRAPH element from Org to ASCII.
  1143. CONTENTS is the contents of the paragraph, as a string. INFO is
  1144. the plist used as a communication channel."
  1145. (org-e-ascii--fill-string
  1146. contents
  1147. (org-e-ascii--current-text-width paragraph info) info))
  1148. ;;;; Plain List
  1149. (defun org-e-ascii-plain-list (plain-list contents info)
  1150. "Transcode a PLAIN-LIST element from Org to ASCII.
  1151. CONTENTS is the contents of the list. INFO is a plist holding
  1152. contextual information."
  1153. contents)
  1154. ;;;; Plain Text
  1155. (defun org-e-ascii-plain-text (text info)
  1156. "Transcode a TEXT string from Org to ASCII.
  1157. INFO is a plist used as a communication channel."
  1158. (if (not (and (eq (plist-get info :ascii-charset) 'utf-8)
  1159. (plist-get info :with-special-strings)))
  1160. text
  1161. ;; Usual replacements in utf-8 with proper option set.
  1162. (replace-regexp-in-string
  1163. "\\.\\.\\." "…"
  1164. (replace-regexp-in-string
  1165. "--" "–"
  1166. (replace-regexp-in-string "---" "—" text)))))
  1167. ;;;; Planning
  1168. (defun org-e-ascii-planning (planning contents info)
  1169. "Transcode a PLANNING element from Org to ASCII.
  1170. CONTENTS is nil. INFO is a plist used as a communication
  1171. channel."
  1172. (mapconcat
  1173. 'identity
  1174. (delq nil
  1175. (list (let ((closed (org-element-property :closed planning)))
  1176. (when closed (concat org-closed-string " "
  1177. (org-translate-time closed))))
  1178. (let ((deadline (org-element-property :deadline planning)))
  1179. (when deadline (concat org-deadline-string " "
  1180. (org-translate-time deadline))))
  1181. (let ((scheduled (org-element-property :scheduled planning)))
  1182. (when scheduled (concat org-scheduled-string " "
  1183. (org-translate-time scheduled))))))
  1184. " "))
  1185. ;;;; Property Drawer
  1186. ;;
  1187. ;; Property drawers are ignored.
  1188. ;;;; Quote Block
  1189. (defun org-e-ascii-quote-block (quote-block contents info)
  1190. "Transcode a QUOTE-BLOCK element from Org to ASCII.
  1191. CONTENTS holds the contents of the block. INFO is a plist
  1192. holding contextual information."
  1193. (let ((width (org-e-ascii--current-text-width quote-block info)))
  1194. (org-e-ascii--indent-string
  1195. (org-remove-indentation
  1196. (org-e-ascii--fill-string contents width info))
  1197. org-e-ascii-quote-margin)))
  1198. ;;;; Quote Section
  1199. (defun org-e-ascii-quote-section (quote-section contents info)
  1200. "Transcode a QUOTE-SECTION element from Org to ASCII.
  1201. CONTENTS is nil. INFO is a plist holding contextual information."
  1202. (let ((width (org-e-ascii--current-text-width quote-section info))
  1203. (value
  1204. (org-export-data
  1205. (org-remove-indentation (org-element-property :value quote-section))
  1206. info)))
  1207. (org-e-ascii--indent-string
  1208. value
  1209. (+ org-e-ascii-quote-margin
  1210. ;; Don't apply inner margin if parent headline is low level.
  1211. (let ((headline (org-export-get-parent-headline quote-section)))
  1212. (if (org-export-low-level-p headline info) 0
  1213. org-e-ascii-inner-margin))))))
  1214. ;;;; Radio Target
  1215. (defun org-e-ascii-radio-target (radio-target contents info)
  1216. "Transcode a RADIO-TARGET object from Org to ASCII.
  1217. CONTENTS is the contents of the target. INFO is a plist holding
  1218. contextual information."
  1219. contents)
  1220. ;;;; Section
  1221. (defun org-e-ascii-section (section contents info)
  1222. "Transcode a SECTION element from Org to ASCII.
  1223. CONTENTS is the contents of the section. INFO is a plist holding
  1224. contextual information."
  1225. (org-e-ascii--indent-string
  1226. (concat
  1227. contents
  1228. (when org-e-ascii-links-to-notes
  1229. ;; Add list of links at the end of SECTION.
  1230. (let ((links (org-e-ascii--describe-links
  1231. (org-e-ascii--unique-links section info)
  1232. (org-e-ascii--current-text-width section info) info)))
  1233. ;; Separate list of links and section contents.
  1234. (when (org-string-nw-p links) (concat "\n\n" links)))))
  1235. ;; Do not apply inner margin if parent headline is low level.
  1236. (let ((headline (org-export-get-parent-headline section)))
  1237. (if (or (not headline) (org-export-low-level-p headline info)) 0
  1238. org-e-ascii-inner-margin))))
  1239. ;;;; Special Block
  1240. (defun org-e-ascii-special-block (special-block contents info)
  1241. "Transcode a SPECIAL-BLOCK element from Org to ASCII.
  1242. CONTENTS holds the contents of the block. INFO is a plist
  1243. holding contextual information."
  1244. contents)
  1245. ;;;; Src Block
  1246. (defun org-e-ascii-src-block (src-block contents info)
  1247. "Transcode a SRC-BLOCK element from Org to ASCII.
  1248. CONTENTS holds the contents of the item. INFO is a plist holding
  1249. contextual information."
  1250. (let ((caption (org-e-ascii--build-caption src-block info)))
  1251. (concat
  1252. (when (and caption org-e-ascii-caption-above) (concat caption "\n"))
  1253. (org-e-ascii--box-string
  1254. (org-export-format-code-default src-block info) info)
  1255. (when (and caption (not org-e-ascii-caption-above))
  1256. (concat "\n" caption)))))
  1257. ;;;; Statistics Cookie
  1258. (defun org-e-ascii-statistics-cookie (statistics-cookie contents info)
  1259. "Transcode a STATISTICS-COOKIE object from Org to ASCII.
  1260. CONTENTS is nil. INFO is a plist holding contextual information."
  1261. (org-element-property :value statistics-cookie))
  1262. ;;;; Subscript
  1263. (defun org-e-ascii-subscript (subscript contents info)
  1264. "Transcode a SUBSCRIPT object from Org to ASCII.
  1265. CONTENTS is the contents of the object. INFO is a plist holding
  1266. contextual information."
  1267. (if (org-element-property :use-brackets-p subscript)
  1268. (format "_{%s}" contents)
  1269. (format "_%s" contents)))
  1270. ;;;; Superscript
  1271. (defun org-e-ascii-superscript (superscript contents info)
  1272. "Transcode a SUPERSCRIPT object from Org to ASCII.
  1273. CONTENTS is the contents of the object. INFO is a plist holding
  1274. contextual information."
  1275. (if (org-element-property :use-brackets-p superscript)
  1276. (format "_{%s}" contents)
  1277. (format "_%s" contents)))
  1278. ;;;; Strike-through
  1279. (defun org-e-ascii-strike-through (strike-through contents info)
  1280. "Transcode STRIKE-THROUGH from Org to ASCII.
  1281. CONTENTS is text with strike-through markup. INFO is a plist
  1282. holding contextual information."
  1283. (format "+%s+" contents))
  1284. ;;;; Table
  1285. (defun org-e-ascii-table (table contents info)
  1286. "Transcode a TABLE element from Org to ASCII.
  1287. CONTENTS is the contents of the table. INFO is a plist holding
  1288. contextual information."
  1289. (let ((caption (org-e-ascii--build-caption table info)))
  1290. (concat
  1291. ;; Possibly add a caption string above.
  1292. (when (and caption org-e-ascii-caption-above) (concat caption "\n"))
  1293. ;; Insert table. Note: "table.el" tables are left unmodified.
  1294. (if (eq (org-element-property :type table) 'org) contents
  1295. (org-remove-indentation (org-element-property :value table)))
  1296. ;; Possible add a caption string below.
  1297. (when (and caption (not org-e-ascii-caption-above))
  1298. (concat "\n" caption)))))
  1299. ;;;; Table Cell
  1300. (defun org-e-ascii--table-cell-width (table-cell info)
  1301. "Return width of TABLE-CELL.
  1302. INFO is a plist used as a communication channel.
  1303. Width of a cell is determined either by a width cookie in the
  1304. same column as the cell, or by the maximum cell's length in that
  1305. column.
  1306. When `org-e-ascii-table-widen-columns' is non-nil, width cookies
  1307. are ignored."
  1308. (or (and (not org-e-ascii-table-widen-columns)
  1309. (org-export-table-cell-width table-cell info))
  1310. (let* ((max-width 0)
  1311. (table (org-export-get-parent-table table-cell))
  1312. (specialp (org-export-table-has-special-column-p table))
  1313. (col (cdr (org-export-table-cell-address table-cell info))))
  1314. (org-element-map
  1315. table 'table-row
  1316. (lambda (row)
  1317. (setq max-width
  1318. (max (length
  1319. (org-export-data
  1320. (org-element-contents
  1321. (elt (if specialp (cdr (org-element-contents row))
  1322. (org-element-contents row))
  1323. col))
  1324. info))
  1325. max-width)))
  1326. info)
  1327. max-width)))
  1328. (defun org-e-ascii-table-cell (table-cell contents info)
  1329. "Transcode a TABLE-CELL object from Org to ASCII.
  1330. CONTENTS is the cell contents. INFO is a plist used as
  1331. a communication channel."
  1332. ;; Determine column width. When `org-e-ascii-table-widen-columns'
  1333. ;; is nil and some width cookie has set it, use that value.
  1334. ;; Otherwise, compute the maximum width among transcoded data of
  1335. ;; each cell in the column.
  1336. (let ((width (org-e-ascii--table-cell-width table-cell info)))
  1337. ;; When contents are too large, truncate them.
  1338. (unless (or org-e-ascii-table-widen-columns (<= (length contents) width))
  1339. (setq contents (concat (substring contents 0 (- width 2)) "=>")))
  1340. ;; Align contents correctly within the cell.
  1341. (let* ((indent-tabs-mode nil)
  1342. (data
  1343. (when contents
  1344. (org-e-ascii--justify-string
  1345. contents width
  1346. (org-export-table-cell-alignment table-cell info)))))
  1347. (setq contents (concat data (make-string (- width (length data)) ? ))))
  1348. ;; Return cell.
  1349. (concat (format " %s " contents)
  1350. (when (memq 'right (org-export-table-cell-borders table-cell info))
  1351. (if (eq (plist-get info :ascii-charset) 'utf-8) "│" "|")))))
  1352. ;;;; Table Row
  1353. (defun org-e-ascii-table-row (table-row contents info)
  1354. "Transcode a TABLE-ROW element from Org to ASCII.
  1355. CONTENTS is the row contents. INFO is a plist used as
  1356. a communication channel."
  1357. (when (eq (org-element-property :type table-row) 'standard)
  1358. (let ((build-hline
  1359. (function
  1360. (lambda (lcorner horiz vert rcorner)
  1361. (concat
  1362. (apply
  1363. 'concat
  1364. (org-element-map
  1365. table-row 'table-cell
  1366. (lambda (cell)
  1367. (let ((width (org-e-ascii--table-cell-width cell info))
  1368. (borders (org-export-table-cell-borders cell info)))
  1369. (concat
  1370. ;; In order to know if CELL starts the row, do
  1371. ;; not compare it with the first cell in the row
  1372. ;; as there might be a special column. Instead,
  1373. ;; compare it with the first exportable cell,
  1374. ;; obtained with `org-element-map'.
  1375. (when (and (memq 'left borders)
  1376. (eq (org-element-map
  1377. table-row 'table-cell 'identity info t)
  1378. cell))
  1379. lcorner)
  1380. (make-string (+ 2 width) (string-to-char horiz))
  1381. (cond
  1382. ((not (memq 'right borders)) nil)
  1383. ((eq (car (last (org-element-contents table-row))) cell)
  1384. rcorner)
  1385. (t vert)))))
  1386. info)) "\n"))))
  1387. (utf8p (eq (plist-get info :ascii-charset) 'utf-8))
  1388. (borders (org-export-table-cell-borders
  1389. (org-element-map table-row 'table-cell 'identity info t)
  1390. info)))
  1391. (concat (cond
  1392. ((and (memq 'top borders) (or utf8p (memq 'above borders)))
  1393. (if utf8p (funcall build-hline "┍" "━" "┯" "┑")
  1394. (funcall build-hline "+" "-" "+" "+")))
  1395. ((memq 'above borders)
  1396. (if utf8p (funcall build-hline "├" "─" "┼" "┤")
  1397. (funcall build-hline "+" "-" "+" "+"))))
  1398. (when (memq 'left borders) (if utf8p "│" "|"))
  1399. contents "\n"
  1400. (when (and (memq 'bottom borders) (or utf8p (memq 'below borders)))
  1401. (if utf8p (funcall build-hline "┕" "━" "┷" "┙")
  1402. (funcall build-hline "+" "-" "+" "+")))))))
  1403. ;;;; Target
  1404. ;; Targets are invisible.
  1405. ;;;; Timestamp
  1406. (defun org-e-ascii-timestamp (timestamp contents info)
  1407. "Transcode a TIMESTAMP object from Org to ASCII.
  1408. CONTENTS is nil. INFO is a plist holding contextual information."
  1409. (let ((value (org-translate-time (org-element-property :value timestamp)))
  1410. (range-end
  1411. (org-translate-time (org-element-property :range-end timestamp)))
  1412. (utf8p (eq (plist-get info :ascii-charset) 'utf-8)))
  1413. (concat value
  1414. (when range-end (concat (if utf8p "–" "--") range-end)))))
  1415. ;;;; Underline
  1416. (defun org-e-ascii-underline (underline contents info)
  1417. "Transcode UNDERLINE from Org to ASCII.
  1418. CONTENTS is the text with underline markup. INFO is a plist
  1419. holding contextual information."
  1420. (format "_%s_" contents))
  1421. ;;;; Verbatim
  1422. (defun org-e-ascii-verbatim (verbatim contents info)
  1423. "Return a VERBATIM object from Org to ASCII.
  1424. CONTENTS is nil. INFO is a plist holding contextual information."
  1425. (format org-e-ascii-verbatim-format
  1426. (org-element-property :value verbatim)))
  1427. ;;;; Verse Block
  1428. (defun org-e-ascii-verse-block (verse-block contents info)
  1429. "Transcode a VERSE-BLOCK element from Org to ASCII.
  1430. CONTENTS is verse block contents. INFO is a plist holding
  1431. contextual information."
  1432. (let ((verse-width (org-e-ascii--current-text-width verse-block info)))
  1433. (org-e-ascii--indent-string
  1434. (org-e-ascii--justify-string contents verse-width 'left)
  1435. org-e-ascii-quote-margin)))
  1436. ;;; Filter
  1437. (defun org-e-ascii-filter-headline-blank-lines (headline back-end info)
  1438. "Filter controlling number of blank lines after an headline.
  1439. HEADLINE is a string representing a transcoded headline.
  1440. BACK-END is symbol specifying back-end used for export. INFO is
  1441. plist containing the communication channel.
  1442. This function only applies to `e-ascii' back-end. See
  1443. `org-e-ascii-headline-spacing' for information.
  1444. For any other back-end, HEADLINE is returned as-is."
  1445. (if (not (and (eq back-end 'e-ascii) org-e-ascii-headline-spacing)) headline
  1446. (let ((blanks (make-string (1+ (cdr org-e-ascii-headline-spacing)) ?\n)))
  1447. (replace-regexp-in-string "\n\\(?:\n[ \t]*\\)*\\'" blanks headline))))
  1448. ;;; Interactive function
  1449. ;;;###autoload
  1450. (defun org-e-ascii-export-as-ascii
  1451. (&optional subtreep visible-only body-only ext-plist)
  1452. "Export current buffer to a text buffer.
  1453. If narrowing is active in the current buffer, only export its
  1454. narrowed part.
  1455. If a region is active, export that region.
  1456. When optional argument SUBTREEP is non-nil, export the sub-tree
  1457. at point, extracting information from the headline properties
  1458. first.
  1459. When optional argument VISIBLE-ONLY is non-nil, don't export
  1460. contents of hidden elements.
  1461. When optional argument BODY-ONLY is non-nil, strip title, table
  1462. of contents and footnote definitions from output.
  1463. EXT-PLIST, when provided, is a property list with external
  1464. parameters overriding Org default settings, but still inferior to
  1465. file-local settings.
  1466. When optional argument PUB-DIR is set, use it as the publishing
  1467. directory.
  1468. Return output file's name."
  1469. (interactive)
  1470. (let ((outbuf (org-export-to-buffer
  1471. 'e-ascii "*Org E-ASCII Export*"
  1472. subtreep visible-only body-only ext-plist)))
  1473. (with-current-buffer outbuf (text-mode))
  1474. (when org-export-show-temporary-export-buffer
  1475. (switch-to-buffer-other-window outbuf))))
  1476. ;;;###autoload
  1477. (defun org-e-ascii-export-to-ascii
  1478. (&optional subtreep visible-only body-only ext-plist pub-dir)
  1479. "Export current buffer to a text file.
  1480. If narrowing is active in the current buffer, only export its
  1481. narrowed part.
  1482. If a region is active, export that region.
  1483. When optional argument SUBTREEP is non-nil, export the sub-tree
  1484. at point, extracting information from the headline properties
  1485. first.
  1486. When optional argument VISIBLE-ONLY is non-nil, don't export
  1487. contents of hidden elements.
  1488. When optional argument BODY-ONLY is non-nil, strip title, table
  1489. of contents and footnote definitions from output.
  1490. EXT-PLIST, when provided, is a property list with external
  1491. parameters overriding Org default settings, but still inferior to
  1492. file-local settings.
  1493. When optional argument PUB-DIR is set, use it as the publishing
  1494. directory.
  1495. Return output file's name."
  1496. (interactive)
  1497. (let ((outfile (org-export-output-file-name ".txt" subtreep pub-dir)))
  1498. (org-export-to-file
  1499. 'e-ascii outfile subtreep visible-only body-only ext-plist)))
  1500. (provide 'org-e-ascii)
  1501. ;;; org-e-ascii.el ends here