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