org-agenda.el (org-search-view): Fix checking `org-agenda-show-inherited-tags'
[org-mode/org-tableheadings.git] / contrib / lisp / org-e-ascii.el
blob8391664cbe43abfb0e092feb810dd148370a08d9
1 ;;; org-e-ascii.el --- ASCII Back-End For Org Export Engine
3 ;; Copyright (C) 2012, 2013 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 ;; It provides two commands for export, depending on the desired
26 ;; output: `org-e-ascii-export-as-ascii' (temporary buffer) and
27 ;; `org-e-ascii-export-to-ascii' ("txt" file).
29 ;; Output encoding is specified through `org-e-ascii-charset'
30 ;; variable, among `ascii', `latin1' and `utf-8' symbols.
32 ;; By default, horizontal rules span over the full text with, but with
33 ;; a given width attribute (set though #+ATTR_ASCII: :width <num>)
34 ;; they can be shortened and centered.
36 ;;; Code:
38 (eval-when-compile (require 'cl))
39 (require 'org-export)
41 (declare-function aa2u "ext:ascii-art-to-unicode" ())
43 ;;; Define Back-End
45 ;; The following setting won't allow to modify preferred charset
46 ;; through a buffer keyword or an option item, but, since the property
47 ;; will appear in communication channel nonetheless, it allows to
48 ;; override `org-e-ascii-charset' variable on the fly by the ext-plist
49 ;; mechanism.
51 ;; We also install a filter for headlines and sections, in order to
52 ;; control blank lines separating them in output string.
54 (org-export-define-backend e-ascii
55 ((bold . org-e-ascii-bold)
56 (center-block . org-e-ascii-center-block)
57 (clock . org-e-ascii-clock)
58 (code . org-e-ascii-code)
59 (drawer . org-e-ascii-drawer)
60 (dynamic-block . org-e-ascii-dynamic-block)
61 (entity . org-e-ascii-entity)
62 (example-block . org-e-ascii-example-block)
63 (export-block . org-e-ascii-export-block)
64 (export-snippet . org-e-ascii-export-snippet)
65 (fixed-width . org-e-ascii-fixed-width)
66 (footnote-definition . org-e-ascii-footnote-definition)
67 (footnote-reference . org-e-ascii-footnote-reference)
68 (headline . org-e-ascii-headline)
69 (horizontal-rule . org-e-ascii-horizontal-rule)
70 (inline-src-block . org-e-ascii-inline-src-block)
71 (inlinetask . org-e-ascii-inlinetask)
72 (italic . org-e-ascii-italic)
73 (item . org-e-ascii-item)
74 (keyword . org-e-ascii-keyword)
75 (latex-environment . org-e-ascii-latex-environment)
76 (latex-fragment . org-e-ascii-latex-fragment)
77 (line-break . org-e-ascii-line-break)
78 (link . org-e-ascii-link)
79 (macro . org-e-ascii-macro)
80 (paragraph . org-e-ascii-paragraph)
81 (plain-list . org-e-ascii-plain-list)
82 (plain-text . org-e-ascii-plain-text)
83 (planning . org-e-ascii-planning)
84 (quote-block . org-e-ascii-quote-block)
85 (quote-section . org-e-ascii-quote-section)
86 (radio-target . org-e-ascii-radio-target)
87 (section . org-e-ascii-section)
88 (special-block . org-e-ascii-special-block)
89 (src-block . org-e-ascii-src-block)
90 (statistics-cookie . org-e-ascii-statistics-cookie)
91 (strike-through . org-e-ascii-strike-through)
92 (subscript . org-e-ascii-subscript)
93 (superscript . org-e-ascii-superscript)
94 (table . org-e-ascii-table)
95 (table-cell . org-e-ascii-table-cell)
96 (table-row . org-e-ascii-table-row)
97 (target . org-e-ascii-target)
98 (template . org-e-ascii-template)
99 (timestamp . org-e-ascii-timestamp)
100 (underline . org-e-ascii-underline)
101 (verbatim . org-e-ascii-verbatim)
102 (verse-block . org-e-ascii-verse-block))
103 :export-block "ASCII"
104 :filters-alist ((:filter-headline . org-e-ascii-filter-headline-blank-lines)
105 (:filter-section . org-e-ascii-filter-headline-blank-lines))
106 :options-alist ((:ascii-charset nil nil org-e-ascii-charset)))
110 ;;; User Configurable Variables
112 (defgroup org-export-e-ascii nil
113 "Options for exporting Org mode files to ASCII."
114 :tag "Org Export ASCII"
115 :group 'org-export)
117 (defcustom org-e-ascii-text-width 72
118 "Maximum width of exported text.
119 This number includes margin size, as set in
120 `org-e-ascii-global-margin'."
121 :group 'org-export-e-ascii
122 :type 'integer)
124 (defcustom org-e-ascii-global-margin 0
125 "Width of the left margin, in number of characters."
126 :group 'org-export-e-ascii
127 :type 'integer)
129 (defcustom org-e-ascii-inner-margin 2
130 "Width of the inner margin, in number of characters.
131 Inner margin is applied between each headline."
132 :group 'org-export-e-ascii
133 :type 'integer)
135 (defcustom org-e-ascii-quote-margin 6
136 "Width of margin used for quoting text, in characters.
137 This margin is applied on both sides of the text."
138 :group 'org-export-e-ascii
139 :type 'integer)
141 (defcustom org-e-ascii-inlinetask-width 30
142 "Width of inline tasks, in number of characters.
143 This number ignores any margin."
144 :group 'org-export-e-ascii
145 :type 'integer)
147 (defcustom org-e-ascii-headline-spacing '(1 . 2)
148 "Number of blank lines inserted around headlines.
150 This variable can be set to a cons cell. In that case, its car
151 represents the number of blank lines present before headline
152 contents whereas its cdr reflects the number of blank lines after
153 contents.
155 A nil value replicates the number of blank lines found in the
156 original Org buffer at the same place."
157 :group 'org-export-e-ascii
158 :type '(choice
159 (const :tag "Replicate original spacing" nil)
160 (cons :tag "Set an uniform spacing"
161 (integer :tag "Number of blank lines before contents")
162 (integer :tag "Number of blank lines after contents"))))
164 (defcustom org-e-ascii-charset 'ascii
165 "The charset allowed to represent various elements and objects.
166 Possible values are:
167 `ascii' Only use plain ASCII characters
168 `latin1' Include Latin-1 characters
169 `utf-8' Use all UTF-8 characters"
170 :group 'org-export-e-ascii
171 :type '(choice
172 (const :tag "ASCII" ascii)
173 (const :tag "Latin-1" latin1)
174 (const :tag "UTF-8" utf-8)))
176 (defcustom org-e-ascii-underline '((ascii ?= ?~ ?-)
177 (latin1 ?= ?~ ?-)
178 (utf-8 ?═ ?─ ?╌ ?┄ ?┈))
179 "Characters for underlining headings in ASCII export.
181 Alist whose key is a symbol among `ascii', `latin1' and `utf-8'
182 and whose value is a list of characters.
184 For each supported charset, this variable associates a sequence
185 of underline characters. In a sequence, the characters will be
186 used in order for headlines level 1, 2, ... If no character is
187 available for a given level, the headline won't be underlined."
188 :group 'org-export-e-ascii
189 :type '(list
190 (cons :tag "Underline characters sequence"
191 (const :tag "ASCII charset" ascii)
192 (repeat character))
193 (cons :tag "Underline characters sequence"
194 (const :tag "Latin-1 charset" latin1)
195 (repeat character))
196 (cons :tag "Underline characters sequence"
197 (const :tag "UTF-8 charset" utf-8)
198 (repeat character))))
200 (defcustom org-e-ascii-bullets '((ascii ?* ?+ ?-)
201 (latin1 ?§ ?¶)
202 (utf-8 ?◊))
203 "Bullet characters for headlines converted to lists in ASCII export.
205 Alist whose key is a symbol among `ascii', `latin1' and `utf-8'
206 and whose value is a list of characters.
208 The first character is used for the first level considered as low
209 level, and so on. If there are more levels than characters given
210 here, the list will be repeated.
212 Note that this variable doesn't affect plain lists
213 representation."
214 :group 'org-export-e-ascii
215 :type '(list
216 (cons :tag "Bullet characters for low level headlines"
217 (const :tag "ASCII charset" ascii)
218 (repeat character))
219 (cons :tag "Bullet characters for low level headlines"
220 (const :tag "Latin-1 charset" latin1)
221 (repeat character))
222 (cons :tag "Bullet characters for low level headlines"
223 (const :tag "UTF-8 charset" utf-8)
224 (repeat character))))
226 (defcustom org-e-ascii-links-to-notes t
227 "Non-nil means convert links to notes before the next headline.
228 When nil, the link will be exported in place. If the line
229 becomes long in this way, it will be wrapped."
230 :group 'org-export-e-ascii
231 :type 'boolean)
233 (defcustom org-e-ascii-table-keep-all-vertical-lines nil
234 "Non-nil means keep all vertical lines in ASCII tables.
235 When nil, vertical lines will be removed except for those needed
236 for column grouping."
237 :group 'org-export-e-ascii
238 :type 'boolean)
240 (defcustom org-e-ascii-table-widen-columns t
241 "Non-nil means widen narrowed columns for export.
242 When nil, narrowed columns will look in ASCII export just like in
243 Org mode, i.e. with \"=>\" as ellipsis."
244 :group 'org-export-e-ascii
245 :type 'boolean)
247 (defcustom org-e-ascii-table-use-ascii-art nil
248 "Non-nil means table.el tables are turned into ascii-art.
250 It only makes sense when export charset is `utf-8'. It is nil by
251 default since it requires ascii-art-to-unicode.el package. You
252 can download it here:
254 http://gnuvola.org/software/j/aa2u/ascii-art-to-unicode.el.")
256 (defcustom org-e-ascii-caption-above nil
257 "When non-nil, place caption string before the element.
258 Otherwise, place it right after it."
259 :group 'org-export-e-ascii
260 :type 'boolean)
262 (defcustom org-e-ascii-verbatim-format "`%s'"
263 "Format string used for verbatim text and inline code."
264 :group 'org-export-e-ascii
265 :type 'string)
267 (defcustom org-e-ascii-format-drawer-function nil
268 "Function called to format a drawer in ASCII.
270 The function must accept two parameters:
271 NAME the drawer name, like \"LOGBOOK\"
272 CONTENTS the contents of the drawer.
273 WIDTH the text width within the drawer.
275 The function should return either the string to be exported or
276 nil to ignore the drawer.
278 For example, the variable could be set to the following function
279 in order to mimic default behaviour:
281 \(defun org-e-ascii-format-drawer-default \(name contents width\)
282 \"Format a drawer element for ASCII export.\"
283 contents\)"
284 :group 'org-export-e-ascii
285 :type 'function)
287 (defcustom org-e-ascii-format-inlinetask-function nil
288 "Function called to format an inlinetask in ASCII.
290 The function must accept six parameters:
291 TODO the todo keyword, as a string
292 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
293 PRIORITY the inlinetask priority, as a string
294 NAME the inlinetask name, as a string.
295 TAGS the inlinetask tags, as a list of strings.
296 CONTENTS the contents of the inlinetask, as a string.
298 The function should return either the string to be exported or
299 nil to ignore the inline task.
301 For example, the variable could be set to the following function
302 in order to mimic default behaviour:
304 \(defun org-e-ascii-format-inlinetask-default
305 \(todo type priority name tags contents\)
306 \"Format an inline task element for ASCII export.\"
307 \(let* \(\(utf8p \(eq \(plist-get info :ascii-charset\) 'utf-8\)\)
308 \(width org-e-ascii-inlinetask-width\)
309 \(org-e-ascii--indent-string
310 \(concat
311 ;; Top line, with an additional blank line if not in UTF-8.
312 \(make-string width \(if utf8p ?━ ?_\)\) \"\\n\"
313 \(unless utf8p \(concat \(make-string width ? \) \"\\n\"\)\)
314 ;; Add title. Fill it if wider than inlinetask.
315 \(let \(\(title \(org-e-ascii--build-title inlinetask info width\)\)\)
316 \(if \(<= \(length title\) width\) title
317 \(org-e-ascii--fill-string title width info\)\)\)
318 \"\\n\"
319 ;; If CONTENTS is not empty, insert it along with
320 ;; a separator.
321 \(when \(org-string-nw-p contents\)
322 \(concat \(make-string width \(if utf8p ?─ ?-\)\) \"\\n\" contents\)\)
323 ;; Bottom line.
324 \(make-string width \(if utf8p ?━ ?_\)\)\)
325 ;; Flush the inlinetask to the right.
326 \(- \(plist-get info :ascii-width\)
327 \(plist-get info :ascii-margin\)
328 \(plist-get info :ascii-inner-margin\)
329 \(org-e-ascii--current-text-width inlinetask info\)\)"
330 :group 'org-export-e-ascii
331 :type 'function)
335 ;;; Internal Functions
337 ;; Internal functions fall into three categories.
339 ;; The first one is about text formatting. The core function is
340 ;; `org-e-ascii--current-text-width', which determines the current
341 ;; text width allowed to a given element. In other words, it helps
342 ;; keeping each line width within maximum text width defined in
343 ;; `org-e-ascii-text-width'. Once this information is known,
344 ;; `org-e-ascii--fill-string', `org-e-ascii--justify-string',
345 ;; `org-e-ascii--box-string' and `org-e-ascii--indent-string' can
346 ;; operate on a given output string.
348 ;; The second category contains functions handling elements listings,
349 ;; triggered by "#+TOC:" keyword. As such, `org-e-ascii--build-toc'
350 ;; returns a complete table of contents, `org-e-ascii--list-listings'
351 ;; returns a list of referenceable src-block elements, and
352 ;; `org-e-ascii--list-tables' does the same for table elements.
354 ;; The third category includes general helper functions.
355 ;; `org-e-ascii--build-title' creates the title for a given headline
356 ;; or inlinetask element. `org-e-ascii--build-caption' returns the
357 ;; caption string associated to a table or a src-block.
358 ;; `org-e-ascii--describe-links' creates notes about links for
359 ;; insertion at the end of a section. It uses
360 ;; `org-e-ascii--unique-links' to get the list of links to describe.
361 ;; Eventually, `org-e-ascii--translate' translates a string according
362 ;; to language and charset specification.
365 (defun org-e-ascii--fill-string (s text-width info &optional justify)
366 "Fill a string with specified text-width and return it.
368 S is the string being filled. TEXT-WIDTH is an integer
369 specifying maximum length of a line. INFO is the plist used as
370 a communication channel.
372 Optional argument JUSTIFY can specify any type of justification
373 among `left', `center', `right' or `full'. A nil value is
374 equivalent to `left'. For a justification that doesn't also fill
375 string, see `org-e-ascii--justify-string'.
377 Return nil if S isn't a string."
378 ;; Don't fill paragraph when break should be preserved.
379 (cond ((not (stringp s)) nil)
380 ((plist-get info :preserve-breaks) s)
381 (t (with-temp-buffer
382 (let ((fill-column text-width)
383 (use-hard-newlines t))
384 (insert s)
385 (fill-region (point-min) (point-max) justify))
386 (buffer-string)))))
388 (defun org-e-ascii--justify-string (s text-width how)
389 "Justify string S.
390 TEXT-WIDTH is an integer specifying maximum length of a line.
391 HOW determines the type of justification: it can be `left',
392 `right', `full' or `center'."
393 (with-temp-buffer
394 (insert s)
395 (goto-char (point-min))
396 (let ((fill-column text-width))
397 (while (< (point) (point-max))
398 (justify-current-line how)
399 (forward-line)))
400 (buffer-string)))
402 (defun org-e-ascii--indent-string (s width)
403 "Indent string S by WIDTH white spaces.
404 Empty lines are not indented."
405 (when (stringp s)
406 (replace-regexp-in-string
407 "\\(^\\)\\(?:.*\\S-\\)" (make-string width ? ) s nil nil 1)))
409 (defun org-e-ascii--box-string (s info)
410 "Return string S with a partial box to its left.
411 INFO is a plist used as a communicaton channel."
412 (let ((utf8p (eq (plist-get info :ascii-charset) 'utf-8)))
413 (format (if utf8p "╭────\n%s\n╰────" ",----\n%s\n`----")
414 (replace-regexp-in-string
415 "^" (if utf8p "│ " "| ")
416 ;; Remove last newline character.
417 (replace-regexp-in-string "\n[ \t]*\\'" "" s)))))
419 (defun org-e-ascii--current-text-width (element info)
420 "Return maximum text width for ELEMENT's contents.
421 INFO is a plist used as a communication channel."
422 (case (org-element-type element)
423 ;; Elements with an absolute width: `headline' and `inlinetask'.
424 (inlinetask org-e-ascii-inlinetask-width)
425 ('headline
426 (- org-e-ascii-text-width
427 (let ((low-level-rank (org-export-low-level-p element info)))
428 (if low-level-rank (* low-level-rank 2) org-e-ascii-global-margin))))
429 ;; Elements with a relative width: store maximum text width in
430 ;; TOTAL-WIDTH.
431 (otherwise
432 (let* ((genealogy (cons element (org-export-get-genealogy element)))
433 ;; Total width is determined by the presence, or not, of an
434 ;; inline task among ELEMENT parents.
435 (total-width
436 (if (loop for parent in genealogy
437 thereis (eq (org-element-type parent) 'inlinetask))
438 org-e-ascii-inlinetask-width
439 ;; No inlinetask: Remove global margin from text width.
440 (- org-e-ascii-text-width
441 org-e-ascii-global-margin
442 (let ((parent (org-export-get-parent-headline element)))
443 ;; Inner margin doesn't apply to text before first
444 ;; headline.
445 (if (not parent) 0
446 (let ((low-level-rank
447 (org-export-low-level-p parent info)))
448 ;; Inner margin doesn't apply to contents of
449 ;; low level headlines, since they've got their
450 ;; own indentation mechanism.
451 (if low-level-rank (* low-level-rank 2)
452 org-e-ascii-inner-margin))))))))
453 (- total-width
454 ;; Each `quote-block', `quote-section' and `verse-block' above
455 ;; narrows text width by twice the standard margin size.
456 (+ (* (loop for parent in genealogy
457 when (memq (org-element-type parent)
458 '(quote-block quote-section verse-block))
459 count parent)
460 2 org-e-ascii-quote-margin)
461 ;; Text width within a plain-list is restricted by
462 ;; indentation of current item. If that's the case,
463 ;; compute it with the help of `:structure' property from
464 ;; parent item, if any.
465 (let ((parent-item
466 (if (eq (org-element-type element) 'item) element
467 (loop for parent in genealogy
468 when (eq (org-element-type parent) 'item)
469 return parent))))
470 (if (not parent-item) 0
471 ;; Compute indentation offset of the current item,
472 ;; that is the sum of the difference between its
473 ;; indentation and the indentation of the top item in
474 ;; the list and current item bullet's length. Also
475 ;; remove checkbox length, and tag length (for
476 ;; description lists) or bullet length.
477 (let ((struct (org-element-property :structure parent-item))
478 (beg-item (org-element-property :begin parent-item)))
479 (+ (- (org-list-get-ind beg-item struct)
480 (org-list-get-ind
481 (org-list-get-top-point struct) struct))
482 (length (org-e-ascii--checkbox parent-item info))
483 (length
484 (or (org-list-get-tag beg-item struct)
485 (org-list-get-bullet beg-item struct)))))))))))))
487 (defun org-e-ascii--build-title
488 (element info text-width &optional underline notags)
489 "Format ELEMENT title and return it.
491 ELEMENT is either an `headline' or `inlinetask' element. INFO is
492 a plist used as a communication channel. TEXT-WIDTH is an
493 integer representing the maximum length of a line.
495 When optional argument UNDERLINE is non-nil, underline title,
496 without the tags, according to `org-e-ascii-underline'
497 specifications.
499 if optional argument NOTAGS is nil, no tags will be added to the
500 title."
501 (let* ((headlinep (eq (org-element-type element) 'headline))
502 (numbers
503 ;; Numbering is specific to headlines.
504 (and headlinep (org-export-numbered-headline-p element info)
505 ;; All tests passed: build numbering string.
506 (concat
507 (mapconcat
508 'number-to-string
509 (org-export-get-headline-number element info) ".")
510 " ")))
511 (text (org-export-data (org-element-property :title element) info))
512 (todo
513 (and (plist-get info :with-todo-keywords)
514 (let ((todo (org-element-property :todo-keyword element)))
515 (and todo (concat (org-export-data todo info) " ")))))
516 (tags (and (not notags)
517 (plist-get info :with-tags)
518 (let ((tag-list (org-export-get-tags element info)))
519 (and tag-list
520 (format ":%s:"
521 (mapconcat 'identity tag-list ":"))))))
522 (priority
523 (and (plist-get info :with-priority)
524 (let ((char (org-element-property :priority element)))
525 (and char (format "(#%c) " char)))))
526 (first-part (concat numbers todo priority text)))
527 (concat
528 first-part
529 ;; Align tags, if any.
530 (when tags
531 (format
532 (format " %%%ds"
533 (max (- text-width (1+ (length first-part))) (length tags)))
534 tags))
535 ;; Maybe underline text, if ELEMENT type is `headline' and an
536 ;; underline character has been defined.
537 (when (and underline headlinep)
538 (let ((under-char
539 (nth (1- (org-export-get-relative-level element info))
540 (cdr (assq (plist-get info :ascii-charset)
541 org-e-ascii-underline)))))
542 (and under-char
543 (concat "\n"
544 (make-string (length first-part) under-char))))))))
546 (defun org-e-ascii--has-caption-p (element info)
547 "Non-nil when ELEMENT has a caption affiliated keyword.
548 INFO is a plist used as a communication channel. This function
549 is meant to be used as a predicate for `org-export-get-ordinal'."
550 (org-element-property :caption element))
552 (defun org-e-ascii--build-caption (element info)
553 "Return caption string for ELEMENT, if applicable.
555 INFO is a plist used as a communication channel.
557 The caption string contains the sequence number of ELEMENT along
558 with its real caption. Return nil when ELEMENT has no affiliated
559 caption keyword."
560 (let ((caption (org-element-property :caption element)))
561 (when caption
562 ;; Get sequence number of current src-block among every
563 ;; src-block with a caption.
564 (let ((reference
565 (org-export-get-ordinal
566 element info nil 'org-e-ascii--has-caption-p))
567 (title-fmt (org-e-ascii--translate
568 (case (org-element-type element)
569 (table "Table %d: %s")
570 (src-block "Listing %d: %s"))
571 info)))
572 (org-e-ascii--fill-string
573 (format title-fmt reference (org-export-data (car caption) info))
574 (org-e-ascii--current-text-width element info) info)))))
576 (defun org-e-ascii--build-toc (info &optional n keyword)
577 "Return a table of contents.
579 INFO is a plist used as a communication channel.
581 Optional argument N, when non-nil, is an integer specifying the
582 depth of the table.
584 Optional argument KEYWORD specifies the TOC keyword, if any, from
585 which the table of contents generation has been initiated."
586 (let ((title (org-e-ascii--translate "Table of Contents" info)))
587 (concat
588 title "\n"
589 (make-string (length title)
590 (if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))
591 "\n\n"
592 (let ((text-width
593 (if keyword (org-e-ascii--current-text-width keyword info)
594 (- org-e-ascii-text-width org-e-ascii-global-margin))))
595 (mapconcat
596 (lambda (headline)
597 (let* ((level (org-export-get-relative-level headline info))
598 (indent (* (1- level) 3)))
599 (concat
600 (unless (zerop indent) (concat (make-string (1- indent) ?.) " "))
601 (org-e-ascii--build-title
602 headline info (- text-width indent) nil
603 (eq (plist-get info :with-tags) 'not-in-toc)))))
604 (org-export-collect-headlines info n) "\n")))))
606 (defun org-e-ascii--list-listings (keyword info)
607 "Return a list of listings.
609 KEYWORD is the keyword that initiated the list of listings
610 generation. INFO is a plist used as a communication channel."
611 (let ((title (org-e-ascii--translate "List of Listings" info)))
612 (concat
613 title "\n"
614 (make-string (length title)
615 (if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))
616 "\n\n"
617 (let ((text-width
618 (if keyword (org-e-ascii--current-text-width keyword info)
619 (- org-e-ascii-text-width org-e-ascii-global-margin)))
620 ;; Use a counter instead of retreiving ordinal of each
621 ;; src-block.
622 (count 0))
623 (mapconcat
624 (lambda (src-block)
625 ;; Store initial text so its length can be computed. This is
626 ;; used to properly align caption right to it in case of
627 ;; filling (like contents of a description list item).
628 (let ((initial-text
629 (format (org-e-ascii--translate "Listing %d:" info)
630 (incf count))))
631 (concat
632 initial-text " "
633 (org-trim
634 (org-e-ascii--indent-string
635 (org-e-ascii--fill-string
636 (let ((caption (org-element-property :caption src-block)))
637 ;; Use short name in priority, if available.
638 (org-export-data (or (cdr caption) (car caption)) info))
639 (- text-width (length initial-text)) info)
640 (length initial-text))))))
641 (org-export-collect-listings info) "\n")))))
643 (defun org-e-ascii--list-tables (keyword info)
644 "Return a list of listings.
646 KEYWORD is the keyword that initiated the list of listings
647 generation. INFO is a plist used as a communication channel."
648 (let ((title (org-e-ascii--translate "List of Tables" info)))
649 (concat
650 title "\n"
651 (make-string (length title)
652 (if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))
653 "\n\n"
654 (let ((text-width
655 (if keyword (org-e-ascii--current-text-width keyword info)
656 (- org-e-ascii-text-width org-e-ascii-global-margin)))
657 ;; Use a counter instead of retreiving ordinal of each
658 ;; src-block.
659 (count 0))
660 (mapconcat
661 (lambda (table)
662 ;; Store initial text so its length can be computed. This is
663 ;; used to properly align caption right to it in case of
664 ;; filling (like contents of a description list item).
665 (let ((initial-text
666 (format (org-e-ascii--translate "Table %d:" info)
667 (incf count))))
668 (concat
669 initial-text " "
670 (org-trim
671 (org-e-ascii--indent-string
672 (org-e-ascii--fill-string
673 (let ((caption (org-element-property :caption table)))
674 ;; Use short name in priority, if available.
675 (org-export-data (or (cdr caption) (car caption)) info))
676 (- text-width (length initial-text)) info)
677 (length initial-text))))))
678 (org-export-collect-tables info) "\n")))))
680 (defun org-e-ascii--unique-links (element info)
681 "Return a list of unique link references in ELEMENT.
683 ELEMENT is either an headline element or a section element. INFO
684 is a plist used as a communication channel."
685 (let* (seen
686 (unique-link-p
687 (function
688 ;; Return LINK if it wasn't referenced so far, or nil.
689 ;; Update SEEN links along the way.
690 (lambda (link)
691 (let ((footprint
692 (cons (org-element-property :raw-link link)
693 (org-element-contents link))))
694 (unless (member footprint seen)
695 (push footprint seen) link)))))
696 ;; If at a section, find parent headline, if any, in order to
697 ;; count links that might be in the title.
698 (headline
699 (if (eq (org-element-type element) 'headline) element
700 (or (org-export-get-parent-headline element) element))))
701 ;; Get all links in HEADLINE.
702 (org-element-map
703 headline 'link (lambda (link) (funcall unique-link-p link)) info)))
705 (defun org-e-ascii--describe-links (links width info)
706 "Return a string describing a list of links.
708 LINKS is a list of link type objects, as returned by
709 `org-e-ascii--unique-links'. WIDTH is the text width allowed for
710 the output string. INFO is a plist used as a communication
711 channel."
712 (mapconcat
713 (lambda (link)
714 (let ((type (org-element-property :type link))
715 (anchor (let ((desc (org-element-contents link)))
716 (if (not desc) (org-element-property :raw-link link)
717 (org-export-data desc info)))))
718 (cond
719 ;; Coderefs, radio links and fuzzy links are ignored.
720 ((member type '("coderef" "radio" "fuzzy")) nil)
721 ;; Id and custom-id links: Headlines refer to their numbering.
722 ((member type '("custom-id" "id"))
723 (let ((dest (org-export-resolve-id-link link info)))
724 (concat
725 (org-e-ascii--fill-string
726 (format
727 "[%s] %s"
728 anchor
729 (if (not dest) (org-e-ascii--translate "Unknown reference" info)
730 (format
731 (org-e-ascii--translate "See section %s" info)
732 (mapconcat 'number-to-string
733 (org-export-get-headline-number dest info) "."))))
734 width info) "\n\n")))
735 ;; Do not add a link that cannot be resolved and doesn't have
736 ;; any description: destination is already visible in the
737 ;; paragraph.
738 ((not (org-element-contents link)) nil)
740 (concat
741 (org-e-ascii--fill-string
742 (format "[%s] %s" anchor (org-element-property :raw-link link))
743 width info)
744 "\n\n")))))
745 links ""))
747 (defun org-e-ascii--checkbox (item info)
748 "Return checkbox string for ITEM or nil.
749 INFO is a plist used as a communication channel."
750 (let ((utf8p (eq (plist-get info :ascii-charset) 'utf-8)))
751 (case (org-element-property :checkbox item)
752 (on (if utf8p "☑ " "[X] "))
753 (off (if utf8p "☐ " "[ ] "))
754 (trans (if utf8p "☒ " "[-] ")))))
758 ;;; Template
760 (defun org-e-ascii-template--document-title (info)
761 "Return document title, as a string.
762 INFO is a plist used as a communication channel."
763 (let ((text-width org-e-ascii-text-width)
764 (title (org-export-data (plist-get info :title) info))
765 (author (and (plist-get info :with-author)
766 (let ((auth (plist-get info :author)))
767 (and auth (org-export-data auth info)))))
768 (email (and (plist-get info :with-email)
769 (org-export-data (plist-get info :email) info)))
770 (date (org-export-data (plist-get info :date) info)))
771 ;; There are two types of title blocks depending on the presence
772 ;; of a title to display.
773 (if (string= title "")
774 ;; Title block without a title. DATE is positioned at the top
775 ;; right of the document, AUTHOR to the top left and EMAIL
776 ;; just below.
777 (cond
778 ((and (org-string-nw-p date) (org-string-nw-p author))
779 (concat
780 author
781 (make-string (- text-width (length date) (length author)) ? )
782 date
783 (when (org-string-nw-p email) (concat "\n" email))
784 "\n\n\n"))
785 ((and (org-string-nw-p date) (org-string-nw-p email))
786 (concat
787 email
788 (make-string (- text-width (length date) (length email)) ? )
789 date "\n\n\n"))
790 ((org-string-nw-p date)
791 (concat
792 (org-e-ascii--justify-string date text-width 'right)
793 "\n\n\n"))
794 ((and (org-string-nw-p author) (org-string-nw-p email))
795 (concat author "\n" email "\n\n\n"))
796 ((org-string-nw-p author) (concat author "\n\n\n"))
797 ((org-string-nw-p email) (concat email "\n\n\n")))
798 ;; Title block with a title. Document's TITLE, along with the
799 ;; AUTHOR and its EMAIL are both overlined and an underlined,
800 ;; centered. Date is just below, also centered.
801 (let* ((utf8p (eq (plist-get info :ascii-charset) 'utf-8))
802 ;; Format TITLE. It may be filled if it is too wide,
803 ;; that is wider than the two thirds of the total width.
804 (title-len (min (length title) (/ (* 2 text-width) 3)))
805 (formatted-title (org-e-ascii--fill-string title title-len info))
806 (line
807 (make-string
808 (min (+ (max title-len (length author) (length email)) 2)
809 text-width) (if utf8p ?━ ?_))))
810 (org-e-ascii--justify-string
811 (concat line "\n"
812 (unless utf8p "\n")
813 (upcase formatted-title)
814 (cond
815 ((and (org-string-nw-p author) (org-string-nw-p email))
816 (concat (if utf8p "\n\n\n" "\n\n") author "\n" email))
817 ((org-string-nw-p author)
818 (concat (if utf8p "\n\n\n" "\n\n") author))
819 ((org-string-nw-p email)
820 (concat (if utf8p "\n\n\n" "\n\n") email)))
821 "\n" line
822 (when (org-string-nw-p date) (concat "\n\n\n" date))
823 "\n\n\n") text-width 'center)))))
825 (defun org-e-ascii-template (contents info)
826 "Return complete document string after ASCII conversion.
827 CONTENTS is the transcoded contents string. INFO is a plist
828 holding export options."
829 (org-element-normalize-string
830 (org-e-ascii--indent-string
831 (let ((text-width (- org-e-ascii-text-width org-e-ascii-global-margin)))
832 ;; 1. Build title block.
833 (concat
834 (org-e-ascii-template--document-title info)
835 ;; 2. Table of contents.
836 (let ((depth (plist-get info :with-toc)))
837 (when depth
838 (concat
839 (org-e-ascii--build-toc info (and (wholenump depth) depth))
840 "\n\n\n")))
841 ;; 3. Document's body.
842 contents
843 ;; 4. Footnote definitions.
844 (let ((definitions (org-export-collect-footnote-definitions
845 (plist-get info :parse-tree) info))
846 ;; Insert full links right inside the footnote definition
847 ;; as they have no chance to be inserted later.
848 (org-e-ascii-links-to-notes nil))
849 (when definitions
850 (concat
851 "\n\n\n"
852 (let ((title (org-e-ascii--translate "Footnotes" info)))
853 (concat
854 title "\n"
855 (make-string
856 (length title)
857 (if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))))
858 "\n\n"
859 (mapconcat
860 (lambda (ref)
861 (let ((id (format "[%s] " (car ref))))
862 ;; Distinguish between inline definitions and
863 ;; full-fledged definitions.
864 (org-trim
865 (let ((def (nth 2 ref)))
866 (if (eq (org-element-type def) 'org-data)
867 ;; Full-fledged definition: footnote ID is
868 ;; inserted inside the first parsed paragraph
869 ;; (FIRST), if any, to be sure filling will
870 ;; take it into consideration.
871 (let ((first (car (org-element-contents def))))
872 (if (not (eq (org-element-type first) 'paragraph))
873 (concat id "\n" (org-export-data def info))
874 (push id (nthcdr 2 first))
875 (org-export-data def info)))
876 ;; Fill paragraph once footnote ID is inserted in
877 ;; order to have a correct length for first line.
878 (org-e-ascii--fill-string
879 (concat id (org-export-data def info))
880 text-width info))))))
881 definitions "\n\n"))))
882 ;; 5. Creator. Ignore `comment' value as there are no comments in
883 ;; ASCII. Justify it to the bottom right.
884 (let ((creator-info (plist-get info :with-creator)))
885 (unless (or (not creator-info) (eq creator-info 'comment))
886 (concat
887 "\n\n\n"
888 (org-e-ascii--fill-string
889 (plist-get info :creator) text-width info 'right))))))
890 org-e-ascii-global-margin)))
892 (defun org-e-ascii--translate (s info)
893 "Translate string S according to specified language and charset.
894 INFO is a plist used as a communication channel."
895 (let ((charset (intern (format ":%s" (plist-get info :ascii-charset)))))
896 (org-export-translate s charset info)))
900 ;;; Transcode Functions
902 ;;;; Babel Call
904 ;; Babel Calls are ignored.
907 ;;;; Bold
909 (defun org-e-ascii-bold (bold contents info)
910 "Transcode BOLD from Org to ASCII.
911 CONTENTS is the text with bold markup. INFO is a plist holding
912 contextual information."
913 (format "*%s*" contents))
916 ;;;; Center Block
918 (defun org-e-ascii-center-block (center-block contents info)
919 "Transcode a CENTER-BLOCK element from Org to ASCII.
920 CONTENTS holds the contents of the block. INFO is a plist
921 holding contextual information."
922 (org-e-ascii--justify-string
923 contents (org-e-ascii--current-text-width center-block info) 'center))
926 ;;;; Clock
928 (defun org-e-ascii-clock (clock contents info)
929 "Transcode a CLOCK object from Org to ASCII.
930 CONTENTS is nil. INFO is a plist holding contextual
931 information."
932 (concat org-clock-string " "
933 (org-translate-time (org-element-property :value clock))
934 (let ((time (org-element-property :time clock)))
935 (and time
936 (concat " => "
937 (apply 'format
938 "%2s:%02s"
939 (org-split-string time ":")))))))
942 ;;;; Code
944 (defun org-e-ascii-code (code contents info)
945 "Return a CODE object from Org to ASCII.
946 CONTENTS is nil. INFO is a plist holding contextual
947 information."
948 (format org-e-ascii-verbatim-format (org-element-property :value code)))
951 ;;;; Comment
953 ;; Comments are ignored.
956 ;;;; Comment Block
958 ;; Comment Blocks are ignored.
961 ;;;; Drawer
963 (defun org-e-ascii-drawer (drawer contents info)
964 "Transcode a DRAWER element from Org to ASCII.
965 CONTENTS holds the contents of the block. INFO is a plist
966 holding contextual information."
967 (let ((name (org-element-property :drawer-name drawer))
968 (width (org-e-ascii--current-text-width drawer info)))
969 (if (functionp org-e-ascii-format-drawer-function)
970 (funcall org-e-ascii-format-drawer-function name contents width)
971 ;; If there's no user defined function: simply
972 ;; display contents of the drawer.
973 contents)))
976 ;;;; Dynamic Block
978 (defun org-e-ascii-dynamic-block (dynamic-block contents info)
979 "Transcode a DYNAMIC-BLOCK element from Org to ASCII.
980 CONTENTS holds the contents of the block. INFO is a plist
981 holding contextual information."
982 contents)
985 ;;;; Entity
987 (defun org-e-ascii-entity (entity contents info)
988 "Transcode an ENTITY object from Org to ASCII.
989 CONTENTS are the definition itself. INFO is a plist holding
990 contextual information."
991 (org-element-property
992 (intern (concat ":" (symbol-name (plist-get info :ascii-charset))))
993 entity))
996 ;;;; Example Block
998 (defun org-e-ascii-example-block (example-block contents info)
999 "Transcode a EXAMPLE-BLOCK element from Org to ASCII.
1000 CONTENTS is nil. INFO is a plist holding contextual information."
1001 (org-e-ascii--box-string
1002 (org-export-format-code-default example-block info) info))
1005 ;;;; Export Snippet
1007 (defun org-e-ascii-export-snippet (export-snippet contents info)
1008 "Transcode a EXPORT-SNIPPET object from Org to ASCII.
1009 CONTENTS is nil. INFO is a plist holding contextual information."
1010 (when (eq (org-export-snippet-backend export-snippet) 'e-ascii)
1011 (org-element-property :value export-snippet)))
1014 ;;;; Export Block
1016 (defun org-e-ascii-export-block (export-block contents info)
1017 "Transcode a EXPORT-BLOCK element from Org to ASCII.
1018 CONTENTS is nil. INFO is a plist holding contextual information."
1019 (when (string= (org-element-property :type export-block) "ASCII")
1020 (org-remove-indentation (org-element-property :value export-block))))
1023 ;;;; Fixed Width
1025 (defun org-e-ascii-fixed-width (fixed-width contents info)
1026 "Transcode a FIXED-WIDTH element from Org to ASCII.
1027 CONTENTS is nil. INFO is a plist holding contextual information."
1028 (org-e-ascii--box-string
1029 (org-remove-indentation
1030 (org-element-property :value fixed-width)) info))
1033 ;;;; Footnote Definition
1035 ;; Footnote Definitions are ignored. They are compiled at the end of
1036 ;; the document, by `org-e-ascii-template'.
1039 ;;;; Footnote Reference
1041 (defun org-e-ascii-footnote-reference (footnote-reference contents info)
1042 "Transcode a FOOTNOTE-REFERENCE element from Org to ASCII.
1043 CONTENTS is nil. INFO is a plist holding contextual information."
1044 (format "[%s]" (org-export-get-footnote-number footnote-reference info)))
1047 ;;;; Headline
1049 (defun org-e-ascii-headline (headline contents info)
1050 "Transcode an HEADLINE element from Org to ASCII.
1051 CONTENTS holds the contents of the headline. INFO is a plist
1052 holding contextual information."
1053 ;; Don't export footnote section, which will be handled at the end
1054 ;; of the template.
1055 (unless (org-element-property :footnote-section-p headline)
1056 (let* ((low-level-rank (org-export-low-level-p headline info))
1057 (width (org-e-ascii--current-text-width headline info))
1058 ;; Blank lines between headline and its contents.
1059 ;; `org-e-ascii-headline-spacing', when set, overwrites
1060 ;; original buffer's spacing.
1061 (pre-blanks
1062 (make-string
1063 (if org-e-ascii-headline-spacing (car org-e-ascii-headline-spacing)
1064 (org-element-property :pre-blank headline)) ?\n))
1065 ;; Even if HEADLINE has no section, there might be some
1066 ;; links in its title that we shouldn't forget to describe.
1067 (links
1068 (unless (or (eq (caar (org-element-contents headline)) 'section))
1069 (let ((title (org-element-property :title headline)))
1070 (when (consp title)
1071 (org-e-ascii--describe-links
1072 (org-e-ascii--unique-links title info) width info))))))
1073 ;; Deep subtree: export it as a list item.
1074 (if low-level-rank
1075 (concat
1076 ;; Bullet.
1077 (let ((bullets (cdr (assq (plist-get info :ascii-charset)
1078 org-e-ascii-bullets))))
1079 (char-to-string
1080 (nth (mod (1- low-level-rank) (length bullets)) bullets)))
1082 ;; Title.
1083 (org-e-ascii--build-title headline info width) "\n"
1084 ;; Contents, indented by length of bullet.
1085 pre-blanks
1086 (org-e-ascii--indent-string
1087 (concat contents
1088 (when (org-string-nw-p links) (concat "\n\n" links)))
1090 ;; Else: Standard headline.
1091 (concat
1092 (org-e-ascii--build-title headline info width 'underline)
1093 "\n" pre-blanks
1094 (concat (when (org-string-nw-p links) links) contents))))))
1097 ;;;; Horizontal Rule
1099 (defun org-e-ascii-horizontal-rule (horizontal-rule contents info)
1100 "Transcode an HORIZONTAL-RULE object from Org to ASCII.
1101 CONTENTS is nil. INFO is a plist holding contextual
1102 information."
1103 (let ((text-width (org-e-ascii--current-text-width horizontal-rule info))
1104 (spec-width
1105 (org-export-read-attribute :attr_ascii horizontal-rule :width)))
1106 (org-e-ascii--justify-string
1107 (make-string (if (wholenump spec-width) spec-width text-width)
1108 (if (eq (plist-get info :ascii-charset) 'utf-8) ?― ?-))
1109 text-width 'center)))
1112 ;;;; Inline Babel Call
1114 ;; Inline Babel Calls are ignored.
1117 ;;;; Inline Src Block
1119 (defun org-e-ascii-inline-src-block (inline-src-block contents info)
1120 "Transcode an INLINE-SRC-BLOCK element from Org to ASCII.
1121 CONTENTS holds the contents of the item. INFO is a plist holding
1122 contextual information."
1123 (format org-e-ascii-verbatim-format
1124 (org-element-property :value inline-src-block)))
1127 ;;;; Inlinetask
1129 (defun org-e-ascii-inlinetask (inlinetask contents info)
1130 "Transcode an INLINETASK element from Org to ASCII.
1131 CONTENTS holds the contents of the block. INFO is a plist
1132 holding contextual information."
1133 (let ((width (org-e-ascii--current-text-width inlinetask info)))
1134 ;; If `org-e-ascii-format-inlinetask-function' is provided, call it
1135 ;; with appropriate arguments.
1136 (if (functionp org-e-ascii-format-inlinetask-function)
1137 (funcall org-e-ascii-format-inlinetask-function
1138 ;; todo.
1139 (and (plist-get info :with-todo-keywords)
1140 (let ((todo (org-element-property
1141 :todo-keyword inlinetask)))
1142 (and todo (org-export-data todo info))))
1143 ;; todo-type
1144 (org-element-property :todo-type inlinetask)
1145 ;; priority
1146 (and (plist-get info :with-priority)
1147 (org-element-property :priority inlinetask))
1148 ;; title
1149 (org-export-data (org-element-property :title inlinetask) info)
1150 ;; tags
1151 (and (plist-get info :with-tags)
1152 (org-element-property :tags inlinetask))
1153 ;; contents and width
1154 contents width)
1155 ;; Otherwise, use a default template.
1156 (let* ((utf8p (eq (plist-get info :ascii-charset) 'utf-8)))
1157 (org-e-ascii--indent-string
1158 (concat
1159 ;; Top line, with an additional blank line if not in UTF-8.
1160 (make-string width (if utf8p ?━ ?_)) "\n"
1161 (unless utf8p (concat (make-string width ? ) "\n"))
1162 ;; Add title. Fill it if wider than inlinetask.
1163 (let ((title (org-e-ascii--build-title inlinetask info width)))
1164 (if (<= (length title) width) title
1165 (org-e-ascii--fill-string title width info)))
1166 "\n"
1167 ;; If CONTENTS is not empty, insert it along with
1168 ;; a separator.
1169 (when (org-string-nw-p contents)
1170 (concat (make-string width (if utf8p ?─ ?-)) "\n" contents))
1171 ;; Bottom line.
1172 (make-string width (if utf8p ?━ ?_)))
1173 ;; Flush the inlinetask to the right.
1174 (- org-e-ascii-text-width org-e-ascii-global-margin
1175 (if (not (org-export-get-parent-headline inlinetask)) 0
1176 org-e-ascii-inner-margin)
1177 (org-e-ascii--current-text-width inlinetask info)))))))
1179 ;;;; Italic
1181 (defun org-e-ascii-italic (italic contents info)
1182 "Transcode italic from Org to ASCII.
1183 CONTENTS is the text with italic markup. INFO is a plist holding
1184 contextual information."
1185 (format "/%s/" contents))
1188 ;;;; Item
1190 (defun org-e-ascii-item (item contents info)
1191 "Transcode an ITEM element from Org to ASCII.
1192 CONTENTS holds the contents of the item. INFO is a plist holding
1193 contextual information."
1194 (let* ((utf8p (eq (plist-get info :ascii-charset) 'utf-8))
1195 (checkbox (org-e-ascii--checkbox item info))
1196 (list-type (org-element-property :type (org-export-get-parent item)))
1197 (bullet
1198 ;; First parent of ITEM is always the plain-list. Get
1199 ;; `:type' property from it.
1200 (org-list-bullet-string
1201 (case list-type
1202 (descriptive
1203 (concat checkbox
1204 (org-export-data (org-element-property :tag item) info)
1205 ": "))
1206 (ordered
1207 ;; Return correct number for ITEM, paying attention to
1208 ;; counters.
1209 (let* ((struct (org-element-property :structure item))
1210 (bul (org-element-property :bullet item))
1211 (num (number-to-string
1212 (car (last (org-list-get-item-number
1213 (org-element-property :begin item)
1214 struct
1215 (org-list-prevs-alist struct)
1216 (org-list-parents-alist struct)))))))
1217 (replace-regexp-in-string "[0-9]+" num bul)))
1218 (t (let ((bul (org-element-property :bullet item)))
1219 ;; Change bullets into more visible form if UTF-8 is active.
1220 (if (not utf8p) bul
1221 (replace-regexp-in-string
1222 "-" "•"
1223 (replace-regexp-in-string
1224 "+" "⁃"
1225 (replace-regexp-in-string "*" "‣" bul))))))))))
1226 (concat
1227 bullet
1228 (unless (eq list-type 'descriptive) checkbox)
1229 ;; Contents: Pay attention to indentation. Note: check-boxes are
1230 ;; already taken care of at the paragraph level so they don't
1231 ;; interfere with indentation.
1232 (let ((contents (org-e-ascii--indent-string contents (length bullet))))
1233 (if (eq (org-element-type (car (org-element-contents item))) 'paragraph)
1234 (org-trim contents)
1235 (concat "\n" contents))))))
1238 ;;;; Keyword
1240 (defun org-e-ascii-keyword (keyword contents info)
1241 "Transcode a KEYWORD element from Org to ASCII.
1242 CONTENTS is nil. INFO is a plist holding contextual
1243 information."
1244 (let ((key (org-element-property :key keyword))
1245 (value (org-element-property :value keyword)))
1246 (cond
1247 ((string= key "ASCII") value)
1248 ((string= key "TOC")
1249 (let ((value (downcase value)))
1250 (cond
1251 ((string-match "\\<headlines\\>" value)
1252 (let ((depth (or (and (string-match "[0-9]+" value)
1253 (string-to-number (match-string 0 value)))
1254 (plist-get info :with-toc))))
1255 (org-e-ascii--build-toc
1256 info (and (wholenump depth) depth) keyword)))
1257 ((string= "tables" value)
1258 (org-e-ascii--list-tables keyword info))
1259 ((string= "listings" value)
1260 (org-e-ascii--list-listings keyword info))))))))
1263 ;;;; Latex Environment
1265 (defun org-e-ascii-latex-environment (latex-environment contents info)
1266 "Transcode a LATEX-ENVIRONMENT element from Org to ASCII.
1267 CONTENTS is nil. INFO is a plist holding contextual
1268 information."
1269 (org-remove-indentation (org-element-property :value latex-environment)))
1272 ;;;; Latex Fragment
1274 (defun org-e-ascii-latex-fragment (latex-fragment contents info)
1275 "Transcode a LATEX-FRAGMENT object from Org to ASCII.
1276 CONTENTS is nil. INFO is a plist holding contextual
1277 information."
1278 (org-element-property :value latex-fragment))
1281 ;;;; Line Break
1283 (defun org-e-ascii-line-break (line-break contents info)
1284 "Transcode a LINE-BREAK object from Org to ASCII.
1285 CONTENTS is nil. INFO is a plist holding contextual
1286 information." hard-newline)
1289 ;;;; Link
1291 (defun org-e-ascii-link (link desc info)
1292 "Transcode a LINK object from Org to ASCII.
1294 DESC is the description part of the link, or the empty string.
1295 INFO is a plist holding contextual information."
1296 (let ((raw-link (org-element-property :raw-link link))
1297 (type (org-element-property :type link)))
1298 (cond
1299 ((string= type "coderef")
1300 (let ((ref (org-element-property :path link)))
1301 (format (org-export-get-coderef-format ref desc)
1302 (org-export-resolve-coderef ref info))))
1303 ;; Do not apply a special syntax on radio links. Though, use
1304 ;; transcoded target's contents as output.
1305 ((string= type "radio")
1306 (let ((destination (org-export-resolve-radio-link link info)))
1307 (when destination
1308 (org-export-data (org-element-contents destination) info))))
1309 ;; Do not apply a special syntax on fuzzy links pointing to
1310 ;; targets.
1311 ((string= type "fuzzy")
1312 (let ((destination (org-export-resolve-fuzzy-link link info)))
1313 ;; Ignore invisible "#+TARGET: path".
1314 (unless (eq (org-element-type destination) 'keyword)
1315 (if (org-string-nw-p desc) desc
1316 (when destination
1317 (let ((number
1318 (org-export-get-ordinal
1319 destination info nil 'org-e-ascii--has-caption-p)))
1320 (when number
1321 (if (atom number) (number-to-string number)
1322 (mapconcat 'number-to-string number ".")))))))))
1324 (if (not (org-string-nw-p desc)) (format "[%s]" raw-link)
1325 (concat
1326 (format "[%s]" desc)
1327 (unless org-e-ascii-links-to-notes (format " (%s)" raw-link))))))))
1330 ;;;; Macro
1332 (defun org-e-ascii-macro (macro contents info)
1333 "Transcode a MACRO element from Org to ASCII.
1334 CONTENTS is nil. INFO is a plist holding contextual
1335 information."
1336 (org-export-expand-macro macro info))
1339 ;;;; Paragraph
1341 (defun org-e-ascii-paragraph (paragraph contents info)
1342 "Transcode a PARAGRAPH element from Org to ASCII.
1343 CONTENTS is the contents of the paragraph, as a string. INFO is
1344 the plist used as a communication channel."
1345 (org-e-ascii--fill-string
1346 contents
1347 (org-e-ascii--current-text-width paragraph info) info))
1350 ;;;; Plain List
1352 (defun org-e-ascii-plain-list (plain-list contents info)
1353 "Transcode a PLAIN-LIST element from Org to ASCII.
1354 CONTENTS is the contents of the list. INFO is a plist holding
1355 contextual information."
1356 contents)
1359 ;;;; Plain Text
1361 (defun org-e-ascii-plain-text (text info)
1362 "Transcode a TEXT string from Org to ASCII.
1363 INFO is a plist used as a communication channel."
1364 (if (not (and (eq (plist-get info :ascii-charset) 'utf-8)
1365 (plist-get info :with-special-strings)))
1366 text
1367 ;; Usual replacements in utf-8 with proper option set.
1368 (replace-regexp-in-string
1369 "\\.\\.\\." "…"
1370 (replace-regexp-in-string
1371 "--" "–"
1372 (replace-regexp-in-string "---" "—" text)))))
1375 ;;;; Planning
1377 (defun org-e-ascii-planning (planning contents info)
1378 "Transcode a PLANNING element from Org to ASCII.
1379 CONTENTS is nil. INFO is a plist used as a communication
1380 channel."
1381 (mapconcat
1382 'identity
1383 (delq nil
1384 (list (let ((closed (org-element-property :closed planning)))
1385 (when closed (concat org-closed-string " "
1386 (org-translate-time closed))))
1387 (let ((deadline (org-element-property :deadline planning)))
1388 (when deadline (concat org-deadline-string " "
1389 (org-translate-time deadline))))
1390 (let ((scheduled (org-element-property :scheduled planning)))
1391 (when scheduled (concat org-scheduled-string " "
1392 (org-translate-time scheduled))))))
1393 " "))
1396 ;;;; Property Drawer
1398 ;; Property drawers are ignored.
1401 ;;;; Quote Block
1403 (defun org-e-ascii-quote-block (quote-block contents info)
1404 "Transcode a QUOTE-BLOCK element from Org to ASCII.
1405 CONTENTS holds the contents of the block. INFO is a plist
1406 holding contextual information."
1407 (let ((width (org-e-ascii--current-text-width quote-block info)))
1408 (org-e-ascii--indent-string
1409 (org-remove-indentation
1410 (org-e-ascii--fill-string contents width info))
1411 org-e-ascii-quote-margin)))
1414 ;;;; Quote Section
1416 (defun org-e-ascii-quote-section (quote-section contents info)
1417 "Transcode a QUOTE-SECTION element from Org to ASCII.
1418 CONTENTS is nil. INFO is a plist holding contextual information."
1419 (let ((width (org-e-ascii--current-text-width quote-section info))
1420 (value
1421 (org-export-data
1422 (org-remove-indentation (org-element-property :value quote-section))
1423 info)))
1424 (org-e-ascii--indent-string
1425 value
1426 (+ org-e-ascii-quote-margin
1427 ;; Don't apply inner margin if parent headline is low level.
1428 (let ((headline (org-export-get-parent-headline quote-section)))
1429 (if (org-export-low-level-p headline info) 0
1430 org-e-ascii-inner-margin))))))
1433 ;;;; Radio Target
1435 (defun org-e-ascii-radio-target (radio-target contents info)
1436 "Transcode a RADIO-TARGET object from Org to ASCII.
1437 CONTENTS is the contents of the target. INFO is a plist holding
1438 contextual information."
1439 contents)
1441 ;;;; Section
1443 (defun org-e-ascii-section (section contents info)
1444 "Transcode a SECTION element from Org to ASCII.
1445 CONTENTS is the contents of the section. INFO is a plist holding
1446 contextual information."
1447 (org-e-ascii--indent-string
1448 (concat
1449 contents
1450 (when org-e-ascii-links-to-notes
1451 ;; Add list of links at the end of SECTION.
1452 (let ((links (org-e-ascii--describe-links
1453 (org-e-ascii--unique-links section info)
1454 (org-e-ascii--current-text-width section info) info)))
1455 ;; Separate list of links and section contents.
1456 (when (org-string-nw-p links) (concat "\n\n" links)))))
1457 ;; Do not apply inner margin if parent headline is low level.
1458 (let ((headline (org-export-get-parent-headline section)))
1459 (if (or (not headline) (org-export-low-level-p headline info)) 0
1460 org-e-ascii-inner-margin))))
1463 ;;;; Special Block
1465 (defun org-e-ascii-special-block (special-block contents info)
1466 "Transcode a SPECIAL-BLOCK element from Org to ASCII.
1467 CONTENTS holds the contents of the block. INFO is a plist
1468 holding contextual information."
1469 contents)
1472 ;;;; Src Block
1474 (defun org-e-ascii-src-block (src-block contents info)
1475 "Transcode a SRC-BLOCK element from Org to ASCII.
1476 CONTENTS holds the contents of the item. INFO is a plist holding
1477 contextual information."
1478 (let ((caption (org-e-ascii--build-caption src-block info)))
1479 (concat
1480 (when (and caption org-e-ascii-caption-above) (concat caption "\n"))
1481 (org-e-ascii--box-string
1482 (org-export-format-code-default src-block info) info)
1483 (when (and caption (not org-e-ascii-caption-above))
1484 (concat "\n" caption)))))
1486 ;;;; Statistics Cookie
1488 (defun org-e-ascii-statistics-cookie (statistics-cookie contents info)
1489 "Transcode a STATISTICS-COOKIE object from Org to ASCII.
1490 CONTENTS is nil. INFO is a plist holding contextual information."
1491 (org-element-property :value statistics-cookie))
1494 ;;;; Subscript
1496 (defun org-e-ascii-subscript (subscript contents info)
1497 "Transcode a SUBSCRIPT object from Org to ASCII.
1498 CONTENTS is the contents of the object. INFO is a plist holding
1499 contextual information."
1500 (if (org-element-property :use-brackets-p subscript)
1501 (format "_{%s}" contents)
1502 (format "_%s" contents)))
1505 ;;;; Superscript
1507 (defun org-e-ascii-superscript (superscript contents info)
1508 "Transcode a SUPERSCRIPT object from Org to ASCII.
1509 CONTENTS is the contents of the object. INFO is a plist holding
1510 contextual information."
1511 (if (org-element-property :use-brackets-p superscript)
1512 (format "_{%s}" contents)
1513 (format "_%s" contents)))
1516 ;;;; Strike-through
1518 (defun org-e-ascii-strike-through (strike-through contents info)
1519 "Transcode STRIKE-THROUGH from Org to ASCII.
1520 CONTENTS is text with strike-through markup. INFO is a plist
1521 holding contextual information."
1522 (format "+%s+" contents))
1525 ;;;; Table
1527 (defun org-e-ascii-table (table contents info)
1528 "Transcode a TABLE element from Org to ASCII.
1529 CONTENTS is the contents of the table. INFO is a plist holding
1530 contextual information."
1531 (let ((caption (org-e-ascii--build-caption table info)))
1532 (concat
1533 ;; Possibly add a caption string above.
1534 (when (and caption org-e-ascii-caption-above) (concat caption "\n"))
1535 ;; Insert table. Note: "table.el" tables are left unmodified.
1536 (cond ((eq (org-element-property :type table) 'org) contents)
1537 ((and org-e-ascii-table-use-ascii-art
1538 (eq (plist-get info :ascii-charset) 'utf-8)
1539 (require 'ascii-art-to-unicode nil t))
1540 (with-temp-buffer
1541 (insert (org-remove-indentation
1542 (org-element-property :value table)))
1543 (goto-char (point-min))
1544 (aa2u)
1545 (goto-char (point-max))
1546 (skip-chars-backward " \r\t\n")
1547 (buffer-substring (point-min) (point))))
1548 (t (org-remove-indentation (org-element-property :value table))))
1549 ;; Possible add a caption string below.
1550 (when (and caption (not org-e-ascii-caption-above))
1551 (concat "\n" caption)))))
1554 ;;;; Table Cell
1556 (defun org-e-ascii--table-cell-width (table-cell info)
1557 "Return width of TABLE-CELL.
1559 INFO is a plist used as a communication channel.
1561 Width of a cell is determined either by a width cookie in the
1562 same column as the cell, or by the maximum cell's length in that
1563 column.
1565 When `org-e-ascii-table-widen-columns' is non-nil, width cookies
1566 are ignored."
1567 (or (and (not org-e-ascii-table-widen-columns)
1568 (org-export-table-cell-width table-cell info))
1569 (let* ((max-width 0)
1570 (table (org-export-get-parent-table table-cell))
1571 (specialp (org-export-table-has-special-column-p table))
1572 (col (cdr (org-export-table-cell-address table-cell info))))
1573 (org-element-map
1574 table 'table-row
1575 (lambda (row)
1576 (setq max-width
1577 (max (length
1578 (org-export-data
1579 (org-element-contents
1580 (elt (if specialp (cdr (org-element-contents row))
1581 (org-element-contents row))
1582 col))
1583 info))
1584 max-width)))
1585 info)
1586 max-width)))
1588 (defun org-e-ascii-table-cell (table-cell contents info)
1589 "Transcode a TABLE-CELL object from Org to ASCII.
1590 CONTENTS is the cell contents. INFO is a plist used as
1591 a communication channel."
1592 ;; Determine column width. When `org-e-ascii-table-widen-columns'
1593 ;; is nil and some width cookie has set it, use that value.
1594 ;; Otherwise, compute the maximum width among transcoded data of
1595 ;; each cell in the column.
1596 (let ((width (org-e-ascii--table-cell-width table-cell info)))
1597 ;; When contents are too large, truncate them.
1598 (unless (or org-e-ascii-table-widen-columns (<= (length contents) width))
1599 (setq contents (concat (substring contents 0 (- width 2)) "=>")))
1600 ;; Align contents correctly within the cell.
1601 (let* ((indent-tabs-mode nil)
1602 (data
1603 (when contents
1604 (org-e-ascii--justify-string
1605 contents width
1606 (org-export-table-cell-alignment table-cell info)))))
1607 (setq contents (concat data (make-string (- width (length data)) ? ))))
1608 ;; Return cell.
1609 (concat (format " %s " contents)
1610 (when (memq 'right (org-export-table-cell-borders table-cell info))
1611 (if (eq (plist-get info :ascii-charset) 'utf-8) "│" "|")))))
1614 ;;;; Table Row
1616 (defun org-e-ascii-table-row (table-row contents info)
1617 "Transcode a TABLE-ROW element from Org to ASCII.
1618 CONTENTS is the row contents. INFO is a plist used as
1619 a communication channel."
1620 (when (eq (org-element-property :type table-row) 'standard)
1621 (let ((build-hline
1622 (function
1623 (lambda (lcorner horiz vert rcorner)
1624 (concat
1625 (apply
1626 'concat
1627 (org-element-map
1628 table-row 'table-cell
1629 (lambda (cell)
1630 (let ((width (org-e-ascii--table-cell-width cell info))
1631 (borders (org-export-table-cell-borders cell info)))
1632 (concat
1633 ;; In order to know if CELL starts the row, do
1634 ;; not compare it with the first cell in the row
1635 ;; as there might be a special column. Instead,
1636 ;; compare it with the first exportable cell,
1637 ;; obtained with `org-element-map'.
1638 (when (and (memq 'left borders)
1639 (eq (org-element-map
1640 table-row 'table-cell 'identity info t)
1641 cell))
1642 lcorner)
1643 (make-string (+ 2 width) (string-to-char horiz))
1644 (cond
1645 ((not (memq 'right borders)) nil)
1646 ((eq (car (last (org-element-contents table-row))) cell)
1647 rcorner)
1648 (t vert)))))
1649 info)) "\n"))))
1650 (utf8p (eq (plist-get info :ascii-charset) 'utf-8))
1651 (borders (org-export-table-cell-borders
1652 (org-element-map table-row 'table-cell 'identity info t)
1653 info)))
1654 (concat (cond
1655 ((and (memq 'top borders) (or utf8p (memq 'above borders)))
1656 (if utf8p (funcall build-hline "┍" "━" "┯" "┑")
1657 (funcall build-hline "+" "-" "+" "+")))
1658 ((memq 'above borders)
1659 (if utf8p (funcall build-hline "├" "─" "┼" "┤")
1660 (funcall build-hline "+" "-" "+" "+"))))
1661 (when (memq 'left borders) (if utf8p "│" "|"))
1662 contents "\n"
1663 (when (and (memq 'bottom borders) (or utf8p (memq 'below borders)))
1664 (if utf8p (funcall build-hline "┕" "━" "┷" "┙")
1665 (funcall build-hline "+" "-" "+" "+")))))))
1668 ;;;; Target
1670 ;; Targets are invisible.
1673 ;;;; Timestamp
1675 (defun org-e-ascii-timestamp (timestamp contents info)
1676 "Transcode a TIMESTAMP object from Org to ASCII.
1677 CONTENTS is nil. INFO is a plist holding contextual information."
1678 (let ((value (org-translate-time (org-element-property :value timestamp)))
1679 (range-end
1680 (org-translate-time (org-element-property :range-end timestamp)))
1681 (utf8p (eq (plist-get info :ascii-charset) 'utf-8)))
1682 (concat value
1683 (when range-end (concat (if utf8p "–" "--") range-end)))))
1686 ;;;; Underline
1688 (defun org-e-ascii-underline (underline contents info)
1689 "Transcode UNDERLINE from Org to ASCII.
1690 CONTENTS is the text with underline markup. INFO is a plist
1691 holding contextual information."
1692 (format "_%s_" contents))
1695 ;;;; Verbatim
1697 (defun org-e-ascii-verbatim (verbatim contents info)
1698 "Return a VERBATIM object from Org to ASCII.
1699 CONTENTS is nil. INFO is a plist holding contextual information."
1700 (format org-e-ascii-verbatim-format
1701 (org-element-property :value verbatim)))
1704 ;;;; Verse Block
1706 (defun org-e-ascii-verse-block (verse-block contents info)
1707 "Transcode a VERSE-BLOCK element from Org to ASCII.
1708 CONTENTS is verse block contents. INFO is a plist holding
1709 contextual information."
1710 (let ((verse-width (org-e-ascii--current-text-width verse-block info)))
1711 (org-e-ascii--indent-string
1712 (org-e-ascii--justify-string contents verse-width 'left)
1713 org-e-ascii-quote-margin)))
1716 ;;; Filter
1718 (defun org-e-ascii-filter-headline-blank-lines (headline back-end info)
1719 "Filter controlling number of blank lines after an headline.
1721 HEADLINE is a string representing a transcoded headline.
1722 BACK-END is symbol specifying back-end used for export. INFO is
1723 plist containing the communication channel.
1725 This function only applies to `e-ascii' back-end. See
1726 `org-e-ascii-headline-spacing' for information.
1728 For any other back-end, HEADLINE is returned as-is."
1729 (if (not org-e-ascii-headline-spacing) headline
1730 (let ((blanks (make-string (1+ (cdr org-e-ascii-headline-spacing)) ?\n)))
1731 (replace-regexp-in-string "\n\\(?:\n[ \t]*\\)*\\'" blanks headline))))
1735 ;;; Interactive function
1737 ;;;###autoload
1738 (defun org-e-ascii-export-as-ascii
1739 (&optional subtreep visible-only body-only ext-plist)
1740 "Export current buffer to a text buffer.
1742 If narrowing is active in the current buffer, only export its
1743 narrowed part.
1745 If a region is active, export that region.
1747 When optional argument SUBTREEP is non-nil, export the sub-tree
1748 at point, extracting information from the headline properties
1749 first.
1751 When optional argument VISIBLE-ONLY is non-nil, don't export
1752 contents of hidden elements.
1754 When optional argument BODY-ONLY is non-nil, strip title, table
1755 of contents and footnote definitions from output.
1757 EXT-PLIST, when provided, is a property list with external
1758 parameters overriding Org default settings, but still inferior to
1759 file-local settings.
1761 Export is done in a buffer named \"*Org E-ASCII Export*\", which
1762 will be displayed when `org-export-show-temporary-export-buffer'
1763 is non-nil."
1764 (interactive)
1765 (let ((outbuf (org-export-to-buffer
1766 'e-ascii "*Org E-ASCII Export*"
1767 subtreep visible-only body-only ext-plist)))
1768 (with-current-buffer outbuf (text-mode))
1769 (when org-export-show-temporary-export-buffer
1770 (switch-to-buffer-other-window outbuf))))
1772 ;;;###autoload
1773 (defun org-e-ascii-export-to-ascii
1774 (&optional subtreep visible-only body-only ext-plist pub-dir)
1775 "Export current buffer to a text file.
1777 If narrowing is active in the current buffer, only export its
1778 narrowed part.
1780 If a region is active, export that region.
1782 When optional argument SUBTREEP is non-nil, export the sub-tree
1783 at point, extracting information from the headline properties
1784 first.
1786 When optional argument VISIBLE-ONLY is non-nil, don't export
1787 contents of hidden elements.
1789 When optional argument BODY-ONLY is non-nil, strip title, table
1790 of contents and footnote definitions from output.
1792 EXT-PLIST, when provided, is a property list with external
1793 parameters overriding Org default settings, but still inferior to
1794 file-local settings.
1796 When optional argument PUB-DIR is set, use it as the publishing
1797 directory.
1799 Return output file's name."
1800 (interactive)
1801 (let ((outfile (org-export-output-file-name ".txt" subtreep pub-dir)))
1802 (org-export-to-file
1803 'e-ascii outfile subtreep visible-only body-only ext-plist)))
1806 (provide 'org-e-ascii)
1807 ;;; org-e-ascii.el ends here