org-export: Add `org-export-headline-numbered-p' predicate
[org-mode.git] / EXPERIMENTAL / org-e-ascii.el
blob5a2b0c2dc03fd41ddc3033469466ed5e5935edd4
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 (org-export-numbered-headline-p element info)
589 ;; All tests passed: build numbering string.
590 (concat
591 (mapconcat
592 #'number-to-string
593 (org-export-get-headline-number element info) ".")
594 " ")))
595 (text (org-export-secondary-string
596 (org-element-property :title element) 'e-ascii info))
597 (todo
598 (and (plist-get info :with-todo-keywords)
599 (let ((todo (org-element-property :todo-keyword element)))
600 (and todo
601 (concat (org-export-secondary-string todo 'e-ascii info)
602 " ")))))
603 (tags (and (not notags)
604 (plist-get info :with-tags)
605 (org-element-property :tags element)))
606 (priority
607 (and (plist-get info :with-priority)
608 (concat (org-element-property :priority element) " ")))
609 (first-part (concat numbers todo priority text)))
610 (concat
611 first-part
612 ;; Align tags, if any.
613 (when tags
614 (format
615 (format " %%%ds"
616 (max (- text-width (1+ (length first-part))) (length tags)))
617 tags))
618 ;; Maybe underline text, if ELEMENT type is `headline' and an
619 ;; underline character has been defined.
620 (when (and underline headlinep)
621 (let ((under-char
622 (nth (1- (org-export-get-relative-level element info))
623 (cdr (assq (plist-get info :ascii-charset)
624 org-e-ascii-underline)))))
625 (and under-char
626 (concat "\n"
627 (make-string (length first-part) under-char))))))))
629 (defun org-e-ascii--build-caption (element info)
630 "Return caption string for ELEMENT, if applicable.
632 INFO is a plist used as a communication channel.
634 The caption string contains the sequence number of ELEMENT if it
635 has a name affiliated keyword, along with the real caption, if
636 any. Return nil when ELEMENT has no affiliated caption or name
637 keyword."
638 (let ((caption (org-element-property :caption element))
639 (name (org-element-property :name element)))
640 (when (or caption name)
641 ;; Get sequence number of current src-block among every
642 ;; src-block with either a caption or a name.
643 (let ((reference
644 (org-export-get-ordinal
645 element info nil nil
646 (lambda (el) (or (org-element-property :caption el)
647 (org-element-property :name el)))))
648 (title-fmt (org-e-ascii--translate
649 (case (org-element-type element)
650 (table "Table %d: %s")
651 (src-block "Listing %d: %s")) info)))
652 (org-e-ascii--fill-string
653 (format
654 title-fmt reference
655 (if (not caption) name
656 (org-export-secondary-string (car caption) 'e-ascii info)))
657 (org-e-ascii--current-text-width element info) info)))))
659 (defun org-e-ascii--build-toc (info &optional n keyword)
660 "Return a table of contents.
662 INFO is a plist used as a communication channel.
664 Optional argument N, when non-nil, is an integer specifying the
665 depth of the table.
667 Optional argument KEYWORD specifies the TOC keyword, if any, from
668 which the table of contents generation has been initiated."
669 (let ((title (org-e-ascii--translate "Table Of Contents\n" info)))
670 (concat
671 title
672 (make-string (1- (length title))
673 (if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))
674 "\n\n"
675 (let ((text-width
676 (if keyword (org-e-ascii--current-text-width keyword info)
677 (- org-e-ascii-text-width org-e-ascii-global-margin))))
678 (mapconcat
679 (lambda (headline)
680 (let* ((level (org-export-get-relative-level headline info))
681 (indent (* (1- level) 3)))
682 (concat
683 (unless (zerop indent) (concat (make-string (1- indent) ?.) " "))
684 (org-e-ascii--build-title
685 headline info (- text-width indent) nil
686 (eq (plist-get info :with-tags) 'not-in-toc)))))
687 (org-export-collect-headlines info n) "\n")))))
689 (defun org-e-ascii--list-listings (keyword info)
690 "Return a list of listings.
692 KEYWORD is the keyword that initiated the list of listings
693 generation. INFO is a plist used as a communication channel."
694 (let ((title (org-e-ascii--translate "List Of Listings\n" info)))
695 (concat
696 title
697 (make-string (1- (length title))
698 (if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))
699 "\n\n"
700 (let ((text-width
701 (if keyword (org-e-ascii--current-text-width keyword info)
702 (- org-e-ascii-text-width org-e-ascii-global-margin)))
703 ;; Use a counter instead of retreiving ordinal of each
704 ;; src-block.
705 (count 0))
706 (mapconcat
707 (lambda (src-block)
708 ;; Store initial text so its length can be computed. This is
709 ;; used to properly align caption right to it in case of
710 ;; filling (like contents of a description list item).
711 (let ((initial-text
712 (format (org-e-ascii--translate "Listing %d: " info)
713 (incf count))))
714 (concat
715 initial-text
716 (org-trim
717 (org-e-ascii--indent-string
718 (org-e-ascii--fill-string
719 (let ((caption (org-element-property :caption src-block)))
720 (if (not caption) (org-element-property :name src-block)
721 (org-export-secondary-string
722 ;; Use short name in priority, if available.
723 (or (cdr caption) (car caption)) 'e-ascii info)))
724 (- text-width (length initial-text)) info)
725 (length initial-text))))))
726 (org-export-collect-listings info) "\n")))))
728 (defun org-e-ascii--list-tables (keyword info)
729 "Return a list of listings.
731 KEYWORD is the keyword that initiated the list of listings
732 generation. INFO is a plist used as a communication channel."
733 (let ((title (org-e-ascii--translate "List Of Tables\n" info)))
734 (concat
735 title
736 (make-string (1- (length title))
737 (if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))
738 "\n\n"
739 (let ((text-width
740 (if keyword (org-e-ascii--current-text-width keyword info)
741 (- org-e-ascii-text-width org-e-ascii-global-margin)))
742 ;; Use a counter instead of retreiving ordinal of each
743 ;; src-block.
744 (count 0))
745 (mapconcat
746 (lambda (table)
747 ;; Store initial text so its length can be computed. This is
748 ;; used to properly align caption right to it in case of
749 ;; filling (like contents of a description list item).
750 (let ((initial-text
751 (format (org-e-ascii--translate "Table %d: " info)
752 (incf count))))
753 (concat
754 initial-text
755 (org-trim
756 (org-e-ascii--indent-string
757 (org-e-ascii--fill-string
758 (let ((caption (org-element-property :caption table)))
759 (if (not caption) (org-element-property :name table)
760 ;; Use short name in priority, if available.
761 (org-export-secondary-string
762 (or (cdr caption) (car caption)) 'e-ascii info)))
763 (- text-width (length initial-text)) info)
764 (length initial-text))))))
765 (org-export-collect-tables info) "\n")))))
767 (defun org-e-ascii--unique-links (element info)
768 "Return a list of unique link references in ELEMENT.
770 ELEMENT is either an headline element or a section element. INFO
771 is a plist used as a communication channel.
773 It covers links that may be found current headline's title, in
774 the following section and in any inlinetask's title there."
775 (let* (seen
776 (unique-link-p
777 (function
778 ;; Return LINK if it wasn't referenced so far, or nil.
779 ;; Update SEEN links along the way.
780 (lambda (link)
781 (let ((footprint
782 (cons (org-element-property :raw-link link)
783 (org-element-contents link))))
784 (unless (member footprint seen)
785 (push footprint seen) link)))))
786 (harvest-links-in-title
787 (function
788 ;; Return a list of all unique links in ELEMENT. ELEMENT
789 ;; may be an headline or an inlinetask element.
790 (lambda (element)
791 (let (acc)
792 (dolist (obj (org-element-property :title element) acc)
793 (when (eq (org-element-type obj) 'link)
794 (let ((link (funcall unique-link-p obj)))
795 (and link (push link acc)))))))))
796 ;; Retrieve HEADLINE's section, if it exists.
797 (section (if (eq (org-element-type element) 'section) element
798 (let ((sec (car (org-element-contents element))))
799 (and (eq (org-element-type sec) 'section) sec))))
800 (headline (if (eq (org-element-type element) 'headline) element
801 (org-export-get-parent-headline element info))))
802 (append
803 ;; Links that may be in HEADLINE's title.
804 (funcall harvest-links-in-title headline)
805 ;; Get all links in SECTION.
806 (org-element-map
807 section 'link (lambda (link) (funcall unique-link-p link)) info))))
809 (defun org-e-ascii--describe-links (links width info)
810 "Return a string describing a list of links.
812 LINKS is a list of link type objects, as returned by
813 `org-e-ascii--unique-links'. WIDTH is the text width allowed for
814 the output string. INFO is a plist used as a communication
815 channel."
816 (mapconcat
817 (lambda (link)
818 (let ((type (org-element-property :type link))
819 (anchor (let ((desc (org-element-contents link)))
820 (if (not desc) (org-element-property :raw-link link)
821 (org-export-secondary-string desc 'e-ascii info)))))
822 (cond
823 ;; Coderefs, radio links and ref links are ignored.
824 ((member type '("coderef" "radio" "ref")) nil)
825 ;; Id, custom-id and fuzzy links (with the exception of
826 ;; targets): Headlines refer to their numbering.
827 ((member type '("custom-id" "fuzzy" "id"))
828 (let ((destination (if (string= type "fuzzy")
829 (org-export-resolve-fuzzy-link link info)
830 (org-export-resolve-id-link link info))))
831 (unless (eq (org-element-type destination) 'target)
832 (concat
833 (org-e-ascii--fill-string
834 (format
835 "[%s] %s"
836 anchor
837 (if (not destination)
838 (org-e-ascii--translate "Unknown reference" info)
839 (format
840 (org-e-ascii--translate "See section %s" info)
841 (mapconcat 'number-to-string
842 (org-export-get-headline-number destination info)
843 "."))))
844 width info) "\n\n"))))
845 ;; Do not add a link that cannot be resolved and doesn't have
846 ;; any description: destination is already visible in the
847 ;; paragraph.
848 ((not (org-element-contents link)) nil)
850 (concat
851 (org-e-ascii--fill-string
852 (format "[%s] %s" anchor (org-element-property :raw-link link))
853 width info)
854 "\n\n")))))
855 links ""))
859 ;;; Template
861 (defun org-e-ascii-template--document-title (info)
862 "Return document title, as a string.
863 INFO is a plist used as a communication channel."
864 (let ((text-width org-e-ascii-text-width)
865 (title (org-export-secondary-string
866 (plist-get info :title) 'e-ascii info))
867 (author
868 (and (plist-get info :with-author)
869 (let ((auth (plist-get info :author)))
870 (and auth (org-export-secondary-string auth 'e-ascii info)))))
871 (email
872 (and (plist-get info :with-email)
873 (org-export-secondary-string
874 (plist-get info :email) 'e-ascii info)))
875 (date (plist-get info :date)))
876 ;; There are two types of title blocks depending on the presence
877 ;; of a title to display.
878 (if (string= title "")
879 ;; Title block without a title. DATE is positioned at the top
880 ;; right of the document, AUTHOR to the top left and EMAIL
881 ;; just below.
882 (cond
883 ((and (org-string-nw-p date) (org-string-nw-p author))
884 (concat
885 author
886 (make-string (- text-width (length date) (length author)) ? )
887 date
888 (when (org-string-nw-p email) (concat "\n" email))
889 "\n\n\n"))
890 ((and (org-string-nw-p date) (org-string-nw-p email))
891 (concat
892 email
893 (make-string (- text-width (length date) (length email)) ? )
894 date "\n\n\n"))
895 ((org-string-nw-p date)
896 (concat
897 (org-e-ascii--justify-string date text-width 'right)
898 "\n\n\n"))
899 ((and (org-string-nw-p author) (org-string-nw-p email))
900 (concat author "\n" email "\n\n\n"))
901 ((org-string-nw-p author) (concat author "\n\n\n"))
902 ((org-string-nw-p email) (concat email "\n\n\n")))
903 ;; Title block with a title. Document's TITLE, along with the
904 ;; AUTHOR and its EMAIL are both overlined and an underlined,
905 ;; centered. Date is just below, also centered.
906 (let* ((utf8p (eq (plist-get info :ascii-charset) 'utf-8))
907 ;; Format TITLE. It may be filled if it is too wide,
908 ;; that is wider than the two thirds of the total width.
909 (title-len (min (length title) (/ (* 2 text-width) 3)))
910 (formatted-title (org-e-ascii--fill-string title title-len info))
911 (line
912 (make-string
913 (min (+ (max title-len (length author) (length email)) 2)
914 text-width) (if utf8p ?━ ?_))))
915 (org-e-ascii--justify-string
916 (concat line "\n"
917 (unless utf8p "\n")
918 (upcase formatted-title)
919 (cond
920 ((and (org-string-nw-p author) (org-string-nw-p email))
921 (concat (if utf8p "\n\n\n" "\n\n") author "\n" email))
922 ((org-string-nw-p author)
923 (concat (if utf8p "\n\n\n" "\n\n") author))
924 ((org-string-nw-p email)
925 (concat (if utf8p "\n\n\n" "\n\n") email)))
926 "\n" line
927 (when (org-string-nw-p date) (concat "\n\n\n" date))
928 "\n\n\n") text-width 'center)))))
930 (defun org-e-ascii-template (contents info)
931 "Return complete document string after ASCII conversion.
932 CONTENTS is the transcoded contents string. INFO is a plist
933 holding export options."
934 (org-element-normalize-string
935 (org-e-ascii--indent-string
936 (let ((text-width (- org-e-ascii-text-width org-e-ascii-global-margin)))
937 ;; 1. Build title block.
938 (concat
939 (org-e-ascii-template--document-title info)
940 ;; 2. Table of contents.
941 (let ((depth (plist-get info :with-toc)))
942 (when depth
943 (concat
944 (org-e-ascii--build-toc info (and (wholenump depth) depth))
945 "\n\n\n")))
946 ;; 3. Document's body.
947 contents
948 ;; 4. Footnote definitions.
949 (let ((definitions (org-export-collect-footnote-definitions
950 (plist-get info :parse-tree) info))
951 ;; Insert full links right inside the footnote definition
952 ;; as they have no chance to be inserted later.
953 (org-e-ascii-links-to-notes nil))
954 (when definitions
955 (concat
956 "\n\n\n"
957 (let ((title (org-e-ascii--translate "Footnotes\n" info)))
958 (concat
959 title
960 (make-string
961 (1- (length title))
962 (if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))))
963 "\n\n"
964 (mapconcat
965 (lambda (ref)
966 (let ((id (format "[%s] " (car ref))))
967 ;; Distinguish between inline definitions and
968 ;; full-fledged definitions.
969 (org-trim
970 (let ((def (nth 2 ref)))
971 (if (eq (org-element-type def) 'org-data)
972 ;; Full-fledged definition: footnote ID is
973 ;; inserted inside the first parsed paragraph
974 ;; (FIRST), if any, to be sure filling will
975 ;; take it into consideration.
976 (let ((first (car (org-element-contents def))))
977 (if (not (eq (org-element-type first) 'paragraph))
978 (concat id "\n" (org-export-data def 'e-ascii info))
979 (push id (nthcdr 2 first))
980 (org-export-data def 'e-ascii info)))
981 ;; Fill paragraph once footnote ID is inserted in
982 ;; order to have a correct length for first line.
983 (org-e-ascii--fill-string
984 (concat id (org-export-secondary-string def 'e-ascii info))
985 text-width info))))))
986 definitions "\n\n"))))
987 ;; 5. Creator. Ignore `comment' value as there are no comments in
988 ;; ASCII. Justify it to the bottom right.
989 (let ((creator-info (plist-get info :with-creator)))
990 (unless (or (not creator-info) (eq creator-info 'comment))
991 (concat
992 "\n\n\n"
993 (org-e-ascii--fill-string
994 (plist-get info :creator) text-width info 'right))))))
995 org-e-ascii-global-margin)))
997 (defun org-e-ascii--translate (s info)
998 "Translate string S.
1000 INFO is a plist used as a communication channel.
1002 Translation depends on `:language' property and allowed charset.
1003 If no translation in found for a given language and a given
1004 charset, fall-back to S."
1005 (let* ((charset (intern (format ":%s" (plist-get info :ascii-charset))))
1006 (lang (plist-get info :language))
1007 (translations (cdr (assoc s org-e-ascii-dictionary))))
1008 (or (plist-get (cdr (assoc lang translations)) charset) s)))
1012 ;;; Transcode Functions
1014 ;;;; Babel Call
1016 ;; Babel Calls are ignored.
1019 ;;;; Center Block
1021 (defun org-e-ascii-center-block (center-block contents info)
1022 "Transcode a CENTER-BLOCK element from Org to ASCII.
1023 CONTENTS holds the contents of the block. INFO is a plist
1024 holding contextual information."
1025 (org-e-ascii--justify-string
1026 contents (org-e-ascii--current-text-width center-block info) 'center))
1029 ;;;; Comment
1031 ;; Comments are ignored.
1034 ;;;; Comment Block
1036 ;; Comment Blocks are ignored.
1039 ;;;; Drawer
1041 (defun org-e-ascii-drawer (drawer contents info)
1042 "Transcode a DRAWER element from Org to ASCII.
1043 CONTENTS holds the contents of the block. INFO is a plist
1044 holding contextual information."
1045 (let ((name (org-element-property :drawer-name drawer))
1046 (width (org-e-ascii--current-text-width drawer info)))
1047 (if (functionp org-e-ascii-format-drawer-function)
1048 (funcall org-e-ascii-format-drawer-function name contents width)
1049 ;; If there's no user defined function: simply
1050 ;; display contents of the drawer.
1051 contents)))
1054 ;;;; Dynamic Block
1056 (defun org-e-ascii-dynamic-block (dynamic-block contents info)
1057 "Transcode a DYNAMIC-BLOCK element from Org to ASCII.
1058 CONTENTS holds the contents of the block. INFO is a plist
1059 holding contextual information. See
1060 `org-export-data'."
1061 contents)
1064 ;;;; Emphasis
1066 (defun org-e-ascii-emphasis (emphasis contents info)
1067 "Transcode EMPHASIS from Org to ASCII.
1068 CONTENTS is the contents of the emphasized text. INFO is a plist
1069 holding contextual information.."
1070 (let ((marker (org-element-property :marker emphasis)))
1071 ;; Leave emphasis markers as-is.
1072 (concat marker contents marker)))
1075 ;;;; Entity
1077 (defun org-e-ascii-entity (entity contents info)
1078 "Transcode an ENTITY object from Org to ASCII.
1079 CONTENTS are the definition itself. INFO is a plist holding
1080 contextual information."
1081 (org-element-property
1082 (intern (concat ":" (symbol-name (plist-get info :ascii-charset))))
1083 entity))
1086 ;;;; Example Block
1088 (defun org-e-ascii-example-block (example-block contents info)
1089 "Transcode a EXAMPLE-BLOCK element from Org to ASCII.
1090 CONTENTS is nil. INFO is a plist holding contextual information."
1091 (org-e-ascii--box-string (org-export-handle-code example-block info) info))
1094 ;;;; Export Snippet
1096 (defun org-e-ascii-export-snippet (export-snippet contents info)
1097 "Transcode a EXPORT-SNIPPET object from Org to ASCII.
1098 CONTENTS is nil. INFO is a plist holding contextual information."
1099 (when (eq (org-export-snippet-backend export-snippet) 'e-ascii)
1100 (org-element-property :value export-snippet)))
1103 ;;;; Export Block
1105 (defun org-e-ascii-export-block (export-block contents info)
1106 "Transcode a EXPORT-BLOCK element from Org to ASCII.
1107 CONTENTS is nil. INFO is a plist holding contextual information."
1108 (when (string= (org-element-property :type export-block) "ascii")
1109 (org-remove-indentation (org-element-property :value export-block))))
1112 ;;;; Fixed Width
1114 (defun org-e-ascii-fixed-width (fixed-width contents info)
1115 "Transcode a FIXED-WIDTH element from Org to ASCII.
1116 CONTENTS is nil. INFO is a plist holding contextual information."
1117 (org-e-ascii--box-string
1118 (replace-regexp-in-string
1119 "^[ \t]*: ?" "" (org-element-property :value fixed-width)) info))
1122 ;;;; Footnote Definition
1124 ;; Footnote Definitions are ignored. They are compiled at the end of
1125 ;; the document, by `org-e-ascii-template'.
1128 ;;;; Footnote Reference
1130 (defun org-e-ascii-footnote-reference (footnote-reference contents info)
1131 "Transcode a FOOTNOTE-REFERENCE element from Org to ASCII.
1132 CONTENTS is nil. INFO is a plist holding contextual information."
1133 (format "[%s]" (org-export-get-footnote-number footnote-reference info)))
1136 ;;;; Headline
1138 (defun org-e-ascii-headline (headline contents info)
1139 "Transcode an HEADLINE element from Org to ASCII.
1140 CONTENTS holds the contents of the headline. INFO is a plist
1141 holding contextual information."
1142 ;; Don't export footnote section, which will be handled at the end
1143 ;; of the template.
1144 (unless (org-element-property :footnote-section-p headline)
1145 (let* ((low-level-rank (org-export-low-level-p headline info))
1146 (width (org-e-ascii--current-text-width headline info))
1147 ;; Blank lines between headline and its contents.
1148 ;; `org-e-ascii-headline-spacing', when set, overwrites
1149 ;; original buffer's spacing.
1150 (pre-blanks
1151 (make-string
1152 (if org-e-ascii-headline-spacing (car org-e-ascii-headline-spacing)
1153 (org-element-property :pre-blank headline)) ?\n))
1154 ;; Even if HEADLINE has no section, there might be some
1155 ;; links in its title that we shouldn't forget to describe.
1156 (links
1157 (unless (eq (caar (org-element-contents headline)) 'section)
1158 (org-e-ascii--describe-links
1159 (org-e-ascii--unique-links headline info) width info))))
1160 ;; Deep subtree: export it as a list item.
1161 (if low-level-rank
1162 (concat
1163 ;; Bullet.
1164 (let ((bullets (cdr (assq (plist-get info :ascii-charset)
1165 org-e-ascii-bullets))))
1166 (char-to-string
1167 (nth (mod (1- low-level-rank) (length bullets)) bullets)))
1169 ;; Title.
1170 (org-e-ascii--build-title headline info width) "\n"
1171 ;; Contents, indented by length of bullet.
1172 pre-blanks
1173 (org-e-ascii--indent-string
1174 (concat contents
1175 (when (org-string-nw-p links) (concat "\n\n" links)))
1177 ;; Else: Standard headline.
1178 (concat
1179 (org-e-ascii--build-title headline info width 'underline)
1180 "\n" pre-blanks
1181 (concat (when (org-string-nw-p links) links) contents))))))
1184 ;;;; Horizontal Rule
1186 (defun org-e-ascii-horizontal-rule (horizontal-rule contents info)
1187 "Transcode an HORIZONTAL-RULE object from Org to ASCII.
1188 CONTENTS is nil. INFO is a plist holding contextual
1189 information."
1190 (let ((attr
1191 (read
1192 (format
1193 "(%s)"
1194 (mapconcat
1195 #'identity
1196 (org-element-property :attr_ascii horizontal-rule)
1197 " ")))))
1198 (make-string (or (and (wholenump (plist-get attr :width))
1199 (plist-get attr :width))
1200 (org-e-ascii--current-text-width horizontal-rule info))
1201 (if (eq (plist-get info :ascii-charset) 'utf-8) ?― ?-))))
1204 ;;;; Inline Babel Call
1206 ;; Inline Babel Calls are ignored.
1209 ;;;; Inline Src Block
1211 (defun org-e-ascii-inline-src-block (inline-src-block contents info)
1212 "Transcode an INLINE-SRC-BLOCK element from Org to ASCII.
1213 CONTENTS holds the contents of the item. INFO is a plist holding
1214 contextual information."
1215 (format org-e-ascii-verbatim-format
1216 (org-element-property :value inline-src-block)))
1219 ;;;; Inlinetask
1221 (defun org-e-ascii-inlinetask (inlinetask contents info)
1222 "Transcode an INLINETASK element from Org to ASCII.
1223 CONTENTS holds the contents of the block. INFO is a plist
1224 holding contextual information."
1225 (let ((width (org-e-ascii--current-text-width inlinetask info))
1226 (title (org-export-secondary-string
1227 (org-element-property :title inlinetask) 'e-ascii info))
1228 (todo (and (plist-get info :with-todo-keywords)
1229 (let ((todo (org-element-property
1230 :todo-keyword inlinetask)))
1231 (and todo
1232 (org-export-secondary-string todo 'e-ascii info)))))
1233 (todo-type (org-element-property :todo-type inlinetask))
1234 (tags (and (plist-get info :with-tags)
1235 (org-element-property :tags inlinetask)))
1236 (priority (and (plist-get info :with-priority)
1237 (org-element-property :priority inlinetask))))
1238 ;; If `org-e-ascii-format-inlinetask-function' is provided, call it
1239 ;; with appropriate arguments.
1240 (if (functionp org-e-ascii-format-inlinetask-function)
1241 (funcall org-e-ascii-format-inlinetask-function
1242 todo todo-type priority title tags contents width)
1243 ;; Otherwise, use a default template.
1244 (let* ((utf8p (eq (plist-get info :ascii-charset) 'utf-8)))
1245 (org-e-ascii--indent-string
1246 (concat
1247 ;; Top line, with an additional blank line if not in UTF-8.
1248 (make-string width (if utf8p ?━ ?_)) "\n"
1249 (unless utf8p (concat (make-string width ? ) "\n"))
1250 ;; Add title. Fill it if wider than inlinetask.
1251 (let ((title (org-e-ascii--build-title inlinetask info width)))
1252 (if (<= (length title) width) title
1253 (org-e-ascii--fill-string title width info)))
1254 "\n"
1255 ;; If CONTENTS is not empty, insert it along with
1256 ;; a separator.
1257 (when (org-string-nw-p contents)
1258 (concat (make-string width (if utf8p ?─ ?-)) "\n" contents))
1259 ;; Bottom line.
1260 (make-string width (if utf8p ?━ ?_)))
1261 ;; Flush the inlinetask to the right.
1262 (- org-e-ascii-text-width org-e-ascii-global-margin
1263 (if (not (org-export-get-parent-headline inlinetask info)) 0
1264 org-e-ascii-inner-margin)
1265 (org-e-ascii--current-text-width inlinetask info)))))))
1268 ;;;; Item
1270 (defun org-e-ascii-item (item contents info)
1271 "Transcode an ITEM element from Org to ASCII.
1272 CONTENTS holds the contents of the item. INFO is a plist holding
1273 contextual information."
1274 (let ((bullet
1275 ;; First parent of ITEM is always the plain-list. Get
1276 ;; `:type' property from it.
1277 (org-list-bullet-string
1278 (case (org-element-property :type (org-export-get-parent item info))
1279 (descriptive
1280 (concat
1281 (org-export-secondary-string
1282 (org-element-property :tag item) 'e-ascii info) ": "))
1283 (ordered
1284 ;; Return correct number for ITEM, paying attention to
1285 ;; counters.
1286 (let* ((struct (org-element-property :structure item))
1287 (bul (org-element-property :bullet item))
1288 (num
1289 (number-to-string
1290 (car (last (org-list-get-item-number
1291 (org-element-property :begin item)
1292 struct
1293 (org-list-prevs-alist struct)
1294 (org-list-parents-alist struct)))))))
1295 (replace-regexp-in-string "[0-9]+" num bul)))
1296 (t (let ((bul (org-element-property :bullet item)))
1297 ;; Change bullets into more visible form if UTF-8 is active.
1298 (if (not (eq (plist-get info :ascii-charset) 'utf-8)) bul
1299 (replace-regexp-in-string
1300 "-" "•"
1301 (replace-regexp-in-string
1302 "+" "⁃"
1303 (replace-regexp-in-string "*" "‣" bul))))))))))
1304 (concat
1305 bullet
1306 ;; Contents: Pay attention to indentation. Note: check-boxes are
1307 ;; already taken care of at the paragraph level so they don't
1308 ;; interfere with indentation.
1309 (let ((contents (org-e-ascii--indent-string contents (length bullet))))
1310 (if (eq (caar (org-element-contents item)) 'paragraph)
1311 (org-trim contents)
1312 (concat "\n" contents))))))
1315 ;;;; Keyword
1317 (defun org-e-ascii-keyword (keyword contents info)
1318 "Transcode a KEYWORD element from Org to ASCII.
1319 CONTENTS is nil. INFO is a plist holding contextual
1320 information."
1321 (let ((key (downcase (org-element-property :key keyword)))
1322 (value (org-element-property :value keyword)))
1323 (cond
1324 ((string= key "ascii") value)
1325 ((string= key "toc")
1326 (let ((value (downcase value)))
1327 (cond
1328 ((string-match "\\<headlines\\>" value)
1329 (let ((depth (or (and (string-match "[0-9]+" value)
1330 (string-to-number (match-string 0 value)))
1331 (plist-get info :with-toc))))
1332 (org-e-ascii--build-toc
1333 info (and (wholenump depth) depth) keyword)))
1334 ((string= "tables" value)
1335 (org-e-ascii--list-tables keyword info))
1336 ((string= "listings" value)
1337 (org-e-ascii--list-listings keyword info))))))))
1340 ;;;; Latex Environment
1342 (defun org-e-ascii-latex-environment (latex-environment contents info)
1343 "Transcode a LATEX-ENVIRONMENT element from Org to ASCII.
1344 CONTENTS is nil. INFO is a plist holding contextual
1345 information."
1346 (org-remove-indentation (org-element-property :value latex-environment)))
1349 ;;;; Latex Fragment
1351 (defun org-e-ascii-latex-fragment (latex-fragment contents info)
1352 "Transcode a LATEX-FRAGMENT object from Org to ASCII.
1353 CONTENTS is nil. INFO is a plist holding contextual
1354 information."
1355 (org-element-property :value latex-fragment))
1358 ;;;; Line Break
1360 (defun org-e-ascii-line-break (line-break contents info)
1361 "Transcode a LINE-BREAK object from Org to ASCII.
1362 CONTENTS is nil. INFO is a plist holding contextual
1363 information." hard-newline)
1366 ;;;; Link
1368 (defun org-e-ascii-link (link desc info)
1369 "Transcode a LINK object from Org to ASCII.
1371 DESC is the description part of the link, or the empty string.
1372 INFO is a plist holding contextual information."
1373 (let ((raw-link (org-element-property :raw-link link))
1374 (type (org-element-property :type link)))
1375 (cond
1376 ((string= type "coderef")
1377 (let ((ref (org-element-property :path link)))
1378 (format (org-export-get-coderef-format ref desc)
1379 (org-export-resolve-coderef ref info))))
1380 ;; Do not apply a special syntax on radio links. Though, parse
1381 ;; and transcode path to have a proper display of contents.
1382 ((string= type "radio")
1383 (org-export-secondary-string
1384 (org-element-parse-secondary-string
1385 (org-element-property :path link)
1386 (cdr (assq 'radio-target org-element-object-restrictions)))
1387 'e-ascii info))
1388 ;; Ref link: If there's no description (DESC, return link's
1389 ;; destination sequence number among elements of same
1390 ;; type. Otherwise, use DESC.
1391 ((string= type "ref")
1392 (if (org-string-nw-p desc) desc
1393 (format "%d"
1394 (org-export-get-ordinal
1395 (org-export-resolve-ref-link link info)
1396 info nil nil
1397 (lambda (el) (or (org-element-property :caption el)
1398 (org-element-property :name el)))))))
1399 ;; Do not apply a special syntax on fuzzy links pointing to
1400 ;; targets.
1401 ((and (string= type "fuzzy")
1402 (let ((path (org-element-property :path link)))
1403 (loop for target in (plist-get info :target-list)
1404 thereis (string=
1405 (org-element-property :raw-value target)
1406 path))))
1407 (if (org-string-nw-p desc) desc raw-link))
1409 (concat (format "[%s]" (if (org-string-nw-p desc) desc raw-link))
1410 (unless org-e-ascii-links-to-notes (format " (%s)" raw-link)))))))
1413 ;;;; Macro
1415 (defun org-e-ascii-macro (macro contents info)
1416 "Transcode a MACRO element from Org to ASCII.
1417 CONTENTS is nil. INFO is a plist holding contextual
1418 information."
1419 (org-export-expand-macro macro info))
1422 ;;;; Paragraph
1424 (defun org-e-ascii-paragraph (paragraph contents info)
1425 "Transcode a PARAGRAPH element from Org to ASCII.
1426 CONTENTS is the contents of the paragraph, as a string. INFO is
1427 the plist used as a communication channel."
1428 (org-e-ascii--fill-string
1429 (let ((parent (org-export-get-parent paragraph info)))
1430 ;; If PARAGRAPH is the first one in a list element, be sure to
1431 ;; add the check-box in front of it, before any filling. Later,
1432 ;; it would interfere with line width.
1433 (if (and (eq (org-element-type parent) 'item)
1434 (equal (car (org-element-contents parent)) paragraph))
1435 (let ((utf8p (eq (plist-get info :ascii-charset) 'utf-8)))
1436 (concat (case (org-element-property :checkbox parent)
1437 (on (if utf8p "☑ " "[X] "))
1438 (off (if utf8p "☐ " "[ ] "))
1439 (trans (if utf8p "☒ " "[-] ")))
1440 contents))
1441 contents))
1442 (org-e-ascii--current-text-width paragraph info) info))
1445 ;;;; Plain List
1447 (defun org-e-ascii-plain-list (plain-list contents info)
1448 "Transcode a PLAIN-LIST element from Org to ASCII.
1449 CONTENTS is the contents of the list. INFO is a plist holding
1450 contextual information."
1451 contents)
1454 ;;;; Plain Text
1456 (defun org-e-ascii-plain-text (text info)
1457 "Transcode a TEXT string from Org to ASCII.
1458 INFO is a plist used as a communication channel."
1459 (if (not (and (eq (plist-get info :ascii-charset) 'utf-8)
1460 (plist-get info :with-special-strings)))
1461 text
1462 ;; Usual replacements in utf-8 with proper option set.
1463 (replace-regexp-in-string
1464 "\\.\\.\\." "…"
1465 (replace-regexp-in-string
1466 "--" "–"
1467 (replace-regexp-in-string "---" "—" text)))))
1470 ;;;; Property Drawer
1472 (defun org-e-ascii-property-drawer (property-drawer contents info)
1473 "Transcode a PROPERTY-DRAWER element from Org to ASCII.
1474 CONTENTS is nil. INFO is a plist used as a communication
1475 channel."
1476 ;; The property drawer isn't exported but we want separating blank
1477 ;; lines nonetheless.
1481 ;;;; Quote Block
1483 (defun org-e-ascii-quote-block (quote-block contents info)
1484 "Transcode a QUOTE-BLOCK element from Org to ASCII.
1485 CONTENTS holds the contents of the block. INFO is a plist
1486 holding contextual information."
1487 (let ((width (org-e-ascii--current-text-width quote-block info)))
1488 (org-e-ascii--indent-string
1489 (org-remove-indentation
1490 (org-e-ascii--fill-string contents width info))
1491 org-e-ascii-quote-margin)))
1494 ;;;; Quote Section
1496 (defun org-e-ascii-quote-section (quote-section contents info)
1497 "Transcode a QUOTE-SECTION element from Org to ASCII.
1498 CONTENTS is nil. INFO is a plist holding contextual information."
1499 (let ((width (org-e-ascii--current-text-width quote-section info))
1500 (value
1501 (org-export-secondary-string
1502 (org-remove-indentation
1503 (org-element-property :value quote-section)) 'e-ascii info)))
1504 (org-e-ascii--indent-string
1505 value
1506 (+ org-e-ascii-quote-margin
1507 ;; Don't apply inner margin if parent headline is low level.
1508 (let ((headline (org-export-get-parent-headline quote-section info)))
1509 (if (org-export-low-level-p headline info) 0
1510 org-e-ascii-inner-margin))))))
1513 ;;;; Radio Target
1515 (defun org-e-ascii-radio-target (radio-target contents info)
1516 "Transcode a RADIO-TARGET object from Org to ASCII.
1517 CONTENTS is the contents of the target. INFO is a plist holding
1518 contextual information."
1519 contents)
1521 ;;;; Section
1523 (defun org-e-ascii-section (section contents info)
1524 "Transcode a SECTION element from Org to ASCII.
1525 CONTENTS is the contents of the section. INFO is a plist holding
1526 contextual information."
1527 (org-e-ascii--indent-string
1528 (concat
1529 contents
1530 (when org-e-ascii-links-to-notes
1531 ;; Add list of links at the end of SECTION.
1532 (let ((links (org-e-ascii--describe-links
1533 (org-e-ascii--unique-links section info)
1534 (org-e-ascii--current-text-width section info) info)))
1535 ;; Separate list of links and section contents.
1536 (when (org-string-nw-p links) (concat "\n\n" links)))))
1537 ;; Do not apply inner margin if parent headline is low level.
1538 (let ((headline (org-export-get-parent-headline section info)))
1539 (if (or (not headline) (org-export-low-level-p headline info)) 0
1540 org-e-ascii-inner-margin))))
1543 ;;;; Special Block
1545 (defun org-e-ascii-special-block (special-block contents info)
1546 "Transcode a SPECIAL-BLOCK element from Org to ASCII.
1547 CONTENTS holds the contents of the block. INFO is a plist
1548 holding contextual information."
1549 contents)
1552 ;;;; Src Block
1554 (defun org-e-ascii-src-block (src-block contents info)
1555 "Transcode a SRC-BLOCK element from Org to ASCII.
1556 CONTENTS holds the contents of the item. INFO is a plist holding
1557 contextual information."
1558 (let ((caption (org-e-ascii--build-caption src-block info)))
1559 (concat
1560 (when (and caption org-e-ascii-caption-above) (concat caption "\n"))
1561 (org-e-ascii--box-string (org-export-handle-code src-block info) info)
1562 (when (and caption (not org-e-ascii-caption-above))
1563 (concat "\n" caption)))))
1565 ;;;; Statistics Cookie
1567 (defun org-e-ascii-statistics-cookie (statistics-cookie contents info)
1568 "Transcode a STATISTICS-COOKIE object from Org to ASCII.
1569 CONTENTS is nil. INFO is a plist holding contextual information."
1570 (org-element-property :value statistics-cookie))
1573 ;;;; Subscript
1575 (defun org-e-ascii-subscript (subscript contents info)
1576 "Transcode a SUBSCRIPT object from Org to ASCII.
1577 CONTENTS is the contents of the object. INFO is a plist holding
1578 contextual information."
1579 (if (org-element-property :use-brackets-p subscript)
1580 (format "_{%s}" contents)
1581 (format "_%s" contents)))
1584 ;;;; Superscript
1586 (defun org-e-ascii-superscript (superscript contents info)
1587 "Transcode a SUPERSCRIPT object from Org to ASCII.
1588 CONTENTS is the contents of the object. INFO is a plist holding
1589 contextual information."
1590 (if (org-element-property :use-brackets-p superscript)
1591 (format "_{%s}" contents)
1592 (format "_%s" contents)))
1595 ;;;; Table
1597 ;; While `org-e-ascii-table' is the callback function expected by
1598 ;; org-export mechanism, it requires four subroutines to display
1599 ;; tables accordingly to chosen charset, alignment and width
1600 ;; specifications.
1602 ;; Thus, `org-e-ascii-table--column-width' computes the display width
1603 ;; for each column in the table,
1604 ;; `org-e-ascii-table--vertical-separators' returns a vector
1605 ;; containing separators (or lack thereof),
1606 ;; `org-e-ascii-table--build-hline' creates various hline strings,
1607 ;; depending on charset, separators and position within the tabl and
1608 ;; `org-e-ascii-table--format-cell' properly aligns contents within
1609 ;; a given cell and width.
1611 (defun org-e-ascii-table (table contents info)
1612 "Transcode a TABLE element from Org to ASCII.
1613 CONTENTS is nil. INFO is a plist holding contextual information."
1614 (let ((raw-table (org-element-property :raw-table table))
1615 (caption (org-e-ascii--build-caption table info)))
1616 (concat
1617 ;; Possibly add a caption string above.
1618 (when (and caption org-e-ascii-caption-above) (concat caption "\n"))
1619 ;; Insert table. Note: "table.el" tables are left unmodified.
1620 (if (eq (org-element-property :type table) 'table.el) raw-table
1621 (let* ((utf8p (eq (plist-get info :ascii-charset) 'utf-8))
1622 ;; Extract information out of the raw table (TABLE-INFO)
1623 ;; and clean it (CLEAN-TABLE).
1624 (table-info (org-export-table-format-info raw-table))
1625 (special-col-p (plist-get table-info :special-column-p))
1626 (alignment (plist-get table-info :alignment))
1627 (clean-table (org-export-clean-table raw-table special-col-p))
1628 ;; Change table into lisp, much like
1629 ;; `org-table-to-lisp', though cells are parsed and
1630 ;; transcoded along the way.
1631 (lisp-table
1632 (mapcar
1633 (lambda (line)
1634 (if (string-match org-table-hline-regexp line) 'hline
1635 (mapcar
1636 (lambda (cell)
1637 (org-trim
1638 (org-export-secondary-string
1639 (org-element-parse-secondary-string
1640 cell
1641 (cdr (assq 'item org-element-string-restrictions)))
1642 'e-ascii info)))
1643 (org-split-string (org-trim line) "\\s-?|\\s-?"))))
1644 (org-split-string clean-table "[ \t]*\n[ \t]*")))
1645 ;; Compute real column widths.
1646 (column-widths
1647 (org-e-ascii-table--column-width lisp-table table-info))
1648 ;; Construct separators according to column groups.
1649 (separators (org-e-ascii-table--vertical-separators table-info))
1650 ;; Build different `hline' strings, depending on
1651 ;; separators, column widths and position.
1652 (hline-standard
1653 (org-e-ascii-table--build-hline
1654 nil separators column-widths info))
1655 (hline-top
1656 (and utf8p (org-e-ascii-table--build-hline
1657 'top separators column-widths info)))
1658 (hline-bottom
1659 (and utf8p (org-e-ascii-table--build-hline
1660 'bottom separators column-widths info))))
1661 ;; Now build table back, with correct alignment, considering
1662 ;; columns widths and separators.
1663 (mapconcat
1664 (lambda (line)
1665 (cond
1666 ((eq line 'hline) hline-standard)
1667 ((eq line 'hline-bottom) hline-bottom)
1668 ((eq line 'hline-top) hline-top)
1669 (t (loop for cell in line
1670 for col from 0 to (length line)
1671 concat
1672 (concat
1673 (let ((sep (aref separators col)))
1674 (if (and utf8p (not (string= sep ""))) "│" sep))
1675 (org-e-ascii-table--format-cell
1676 cell col column-widths alignment info)) into l
1677 finally return
1678 (concat l
1679 (let ((sep (aref separators col)))
1680 (if (and utf8p (not (string= sep ""))) "│"
1681 sep)))))))
1682 ;; If charset is `utf-8', make sure lisp-table always starts
1683 ;; with `hline-top' and ends with `hline-bottom'.
1684 (if (not utf8p) lisp-table
1685 (setq lisp-table
1686 (cons 'hline-top
1687 (if (eq (car lisp-table) 'hline) (cdr lisp-table)
1688 lisp-table)))
1689 (setq lisp-table
1690 (nconc
1691 (if (eq (car (last lisp-table)) 'hline) (butlast lisp-table)
1692 lisp-table)
1693 '(hline-bottom)))) "\n")))
1694 ;; Possible add a caption string below.
1695 (when (and caption (not org-e-ascii-caption-above))
1696 (concat "\n" caption)))))
1698 (defun org-e-ascii-table--column-width (table table-info)
1699 "Return vector of TABLE columns width.
1701 TABLE is the Lisp representation of the Org table considered.
1702 TABLE-INFO holds information about the table. See
1703 `org-export-table-format-info'.
1705 Unlike to `:width' property from `org-export-table-format-info',
1706 the return value is a vector containing width of every column,
1707 not only those with an explicit width cookie. Special column, if
1708 any, is ignored."
1709 ;; All rows have the same length, but be sure to ignore hlines.
1710 (let ((width (make-vector
1711 (loop for row in table
1712 unless (eq row 'hline)
1713 return (length row))
1714 0)))
1715 ;; Set column width to the maximum width of the cells in that
1716 ;; column.
1717 (mapc
1718 (lambda (line)
1719 (let ((idx 0))
1720 (unless (eq line 'hline)
1721 (mapc (lambda (cell)
1722 (let ((len (length cell)))
1723 (when (> len (aref width idx)) (aset width idx len)))
1724 (incf idx))
1725 line))))
1726 table)
1727 (unless org-e-ascii-table-widen-columns
1728 ;; When colums are not widened, width cookies have precedence
1729 ;; over string lengths. Thus, overwrite the latter with the
1730 ;; former.
1731 (let ((cookies (plist-get table-info :width))
1732 (specialp (plist-get table-info :special-column-p)))
1733 ;; Remove special column from COOKIES vector, if any.
1734 (loop for w across (if specialp (substring cookies 1) cookies)
1735 for idx from 0 to width
1736 when w do (aset width idx w))))
1737 ;; Return value.
1738 width))
1740 (defun org-e-ascii-table--vertical-separators (table-info)
1741 "Return a vector of strings for vertical separators.
1743 TABLE-INFO holds information about considered table. See
1744 `org-export-table-format-info'.
1746 Return value is a vector whose length is one more than the number
1747 of columns in the table. Special column, if any, is ignored."
1748 (let* ((colgroups (plist-get table-info :column-groups))
1749 (separators (make-vector (1+ (length colgroups)) "")))
1750 (if org-e-ascii-table-keep-all-vertical-lines
1751 (make-vector (length separators) "|")
1752 (let ((column 0))
1753 (mapc (lambda (group)
1754 (when (memq group '(start start-end))
1755 (aset separators column "|"))
1756 (when (memq group '(end start-end))
1757 (aset separators (1+ column) "|"))
1758 (incf column))
1759 colgroups)
1760 ;; Remove unneeded special column.
1761 (if (not (plist-get table-info :special-column-p)) separators
1762 (substring separators 1))))))
1764 (defun org-e-ascii-table--format-cell (cell col width alignment info)
1765 "Format CELL with column width and alignment constraints.
1767 CELL is the contents of the cell, as a string.
1769 COL is the column containing the cell considered.
1771 WIDTH is a vector holding every column width, as returned by
1772 `org-e-ascii-table--column-width'.
1774 ALIGNMENT is a vector containing alignment strings for every
1775 column.
1777 INFO is a plist used as a communication channel."
1778 (let ((col-width (if org-e-ascii-table-widen-columns (aref width col)
1779 (or (aref width col) (length cell)))))
1780 ;; When CELL is too large, it has to be truncated.
1781 (unless (or org-e-ascii-table-widen-columns (<= (length cell) col-width))
1782 (setq cell (concat (substring cell 0 (- col-width 2)) "=>")))
1783 (let* ((indent-tabs-mode nil)
1784 (align (aref alignment col))
1785 (aligned-cell
1786 (org-e-ascii--justify-string
1787 (org-trim cell) col-width
1788 (cond ((string= align "c") 'center)
1789 ((string= align "r") 'right)))))
1790 ;; Return aligned cell, with missing white spaces added and
1791 ;; space separators between columns.
1792 (format
1793 " %s "
1794 (concat aligned-cell
1795 (make-string (- col-width (length aligned-cell)) ? ))))))
1797 (defun org-e-ascii-table--build-hline (position separators column-widths info)
1798 "Return string used as an horizontal line in tables.
1800 POSITION is a symbol among `top', `bottom' and nil, which
1801 specifies position of the horizontal line within the table.
1803 SEPARATORS is a vector strings specifying separators used in the
1804 table, as returned by `org-e-ascii-table--vertical-separators'.
1806 COLUMN-WIDTHS is a vector of numbers specifying widths of all
1807 columns in the table, as returned by
1808 `org-e-ascii-table--column-width'.
1810 INFO is a plist used as a communication channel."
1811 (let ((utf8p (eq (plist-get info :ascii-charset) 'utf-8)))
1812 (loop for idx from 0 to (length separators)
1813 for width across column-widths
1814 concat
1815 (concat
1816 (cond ((string= (aref separators idx) "") nil)
1817 ((and utf8p (zerop idx))
1818 (cond ((eq position 'top) "┍")
1819 ((eq position 'bottom) "┕")
1820 (t "├")))
1821 (utf8p
1822 (cond ((eq position 'top) "┯")
1823 ((eq position 'bottom) "┷")
1824 (t "┼")))
1825 (t "+"))
1826 ;; Hline has to cover all the cell and both white spaces
1827 ;; between columns.
1828 (make-string (+ width 2)
1829 (cond ((not utf8p) ?-)
1830 ((not position) ?─)
1831 (t ?━))))
1832 into hline
1833 finally return
1834 ;; There is one separator more than columns, so handle it
1835 ;; here.
1836 (concat
1837 hline
1838 (cond
1839 ((string= (aref separators idx) "") nil)
1840 (utf8p (cond ((eq position 'top) "┑")
1841 ((eq position 'bottom) "┙")
1842 (t "┤")))
1843 (t "+"))))))
1846 ;;;; Target
1848 (defun org-e-ascii-target (target contents info)
1849 "Transcode a TARGET object from Org to ASCII.
1850 CONTENTS is the contents of the target. INFO is a plist holding
1851 contextual information."
1852 contents)
1855 ;;;; Time-stamp
1857 (defun org-e-ascii-time-stamp (time-stamp contents info)
1858 "Transcode a TIME-STAMP object from Org to ASCII.
1859 CONTENTS is nil. INFO is a plist holding contextual information."
1860 ;; Return time-stamps as-is.
1861 (org-element-time-stamp-interpreter time-stamp contents))
1864 ;;;; Verbatim
1866 (defun org-e-ascii-verbatim (verbatim contents info)
1867 "Return a VERBATIM object from Org to ASCII.
1868 CONTENTS is nil. INFO is a plist holding contextual information."
1869 (format org-e-ascii-verbatim-format
1870 (org-element-property :value verbatim)))
1873 ;;;; Verse Block
1875 (defun org-e-ascii-verse-block (verse-block contents info)
1876 "Transcode a VERSE-BLOCK element from Org to ASCII.
1877 CONTENTS is nil. INFO is a plist holding contextual information."
1878 (let ((verse-width (org-e-ascii--current-text-width verse-block info)))
1879 (org-e-ascii--indent-string
1880 (org-e-ascii--justify-string
1881 (org-export-secondary-string
1882 (org-element-property :value verse-block) 'e-ascii info)
1883 verse-width 'left)
1884 org-e-ascii-quote-margin)))
1887 ;;; Filter
1889 (defun org-e-ascii-filter-headline-blank-lines (headline back-end info)
1890 "Filter controlling number of blank lines after an headline.
1892 HEADLINE is a string representing a transcoded headline.
1893 BACK-END is symbol specifying back-end used for export. INFO is
1894 plist containing the communication channel.
1896 This function only applies to `e-ascii' back-end. See
1897 `org-e-ascii-headline-spacing' for information.
1899 For any other back-end, HEADLINE is returned as-is."
1900 (if (not (and (eq back-end 'e-ascii) org-e-ascii-headline-spacing)) headline
1901 (let ((blanks (make-string (1+ (cdr org-e-ascii-headline-spacing)) ?\n)))
1902 (replace-regexp-in-string "\n\\(?:\n[ \t]*\\)*\\'" blanks headline))))
1906 ;;; Interactive function
1908 (defun org-e-ascii-export-to-ascii
1909 (&optional subtreep visible-only body-only ext-plist pub-dir)
1910 "Export current buffer to a text file.
1912 If narrowing is active in the current buffer, only export its
1913 narrowed part.
1915 If a region is active, export that region.
1917 When optional argument SUBTREEP is non-nil, export the sub-tree
1918 at point, extracting information from the headline properties
1919 first.
1921 When optional argument VISIBLE-ONLY is non-nil, don't export
1922 contents of hidden elements.
1924 When optional argument BODY-ONLY is non-nil, strip title, table
1925 of contents and footnote definitions from output.
1927 EXT-PLIST, when provided, is a property list with external
1928 parameters overriding Org default settings, but still inferior to
1929 file-local settings.
1931 When optional argument PUB-DIR is set, use it as the publishing
1932 directory.
1934 Return output file's name."
1935 (interactive)
1936 (let ((outfile (org-export-output-file-name ".txt" subtreep pub-dir)))
1937 (org-export-to-file
1938 'e-ascii outfile subtreep visible-only body-only ext-plist)))
1941 (provide 'org-e-ascii)
1942 ;;; org-e-ascii.el ends here