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