org-export: Fix bug with recursive file inclusion and relative paths
[org-mode/org-mode-NeilSmithlineMods.git] / EXPERIMENTAL / org-e-ascii.el
blobfbf8bd80a06557612bd449d9051da8327c578e1b
1 ;;; org-e-ascii.el --- ASCII Back-End For Org Export Engine
3 ;; Copyright (C) 2012 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; This program is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
21 ;;; Commentary:
23 ;; This library implements an ASCII back-end for Org generic exporter.
25 ;; To test it, run
27 ;; M-: (org-export-to-buffer 'e-ascii "*Test e-ASCII*") RET
29 ;; in an Org mode buffer then switch to that buffer to see the ASCII
30 ;; export. See contrib/lisp/org-export.el for more details on how
31 ;; this exporter works.
33 ;;; Code:
35 (eval-when-compile (require 'cl))
37 (declare-function org-element-contents "org-element" (element))
38 (declare-function org-element-property "org-element" (property element))
39 (declare-function org-element-normalize-string "org-element" (s))
40 (declare-function org-element-map "org-element"
41 (data types fun &optional info first-match))
42 (declare-function org-element-time-stamp-interpreter
43 "org-element" (time-stamp contents))
45 (declare-function org-export-clean-table "org-export" (table specialp))
46 (declare-function org-export-collect-footnote-definitions
47 "org-export" (data info))
48 (declare-function org-export-collect-headlines "org-export" (info &optional n))
49 (declare-function org-export-collect-listings "org-export" (info))
50 (declare-function org-export-collect-tables "org-export" (info))
51 (declare-function org-export-data "org-export" (data backend info))
52 (declare-function org-export-expand-macro "org-export" (macro info))
53 (declare-function org-export-get-coderef-format "org-export" (path desc))
54 (declare-function org-export-get-footnote-number "org-export" (footnote info))
55 (declare-function org-export-get-headline-number "org-export" (headline info))
56 (declare-function org-export-get-ordinal "org-export"
57 (element info &optional types within-section predicate))
58 (declare-function org-export-get-parent-headline "org-export" (blob info))
59 (declare-function org-export-get-relative-level "org-export" (headline info))
60 (declare-function org-export-handle-code
61 "org-export" (element info &optional num-fmt ref-fmt delayed))
62 (declare-function org-export-included-file "org-export" (keyword backend info))
63 (declare-function org-export-low-level-p "org-export" (headline info))
64 (declare-function org-export-output-file-name "org-export"
65 (extension &optional subtreep pub-dir))
66 (declare-function org-export-resolve-coderef "org-export" (ref info))
67 (declare-function org-export-resolve-fuzzy-link "org-export" (link info))
68 (declare-function org-export-resolve-id-link "org-export" (link info))
69 (declare-function org-export-resolve-ref-link "org-export" (link info))
70 (declare-function org-export-secondary-string
71 "org-export" (secondary backend info))
72 (declare-function org-export-table-format-info "org-export" (table))
73 (declare-function
74 org-export-to-file "org-export"
75 (backend file &optional subtreep visible-only body-only ext-plist))
79 ;;; Internal Variables
81 ;; The following setting won't allow to modify preferred charset
82 ;; through a buffer keyword or an option item, but, since the property
83 ;; will appear in communication channel nonetheless, it allows to
84 ;; override `org-e-ascii-charset' variable on the fly by the ext-plist
85 ;; mechanism.
87 ;; We also install a filter for headlines and sections, in order to
88 ;; control blank lines separating them in output string.
90 (defconst org-e-ascii-option-alist
91 '((:ascii-charset nil nil org-e-ascii-charset)
93 "Alist between ASCII export properties and ways to set them.
94 See `org-export-option-alist' for more information on the
95 structure or the values.")
97 (defconst org-e-ascii-filters-alist
98 '((:filter-headline . org-e-ascii-filter-headline-blank-lines)
99 (:filter-section . org-e-ascii-filter-headline-blank-lines))
100 "Alist between filters keywords and back-end specific filters.
101 See `org-export-filters-alist' for more information.")
103 (defconst org-e-ascii-dictionary
104 '(("Footnotes\n"
105 ("en"
106 :ascii "Footnotes\n"
107 :latin1 "Footnotes\n"
108 :utf-8 "Footnotes\n")
109 ("fr"
110 :ascii "Notes de bas de page\n"
111 :latin1 "Notes de bas de page\n"
112 :utf-8 "Notes de bas de page\n"))
113 ("Listing %d: %s"
114 ("en"
115 :ascii "Listing %d: %s"
116 :latin1 "Listing %d: %s"
117 :utf-8 "Listing %d: %s")
118 ("fr"
119 :ascii "Programme %d : %s"
120 :latin1 "Programme %d : %s"
121 :utf-8 "Programme nº %d : %s"))
122 ("List Of Listings\n"
123 ("en"
124 :ascii "List Of Listings\n"
125 :latin1 "List Of Listings\n"
126 :utf-8 "List Of Listings\n")
127 ("fr"
128 :ascii "Liste des programmes\n"
129 :latin1 "Liste des programmes\n"
130 :utf-8 "Liste des programmes\n"))
131 ("List Of Tables\n"
132 ("en"
133 :ascii "List Of Tables\n"
134 :latin1 "List Of Tables\n"
135 :utf-8 "List Of Tables\n")
136 ("fr"
137 :ascii "Liste des tableaux\n"
138 :latin1 "Liste des tableaux\n"
139 :utf-8 "Liste des tableaux\n"))
140 ("Listing %d: "
141 ("en"
142 :ascii "Listing %d: "
143 :latin1 "Listing %d: "
144 :utf-8 "Listing %d: ")
145 ("fr"
146 :ascii "Programme %d : "
147 :latin1 "Programme %d : "
148 :utf-8 "Programme nº %d : "))
149 ("Table Of Contents\n"
150 ("en"
151 :ascii "Table Of Contents\n"
152 :latin1 "Table Of Contents\n"
153 :utf-8 "Table Of Contents\n")
154 ("fr"
155 :ascii "Sommaire\n"
156 :latin1 "Table des matières\n"
157 :utf-8 "Table des matières\n"))
158 ("Table %d: %s"
159 ("en"
160 :ascii "Table %d: %s"
161 :latin1 "Table %d: %s"
162 :utf-8 "Table %d: %s")
163 ("fr"
164 :ascii "Tableau %d : %s"
165 :latin1 "Tableau %d : %s"
166 :utf-8 "Tableau nº %d : %s"))
167 ("See section %s"
168 ("en"
169 :ascii "See section %s"
170 :latin1 "See section %s"
171 :utf-8 "See section %s")
172 ("fr"
173 :ascii "cf. section %s"
174 :latin1 "cf. section %s"
175 :utf-8 "cf. section %s"))
176 ("Table %d: "
177 ("en"
178 :ascii "Table %d: "
179 :latin1 "Table %d: "
180 :utf-8 "Table %d: ")
181 ("fr"
182 :ascii "Tableau %d : "
183 :latin1 "Tableau %d : "
184 :utf-8 "Tableau nº %d : "))
185 ("Unknown reference"
186 ("en"
187 :ascii "Unknown reference"
188 :latin1 "Unknown reference"
189 :utf-8 "Unknown reference")
190 ("fr"
191 :ascii "Destination inconnue"
192 :latin1 "Référence inconnue"
193 :utf-8 "Référence inconnue")))
194 "Dictionary for ASCII back-end.
196 Alist whose car is the string to translate and cdr is an alist
197 whose car is the language string and cdr is a plist whose
198 properties are possible charsets and value the translated term.
200 It is used as a database for `org-e-ascii--translate'.")
204 ;;; User Configurable Variables
206 (defgroup org-export-e-ascii nil
207 "Options for exporting Org mode files to ASCII."
208 :tag "Org Export ASCII"
209 :group 'org-export)
211 (defcustom org-e-ascii-text-width 72
212 "Maximum width of exported text.
213 This number includes margin size, as set in
214 `org-e-ascii-global-margin'."
215 :group 'org-export-e-ascii
216 :type 'integer)
218 (defcustom org-e-ascii-global-margin 0
219 "Width of the left margin, in number of characters."
220 :group 'org-export-e-ascii
221 :type 'integer)
223 (defcustom org-e-ascii-inner-margin 2
224 "Width of the inner margin, in number of characters.
225 Inner margin is applied between each headline."
226 :group 'org-export-e-ascii
227 :type 'integer)
229 (defcustom org-e-ascii-quote-margin 6
230 "Width of margin used for quoting text, in characters.
231 This margin is applied on both sides of the text."
232 :group 'org-export-e-ascii
233 :type 'integer)
235 (defcustom org-e-ascii-inlinetask-width 30
236 "Width of inline tasks, in number of characters.
237 This number ignores any margin."
238 :group 'org-export-e-ascii
239 :type 'integer)
241 (defcustom org-e-ascii-headline-spacing '(1 . 2)
242 "Number of blank lines inserted around headlines.
244 This variable can be set to a cons cell. In that case, its car
245 represents the number of blank lines present before headline
246 contents whereas its cdr reflects the number of blank lines after
247 contents.
249 A nil value replicates the number of blank lines found in the
250 original Org buffer at the same place."
251 :group 'org-export-e-ascii
252 :type '(choice
253 (const :tag "Replicate original spacing" nil)
254 (cons :tag "Set an uniform spacing"
255 (integer :tag "Number of blank lines before contents")
256 (integer :tag "Number of blank lines after contents"))))
258 (defcustom org-e-ascii-charset 'ascii
259 "The charset allowed to represent various elements and objects.
260 Possible values are:
261 `ascii' Only use plain ASCII characters
262 `latin1' Include Latin-1 characters
263 `utf-8' Use all UTF-8 characters"
264 :group 'org-export-e-ascii
265 :type '(choice
266 (const :tag "ASCII" ascii)
267 (const :tag "Latin-1" latin1)
268 (const :tag "UTF-8" utf-8)))
270 (defcustom org-e-ascii-underline '((ascii ?= ?~ ?-)
271 (latin1 ?= ?~ ?-)
272 (utf-8 ?═ ?─ ?╌ ?┄ ?┈))
273 "Characters for underlining headings in ASCII export.
275 Alist whose key is a symbol among `ascii', `latin1' and `utf-8'
276 and whose value is a list of characters.
278 For each supported charset, this variable associates a sequence
279 of underline characters. In a sequence, the characters will be
280 used in order for headlines level 1, 2, ... If no character is
281 available for a given level, the headline won't be underlined."
282 :group 'org-export-e-ascii
283 :type '(list
284 (cons :tag "Underline characters sequence"
285 (const :tag "ASCII charset" ascii)
286 (repeat character))
287 (cons :tag "Underline characters sequence"
288 (const :tag "Latin-1 charset" latin1)
289 (repeat character))
290 (cons :tag "Underline characters sequence"
291 (const :tag "UTF-8 charset" utf-8)
292 (repeat character))))
294 (defcustom org-e-ascii-bullets '((ascii ?* ?+ ?-)
295 (latin1 ?§ ?¶)
296 (utf-8 ?◊))
297 "Bullet characters for headlines converted to lists in ASCII export.
299 Alist whose key is a symbol among `ascii', `latin1' and `utf-8'
300 and whose value is a list of characters.
302 The first character is used for the first level considered as low
303 level, and so on. If there are more levels than characters given
304 here, the list will be repeated.
306 Note that this variable doesn't affect plain lists
307 representation."
308 :group 'org-export-e-ascii
309 :type '(list
310 (cons :tag "Bullet characters for low level headlines"
311 (const :tag "ASCII charset" ascii)
312 (repeat character))
313 (cons :tag "Bullet characters for low level headlines"
314 (const :tag "Latin-1 charset" latin1)
315 (repeat character))
316 (cons :tag "Bullet characters for low level headlines"
317 (const :tag "UTF-8 charset" utf-8)
318 (repeat character))))
320 (defcustom org-e-ascii-links-to-notes t
321 "Non-nil means convert links to notes before the next headline.
322 When nil, the link will be exported in place. If the line
323 becomes long in this way, it will be wrapped."
324 :group 'org-export-e-ascii
325 :type 'boolean)
327 (defcustom org-e-ascii-table-keep-all-vertical-lines nil
328 "Non-nil means keep all vertical lines in ASCII tables.
329 When nil, vertical lines will be removed except for those needed
330 for column grouping."
331 :group 'org-export-e-ascii
332 :type 'boolean)
334 (defcustom org-e-ascii-table-widen-columns t
335 "Non-nil means widen narrowed columns for export.
336 When nil, narrowed columns will look in ASCII export just like in
337 Org mode, i.e. with \"=>\" as ellipsis."
338 :group 'org-export-e-ascii
339 :type 'boolean)
341 (defcustom org-e-ascii-caption-above nil
342 "When non-nil, place caption string before the element.
343 Otherwise, place it right after it."
344 :group 'org-export-e-ascii
345 :type 'boolean)
347 (defcustom org-e-ascii-verbatim-format "`%s'"
348 "Format string used for verbatim text and inline code."
349 :group 'org-export-e-ascii
350 :type 'string)
352 (defcustom org-e-ascii-format-drawer-function nil
353 "Function called to format a drawer in ASCII.
355 The function must accept two parameters:
356 NAME the drawer name, like \"LOGBOOK\"
357 CONTENTS the contents of the drawer.
358 WIDTH the text width within the drawer.
360 The function should return either the string to be exported or
361 nil to ignore the drawer.
363 For example, the variable could be set to the following function
364 in order to mimic default behaviour:
366 \(defun org-e-ascii-format-drawer-default \(name contents width\)
367 \"Format a drawer element for ASCII export.\"
368 contents\)"
369 :group 'org-export-e-ascii
370 :type 'function)
372 (defcustom org-e-ascii-format-inlinetask-function nil
373 "Function called to format an inlinetask in ASCII.
375 The function must accept six parameters:
376 TODO the todo keyword, as a string
377 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
378 PRIORITY the inlinetask priority, as a string
379 NAME the inlinetask name, as a string.
380 TAGS the inlinetask tags, as a string.
381 CONTENTS the contents of the inlinetask, as a string.
383 The function should return either the string to be exported or
384 nil to ignore the inline task.
386 For example, the variable could be set to the following function
387 in order to mimic default behaviour:
389 \(defun org-e-ascii-format-inlinetask-default
390 \(todo type priority name tags contents\)
391 \"Format an inline task element for ASCII export.\"
392 \(let* \(\(utf8p \(eq \(plist-get info :ascii-charset\) 'utf-8\)\)
393 \(width org-e-ascii-inlinetask-width\)
394 \(org-e-ascii--indent-string
395 \(concat
396 ;; Top line, with an additional blank line if not in UTF-8.
397 \(make-string width \(if utf8p ?━ ?_\)\) \"\\n\"
398 \(unless utf8p \(concat \(make-string width ? \) \"\\n\"\)\)
399 ;; Add title. Fill it if wider than inlinetask.
400 \(let \(\(title \(org-e-ascii--build-title inlinetask info width\)\)\)
401 \(if \(<= \(length title\) width\) title
402 \(org-e-ascii--fill-string title width info\)\)\)
403 \"\\n\"
404 ;; If CONTENTS is not empty, insert it along with
405 ;; a separator.
406 \(when \(org-string-nw-p contents\)
407 \(concat \(make-string width \(if utf8p ?─ ?-\)\) \"\\n\" contents\)\)
408 ;; Bottom line.
409 \(make-string width \(if utf8p ?━ ?_\)\)\)
410 ;; Flush the inlinetask to the right.
411 \(- \(plist-get info :ascii-width\)
412 \(plist-get info :ascii-margin\)
413 \(plist-get info :ascii-inner-margin\)
414 \(org-e-ascii--current-text-width inlinetask info\)\)"
415 :group 'org-export-e-ascii
416 :type 'function)
420 ;;; Internal Functions
422 ;; Internal functions fall into three categories.
424 ;; The first one is about text formatting. The core function is
425 ;; `org-e-ascii--current-text-width', which determines the current
426 ;; text width allowed to a given element. In other words, it helps
427 ;; keeping each line width within maximum text width defined in
428 ;; `org-e-ascii-text-width'. Once this information is known,
429 ;; `org-e-ascii--fill-string', `org-e-ascii--justify-string',
430 ;; `org-e-ascii--box-string' and `org-e-ascii--indent-string' can
431 ;; operate on a given output string.
433 ;; The second category contains functions handling elements listings,
434 ;; triggered by "#+TOC:" keyword. As such, `org-e-ascii--build-toc'
435 ;; returns a complete table of contents, `org-e-ascii--list-listings'
436 ;; returns a list of referenceable src-block elements, and
437 ;; `org-e-ascii--list-tables' does the same for table elements.
439 ;; The third category includes general helper functions.
440 ;; `org-e-ascii--build-title' creates the title for a given headline
441 ;; or inlinetask element. `org-e-ascii--build-caption' returns the
442 ;; caption string associated to a table or a src-block.
443 ;; `org-e-ascii--describe-links' creates notes about links for
444 ;; insertion at the end of a section. It uses
445 ;; `org-e-ascii--unique-links' to get the list of links to describe.
446 ;; Eventually, `org-e-ascii--translate' reads `org-e-ascii-dictionary'
447 ;; to internationalize output.
450 (defun org-e-ascii--fill-string (s text-width info &optional justify)
451 "Fill a string with specified text-width and return it.
453 S is the string being filled. TEXT-WIDTH is an integer
454 specifying maximum length of a line. INFO is the plist used as
455 a communication channel.
457 Optional argument JUSTIFY can specify any type of justification
458 among `left', `center', `right' or `full'. A nil value is
459 equivalent to `left'. For a justification that doesn't also fill
460 string, see `org-e-ascii--justify-string'.
462 Return nil if S isn't a string."
463 ;; Don't fill paragraph when break should be preserved.
464 (cond ((not (stringp s)) nil)
465 ((plist-get info :preserve-breaks) s)
466 (t (with-temp-buffer
467 (let ((fill-column text-width)
468 (use-hard-newlines t))
469 (insert s)
470 (fill-region (point-min) (point-max) justify))
471 (buffer-string)))))
473 (defun org-e-ascii--justify-string (s text-width how)
474 "Justify string S.
475 TEXT-WIDTH is an integer specifying maximum length of a line.
476 HOW determines the type of justification: it can be `left',
477 `right', `full' or `center'."
478 (with-temp-buffer
479 (insert s)
480 (goto-char (point-min))
481 (let ((fill-column text-width))
482 (while (< (point) (point-max))
483 (justify-current-line how)
484 (forward-line)))
485 (buffer-string)))
487 (defun org-e-ascii--indent-string (s width)
488 "Indent string S by WIDTH white spaces.
489 Empty lines are not indented."
490 (when (stringp s)
491 (replace-regexp-in-string
492 "\\(^\\)\\(?:.*\\S-\\)" (make-string width ? ) s nil nil 1)))
494 (defun org-e-ascii--box-string (s info)
495 "Return string S with a partial box to its left.
496 INFO is a plist used as a communicaton channel."
497 (let ((utf8p (eq (plist-get info :ascii-charset) 'utf-8)))
498 (format (if utf8p "╭────\n%s\n╰────" ",----\n%s\n`----")
499 (replace-regexp-in-string
500 "^" (if utf8p "│ " "| ")
501 ;; Remove last newline character.
502 (replace-regexp-in-string "\n[ \t]*\\'" "" s)))))
504 (defun org-e-ascii--current-text-width (element info)
505 "Return maximum text width for ELEMENT's contents.
506 INFO is a plist used as a communication channel."
507 (case (org-element-type element)
508 ;; Elements with an absolute width: `headline' and `inlinetask'.
509 (inlinetask org-e-ascii-inlinetask-width)
510 ('headline
511 (- org-e-ascii-text-width
512 (let ((low-level-rank (org-export-low-level-p element info)))
513 (if low-level-rank (* low-level-rank 2) org-e-ascii-global-margin))))
514 ;; Elements with a relative width: store maximum text width in
515 ;; TOTAL-WIDTH.
516 (otherwise
517 (let* ((genealogy (cons element (org-export-get-genealogy element info)))
518 ;; Total width is determined by the presence, or not, of an
519 ;; inline task among ELEMENT parents.
520 (total-width
521 (if (loop for parent in genealogy
522 thereis (eq (org-element-type parent) 'inlinetask))
523 org-e-ascii-inlinetask-width
524 ;; No inlinetask: Remove global margin from text width.
525 (- org-e-ascii-text-width
526 org-e-ascii-global-margin
527 (let ((parent (org-export-get-parent-headline element info)))
528 ;; Inner margin doesn't apply to text before first
529 ;; headline.
530 (if (not parent) 0
531 (let ((low-level-rank
532 (org-export-low-level-p parent info)))
533 ;; Inner margin doesn't apply to contents of
534 ;; low level headlines, since they've got their
535 ;; own indentation mechanism.
536 (if low-level-rank (* low-level-rank 2)
537 org-e-ascii-inner-margin))))))))
538 (- total-width
539 ;; Each `quote-block', `quote-section' and `verse-block' above
540 ;; narrows text width by twice the standard margin size.
541 (+ (* (loop for parent in genealogy
542 when (memq (org-element-type parent)
543 '(quote-block quote-section verse-block))
544 count parent)
545 2 org-e-ascii-quote-margin)
546 ;; Text width within a plain-list is restricted by
547 ;; indentation of current item. If that's the case,
548 ;; compute it with the help of `:structure' property from
549 ;; parent item, if any.
550 (let ((parent-item
551 (if (eq (org-element-type element) 'item) element
552 (loop for parent in genealogy
553 when (eq (org-element-type parent) 'item)
554 return parent))))
555 (if (not parent-item) 0
556 ;; Compute indentation offset of the current item,
557 ;; that is the sum of the difference between its
558 ;; indentation and the indentation of the top item in
559 ;; the list and current item bullet's length. Also
560 ;; remove tag length (for description lists) or bullet
561 ;; length.
562 (let ((struct (org-element-property :structure parent-item))
563 (beg-item (org-element-property :begin parent-item)))
564 (+ (- (org-list-get-ind beg-item struct)
565 (org-list-get-ind
566 (org-list-get-top-point struct) struct))
567 (length
568 (or (org-list-get-tag beg-item struct)
569 (org-list-get-bullet beg-item struct)))))))))))))
571 (defun org-e-ascii--build-title
572 (element info text-width &optional underline notags)
573 "Format ELEMENT title and return it.
575 ELEMENT is either an `headline' or `inlinetask' element. INFO is
576 a plist used as a communication channel. TEXT-WIDTH is an
577 integer representing the maximum length of a line.
579 When optional argument UNDERLINE is non-nil, underline title,
580 without the tags, according to `org-e-ascii-underline'
581 specifications.
583 if optional argument NOTAGS is nil, no tags will be added to the
584 title."
585 (let* ((headlinep (eq (org-element-type element) 'headline))
586 (numbers
587 ;; Numbering is specific to headlines.
588 (and headlinep
589 ;; Section numbering must be active, and headline's
590 ;; level should be above specified limit, if any.
591 (let ((sec-num (plist-get info :section-numbers)))
592 (if (not (wholenump sec-num)) sec-num
593 (<= (org-export-get-relative-level headline info) sec-num)))
594 ;; All tests passed: build numbering string.
595 (concat
596 (mapconcat
597 #'number-to-string
598 (org-export-get-headline-number element info) ".")
599 " ")))
600 (text (org-export-secondary-string
601 (org-element-property :title element) 'e-ascii info))
602 (todo
603 (and (plist-get info :with-todo-keywords)
604 (let ((todo (org-element-property :todo-keyword element)))
605 (and todo
606 (concat (org-export-secondary-string todo 'e-ascii info)
607 " ")))))
608 (tags (and (not notags)
609 (plist-get info :with-tags)
610 (org-element-property :tags element)))
611 (priority
612 (and (plist-get info :with-priority)
613 (concat (org-element-property :priority element) " ")))
614 (first-part (concat numbers todo priority text)))
615 (concat
616 first-part
617 ;; Align tags, if any.
618 (when tags
619 (format
620 (format " %%%ds"
621 (max (- text-width (1+ (length first-part))) (length tags)))
622 tags))
623 ;; Maybe underline text, if ELEMENT type is `headline' and an
624 ;; underline character has been defined.
625 (when (and underline headlinep)
626 (let ((under-char
627 (nth (1- (org-export-get-relative-level element info))
628 (cdr (assq (plist-get info :ascii-charset)
629 org-e-ascii-underline)))))
630 (and under-char
631 (concat "\n"
632 (make-string (length first-part) under-char))))))))
634 (defun org-e-ascii--build-caption (element info)
635 "Return caption string for ELEMENT, if applicable.
637 INFO is a plist used as a communication channel.
639 The caption string contains the sequence number of ELEMENT if it
640 has a name affiliated keyword, along with the real caption, if
641 any. Return nil when ELEMENT has no affiliated caption or name
642 keyword."
643 (let ((caption (org-element-property :caption element))
644 (name (org-element-property :name element)))
645 (when (or caption name)
646 ;; Get sequence number of current src-block among every
647 ;; src-block with either a caption or a name.
648 (let ((reference
649 (org-export-get-ordinal
650 element info nil nil
651 (lambda (el) (or (org-element-property :caption el)
652 (org-element-property :name el)))))
653 (title-fmt (org-e-ascii--translate
654 (case (org-element-type element)
655 (table "Table %d: %s")
656 (src-block "Listing %d: %s")) info)))
657 (org-e-ascii--fill-string
658 (format
659 title-fmt reference
660 (if (not caption) name
661 (org-export-secondary-string (car caption) 'e-ascii info)))
662 (org-e-ascii--current-text-width element info) info)))))
664 (defun org-e-ascii--build-toc (info &optional n keyword)
665 "Return a table of contents.
667 INFO is a plist used as a communication channel.
669 Optional argument N, when non-nil, is an integer specifying the
670 depth of the table.
672 Optional argument KEYWORD specifies the TOC keyword, if any, from
673 which the table of contents generation has been initiated."
674 (let ((title (org-e-ascii--translate "Table Of Contents\n" info)))
675 (concat
676 title
677 (make-string (1- (length title))
678 (if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))
679 "\n\n"
680 (let ((text-width
681 (if keyword (org-e-ascii--current-text-width keyword info)
682 (- org-e-ascii-text-width org-e-ascii-global-margin))))
683 (mapconcat
684 (lambda (headline)
685 (let* ((level (org-export-get-relative-level headline info))
686 (indent (* (1- level) 3)))
687 (concat
688 (unless (zerop indent) (concat (make-string (1- indent) ?.) " "))
689 (org-e-ascii--build-title
690 headline info (- text-width indent) nil
691 (eq (plist-get info :with-tags) 'not-in-toc)))))
692 (org-export-collect-headlines info n) "\n")))))
694 (defun org-e-ascii--list-listings (keyword info)
695 "Return a list of listings.
697 KEYWORD is the keyword that initiated the list of listings
698 generation. INFO is a plist used as a communication channel."
699 (let ((title (org-e-ascii--translate "List Of Listings\n" info)))
700 (concat
701 title
702 (make-string (1- (length title))
703 (if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))
704 "\n\n"
705 (let ((text-width
706 (if keyword (org-e-ascii--current-text-width keyword info)
707 (- org-e-ascii-text-width org-e-ascii-global-margin)))
708 ;; Use a counter instead of retreiving ordinal of each
709 ;; src-block.
710 (count 0))
711 (mapconcat
712 (lambda (src-block)
713 ;; Store initial text so its length can be computed. This is
714 ;; used to properly align caption right to it in case of
715 ;; filling (like contents of a description list item).
716 (let ((initial-text
717 (format (org-e-ascii--translate "Listing %d: " info)
718 (incf count))))
719 (concat
720 initial-text
721 (org-trim
722 (org-e-ascii--indent-string
723 (org-e-ascii--fill-string
724 (let ((caption (org-element-property :caption src-block)))
725 (if (not caption) (org-element-property :name src-block)
726 (org-export-secondary-string
727 ;; Use short name in priority, if available.
728 (or (cdr caption) (car caption)) 'e-ascii info)))
729 (- text-width (length initial-text)) info)
730 (length initial-text))))))
731 (org-export-collect-listings info) "\n")))))
733 (defun org-e-ascii--list-tables (keyword info)
734 "Return a list of listings.
736 KEYWORD is the keyword that initiated the list of listings
737 generation. INFO is a plist used as a communication channel."
738 (let ((title (org-e-ascii--translate "List Of Tables\n" info)))
739 (concat
740 title
741 (make-string (1- (length title))
742 (if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))
743 "\n\n"
744 (let ((text-width
745 (if keyword (org-e-ascii--current-text-width keyword info)
746 (- org-e-ascii-text-width org-e-ascii-global-margin)))
747 ;; Use a counter instead of retreiving ordinal of each
748 ;; src-block.
749 (count 0))
750 (mapconcat
751 (lambda (table)
752 ;; Store initial text so its length can be computed. This is
753 ;; used to properly align caption right to it in case of
754 ;; filling (like contents of a description list item).
755 (let ((initial-text
756 (format (org-e-ascii--translate "Table %d: " info)
757 (incf count))))
758 (concat
759 initial-text
760 (org-trim
761 (org-e-ascii--indent-string
762 (org-e-ascii--fill-string
763 (let ((caption (org-element-property :caption table)))
764 (if (not caption) (org-element-property :name table)
765 ;; Use short name in priority, if available.
766 (org-export-secondary-string
767 (or (cdr caption) (car caption)) 'e-ascii info)))
768 (- text-width (length initial-text)) info)
769 (length initial-text))))))
770 (org-export-collect-tables info) "\n")))))
772 (defun org-e-ascii--unique-links (element info)
773 "Return a list of unique link references in ELEMENT.
775 ELEMENT is either an headline element or a section element. INFO
776 is a plist used as a communication channel.
778 It covers links that may be found current headline's title, in
779 the following section and in any inlinetask's title there."
780 (let* (seen
781 (unique-link-p
782 (function
783 ;; Return LINK if it wasn't referenced so far, or nil.
784 ;; Update SEEN links along the way.
785 (lambda (link)
786 (let ((footprint
787 (cons (org-element-property :raw-link link)
788 (org-element-contents link))))
789 (unless (member footprint seen)
790 (push footprint seen) link)))))
791 (harvest-links-in-title
792 (function
793 ;; Return a list of all unique links in ELEMENT. ELEMENT
794 ;; may be an headline or an inlinetask element.
795 (lambda (element)
796 (let (acc)
797 (dolist (obj (org-element-property :title element) acc)
798 (when (eq (org-element-type obj) 'link)
799 (let ((link (funcall unique-link-p obj)))
800 (and link (push link acc)))))))))
801 ;; Retrieve HEADLINE's section, if it exists.
802 (section (if (eq (org-element-type element) 'section) element
803 (let ((sec (car (org-element-contents element))))
804 (and (eq (org-element-type sec) 'section) sec))))
805 (headline (if (eq (org-element-type element) 'headline) element
806 (org-export-get-parent-headline element info))))
807 (append
808 ;; Links that may be in HEADLINE's title.
809 (funcall harvest-links-in-title headline)
810 ;; Get all links in SECTION.
811 (org-element-map
812 section 'link (lambda (link local) (funcall unique-link-p link)) info))))
814 (defun org-e-ascii--describe-links (links width info)
815 "Return a string describing a list of links.
817 LINKS is a list of link type objects, as returned by
818 `org-e-ascii--unique-links'. WIDTH is the text width allowed for
819 the output string. INFO is a plist used as a communication
820 channel."
821 (mapconcat
822 (lambda (link)
823 (let ((type (org-element-property :type link))
824 (anchor (let ((desc (org-element-contents link)))
825 (if (not desc) (org-element-property :raw-link link)
826 (org-export-secondary-string desc 'e-ascii info)))))
827 (cond
828 ;; Coderefs, radio links and ref links are ignored.
829 ((member type '("coderef" "radio" "ref")) nil)
830 ;; Id, custom-id and fuzzy links (with the exception of
831 ;; targets): Headlines refer to their numbering.
832 ((member type '("custom-id" "fuzzy" "id"))
833 (let ((destination (if (string= type "fuzzy")
834 (org-export-resolve-fuzzy-link link info)
835 (org-export-resolve-id-link link info))))
836 (unless (eq (org-element-type destination) 'target)
837 (concat
838 (org-e-ascii--fill-string
839 (format
840 "[%s] %s"
841 anchor
842 (if (not destination)
843 (org-e-ascii--translate "Unknown reference" info)
844 (format
845 (org-e-ascii--translate "See section %s" info)
846 (mapconcat 'number-to-string
847 (org-export-get-headline-number destination info)
848 "."))))
849 width info) "\n\n"))))
850 ;; Do not add a link that cannot be resolved and doesn't have
851 ;; any description: destination is already visible in the
852 ;; paragraph.
853 ((not (org-element-contents link)) nil)
855 (concat
856 (org-e-ascii--fill-string
857 (format "[%s] %s" anchor (org-element-property :raw-link link))
858 width info)
859 "\n\n")))))
860 links ""))
864 ;;; Template
866 (defun org-e-ascii-template--document-title (info)
867 "Return document title, as a string.
868 INFO is a plist used as a communication channel."
869 (let ((text-width org-e-ascii-text-width)
870 (title (org-export-secondary-string
871 (plist-get info :title) 'e-ascii info))
872 (author
873 (and (plist-get info :with-author)
874 (let ((auth (plist-get info :author)))
875 (and auth (org-export-secondary-string auth 'e-ascii info)))))
876 (email
877 (and (plist-get info :with-email)
878 (org-export-secondary-string
879 (plist-get info :email) 'e-ascii info)))
880 (date (plist-get info :date)))
881 ;; There are two types of title blocks depending on the presence
882 ;; of a title to display.
883 (if (string= title "")
884 ;; Title block without a title. DATE is positioned at the top
885 ;; right of the document, AUTHOR to the top left and EMAIL
886 ;; just below.
887 (cond
888 ((and (org-string-nw-p date) (org-string-nw-p author))
889 (concat
890 author
891 (make-string (- text-width (length date) (length author)) ? )
892 date
893 (when (org-string-nw-p email) (concat "\n" email))
894 "\n\n\n"))
895 ((and (org-string-nw-p date) (org-string-nw-p email))
896 (concat
897 email
898 (make-string (- text-width (length date) (length email)) ? )
899 date "\n\n\n"))
900 ((org-string-nw-p date)
901 (concat
902 (org-e-ascii--justify-string date text-width 'right)
903 "\n\n\n"))
904 ((and (org-string-nw-p author) (org-string-nw-p email))
905 (concat author "\n" email "\n\n\n"))
906 ((org-string-nw-p author) (concat author "\n\n\n"))
907 ((org-string-nw-p email) (concat email "\n\n\n")))
908 ;; Title block with a title. Document's TITLE, along with the
909 ;; AUTHOR and its EMAIL are both overlined and an underlined,
910 ;; centered. Date is just below, also centered.
911 (let* ((utf8p (eq (plist-get info :ascii-charset) 'utf-8))
912 ;; Format TITLE. It may be filled if it is too wide,
913 ;; that is wider than the two thirds of the total width.
914 (title-len (min (length title) (/ (* 2 text-width) 3)))
915 (formatted-title (org-e-ascii--fill-string title title-len info))
916 (line
917 (make-string
918 (min (+ (max title-len (length author) (length email)) 2)
919 text-width) (if utf8p ?━ ?_))))
920 (org-e-ascii--justify-string
921 (concat line "\n"
922 (unless utf8p "\n")
923 (upcase formatted-title)
924 (cond
925 ((and (org-string-nw-p author) (org-string-nw-p email))
926 (concat (if utf8p "\n\n\n" "\n\n") author "\n" email))
927 ((org-string-nw-p author)
928 (concat (if utf8p "\n\n\n" "\n\n") author))
929 ((org-string-nw-p email)
930 (concat (if utf8p "\n\n\n" "\n\n") email)))
931 "\n" line
932 (when (org-string-nw-p date) (concat "\n\n\n" date))
933 "\n\n\n") text-width 'center)))))
935 (defun org-e-ascii-template (contents info)
936 "Return complete document string after ASCII conversion.
937 CONTENTS is the transcoded contents string. INFO is a plist
938 holding export options."
939 (org-element-normalize-string
940 (org-e-ascii--indent-string
941 (let ((text-width (- org-e-ascii-text-width org-e-ascii-global-margin)))
942 ;; 1. Build title block.
943 (concat
944 (org-e-ascii-template--document-title info)
945 ;; 2. Table of contents.
946 (let ((depth (plist-get info :with-toc)))
947 (when depth
948 (concat
949 (org-e-ascii--build-toc info (and (wholenump depth) depth))
950 "\n\n\n")))
951 ;; 3. Document's body.
952 contents
953 ;; 4. Footnote definitions.
954 (let ((definitions (org-export-collect-footnote-definitions
955 (plist-get info :parse-tree) info))
956 ;; Insert full links right inside the footnote definition
957 ;; as they have no chance to be inserted later.
958 (org-e-ascii-links-to-notes nil))
959 (when definitions
960 (concat
961 "\n\n\n"
962 (let ((title (org-e-ascii--translate "Footnotes\n" info)))
963 (concat
964 title
965 (make-string
966 (1- (length title))
967 (if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))))
968 "\n\n"
969 (mapconcat
970 (lambda (ref)
971 (let ((id (format "[%s] " (car ref))))
972 ;; Distinguish between inline definitions and
973 ;; full-fledged definitions.
974 (org-trim
975 (let ((def (nth 2 ref)))
976 (if (eq (org-element-type def) 'org-data)
977 ;; Full-fledged definition: footnote ID is
978 ;; inserted inside the first parsed paragraph
979 ;; (FIRST), if any, to be sure filling will
980 ;; take it into consideration.
981 (let ((first (car (org-element-contents def))))
982 (if (not (eq (org-element-type first) 'paragraph))
983 (concat id "\n" (org-export-data def 'e-ascii info))
984 (push id (nthcdr 2 first))
985 (org-export-data def 'e-ascii info)))
986 ;; Fill paragraph once footnote ID is inserted in
987 ;; order to have a correct length for first line.
988 (org-e-ascii--fill-string
989 (concat id (org-export-secondary-string def 'e-ascii info))
990 text-width info))))))
991 definitions "\n\n"))))
992 ;; 5. Creator. Ignore `comment' value as there are no comments in
993 ;; ASCII. Justify it to the bottom right.
994 (let ((creator-info (plist-get info :with-creator)))
995 (unless (or (not creator-info) (eq creator-info 'comment))
996 (concat
997 "\n\n\n"
998 (org-e-ascii--fill-string
999 (plist-get info :creator) text-width info 'right))))))
1000 org-e-ascii-global-margin)))
1002 (defun org-e-ascii--translate (s info)
1003 "Translate string S.
1005 INFO is a plist used as a communication channel.
1007 Translation depends on `:language' property and allowed charset.
1008 If no translation in found for a given language and a given
1009 charset, fall-back to S."
1010 (let* ((charset (intern (format ":%s" (plist-get info :ascii-charset))))
1011 (lang (plist-get info :language))
1012 (translations (cdr (assoc s org-e-ascii-dictionary))))
1013 (or (plist-get (cdr (assoc lang translations)) charset) s)))
1017 ;;; Transcode Functions
1019 ;;;; Babel Call
1021 ;; Babel Calls are ignored.
1024 ;;;; Center Block
1026 (defun org-e-ascii-center-block (center-block contents info)
1027 "Transcode a CENTER-BLOCK element from Org to ASCII.
1028 CONTENTS holds the contents of the block. INFO is a plist
1029 holding contextual information."
1030 (org-e-ascii--justify-string
1031 contents (org-e-ascii--current-text-width center-block info) 'center))
1034 ;;;; Comment
1036 ;; Comments are ignored.
1039 ;;;; Comment Block
1041 ;; Comment Blocks are ignored.
1044 ;;;; Drawer
1046 (defun org-e-ascii-drawer (drawer contents info)
1047 "Transcode a DRAWER element from Org to ASCII.
1048 CONTENTS holds the contents of the block. INFO is a plist
1049 holding contextual information."
1050 (let ((name (org-element-property :drawer-name drawer))
1051 (width (org-e-ascii--current-text-width drawer info)))
1052 (if (functionp org-e-ascii-format-drawer-function)
1053 (funcall org-e-ascii-format-drawer-function name contents width)
1054 ;; If there's no user defined function: simply
1055 ;; display contents of the drawer.
1056 contents)))
1059 ;;;; Dynamic Block
1061 (defun org-e-ascii-dynamic-block (dynamic-block contents info)
1062 "Transcode a DYNAMIC-BLOCK element from Org to ASCII.
1063 CONTENTS holds the contents of the block. INFO is a plist
1064 holding contextual information. See
1065 `org-export-data'."
1066 contents)
1069 ;;;; Emphasis
1071 (defun org-e-ascii-emphasis (emphasis contents info)
1072 "Transcode EMPHASIS from Org to ASCII.
1073 CONTENTS is the contents of the emphasized text. INFO is a plist
1074 holding contextual information.."
1075 (let ((marker (org-element-property :marker emphasis)))
1076 ;; Leave emphasis markers as-is.
1077 (concat marker contents marker)))
1080 ;;;; Entity
1082 (defun org-e-ascii-entity (entity contents info)
1083 "Transcode an ENTITY object from Org to ASCII.
1084 CONTENTS are the definition itself. INFO is a plist holding
1085 contextual information."
1086 (org-element-property
1087 (intern (concat ":" (symbol-name (plist-get info :ascii-charset))))
1088 entity))
1091 ;;;; Example Block
1093 (defun org-e-ascii-example-block (example-block contents info)
1094 "Transcode a EXAMPLE-BLOCK element from Org to ASCII.
1095 CONTENTS is nil. INFO is a plist holding contextual information."
1096 (org-e-ascii--box-string (org-export-handle-code example-block info) info))
1099 ;;;; Export Snippet
1101 (defun org-e-ascii-export-snippet (export-snippet contents info)
1102 "Transcode a EXPORT-SNIPPET object from Org to ASCII.
1103 CONTENTS is nil. INFO is a plist holding contextual information."
1104 (when (eq (org-export-snippet-backend export-snippet) 'e-ascii)
1105 (org-element-property :value export-snippet)))
1108 ;;;; Export Block
1110 (defun org-e-ascii-export-block (export-block contents info)
1111 "Transcode a EXPORT-BLOCK element from Org to ASCII.
1112 CONTENTS is nil. INFO is a plist holding contextual information."
1113 (when (string= (org-element-property :type export-block) "ascii")
1114 (org-remove-indentation (org-element-property :value export-block))))
1117 ;;;; Fixed Width
1119 (defun org-e-ascii-fixed-width (fixed-width contents info)
1120 "Transcode a FIXED-WIDTH element from Org to ASCII.
1121 CONTENTS is nil. INFO is a plist holding contextual information."
1122 (org-e-ascii--box-string
1123 (replace-regexp-in-string
1124 "^[ \t]*: ?" "" (org-element-property :value fixed-width)) info))
1127 ;;;; Footnote Definition
1129 ;; Footnote Definitions are ignored. They are compiled at the end of
1130 ;; the document, by `org-e-ascii-template'.
1133 ;;;; Footnote Reference
1135 (defun org-e-ascii-footnote-reference (footnote-reference contents info)
1136 "Transcode a FOOTNOTE-REFERENCE element from Org to ASCII.
1137 CONTENTS is nil. INFO is a plist holding contextual information."
1138 (format "[%s]" (org-export-get-footnote-number footnote-reference info)))
1141 ;;;; Headline
1143 (defun org-e-ascii-headline (headline contents info)
1144 "Transcode an HEADLINE element from Org to ASCII.
1145 CONTENTS holds the contents of the headline. INFO is a plist
1146 holding contextual information."
1147 ;; Don't export footnote section, which will be handled at the end
1148 ;; of the template.
1149 (unless (org-element-property :footnote-section-p headline)
1150 (let* ((low-level-rank (org-export-low-level-p headline info))
1151 (width (org-e-ascii--current-text-width headline info))
1152 ;; Blank lines between headline and its contents.
1153 ;; `org-e-ascii-headline-spacing', when set, overwrites
1154 ;; original buffer's spacing.
1155 (pre-blanks
1156 (make-string
1157 (if org-e-ascii-headline-spacing (car org-e-ascii-headline-spacing)
1158 (org-element-property :pre-blank headline)) ?\n))
1159 ;; Even if HEADLINE has no section, there might be some
1160 ;; links in its title that we shouldn't forget to describe.
1161 (links
1162 (unless (eq (caar (org-element-contents headline)) 'section)
1163 (org-e-ascii--describe-links
1164 (org-e-ascii--unique-links headline info) width info))))
1165 ;; Deep subtree: export it as a list item.
1166 (if low-level-rank
1167 (concat
1168 ;; Bullet.
1169 (let ((bullets (cdr (assq (plist-get info :ascii-charset)
1170 org-e-ascii-bullets))))
1171 (char-to-string
1172 (nth (mod (1- low-level-rank) (length bullets)) bullets)))
1174 ;; Title.
1175 (org-e-ascii--build-title headline info width) "\n"
1176 ;; Contents, indented by length of bullet.
1177 pre-blanks
1178 (org-e-ascii--indent-string
1179 (concat contents
1180 (when (org-string-nw-p links) (concat "\n\n" links)))
1182 ;; Else: Standard headline.
1183 (concat
1184 (org-e-ascii--build-title headline info width 'underline)
1185 "\n" pre-blanks
1186 (concat (when (org-string-nw-p links) links) contents))))))
1189 ;;;; Horizontal Rule
1191 (defun org-e-ascii-horizontal-rule (horizontal-rule contents info)
1192 "Transcode an HORIZONTAL-RULE object from Org to ASCII.
1193 CONTENTS is nil. INFO is a plist holding contextual
1194 information."
1195 (let ((attr
1196 (read
1197 (format
1198 "(%s)"
1199 (mapconcat
1200 #'identity
1201 (org-element-property :attr_ascii horizontal-rule)
1202 " ")))))
1203 (make-string (or (and (wholenump (plist-get attr :width))
1204 (plist-get attr :width))
1205 (org-e-ascii--current-text-width horizontal-rule info))
1206 (if (eq (plist-get info :ascii-charset) 'utf-8) ?― ?-))))
1209 ;;;; Inline Babel Call
1211 ;; Inline Babel Calls are ignored.
1214 ;;;; Inline Src Block
1216 (defun org-e-ascii-inline-src-block (inline-src-block contents info)
1217 "Transcode an INLINE-SRC-BLOCK element from Org to ASCII.
1218 CONTENTS holds the contents of the item. INFO is a plist holding
1219 contextual information."
1220 (format org-e-ascii-verbatim-format
1221 (org-element-property :value inline-src-block)))
1224 ;;;; Inlinetask
1226 (defun org-e-ascii-inlinetask (inlinetask contents info)
1227 "Transcode an INLINETASK element from Org to ASCII.
1228 CONTENTS holds the contents of the block. INFO is a plist
1229 holding contextual information."
1230 (let ((width (org-e-ascii--current-text-width inlinetask info))
1231 (title (org-export-secondary-string
1232 (org-element-property :title inlinetask) 'e-ascii info))
1233 (todo (and (plist-get info :with-todo-keywords)
1234 (let ((todo (org-element-property
1235 :todo-keyword inlinetask)))
1236 (and todo
1237 (org-export-secondary-string todo 'e-ascii info)))))
1238 (todo-type (org-element-property :todo-type inlinetask))
1239 (tags (and (plist-get info :with-tags)
1240 (org-element-property :tags inlinetask)))
1241 (priority (and (plist-get info :with-priority)
1242 (org-element-property :priority inlinetask))))
1243 ;; If `org-e-ascii-format-inlinetask-function' is provided, call it
1244 ;; with appropriate arguments.
1245 (if (functionp org-e-ascii-format-inlinetask-function)
1246 (funcall org-e-ascii-format-inlinetask-function
1247 todo todo-type priority title tags contents width)
1248 ;; Otherwise, use a default template.
1249 (let* ((utf8p (eq (plist-get info :ascii-charset) 'utf-8)))
1250 (org-e-ascii--indent-string
1251 (concat
1252 ;; Top line, with an additional blank line if not in UTF-8.
1253 (make-string width (if utf8p ?━ ?_)) "\n"
1254 (unless utf8p (concat (make-string width ? ) "\n"))
1255 ;; Add title. Fill it if wider than inlinetask.
1256 (let ((title (org-e-ascii--build-title inlinetask info width)))
1257 (if (<= (length title) width) title
1258 (org-e-ascii--fill-string title width info)))
1259 "\n"
1260 ;; If CONTENTS is not empty, insert it along with
1261 ;; a separator.
1262 (when (org-string-nw-p contents)
1263 (concat (make-string width (if utf8p ?─ ?-)) "\n" contents))
1264 ;; Bottom line.
1265 (make-string width (if utf8p ?━ ?_)))
1266 ;; Flush the inlinetask to the right.
1267 (- org-e-ascii-text-width org-e-ascii-global-margin
1268 (if (not (org-export-get-parent-headline inlinetask info)) 0
1269 org-e-ascii-inner-margin)
1270 (org-e-ascii--current-text-width inlinetask info)))))))
1273 ;;;; Item
1275 (defun org-e-ascii-item (item contents info)
1276 "Transcode an ITEM element from Org to ASCII.
1277 CONTENTS holds the contents of the item. INFO is a plist holding
1278 contextual information."
1279 (let ((bullet
1280 ;; First parent of ITEM is always the plain-list. Get
1281 ;; `:type' property from it.
1282 (org-list-bullet-string
1283 (case (org-element-property :type (org-export-get-parent item info))
1284 (descriptive
1285 (concat
1286 (org-export-secondary-string
1287 (org-element-property :tag item) 'e-ascii info) ": "))
1288 (ordered
1289 ;; Return correct number for ITEM, paying attention to
1290 ;; counters.
1291 (let* ((struct (org-element-property :structure item))
1292 (bul (org-element-property :bullet item))
1293 (num
1294 (number-to-string
1295 (car (last (org-list-get-item-number
1296 (org-element-property :begin item)
1297 struct
1298 (org-list-prevs-alist struct)
1299 (org-list-parents-alist struct)))))))
1300 (replace-regexp-in-string "[0-9]+" num bul)))
1301 (t (let ((bul (org-element-property :bullet item)))
1302 ;; Change bullets into more visible form if UTF-8 is active.
1303 (if (not (eq (plist-get info :ascii-charset) 'utf-8)) bul
1304 (replace-regexp-in-string
1305 "-" "•"
1306 (replace-regexp-in-string
1307 "+" "⁃"
1308 (replace-regexp-in-string "*" "‣" bul))))))))))
1309 (concat
1310 bullet
1311 ;; Contents: Pay attention to indentation. Note: check-boxes are
1312 ;; already taken care of at the paragraph level so they don't
1313 ;; interfere with indentation.
1314 (let ((contents (org-e-ascii--indent-string contents (length bullet))))
1315 (if (eq (caar (org-element-contents item)) 'paragraph)
1316 (org-trim contents)
1317 (concat "\n" contents))))))
1320 ;;;; Keyword
1322 (defun org-e-ascii-keyword (keyword contents info)
1323 "Transcode a KEYWORD element from Org to ASCII.
1324 CONTENTS is nil. INFO is a plist holding contextual
1325 information."
1326 (let ((key (downcase (org-element-property :key keyword)))
1327 (value (org-element-property :value keyword)))
1328 (cond
1329 ((string= key "ascii") value)
1330 ((string= key "toc")
1331 (let ((value (downcase value)))
1332 (cond
1333 ((string-match "\\<headlines\\>" value)
1334 (let ((depth (or (and (string-match "[0-9]+" value)
1335 (string-to-number (match-string 0 value)))
1336 (plist-get info :with-toc))))
1337 (org-e-ascii--build-toc
1338 info (and (wholenump depth) depth) keyword)))
1339 ((string= "tables" value)
1340 (org-e-ascii--list-tables keyword info))
1341 ((string= "listings" value)
1342 (org-e-ascii--list-listings keyword info))))))))
1345 ;;;; Latex Environment
1347 (defun org-e-ascii-latex-environment (latex-environment contents info)
1348 "Transcode a LATEX-ENVIRONMENT element from Org to ASCII.
1349 CONTENTS is nil. INFO is a plist holding contextual
1350 information."
1351 (org-remove-indentation (org-element-property :value latex-environment)))
1354 ;;;; Latex Fragment
1356 (defun org-e-ascii-latex-fragment (latex-fragment contents info)
1357 "Transcode a LATEX-FRAGMENT object from Org to ASCII.
1358 CONTENTS is nil. INFO is a plist holding contextual
1359 information."
1360 (org-element-property :value latex-fragment))
1363 ;;;; Line Break
1365 (defun org-e-ascii-line-break (line-break contents info)
1366 "Transcode a LINE-BREAK object from Org to ASCII.
1367 CONTENTS is nil. INFO is a plist holding contextual
1368 information." hard-newline)
1371 ;;;; Link
1373 (defun org-e-ascii-link (link desc info)
1374 "Transcode a LINK object from Org to ASCII.
1376 DESC is the description part of the link, or the empty string.
1377 INFO is a plist holding contextual information."
1378 (let ((raw-link (org-element-property :raw-link link))
1379 (type (org-element-property :type link)))
1380 (cond
1381 ((string= type "coderef")
1382 (let ((ref (org-element-property :path link)))
1383 (format (org-export-get-coderef-format ref desc)
1384 (org-export-resolve-coderef ref info))))
1385 ;; Do not apply a special syntax on radio links. Though, parse
1386 ;; and transcode path to have a proper display of contents.
1387 ((string= type "radio")
1388 (org-export-secondary-string
1389 (org-element-parse-secondary-string
1390 (org-element-property :path link)
1391 (cdr (assq 'radio-target org-element-object-restrictions)))
1392 'e-ascii info))
1393 ;; Ref link: If there's no description (DESC, return link's
1394 ;; destination sequence number among elements of same
1395 ;; type. Otherwise, use DESC.
1396 ((string= type "ref")
1397 (if (org-string-nw-p desc) desc
1398 (format "%d"
1399 (org-export-get-ordinal
1400 (org-export-resolve-ref-link link info)
1401 info nil nil
1402 (lambda (el) (or (org-element-property :caption el)
1403 (org-element-property :name el)))))))
1404 ;; Do not apply a special syntax on fuzzy links pointing to
1405 ;; targets.
1406 ((and (string= type "fuzzy")
1407 (let ((path (org-element-property :path link)))
1408 (loop for target in (plist-get info :target-list)
1409 thereis (string=
1410 (org-element-property :raw-value target)
1411 path))))
1412 (if (org-string-nw-p desc) desc raw-link))
1414 (concat (format "[%s]" (if (org-string-nw-p desc) desc raw-link))
1415 (unless org-e-ascii-links-to-notes (format " (%s)" raw-link)))))))
1418 ;;;; Macro
1420 (defun org-e-ascii-macro (macro contents info)
1421 "Transcode a MACRO element from Org to ASCII.
1422 CONTENTS is nil. INFO is a plist holding contextual
1423 information."
1424 (org-export-expand-macro macro info))
1427 ;;;; Paragraph
1429 (defun org-e-ascii-paragraph (paragraph contents info)
1430 "Transcode a PARAGRAPH element from Org to ASCII.
1431 CONTENTS is the contents of the paragraph, as a string. INFO is
1432 the plist used as a communication channel."
1433 (org-e-ascii--fill-string
1434 (let ((parent (org-export-get-parent paragraph info)))
1435 ;; If PARAGRAPH is the first one in a list element, be sure to
1436 ;; add the check-box in front of it, before any filling. Later,
1437 ;; it would interfere with line width.
1438 (if (and (eq (org-element-type parent) 'item)
1439 (equal (car (org-element-contents parent)) paragraph))
1440 (let ((utf8p (eq (plist-get info :ascii-charset) 'utf-8)))
1441 (concat (case (org-element-property :checkbox parent)
1442 (on (if utf8p "☑ " "[X] "))
1443 (off (if utf8p "☐ " "[ ] "))
1444 (trans (if utf8p "☒ " "[-] ")))
1445 contents))
1446 contents))
1447 (org-e-ascii--current-text-width paragraph info) info))
1450 ;;;; Plain List
1452 (defun org-e-ascii-plain-list (plain-list contents info)
1453 "Transcode a PLAIN-LIST element from Org to ASCII.
1454 CONTENTS is the contents of the list. INFO is a plist holding
1455 contextual information."
1456 contents)
1459 ;;;; Plain Text
1461 (defun org-e-ascii-plain-text (text info)
1462 "Transcode a TEXT string from Org to ASCII.
1463 INFO is a plist used as a communication channel."
1464 (if (not (and (eq (plist-get info :ascii-charset) 'utf-8)
1465 (plist-get info :with-special-strings)))
1466 text
1467 ;; Usual replacements in utf-8 with proper option set.
1468 (replace-regexp-in-string
1469 "\\.\\.\\." "…"
1470 (replace-regexp-in-string
1471 "--" "–"
1472 (replace-regexp-in-string "---" "—" text)))))
1475 ;;;; Property Drawer
1477 (defun org-e-ascii-property-drawer (property-drawer contents info)
1478 "Transcode a PROPERTY-DRAWER element from Org to ASCII.
1479 CONTENTS is nil. INFO is a plist used as a communication
1480 channel."
1481 ;; The property drawer isn't exported but we want separating blank
1482 ;; lines nonetheless.
1486 ;;;; Quote Block
1488 (defun org-e-ascii-quote-block (quote-block contents info)
1489 "Transcode a QUOTE-BLOCK element from Org to ASCII.
1490 CONTENTS holds the contents of the block. INFO is a plist
1491 holding contextual information."
1492 (let ((width (org-e-ascii--current-text-width quote-block info)))
1493 (org-e-ascii--indent-string
1494 (org-remove-indentation
1495 (org-e-ascii--fill-string contents width info))
1496 org-e-ascii-quote-margin)))
1499 ;;;; Quote Section
1501 (defun org-e-ascii-quote-section (quote-section contents info)
1502 "Transcode a QUOTE-SECTION element from Org to ASCII.
1503 CONTENTS is nil. INFO is a plist holding contextual information."
1504 (let ((width (org-e-ascii--current-text-width quote-section info))
1505 (value
1506 (org-export-secondary-string
1507 (org-remove-indentation
1508 (org-element-property :value quote-section)) 'e-ascii info)))
1509 (org-e-ascii--indent-string
1510 value
1511 (+ org-e-ascii-quote-margin
1512 ;; Don't apply inner margin if parent headline is low level.
1513 (let ((headline (org-export-get-parent-headline quote-section info)))
1514 (if (org-export-low-level-p headline info) 0
1515 org-e-ascii-inner-margin))))))
1518 ;;;; Radio Target
1520 (defun org-e-ascii-radio-target (radio-target contents info)
1521 "Transcode a RADIO-TARGET object from Org to ASCII.
1522 CONTENTS is the contents of the target. INFO is a plist holding
1523 contextual information."
1524 contents)
1526 ;;;; Section
1528 (defun org-e-ascii-section (section contents info)
1529 "Transcode a SECTION element from Org to ASCII.
1530 CONTENTS is the contents of the section. INFO is a plist holding
1531 contextual information."
1532 (org-e-ascii--indent-string
1533 (concat
1534 contents
1535 (when org-e-ascii-links-to-notes
1536 ;; Add list of links at the end of SECTION.
1537 (let ((links (org-e-ascii--describe-links
1538 (org-e-ascii--unique-links section info)
1539 (org-e-ascii--current-text-width section info) info)))
1540 ;; Separate list of links and section contents.
1541 (when (org-string-nw-p links) (concat "\n\n" links)))))
1542 ;; Do not apply inner margin if parent headline is low level.
1543 (let ((headline (org-export-get-parent-headline section info)))
1544 (if (or (not headline) (org-export-low-level-p headline info)) 0
1545 org-e-ascii-inner-margin))))
1548 ;;;; Special Block
1550 (defun org-e-ascii-special-block (special-block contents info)
1551 "Transcode a SPECIAL-BLOCK element from Org to ASCII.
1552 CONTENTS holds the contents of the block. INFO is a plist
1553 holding contextual information."
1554 contents)
1557 ;;;; Src Block
1559 (defun org-e-ascii-src-block (src-block contents info)
1560 "Transcode a SRC-BLOCK element from Org to ASCII.
1561 CONTENTS holds the contents of the item. INFO is a plist holding
1562 contextual information."
1563 (let ((caption (org-e-ascii--build-caption src-block info)))
1564 (concat
1565 (when (and caption org-e-ascii-caption-above) (concat caption "\n"))
1566 (org-e-ascii--box-string (org-export-handle-code src-block info) info)
1567 (when (and caption (not org-e-ascii-caption-above))
1568 (concat "\n" caption)))))
1570 ;;;; Statistics Cookie
1572 (defun org-e-ascii-statistics-cookie (statistics-cookie contents info)
1573 "Transcode a STATISTICS-COOKIE object from Org to ASCII.
1574 CONTENTS is nil. INFO is a plist holding contextual information."
1575 (org-element-property :value statistics-cookie))
1578 ;;;; Subscript
1580 (defun org-e-ascii-subscript (subscript contents info)
1581 "Transcode a SUBSCRIPT object from Org to ASCII.
1582 CONTENTS is the contents of the object. INFO is a plist holding
1583 contextual information."
1584 (if (org-element-property :use-brackets-p subscript)
1585 (format "_{%s}" contents)
1586 (format "_%s" contents)))
1589 ;;;; Superscript
1591 (defun org-e-ascii-superscript (superscript contents info)
1592 "Transcode a SUPERSCRIPT object from Org to ASCII.
1593 CONTENTS is the contents of the object. INFO is a plist holding
1594 contextual information."
1595 (if (org-element-property :use-brackets-p superscript)
1596 (format "_{%s}" contents)
1597 (format "_%s" contents)))
1600 ;;;; Table
1602 ;; While `org-e-ascii-table' is the callback function expected by
1603 ;; org-export mechanism, it requires four subroutines to display
1604 ;; tables accordingly to chosen charset, alignment and width
1605 ;; specifications.
1607 ;; Thus, `org-e-ascii-table--column-width' computes the display width
1608 ;; for each column in the table,
1609 ;; `org-e-ascii-table--vertical-separators' returns a vector
1610 ;; containing separators (or lack thereof),
1611 ;; `org-e-ascii-table--build-hline' creates various hline strings,
1612 ;; depending on charset, separators and position within the tabl and
1613 ;; `org-e-ascii-table--format-cell' properly aligns contents within
1614 ;; a given cell and width.
1616 (defun org-e-ascii-table (table contents info)
1617 "Transcode a TABLE element from Org to ASCII.
1618 CONTENTS is nil. INFO is a plist holding contextual information."
1619 (let ((raw-table (org-element-property :raw-table table))
1620 (caption (org-e-ascii--build-caption table info)))
1621 (concat
1622 ;; Possibly add a caption string above.
1623 (when (and caption org-e-ascii-caption-above) (concat caption "\n"))
1624 ;; Insert table. Note: "table.el" tables are left unmodified.
1625 (if (eq (org-element-property :type table) 'table.el) raw-table
1626 (let* ((utf8p (eq (plist-get info :ascii-charset) 'utf-8))
1627 ;; Extract information out of the raw table (TABLE-INFO)
1628 ;; and clean it (CLEAN-TABLE).
1629 (table-info (org-export-table-format-info raw-table))
1630 (special-col-p (plist-get table-info :special-column-p))
1631 (alignment (plist-get table-info :alignment))
1632 (clean-table (org-export-clean-table raw-table special-col-p))
1633 ;; Change table into lisp, much like
1634 ;; `org-table-to-lisp', though cells are parsed and
1635 ;; transcoded along the way.
1636 (lisp-table
1637 (mapcar
1638 (lambda (line)
1639 (if (string-match org-table-hline-regexp line) 'hline
1640 (mapcar
1641 (lambda (cell)
1642 (org-trim
1643 (org-export-secondary-string
1644 (org-element-parse-secondary-string
1645 cell
1646 (cdr (assq 'item org-element-string-restrictions)))
1647 'e-ascii info)))
1648 (org-split-string (org-trim line) "\\s-?|\\s-?"))))
1649 (org-split-string clean-table "[ \t]*\n[ \t]*")))
1650 ;; Compute real column widths.
1651 (column-widths
1652 (org-e-ascii-table--column-width lisp-table table-info))
1653 ;; Construct separators according to column groups.
1654 (separators (org-e-ascii-table--vertical-separators table-info))
1655 ;; Build different `hline' strings, depending on
1656 ;; separators, column widths and position.
1657 (hline-standard
1658 (org-e-ascii-table--build-hline
1659 nil separators column-widths info))
1660 (hline-top
1661 (and utf8p (org-e-ascii-table--build-hline
1662 'top separators column-widths info)))
1663 (hline-bottom
1664 (and utf8p (org-e-ascii-table--build-hline
1665 'bottom separators column-widths info))))
1666 ;; Now build table back, with correct alignment, considering
1667 ;; columns widths and separators.
1668 (mapconcat
1669 (lambda (line)
1670 (cond
1671 ((eq line 'hline) hline-standard)
1672 ((eq line 'hline-bottom) hline-bottom)
1673 ((eq line 'hline-top) hline-top)
1674 (t (loop for cell in line
1675 for col from 0 to (length line)
1676 concat
1677 (concat
1678 (let ((sep (aref separators col)))
1679 (if (and utf8p (not (string= sep ""))) "│" sep))
1680 (org-e-ascii-table--format-cell
1681 cell col column-widths alignment info)) into l
1682 finally return
1683 (concat l
1684 (let ((sep (aref separators col)))
1685 (if (and utf8p (not (string= sep ""))) "│"
1686 sep)))))))
1687 ;; If charset is `utf-8', make sure lisp-table always starts
1688 ;; with `hline-top' and ends with `hline-bottom'.
1689 (if (not utf8p) lisp-table
1690 (setq lisp-table
1691 (cons 'hline-top
1692 (if (eq (car lisp-table) 'hline) (cdr lisp-table)
1693 lisp-table)))
1694 (setq lisp-table
1695 (nconc
1696 (if (eq (car (last lisp-table)) 'hline) (butlast lisp-table)
1697 lisp-table)
1698 '(hline-bottom)))) "\n")))
1699 ;; Possible add a caption string below.
1700 (when (and caption (not org-e-ascii-caption-above))
1701 (concat "\n" caption)))))
1703 (defun org-e-ascii-table--column-width (table table-info)
1704 "Return vector of TABLE columns width.
1706 TABLE is the Lisp representation of the Org table considered.
1707 TABLE-INFO holds information about the table. See
1708 `org-export-table-format-info'.
1710 Unlike to `:width' property from `org-export-table-format-info',
1711 the return value is a vector containing width of every column,
1712 not only those with an explicit width cookie. Special column, if
1713 any, is ignored."
1714 ;; All rows have the same length, but be sure to ignore hlines.
1715 (let ((width (make-vector
1716 (loop for row in table
1717 unless (eq row 'hline)
1718 return (length row))
1719 0)))
1720 ;; Set column width to the maximum width of the cells in that
1721 ;; column.
1722 (mapc
1723 (lambda (line)
1724 (let ((idx 0))
1725 (unless (eq line 'hline)
1726 (mapc (lambda (cell)
1727 (let ((len (length cell)))
1728 (when (> len (aref width idx)) (aset width idx len)))
1729 (incf idx))
1730 line))))
1731 table)
1732 (unless org-e-ascii-table-widen-columns
1733 ;; When colums are not widened, width cookies have precedence
1734 ;; over string lengths. Thus, overwrite the latter with the
1735 ;; former.
1736 (let ((cookies (plist-get table-info :width))
1737 (specialp (plist-get table-info :special-column-p)))
1738 ;; Remove special column from COOKIES vector, if any.
1739 (loop for w across (if specialp (substring cookies 1) cookies)
1740 for idx from 0 to width
1741 when w do (aset width idx w))))
1742 ;; Return value.
1743 width))
1745 (defun org-e-ascii-table--vertical-separators (table-info)
1746 "Return a vector of strings for vertical separators.
1748 TABLE-INFO holds information about considered table. See
1749 `org-export-table-format-info'.
1751 Return value is a vector whose length is one more than the number
1752 of columns in the table. Special column, if any, is ignored."
1753 (let* ((colgroups (plist-get table-info :column-groups))
1754 (separators (make-vector (1+ (length colgroups)) "")))
1755 (if org-e-ascii-table-keep-all-vertical-lines
1756 (make-vector (length separators) "|")
1757 (let ((column 0))
1758 (mapc (lambda (group)
1759 (when (memq group '(start start-end))
1760 (aset separators column "|"))
1761 (when (memq group '(end start-end))
1762 (aset separators (1+ column) "|"))
1763 (incf column))
1764 colgroups)
1765 ;; Remove unneeded special column.
1766 (if (not (plist-get table-info :special-column-p)) separators
1767 (substring separators 1))))))
1769 (defun org-e-ascii-table--format-cell (cell col width alignment info)
1770 "Format CELL with column width and alignment constraints.
1772 CELL is the contents of the cell, as a string.
1774 COL is the column containing the cell considered.
1776 WIDTH is a vector holding every column width, as returned by
1777 `org-e-ascii-table--column-width'.
1779 ALIGNMENT is a vector containing alignment strings for every
1780 column.
1782 INFO is a plist used as a communication channel."
1783 (let ((col-width (if org-e-ascii-table-widen-columns (aref width col)
1784 (or (aref width col) (length cell)))))
1785 ;; When CELL is too large, it has to be truncated.
1786 (unless (or org-e-ascii-table-widen-columns (<= (length cell) col-width))
1787 (setq cell (concat (substring cell 0 (- col-width 2)) "=>")))
1788 (let* ((indent-tabs-mode nil)
1789 (align (aref alignment col))
1790 (aligned-cell
1791 (org-e-ascii--justify-string
1792 (org-trim cell) col-width
1793 (cond ((string= align "c") 'center)
1794 ((string= align "r") 'right)))))
1795 ;; Return aligned cell, with missing white spaces added and
1796 ;; space separators between columns.
1797 (format
1798 " %s "
1799 (concat aligned-cell
1800 (make-string (- col-width (length aligned-cell)) ? ))))))
1802 (defun org-e-ascii-table--build-hline (position separators column-widths info)
1803 "Return string used as an horizontal line in tables.
1805 POSITION is a symbol among `top', `bottom' and nil, which
1806 specifies position of the horizontal line within the table.
1808 SEPARATORS is a vector strings specifying separators used in the
1809 table, as returned by `org-e-ascii-table--vertical-separators'.
1811 COLUMN-WIDTHS is a vector of numbers specifying widths of all
1812 columns in the table, as returned by
1813 `org-e-ascii-table--column-width'.
1815 INFO is a plist used as a communication channel."
1816 (let ((utf8p (eq (plist-get info :ascii-charset) 'utf-8)))
1817 (loop for idx from 0 to (length separators)
1818 for width across column-widths
1819 concat
1820 (concat
1821 (cond ((string= (aref separators idx) "") nil)
1822 ((and utf8p (zerop idx))
1823 (cond ((eq position 'top) "┍")
1824 ((eq position 'bottom) "┕")
1825 (t "├")))
1826 (utf8p
1827 (cond ((eq position 'top) "┯")
1828 ((eq position 'bottom) "┷")
1829 (t "┼")))
1830 (t "+"))
1831 ;; Hline has to cover all the cell and both white spaces
1832 ;; between columns.
1833 (make-string (+ width 2)
1834 (cond ((not utf8p) ?-)
1835 ((not position) ?─)
1836 (t ?━))))
1837 into hline
1838 finally return
1839 ;; There is one separator more than columns, so handle it
1840 ;; here.
1841 (concat
1842 hline
1843 (cond
1844 ((string= (aref separators idx) "") nil)
1845 (utf8p (cond ((eq position 'top) "┑")
1846 ((eq position 'bottom) "┙")
1847 (t "┤")))
1848 (t "+"))))))
1851 ;;;; Target
1853 (defun org-e-ascii-target (target contents info)
1854 "Transcode a TARGET object from Org to ASCII.
1855 CONTENTS is the contents of the target. INFO is a plist holding
1856 contextual information."
1857 contents)
1860 ;;;; Time-stamp
1862 (defun org-e-ascii-time-stamp (time-stamp contents info)
1863 "Transcode a TIME-STAMP object from Org to ASCII.
1864 CONTENTS is nil. INFO is a plist holding contextual information."
1865 ;; Return time-stamps as-is.
1866 (org-element-time-stamp-interpreter time-stamp contents))
1869 ;;;; Verbatim
1871 (defun org-e-ascii-verbatim (verbatim contents info)
1872 "Return a VERBATIM object from Org to ASCII.
1873 CONTENTS is nil. INFO is a plist holding contextual information."
1874 (format org-e-ascii-verbatim-format
1875 (org-element-property :value verbatim)))
1878 ;;;; Verse Block
1880 (defun org-e-ascii-verse-block (verse-block contents info)
1881 "Transcode a VERSE-BLOCK element from Org to ASCII.
1882 CONTENTS is nil. INFO is a plist holding contextual information."
1883 (let ((verse-width (org-e-ascii--current-text-width verse-block info)))
1884 (org-e-ascii--indent-string
1885 (org-e-ascii--justify-string
1886 (org-export-secondary-string
1887 (org-element-property :value verse-block) 'e-ascii info)
1888 verse-width 'left)
1889 org-e-ascii-quote-margin)))
1892 ;;; Filter
1894 (defun org-e-ascii-filter-headline-blank-lines (headline back-end info)
1895 "Filter controlling number of blank lines after an headline.
1897 HEADLINE is a string representing a transcoded headline.
1898 BACK-END is symbol specifying back-end used for export. INFO is
1899 plist containing the communication channel.
1901 This function only applies to `e-ascii' back-end. See
1902 `org-e-ascii-headline-spacing' for information.
1904 For any other back-end, HEADLINE is returned as-is."
1905 (if (not (and (eq back-end 'e-ascii) org-e-ascii-headline-spacing)) headline
1906 (let ((blanks (make-string (1+ (cdr org-e-ascii-headline-spacing)) ?\n)))
1907 (replace-regexp-in-string "\n\\(?:\n[ \t]*\\)*\\'" blanks headline))))
1911 ;;; Interactive function
1913 (defun org-e-ascii-export-to-ascii
1914 (&optional subtreep visible-only body-only ext-plist pub-dir)
1915 "Export current buffer to a text file.
1917 If narrowing is active in the current buffer, only export its
1918 narrowed part.
1920 If a region is active, export that region.
1922 When optional argument SUBTREEP is non-nil, export the sub-tree
1923 at point, extracting information from the headline properties
1924 first.
1926 When optional argument VISIBLE-ONLY is non-nil, don't export
1927 contents of hidden elements.
1929 When optional argument BODY-ONLY is non-nil, strip title, table
1930 of contents and footnote definitions from output.
1932 EXT-PLIST, when provided, is a property list with external
1933 parameters overriding Org default settings, but still inferior to
1934 file-local settings.
1936 When optional argument PUB-DIR is set, use it as the publishing
1937 directory.
1939 Return output file's name."
1940 (interactive)
1941 (let ((outfile (org-export-output-file-name ".txt" subtreep pub-dir)))
1942 (org-export-to-file
1943 'e-ascii outfile subtreep visible-only body-only ext-plist)))
1946 (provide 'org-e-ascii)
1947 ;;; org-e-ascii.el ends here