org-pcomplete: Fix completion for block boundaries
[org-mode.git] / lisp / ox-ascii.el
blob59d0152374aa26316388755d005d40ea2b292244
1 ;;; ox-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 ;; GNU Emacs 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 ;; GNU Emacs 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 GNU Emacs. 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-ascii-export-as-ascii' (temporary buffer) and
27 ;; `org-ascii-export-to-ascii' ("txt" file). Also, three publishing
28 ;; functions are available: `org-ascii-publish-to-ascii',
29 ;; `org-ascii-publish-to-latin1' and `org-ascii-publish-to-utf8'.
31 ;; Output encoding is specified through `org-ascii-charset' variable,
32 ;; among `ascii', `latin1' and `utf-8' symbols.
34 ;; By default, horizontal rules span over the full text with, but with
35 ;; a given width attribute (set though #+ATTR_ASCII: :width <num>)
36 ;; they can be shortened and centered.
38 ;;; Code:
40 (eval-when-compile (require 'cl))
41 (require 'ox)
42 (require 'ox-publish)
44 (declare-function aa2u "ext:ascii-art-to-unicode" ())
46 ;;; Define Back-End
48 ;; The following setting won't allow to modify preferred charset
49 ;; through a buffer keyword or an option item, but, since the property
50 ;; will appear in communication channel nonetheless, it allows to
51 ;; override `org-ascii-charset' variable on the fly by the ext-plist
52 ;; mechanism.
54 ;; We also install a filter for headlines and sections, in order to
55 ;; control blank lines separating them in output string.
57 (org-export-define-backend 'ascii
58 '((bold . org-ascii-bold)
59 (center-block . org-ascii-center-block)
60 (clock . org-ascii-clock)
61 (code . org-ascii-code)
62 (comment . (lambda (&rest args) ""))
63 (comment-block . (lambda (&rest args) ""))
64 (drawer . org-ascii-drawer)
65 (dynamic-block . org-ascii-dynamic-block)
66 (entity . org-ascii-entity)
67 (example-block . org-ascii-example-block)
68 (export-block . org-ascii-export-block)
69 (export-snippet . org-ascii-export-snippet)
70 (fixed-width . org-ascii-fixed-width)
71 (footnote-reference . org-ascii-footnote-reference)
72 (headline . org-ascii-headline)
73 (horizontal-rule . org-ascii-horizontal-rule)
74 (inline-src-block . org-ascii-inline-src-block)
75 (inlinetask . org-ascii-inlinetask)
76 (inner-template . org-ascii-inner-template)
77 (italic . org-ascii-italic)
78 (item . org-ascii-item)
79 (keyword . org-ascii-keyword)
80 (latex-environment . org-ascii-latex-environment)
81 (latex-fragment . org-ascii-latex-fragment)
82 (line-break . org-ascii-line-break)
83 (link . org-ascii-link)
84 (paragraph . org-ascii-paragraph)
85 (plain-list . org-ascii-plain-list)
86 (plain-text . org-ascii-plain-text)
87 (planning . org-ascii-planning)
88 (quote-block . org-ascii-quote-block)
89 (quote-section . org-ascii-quote-section)
90 (radio-target . org-ascii-radio-target)
91 (section . org-ascii-section)
92 (special-block . org-ascii-special-block)
93 (src-block . org-ascii-src-block)
94 (statistics-cookie . org-ascii-statistics-cookie)
95 (strike-through . org-ascii-strike-through)
96 (subscript . org-ascii-subscript)
97 (superscript . org-ascii-superscript)
98 (table . org-ascii-table)
99 (table-cell . org-ascii-table-cell)
100 (table-row . org-ascii-table-row)
101 (target . org-ascii-target)
102 (template . org-ascii-template)
103 (timestamp . org-ascii-timestamp)
104 (underline . org-ascii-underline)
105 (verbatim . org-ascii-verbatim)
106 (verse-block . org-ascii-verse-block))
107 :export-block "ASCII"
108 :menu-entry
109 '(?t "Export to Plain Text"
110 ((?A "As ASCII buffer"
111 (lambda (a s v b)
112 (org-ascii-export-as-ascii a s v b '(:ascii-charset ascii))))
113 (?a "As ASCII file"
114 (lambda (a s v b)
115 (org-ascii-export-to-ascii a s v b '(:ascii-charset ascii))))
116 (?L "As Latin1 buffer"
117 (lambda (a s v b)
118 (org-ascii-export-as-ascii a s v b '(:ascii-charset latin1))))
119 (?l "As Latin1 file"
120 (lambda (a s v b)
121 (org-ascii-export-to-ascii a s v b '(:ascii-charset latin1))))
122 (?U "As UTF-8 buffer"
123 (lambda (a s v b)
124 (org-ascii-export-as-ascii a s v b '(:ascii-charset utf-8))))
125 (?u "As UTF-8 file"
126 (lambda (a s v b)
127 (org-ascii-export-to-ascii a s v b '(:ascii-charset utf-8))))))
128 :filters-alist '((:filter-headline . org-ascii-filter-headline-blank-lines)
129 (:filter-parse-tree org-ascii-filter-paragraph-spacing
130 org-ascii-filter-comment-spacing)
131 (:filter-section . org-ascii-filter-headline-blank-lines))
132 :options-alist '((:ascii-charset nil nil org-ascii-charset)))
136 ;;; User Configurable Variables
138 (defgroup org-export-ascii nil
139 "Options for exporting Org mode files to ASCII."
140 :tag "Org Export ASCII"
141 :group 'org-export)
143 (defcustom org-ascii-text-width 72
144 "Maximum width of exported text.
145 This number includes margin size, as set in
146 `org-ascii-global-margin'."
147 :group 'org-export-ascii
148 :version "24.4"
149 :package-version '(Org . "8.0")
150 :type 'integer)
152 (defcustom org-ascii-global-margin 0
153 "Width of the left margin, in number of characters."
154 :group 'org-export-ascii
155 :version "24.4"
156 :package-version '(Org . "8.0")
157 :type 'integer)
159 (defcustom org-ascii-inner-margin 2
160 "Width of the inner margin, in number of characters.
161 Inner margin is applied between each headline."
162 :group 'org-export-ascii
163 :version "24.4"
164 :package-version '(Org . "8.0")
165 :type 'integer)
167 (defcustom org-ascii-quote-margin 6
168 "Width of margin used for quoting text, in characters.
169 This margin is applied on both sides of the text."
170 :group 'org-export-ascii
171 :version "24.4"
172 :package-version '(Org . "8.0")
173 :type 'integer)
175 (defcustom org-ascii-inlinetask-width 30
176 "Width of inline tasks, in number of characters.
177 This number ignores any margin."
178 :group 'org-export-ascii
179 :version "24.4"
180 :package-version '(Org . "8.0")
181 :type 'integer)
183 (defcustom org-ascii-headline-spacing '(1 . 2)
184 "Number of blank lines inserted around headlines.
186 This variable can be set to a cons cell. In that case, its car
187 represents the number of blank lines present before headline
188 contents whereas its cdr reflects the number of blank lines after
189 contents.
191 A nil value replicates the number of blank lines found in the
192 original Org buffer at the same place."
193 :group 'org-export-ascii
194 :version "24.4"
195 :package-version '(Org . "8.0")
196 :type '(choice
197 (const :tag "Replicate original spacing" nil)
198 (cons :tag "Set an uniform spacing"
199 (integer :tag "Number of blank lines before contents")
200 (integer :tag "Number of blank lines after contents"))))
202 (defcustom org-ascii-indented-line-width 'auto
203 "Additional indentation width for the first line in a paragraph.
204 If the value is an integer, indent the first line of each
205 paragraph by this number. If it is the symbol `auto' preserve
206 indentation from original document."
207 :group 'org-export-ascii
208 :version "24.4"
209 :package-version '(Org . "8.0")
210 :type '(choice
211 (integer :tag "Number of white spaces characters")
212 (const :tag "Preserve original width" auto)))
214 (defcustom org-ascii-paragraph-spacing 'auto
215 "Number of white lines between paragraphs.
216 If the value is an integer, add this number of blank lines
217 between contiguous paragraphs. If is it the symbol `auto', keep
218 the same number of blank lines as in the original document."
219 :group 'org-export-ascii
220 :version "24.4"
221 :package-version '(Org . "8.0")
222 :type '(choice
223 (integer :tag "Number of blank lines")
224 (const :tag "Preserve original spacing" auto)))
226 (defcustom org-ascii-charset 'ascii
227 "The charset allowed to represent various elements and objects.
228 Possible values are:
229 `ascii' Only use plain ASCII characters
230 `latin1' Include Latin-1 characters
231 `utf-8' Use all UTF-8 characters"
232 :group 'org-export-ascii
233 :version "24.4"
234 :package-version '(Org . "8.0")
235 :type '(choice
236 (const :tag "ASCII" ascii)
237 (const :tag "Latin-1" latin1)
238 (const :tag "UTF-8" utf-8)))
240 (defcustom org-ascii-underline '((ascii ?= ?~ ?-)
241 (latin1 ?= ?~ ?-)
242 (utf-8 ?═ ?─ ?╌ ?┄ ?┈))
243 "Characters for underlining headings in ASCII export.
245 Alist whose key is a symbol among `ascii', `latin1' and `utf-8'
246 and whose value is a list of characters.
248 For each supported charset, this variable associates a sequence
249 of underline characters. In a sequence, the characters will be
250 used in order for headlines level 1, 2, ... If no character is
251 available for a given level, the headline won't be underlined."
252 :group 'org-export-ascii
253 :version "24.4"
254 :package-version '(Org . "8.0")
255 :type '(list
256 (cons :tag "Underline characters sequence"
257 (const :tag "ASCII charset" ascii)
258 (repeat character))
259 (cons :tag "Underline characters sequence"
260 (const :tag "Latin-1 charset" latin1)
261 (repeat character))
262 (cons :tag "Underline characters sequence"
263 (const :tag "UTF-8 charset" utf-8)
264 (repeat character))))
266 (defcustom org-ascii-bullets '((ascii ?* ?+ ?-)
267 (latin1 ?§ ?¶)
268 (utf-8 ?◊))
269 "Bullet characters for headlines converted to lists in ASCII export.
271 Alist whose key is a symbol among `ascii', `latin1' and `utf-8'
272 and whose value is a list of characters.
274 The first character is used for the first level considered as low
275 level, and so on. If there are more levels than characters given
276 here, the list will be repeated.
278 Note that this variable doesn't affect plain lists
279 representation."
280 :group 'org-export-ascii
281 :version "24.4"
282 :package-version '(Org . "8.0")
283 :type '(list
284 (cons :tag "Bullet characters for low level headlines"
285 (const :tag "ASCII charset" ascii)
286 (repeat character))
287 (cons :tag "Bullet characters for low level headlines"
288 (const :tag "Latin-1 charset" latin1)
289 (repeat character))
290 (cons :tag "Bullet characters for low level headlines"
291 (const :tag "UTF-8 charset" utf-8)
292 (repeat character))))
294 (defcustom org-ascii-links-to-notes t
295 "Non-nil means convert links to notes before the next headline.
296 When nil, the link will be exported in place. If the line
297 becomes long in this way, it will be wrapped."
298 :group 'org-export-ascii
299 :version "24.4"
300 :package-version '(Org . "8.0")
301 :type 'boolean)
303 (defcustom org-ascii-table-keep-all-vertical-lines nil
304 "Non-nil means keep all vertical lines in ASCII tables.
305 When nil, vertical lines will be removed except for those needed
306 for column grouping."
307 :group 'org-export-ascii
308 :version "24.4"
309 :package-version '(Org . "8.0")
310 :type 'boolean)
312 (defcustom org-ascii-table-widen-columns t
313 "Non-nil means widen narrowed columns for export.
314 When nil, narrowed columns will look in ASCII export just like in
315 Org mode, i.e. with \"=>\" as ellipsis."
316 :group 'org-export-ascii
317 :version "24.4"
318 :package-version '(Org . "8.0")
319 :type 'boolean)
321 (defcustom org-ascii-table-use-ascii-art nil
322 "Non-nil means table.el tables are turned into ascii-art.
324 It only makes sense when export charset is `utf-8'. It is nil by
325 default since it requires ascii-art-to-unicode.el package. You
326 can download it here:
328 http://gnuvola.org/software/j/aa2u/ascii-art-to-unicode.el."
329 :group 'org-export-ascii
330 :version "24.4"
331 :package-version '(Org . "8.0")
332 :type 'boolean)
334 (defcustom org-ascii-caption-above nil
335 "When non-nil, place caption string before the element.
336 Otherwise, place it right after it."
337 :group 'org-export-ascii
338 :version "24.4"
339 :package-version '(Org . "8.0")
340 :type 'boolean)
342 (defcustom org-ascii-verbatim-format "`%s'"
343 "Format string used for verbatim text and inline code."
344 :group 'org-export-ascii
345 :version "24.4"
346 :package-version '(Org . "8.0")
347 :type 'string)
349 (defcustom org-ascii-format-drawer-function nil
350 "Function called to format a drawer in ASCII.
352 The function must accept three parameters:
353 NAME the drawer name, like \"LOGBOOK\"
354 CONTENTS the contents of the drawer.
355 WIDTH the text width within the drawer.
357 The function should return either the string to be exported or
358 nil to ignore the drawer.
360 For example, the variable could be set to the following function
361 in order to mimic default behaviour:
363 \(defun org-ascii-format-drawer-default (name contents width)
364 \"Format a drawer element for ASCII export.\"
365 contents)"
366 :group 'org-export-ascii
367 :version "24.4"
368 :package-version '(Org . "8.0")
369 :type 'function)
371 (defcustom org-ascii-format-inlinetask-function nil
372 "Function called to format an inlinetask in ASCII.
374 The function must accept six parameters:
375 TODO the todo keyword, as a string
376 TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
377 PRIORITY the inlinetask priority, as a string
378 NAME the inlinetask name, as a string.
379 TAGS the inlinetask tags, as a list of strings.
380 CONTENTS the contents of the inlinetask, as a string.
382 The function should return either the string to be exported or
383 nil to ignore the inline task.
385 For example, the variable could be set to the following function
386 in order to mimic default behaviour:
388 \(defun org-ascii-format-inlinetask-default
389 \(todo type priority name tags contents\)
390 \"Format an inline task element for ASCII export.\"
391 \(let* \(\(utf8p \(eq \(plist-get info :ascii-charset\) 'utf-8\)\)
392 \(width org-ascii-inlinetask-width\)
393 \(org-ascii--indent-string
394 \(concat
395 ;; Top line, with an additional blank line if not in UTF-8.
396 \(make-string width \(if utf8p ?━ ?_\)\) \"\\n\"
397 \(unless utf8p \(concat \(make-string width ? \) \"\\n\"\)\)
398 ;; Add title. Fill it if wider than inlinetask.
399 \(let \(\(title \(org-ascii--build-title inlinetask info width\)\)\)
400 \(if \(<= \(length title\) width\) title
401 \(org-ascii--fill-string title width info\)\)\)
402 \"\\n\"
403 ;; If CONTENTS is not empty, insert it along with
404 ;; a separator.
405 \(when \(org-string-nw-p contents\)
406 \(concat \(make-string width \(if utf8p ?─ ?-\)\) \"\\n\" contents\)\)
407 ;; Bottom line.
408 \(make-string width \(if utf8p ?━ ?_\)\)\)
409 ;; Flush the inlinetask to the right.
410 \(- \(plist-get info :ascii-width\)
411 \(plist-get info :ascii-margin\)
412 \(plist-get info :ascii-inner-margin\)
413 \(org-ascii--current-text-width inlinetask info\)\)"
414 :group 'org-export-ascii
415 :version "24.4"
416 :package-version '(Org . "8.0")
417 :type 'function)
421 ;;; Internal Functions
423 ;; Internal functions fall into three categories.
425 ;; The first one is about text formatting. The core function is
426 ;; `org-ascii--current-text-width', which determines the current
427 ;; text width allowed to a given element. In other words, it helps
428 ;; keeping each line width within maximum text width defined in
429 ;; `org-ascii-text-width'. Once this information is known,
430 ;; `org-ascii--fill-string', `org-ascii--justify-string',
431 ;; `org-ascii--box-string' and `org-ascii--indent-string' can
432 ;; operate on a given output string.
434 ;; The second category contains functions handling elements listings,
435 ;; triggered by "#+TOC:" keyword. As such, `org-ascii--build-toc'
436 ;; returns a complete table of contents, `org-ascii--list-listings'
437 ;; returns a list of referenceable src-block elements, and
438 ;; `org-ascii--list-tables' does the same for table elements.
440 ;; The third category includes general helper functions.
441 ;; `org-ascii--build-title' creates the title for a given headline
442 ;; or inlinetask element. `org-ascii--build-caption' returns the
443 ;; caption string associated to a table or a src-block.
444 ;; `org-ascii--describe-links' creates notes about links for
445 ;; insertion at the end of a section. It uses
446 ;; `org-ascii--unique-links' to get the list of links to describe.
447 ;; Eventually, `org-ascii--translate' translates a string according
448 ;; to language and charset specification.
451 (defun org-ascii--fill-string (s text-width info &optional justify)
452 "Fill a string with specified text-width and return it.
454 S is the string being filled. TEXT-WIDTH is an integer
455 specifying maximum length of a line. INFO is the plist used as
456 a communication channel.
458 Optional argument JUSTIFY can specify any type of justification
459 among `left', `center', `right' or `full'. A nil value is
460 equivalent to `left'. For a justification that doesn't also fill
461 string, see `org-ascii--justify-string'.
463 Return nil if S isn't a string."
464 ;; Don't fill paragraph when break should be preserved.
465 (cond ((not (stringp s)) nil)
466 ((plist-get info :preserve-breaks) s)
467 (t (let ((double-space-p sentence-end-double-space))
468 (with-temp-buffer
469 (let ((fill-column text-width)
470 (use-hard-newlines t)
471 (sentence-end-double-space double-space-p))
472 (insert s)
473 (fill-region (point-min) (point-max) justify))
474 (buffer-string))))))
476 (defun org-ascii--justify-string (s text-width how)
477 "Justify string S.
478 TEXT-WIDTH is an integer specifying maximum length of a line.
479 HOW determines the type of justification: it can be `left',
480 `right', `full' or `center'."
481 (with-temp-buffer
482 (insert s)
483 (goto-char (point-min))
484 (let ((fill-column text-width)
485 ;; Disable `adaptive-fill-mode' so it doesn't prevent
486 ;; filling lines matching `adaptive-fill-regexp'.
487 (adaptive-fill-mode nil))
488 (while (< (point) (point-max))
489 (justify-current-line how)
490 (forward-line)))
491 (buffer-string)))
493 (defun org-ascii--indent-string (s width)
494 "Indent string S by WIDTH white spaces.
495 Empty lines are not indented."
496 (when (stringp s)
497 (replace-regexp-in-string
498 "\\(^\\)\\(?:.*\\S-\\)" (make-string width ? ) s nil nil 1)))
500 (defun org-ascii--box-string (s info)
501 "Return string S with a partial box to its left.
502 INFO is a plist used as a communicaton channel."
503 (let ((utf8p (eq (plist-get info :ascii-charset) 'utf-8)))
504 (format (if utf8p "╭────\n%s\n╰────" ",----\n%s\n`----")
505 (replace-regexp-in-string
506 "^" (if utf8p "│ " "| ")
507 ;; Remove last newline character.
508 (replace-regexp-in-string "\n[ \t]*\\'" "" s)))))
510 (defun org-ascii--current-text-width (element info)
511 "Return maximum text width for ELEMENT's contents.
512 INFO is a plist used as a communication channel."
513 (case (org-element-type element)
514 ;; Elements with an absolute width: `headline' and `inlinetask'.
515 (inlinetask org-ascii-inlinetask-width)
516 ('headline
517 (- org-ascii-text-width
518 (let ((low-level-rank (org-export-low-level-p element info)))
519 (if low-level-rank (* low-level-rank 2) org-ascii-global-margin))))
520 ;; Elements with a relative width: store maximum text width in
521 ;; TOTAL-WIDTH.
522 (otherwise
523 (let* ((genealogy (cons element (org-export-get-genealogy element)))
524 ;; Total width is determined by the presence, or not, of an
525 ;; inline task among ELEMENT parents.
526 (total-width
527 (if (loop for parent in genealogy
528 thereis (eq (org-element-type parent) 'inlinetask))
529 org-ascii-inlinetask-width
530 ;; No inlinetask: Remove global margin from text width.
531 (- org-ascii-text-width
532 org-ascii-global-margin
533 (let ((parent (org-export-get-parent-headline element)))
534 ;; Inner margin doesn't apply to text before first
535 ;; headline.
536 (if (not parent) 0
537 (let ((low-level-rank
538 (org-export-low-level-p parent info)))
539 ;; Inner margin doesn't apply to contents of
540 ;; low level headlines, since they've got their
541 ;; own indentation mechanism.
542 (if low-level-rank (* low-level-rank 2)
543 org-ascii-inner-margin))))))))
544 (- total-width
545 ;; Each `quote-block', `quote-section' and `verse-block' above
546 ;; narrows text width by twice the standard margin size.
547 (+ (* (loop for parent in genealogy
548 when (memq (org-element-type parent)
549 '(quote-block quote-section verse-block))
550 count parent)
551 2 org-ascii-quote-margin)
552 ;; Text width within a plain-list is restricted by
553 ;; indentation of current item. If that's the case,
554 ;; compute it with the help of `:structure' property from
555 ;; parent item, if any.
556 (let ((parent-item
557 (if (eq (org-element-type element) 'item) element
558 (loop for parent in genealogy
559 when (eq (org-element-type parent) 'item)
560 return parent))))
561 (if (not parent-item) 0
562 ;; Compute indentation offset of the current item,
563 ;; that is the sum of the difference between its
564 ;; indentation and the indentation of the top item in
565 ;; the list and current item bullet's length. Also
566 ;; remove checkbox length, and tag length (for
567 ;; description lists) or bullet length.
568 (let ((struct (org-element-property :structure parent-item))
569 (beg-item (org-element-property :begin parent-item)))
570 (+ (- (org-list-get-ind beg-item struct)
571 (org-list-get-ind
572 (org-list-get-top-point struct) struct))
573 (length (org-ascii--checkbox parent-item info))
574 (length
575 (or (org-list-get-tag beg-item struct)
576 (org-list-get-bullet beg-item struct)))))))))))))
578 (defun org-ascii--build-title
579 (element info text-width &optional underline notags toc)
580 "Format ELEMENT title and return it.
582 ELEMENT is either an `headline' or `inlinetask' element. INFO is
583 a plist used as a communication channel. TEXT-WIDTH is an
584 integer representing the maximum length of a line.
586 When optional argument UNDERLINE is non-nil, underline title,
587 without the tags, according to `org-ascii-underline'
588 specifications.
590 If optional argument NOTAGS is non-nil, no tags will be added to
591 the title.
593 When optional argument TOC is non-nil, use optional title if
594 possible. It doesn't apply to `inlinetask' elements."
595 (let* ((headlinep (eq (org-element-type element) 'headline))
596 (numbers
597 ;; Numbering is specific to headlines.
598 (and headlinep (org-export-numbered-headline-p element info)
599 ;; All tests passed: build numbering string.
600 (concat
601 (mapconcat
602 'number-to-string
603 (org-export-get-headline-number element info) ".")
604 " ")))
605 (text
606 (org-trim
607 (org-export-data
608 (if (and toc headlinep) (org-export-get-alt-title element info)
609 (org-element-property :title element))
610 info)))
611 (todo
612 (and (plist-get info :with-todo-keywords)
613 (let ((todo (org-element-property :todo-keyword element)))
614 (and todo (concat (org-export-data todo info) " ")))))
615 (tags (and (not notags)
616 (plist-get info :with-tags)
617 (let ((tag-list (org-export-get-tags element info)))
618 (and tag-list
619 (format ":%s:"
620 (mapconcat 'identity tag-list ":"))))))
621 (priority
622 (and (plist-get info :with-priority)
623 (let ((char (org-element-property :priority element)))
624 (and char (format "(#%c) " char)))))
625 (first-part (concat numbers todo priority text)))
626 (concat
627 first-part
628 ;; Align tags, if any.
629 (when tags
630 (format
631 (format " %%%ds"
632 (max (- text-width (1+ (length first-part))) (length tags)))
633 tags))
634 ;; Maybe underline text, if ELEMENT type is `headline' and an
635 ;; underline character has been defined.
636 (when (and underline headlinep)
637 (let ((under-char
638 (nth (1- (org-export-get-relative-level element info))
639 (cdr (assq (plist-get info :ascii-charset)
640 org-ascii-underline)))))
641 (and under-char
642 (concat "\n"
643 (make-string (length first-part) under-char))))))))
645 (defun org-ascii--has-caption-p (element info)
646 "Non-nil when ELEMENT has a caption affiliated keyword.
647 INFO is a plist used as a communication channel. This function
648 is meant to be used as a predicate for `org-export-get-ordinal'."
649 (org-element-property :caption element))
651 (defun org-ascii--build-caption (element info)
652 "Return caption string for ELEMENT, if applicable.
654 INFO is a plist used as a communication channel.
656 The caption string contains the sequence number of ELEMENT along
657 with its real caption. Return nil when ELEMENT has no affiliated
658 caption keyword."
659 (let ((caption (org-export-get-caption element)))
660 (when caption
661 ;; Get sequence number of current src-block among every
662 ;; src-block with a caption.
663 (let ((reference
664 (org-export-get-ordinal
665 element info nil 'org-ascii--has-caption-p))
666 (title-fmt (org-ascii--translate
667 (case (org-element-type element)
668 (table "Table %d:")
669 (src-block "Listing %d:"))
670 info)))
671 (org-ascii--fill-string
672 (concat (format title-fmt reference)
674 (org-export-data caption info))
675 (org-ascii--current-text-width element info) info)))))
677 (defun org-ascii--build-toc (info &optional n keyword)
678 "Return a table of contents.
680 INFO is a plist used as a communication channel.
682 Optional argument N, when non-nil, is an integer specifying the
683 depth of the table.
685 Optional argument KEYWORD specifies the TOC keyword, if any, from
686 which the table of contents generation has been initiated."
687 (let ((title (org-ascii--translate "Table of Contents" info)))
688 (concat
689 title "\n"
690 (make-string (length title)
691 (if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))
692 "\n\n"
693 (let ((text-width
694 (if keyword (org-ascii--current-text-width keyword info)
695 (- org-ascii-text-width org-ascii-global-margin))))
696 (mapconcat
697 (lambda (headline)
698 (let* ((level (org-export-get-relative-level headline info))
699 (indent (* (1- level) 3)))
700 (concat
701 (unless (zerop indent) (concat (make-string (1- indent) ?.) " "))
702 (org-ascii--build-title
703 headline info (- text-width indent) nil
704 (or (not (plist-get info :with-tags))
705 (eq (plist-get info :with-tags) 'not-in-toc))
706 'toc))))
707 (org-export-collect-headlines info n) "\n")))))
709 (defun org-ascii--list-listings (keyword info)
710 "Return a list of listings.
712 KEYWORD is the keyword that initiated the list of listings
713 generation. INFO is a plist used as a communication channel."
714 (let ((title (org-ascii--translate "List of Listings" info)))
715 (concat
716 title "\n"
717 (make-string (length title)
718 (if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))
719 "\n\n"
720 (let ((text-width
721 (if keyword (org-ascii--current-text-width keyword info)
722 (- org-ascii-text-width org-ascii-global-margin)))
723 ;; Use a counter instead of retreiving ordinal of each
724 ;; src-block.
725 (count 0))
726 (mapconcat
727 (lambda (src-block)
728 ;; Store initial text so its length can be computed. This is
729 ;; used to properly align caption right to it in case of
730 ;; filling (like contents of a description list item).
731 (let ((initial-text
732 (format (org-ascii--translate "Listing %d:" info)
733 (incf count))))
734 (concat
735 initial-text " "
736 (org-trim
737 (org-ascii--indent-string
738 (org-ascii--fill-string
739 ;; Use short name in priority, if available.
740 (let ((caption (or (org-export-get-caption src-block t)
741 (org-export-get-caption src-block))))
742 (org-export-data caption info))
743 (- text-width (length initial-text)) info)
744 (length initial-text))))))
745 (org-export-collect-listings info) "\n")))))
747 (defun org-ascii--list-tables (keyword info)
748 "Return a list of tables.
750 KEYWORD is the keyword that initiated the list of tables
751 generation. INFO is a plist used as a communication channel."
752 (let ((title (org-ascii--translate "List of Tables" info)))
753 (concat
754 title "\n"
755 (make-string (length title)
756 (if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))
757 "\n\n"
758 (let ((text-width
759 (if keyword (org-ascii--current-text-width keyword info)
760 (- org-ascii-text-width org-ascii-global-margin)))
761 ;; Use a counter instead of retreiving ordinal of each
762 ;; src-block.
763 (count 0))
764 (mapconcat
765 (lambda (table)
766 ;; Store initial text so its length can be computed. This is
767 ;; used to properly align caption right to it in case of
768 ;; filling (like contents of a description list item).
769 (let ((initial-text
770 (format (org-ascii--translate "Table %d:" info)
771 (incf count))))
772 (concat
773 initial-text " "
774 (org-trim
775 (org-ascii--indent-string
776 (org-ascii--fill-string
777 ;; Use short name in priority, if available.
778 (let ((caption (or (org-export-get-caption table t)
779 (org-export-get-caption table))))
780 (org-export-data caption info))
781 (- text-width (length initial-text)) info)
782 (length initial-text))))))
783 (org-export-collect-tables info) "\n")))))
785 (defun org-ascii--unique-links (element info)
786 "Return a list of unique link references in ELEMENT.
788 ELEMENT is either a headline element or a section element. INFO
789 is a plist used as a communication channel."
790 (let* (seen
791 (unique-link-p
792 (function
793 ;; Return LINK if it wasn't referenced so far, or nil.
794 ;; Update SEEN links along the way.
795 (lambda (link)
796 (let ((footprint
797 (cons (org-element-property :raw-link link)
798 (org-element-contents link))))
799 ;; Ignore LINK if it hasn't been translated already.
800 ;; It can happen if it is located in an affiliated
801 ;; keyword that was ignored.
802 (when (and (org-string-nw-p
803 (gethash link (plist-get info :exported-data)))
804 (not (member footprint seen)))
805 (push footprint seen) link)))))
806 ;; If at a section, find parent headline, if any, in order to
807 ;; count links that might be in the title.
808 (headline
809 (if (eq (org-element-type element) 'headline) element
810 (or (org-export-get-parent-headline element) element))))
811 ;; Get all links in HEADLINE.
812 (org-element-map headline 'link
813 (lambda (l) (funcall unique-link-p l)) info nil nil t)))
815 (defun org-ascii--describe-links (links width info)
816 "Return a string describing a list of links.
818 LINKS is a list of link type objects, as returned by
819 `org-ascii--unique-links'. WIDTH is the text width allowed for
820 the output string. INFO is a plist used as a communication
821 channel."
822 (mapconcat
823 (lambda (link)
824 (let ((type (org-element-property :type link))
825 (anchor (let ((desc (org-element-contents link)))
826 (if desc (org-export-data desc info)
827 (org-element-property :raw-link link)))))
828 (cond
829 ;; Coderefs, radio links and fuzzy links are ignored.
830 ((member type '("coderef" "radio" "fuzzy")) nil)
831 ;; Id and custom-id links: Headlines refer to their numbering.
832 ((member type '("custom-id" "id"))
833 (let ((dest (org-export-resolve-id-link link info)))
834 (concat
835 (org-ascii--fill-string
836 (format
837 "[%s] %s"
838 anchor
839 (if (not dest) (org-ascii--translate "Unknown reference" info)
840 (format
841 (org-ascii--translate "See section %s" info)
842 (mapconcat 'number-to-string
843 (org-export-get-headline-number dest info) "."))))
844 width info) "\n\n")))
845 ;; Do not add a link that cannot be resolved and doesn't have
846 ;; any description: destination is already visible in the
847 ;; paragraph.
848 ((not (org-element-contents link)) nil)
850 (concat
851 (org-ascii--fill-string
852 (format "[%s] %s" anchor (org-element-property :raw-link link))
853 width info)
854 "\n\n")))))
855 links ""))
857 (defun org-ascii--checkbox (item info)
858 "Return checkbox string for ITEM or nil.
859 INFO is a plist used as a communication channel."
860 (let ((utf8p (eq (plist-get info :ascii-charset) 'utf-8)))
861 (case (org-element-property :checkbox item)
862 (on (if utf8p "☑ " "[X] "))
863 (off (if utf8p "☐ " "[ ] "))
864 (trans (if utf8p "☒ " "[-] ")))))
868 ;;; Template
870 (defun org-ascii-template--document-title (info)
871 "Return document title, as a string.
872 INFO is a plist used as a communication channel."
873 (let* ((text-width org-ascii-text-width)
874 ;; Links in the title will not be resolved later, so we make
875 ;; sure their path is located right after them.
876 (org-ascii-links-to-notes nil)
877 (title (org-export-data (plist-get info :title) info))
878 (author (and (plist-get info :with-author)
879 (let ((auth (plist-get info :author)))
880 (and auth (org-export-data auth info)))))
881 (email (and (plist-get info :with-email)
882 (org-export-data (plist-get info :email) info)))
883 (date (and (plist-get info :with-date)
884 (org-export-data (org-export-get-date info) info))))
885 ;; There are two types of title blocks depending on the presence
886 ;; of a title to display.
887 (if (string= title "")
888 ;; Title block without a title. DATE is positioned at the top
889 ;; right of the document, AUTHOR to the top left and EMAIL
890 ;; just below.
891 (cond
892 ((and (org-string-nw-p date) (org-string-nw-p author))
893 (concat
894 author
895 (make-string (- text-width (length date) (length author)) ? )
896 date
897 (when (org-string-nw-p email) (concat "\n" email))
898 "\n\n\n"))
899 ((and (org-string-nw-p date) (org-string-nw-p email))
900 (concat
901 email
902 (make-string (- text-width (length date) (length email)) ? )
903 date "\n\n\n"))
904 ((org-string-nw-p date)
905 (concat
906 (org-ascii--justify-string date text-width 'right)
907 "\n\n\n"))
908 ((and (org-string-nw-p author) (org-string-nw-p email))
909 (concat author "\n" email "\n\n\n"))
910 ((org-string-nw-p author) (concat author "\n\n\n"))
911 ((org-string-nw-p email) (concat email "\n\n\n")))
912 ;; Title block with a title. Document's TITLE, along with the
913 ;; AUTHOR and its EMAIL are both overlined and an underlined,
914 ;; centered. Date is just below, also centered.
915 (let* ((utf8p (eq (plist-get info :ascii-charset) 'utf-8))
916 ;; Format TITLE. It may be filled if it is too wide,
917 ;; that is wider than the two thirds of the total width.
918 (title-len (min (length title) (/ (* 2 text-width) 3)))
919 (formatted-title (org-ascii--fill-string title title-len info))
920 (line
921 (make-string
922 (min (+ (max title-len (length author) (length email)) 2)
923 text-width) (if utf8p ?━ ?_))))
924 (org-ascii--justify-string
925 (concat line "\n"
926 (unless utf8p "\n")
927 (upcase formatted-title)
928 (cond
929 ((and (org-string-nw-p author) (org-string-nw-p email))
930 (concat (if utf8p "\n\n\n" "\n\n") author "\n" email))
931 ((org-string-nw-p author)
932 (concat (if utf8p "\n\n\n" "\n\n") author))
933 ((org-string-nw-p email)
934 (concat (if utf8p "\n\n\n" "\n\n") email)))
935 "\n" line
936 (when (org-string-nw-p date) (concat "\n\n\n" date))
937 "\n\n\n") text-width 'center)))))
939 (defun org-ascii-inner-template (contents info)
940 "Return complete document string after ASCII conversion.
941 CONTENTS is the transcoded contents string. INFO is a plist
942 holding export options."
943 (org-element-normalize-string
944 (org-ascii--indent-string
945 (concat
946 ;; 1. Document's body.
947 contents
948 ;; 2. Footnote definitions.
949 (let ((definitions (org-export-collect-footnote-definitions
950 (plist-get info :parse-tree) info))
951 ;; Insert full links right inside the footnote definition
952 ;; as they have no chance to be inserted later.
953 (org-ascii-links-to-notes nil))
954 (when definitions
955 (concat
956 "\n\n\n"
957 (let ((title (org-ascii--translate "Footnotes" info)))
958 (concat
959 title "\n"
960 (make-string
961 (length title)
962 (if (eq (plist-get info :ascii-charset) 'utf-8) ?─ ?_))))
963 "\n\n"
964 (let ((text-width (- org-ascii-text-width org-ascii-global-margin)))
965 (mapconcat
966 (lambda (ref)
967 (let ((id (format "[%s] " (car ref))))
968 ;; Distinguish between inline definitions and
969 ;; full-fledged definitions.
970 (org-trim
971 (let ((def (nth 2 ref)))
972 (if (eq (org-element-type def) 'org-data)
973 ;; Full-fledged definition: footnote ID is
974 ;; inserted inside the first parsed paragraph
975 ;; (FIRST), if any, to be sure filling will
976 ;; take it into consideration.
977 (let ((first (car (org-element-contents def))))
978 (if (not (eq (org-element-type first) 'paragraph))
979 (concat id "\n" (org-export-data def info))
980 (push id (nthcdr 2 first))
981 (org-export-data def info)))
982 ;; Fill paragraph once footnote ID is inserted
983 ;; in order to have a correct length for first
984 ;; line.
985 (org-ascii--fill-string
986 (concat id (org-export-data def info))
987 text-width info))))))
988 definitions "\n\n"))))))
989 org-ascii-global-margin)))
991 (defun org-ascii-template (contents info)
992 "Return complete document string after ASCII conversion.
993 CONTENTS is the transcoded contents string. INFO is a plist
994 holding export options."
995 (concat
996 ;; 1. Build title block.
997 (org-ascii--indent-string
998 (concat (org-ascii-template--document-title info)
999 ;; 2. Table of contents.
1000 (let ((depth (plist-get info :with-toc)))
1001 (when depth
1002 (concat
1003 (org-ascii--build-toc info (and (wholenump depth) depth))
1004 "\n\n\n"))))
1005 org-ascii-global-margin)
1006 ;; 3. Document's body.
1007 contents
1008 ;; 4. Creator. Ignore `comment' value as there are no comments in
1009 ;; ASCII. Justify it to the bottom right.
1010 (org-ascii--indent-string
1011 (let ((creator-info (plist-get info :with-creator))
1012 (text-width (- org-ascii-text-width org-ascii-global-margin)))
1013 (unless (or (not creator-info) (eq creator-info 'comment))
1014 (concat
1015 "\n\n\n"
1016 (org-ascii--fill-string
1017 (plist-get info :creator) text-width info 'right))))
1018 org-ascii-global-margin)))
1020 (defun org-ascii--translate (s info)
1021 "Translate string S according to specified language and charset.
1022 INFO is a plist used as a communication channel."
1023 (let ((charset (intern (format ":%s" (plist-get info :ascii-charset)))))
1024 (org-export-translate s charset info)))
1028 ;;; Transcode Functions
1030 ;;;; Bold
1032 (defun org-ascii-bold (bold contents info)
1033 "Transcode BOLD from Org to ASCII.
1034 CONTENTS is the text with bold markup. INFO is a plist holding
1035 contextual information."
1036 (format "*%s*" contents))
1039 ;;;; Center Block
1041 (defun org-ascii-center-block (center-block contents info)
1042 "Transcode a CENTER-BLOCK element from Org to ASCII.
1043 CONTENTS holds the contents of the block. INFO is a plist
1044 holding contextual information."
1045 (org-ascii--justify-string
1046 contents (org-ascii--current-text-width center-block info) 'center))
1049 ;;;; Clock
1051 (defun org-ascii-clock (clock contents info)
1052 "Transcode a CLOCK object from Org to ASCII.
1053 CONTENTS is nil. INFO is a plist holding contextual
1054 information."
1055 (concat org-clock-string " "
1056 (org-translate-time
1057 (org-element-property :raw-value
1058 (org-element-property :value clock)))
1059 (let ((time (org-element-property :duration clock)))
1060 (and time
1061 (concat " => "
1062 (apply 'format
1063 "%2s:%02s"
1064 (org-split-string time ":")))))))
1067 ;;;; Code
1069 (defun org-ascii-code (code contents info)
1070 "Return a CODE object from Org to ASCII.
1071 CONTENTS is nil. INFO is a plist holding contextual
1072 information."
1073 (format org-ascii-verbatim-format (org-element-property :value code)))
1076 ;;;; Drawer
1078 (defun org-ascii-drawer (drawer contents info)
1079 "Transcode a DRAWER element from Org to ASCII.
1080 CONTENTS holds the contents of the block. INFO is a plist
1081 holding contextual information."
1082 (let ((name (org-element-property :drawer-name drawer))
1083 (width (org-ascii--current-text-width drawer info)))
1084 (if (functionp org-ascii-format-drawer-function)
1085 (funcall org-ascii-format-drawer-function name contents width)
1086 ;; If there's no user defined function: simply
1087 ;; display contents of the drawer.
1088 contents)))
1091 ;;;; Dynamic Block
1093 (defun org-ascii-dynamic-block (dynamic-block contents info)
1094 "Transcode a DYNAMIC-BLOCK element from Org to ASCII.
1095 CONTENTS holds the contents of the block. INFO is a plist
1096 holding contextual information."
1097 contents)
1100 ;;;; Entity
1102 (defun org-ascii-entity (entity contents info)
1103 "Transcode an ENTITY object from Org to ASCII.
1104 CONTENTS are the definition itself. INFO is a plist holding
1105 contextual information."
1106 (org-element-property
1107 (intern (concat ":" (symbol-name (plist-get info :ascii-charset))))
1108 entity))
1111 ;;;; Example Block
1113 (defun org-ascii-example-block (example-block contents info)
1114 "Transcode a EXAMPLE-BLOCK element from Org to ASCII.
1115 CONTENTS is nil. INFO is a plist holding contextual information."
1116 (org-ascii--box-string
1117 (org-export-format-code-default example-block info) info))
1120 ;;;; Export Snippet
1122 (defun org-ascii-export-snippet (export-snippet contents info)
1123 "Transcode a EXPORT-SNIPPET object from Org to ASCII.
1124 CONTENTS is nil. INFO is a plist holding contextual information."
1125 (when (eq (org-export-snippet-backend export-snippet) 'ascii)
1126 (org-element-property :value export-snippet)))
1129 ;;;; Export Block
1131 (defun org-ascii-export-block (export-block contents info)
1132 "Transcode a EXPORT-BLOCK element from Org to ASCII.
1133 CONTENTS is nil. INFO is a plist holding contextual information."
1134 (when (string= (org-element-property :type export-block) "ASCII")
1135 (org-remove-indentation (org-element-property :value export-block))))
1138 ;;;; Fixed Width
1140 (defun org-ascii-fixed-width (fixed-width contents info)
1141 "Transcode a FIXED-WIDTH element from Org to ASCII.
1142 CONTENTS is nil. INFO is a plist holding contextual information."
1143 (org-ascii--box-string
1144 (org-remove-indentation
1145 (org-element-property :value fixed-width)) info))
1148 ;;;; Footnote Definition
1150 ;; Footnote Definitions are ignored. They are compiled at the end of
1151 ;; the document, by `org-ascii-inner-template'.
1154 ;;;; Footnote Reference
1156 (defun org-ascii-footnote-reference (footnote-reference contents info)
1157 "Transcode a FOOTNOTE-REFERENCE element from Org to ASCII.
1158 CONTENTS is nil. INFO is a plist holding contextual information."
1159 (format "[%s]" (org-export-get-footnote-number footnote-reference info)))
1162 ;;;; Headline
1164 (defun org-ascii-headline (headline contents info)
1165 "Transcode a HEADLINE element from Org to ASCII.
1166 CONTENTS holds the contents of the headline. INFO is a plist
1167 holding contextual information."
1168 ;; Don't export footnote section, which will be handled at the end
1169 ;; of the template.
1170 (unless (org-element-property :footnote-section-p headline)
1171 (let* ((low-level-rank (org-export-low-level-p headline info))
1172 (width (org-ascii--current-text-width headline info))
1173 ;; Blank lines between headline and its contents.
1174 ;; `org-ascii-headline-spacing', when set, overwrites
1175 ;; original buffer's spacing.
1176 (pre-blanks
1177 (make-string
1178 (if org-ascii-headline-spacing (car org-ascii-headline-spacing)
1179 (org-element-property :pre-blank headline)) ?\n))
1180 ;; Even if HEADLINE has no section, there might be some
1181 ;; links in its title that we shouldn't forget to describe.
1182 (links
1183 (unless (or (eq (caar (org-element-contents headline)) 'section))
1184 (let ((title (org-element-property :title headline)))
1185 (when (consp title)
1186 (org-ascii--describe-links
1187 (org-ascii--unique-links title info) width info))))))
1188 ;; Deep subtree: export it as a list item.
1189 (if low-level-rank
1190 (concat
1191 ;; Bullet.
1192 (let ((bullets (cdr (assq (plist-get info :ascii-charset)
1193 org-ascii-bullets))))
1194 (char-to-string
1195 (nth (mod (1- low-level-rank) (length bullets)) bullets)))
1197 ;; Title.
1198 (org-ascii--build-title headline info width) "\n"
1199 ;; Contents, indented by length of bullet.
1200 pre-blanks
1201 (org-ascii--indent-string
1202 (concat contents
1203 (when (org-string-nw-p links) (concat "\n\n" links)))
1205 ;; Else: Standard headline.
1206 (concat
1207 (org-ascii--build-title headline info width 'underline)
1208 "\n" pre-blanks
1209 (concat (when (org-string-nw-p links) links) contents))))))
1212 ;;;; Horizontal Rule
1214 (defun org-ascii-horizontal-rule (horizontal-rule contents info)
1215 "Transcode an HORIZONTAL-RULE object from Org to ASCII.
1216 CONTENTS is nil. INFO is a plist holding contextual
1217 information."
1218 (let ((text-width (org-ascii--current-text-width horizontal-rule info))
1219 (spec-width
1220 (org-export-read-attribute :attr_ascii horizontal-rule :width)))
1221 (org-ascii--justify-string
1222 (make-string (if (and spec-width (string-match "^[0-9]+$" spec-width))
1223 (string-to-number spec-width)
1224 text-width)
1225 (if (eq (plist-get info :ascii-charset) 'utf-8) ?― ?-))
1226 text-width 'center)))
1229 ;;;; Inline Src Block
1231 (defun org-ascii-inline-src-block (inline-src-block contents info)
1232 "Transcode an INLINE-SRC-BLOCK element from Org to ASCII.
1233 CONTENTS holds the contents of the item. INFO is a plist holding
1234 contextual information."
1235 (format org-ascii-verbatim-format
1236 (org-element-property :value inline-src-block)))
1239 ;;;; Inlinetask
1241 (defun org-ascii-inlinetask (inlinetask contents info)
1242 "Transcode an INLINETASK element from Org to ASCII.
1243 CONTENTS holds the contents of the block. INFO is a plist
1244 holding contextual information."
1245 (let ((width (org-ascii--current-text-width inlinetask info)))
1246 ;; If `org-ascii-format-inlinetask-function' is provided, call it
1247 ;; with appropriate arguments.
1248 (if (functionp org-ascii-format-inlinetask-function)
1249 (funcall org-ascii-format-inlinetask-function
1250 ;; todo.
1251 (and (plist-get info :with-todo-keywords)
1252 (let ((todo (org-element-property
1253 :todo-keyword inlinetask)))
1254 (and todo (org-export-data todo info))))
1255 ;; todo-type
1256 (org-element-property :todo-type inlinetask)
1257 ;; priority
1258 (and (plist-get info :with-priority)
1259 (org-element-property :priority inlinetask))
1260 ;; title
1261 (org-export-data (org-element-property :title inlinetask) info)
1262 ;; tags
1263 (and (plist-get info :with-tags)
1264 (org-element-property :tags inlinetask))
1265 ;; contents and width
1266 contents width)
1267 ;; Otherwise, use a default template.
1268 (let* ((utf8p (eq (plist-get info :ascii-charset) 'utf-8)))
1269 (org-ascii--indent-string
1270 (concat
1271 ;; Top line, with an additional blank line if not in UTF-8.
1272 (make-string width (if utf8p ?━ ?_)) "\n"
1273 (unless utf8p (concat (make-string width ? ) "\n"))
1274 ;; Add title. Fill it if wider than inlinetask.
1275 (let ((title (org-ascii--build-title inlinetask info width)))
1276 (if (<= (length title) width) title
1277 (org-ascii--fill-string title width info)))
1278 "\n"
1279 ;; If CONTENTS is not empty, insert it along with
1280 ;; a separator.
1281 (when (org-string-nw-p contents)
1282 (concat (make-string width (if utf8p ?─ ?-)) "\n" contents))
1283 ;; Bottom line.
1284 (make-string width (if utf8p ?━ ?_)))
1285 ;; Flush the inlinetask to the right.
1286 (- org-ascii-text-width org-ascii-global-margin
1287 (if (not (org-export-get-parent-headline inlinetask)) 0
1288 org-ascii-inner-margin)
1289 (org-ascii--current-text-width inlinetask info)))))))
1292 ;;;; Italic
1294 (defun org-ascii-italic (italic contents info)
1295 "Transcode italic from Org to ASCII.
1296 CONTENTS is the text with italic markup. INFO is a plist holding
1297 contextual information."
1298 (format "/%s/" contents))
1301 ;;;; Item
1303 (defun org-ascii-item (item contents info)
1304 "Transcode an ITEM element from Org to ASCII.
1305 CONTENTS holds the contents of the item. INFO is a plist holding
1306 contextual information."
1307 (let* ((utf8p (eq (plist-get info :ascii-charset) 'utf-8))
1308 (checkbox (org-ascii--checkbox item info))
1309 (list-type (org-element-property :type (org-export-get-parent item)))
1310 (bullet
1311 ;; First parent of ITEM is always the plain-list. Get
1312 ;; `:type' property from it.
1313 (org-list-bullet-string
1314 (case list-type
1315 (descriptive
1316 (concat checkbox
1317 (org-export-data (org-element-property :tag item) info)
1318 ": "))
1319 (ordered
1320 ;; Return correct number for ITEM, paying attention to
1321 ;; counters.
1322 (let* ((struct (org-element-property :structure item))
1323 (bul (org-element-property :bullet item))
1324 (num (number-to-string
1325 (car (last (org-list-get-item-number
1326 (org-element-property :begin item)
1327 struct
1328 (org-list-prevs-alist struct)
1329 (org-list-parents-alist struct)))))))
1330 (replace-regexp-in-string "[0-9]+" num bul)))
1331 (t (let ((bul (org-element-property :bullet item)))
1332 ;; Change bullets into more visible form if UTF-8 is active.
1333 (if (not utf8p) bul
1334 (replace-regexp-in-string
1335 "-" "•"
1336 (replace-regexp-in-string
1337 "+" "⁃"
1338 (replace-regexp-in-string "*" "‣" bul))))))))))
1339 (concat
1340 bullet
1341 (unless (eq list-type 'descriptive) checkbox)
1342 ;; Contents: Pay attention to indentation. Note: check-boxes are
1343 ;; already taken care of at the paragraph level so they don't
1344 ;; interfere with indentation.
1345 (let ((contents (org-ascii--indent-string contents (length bullet))))
1346 (if (eq (org-element-type (car (org-element-contents item))) 'paragraph)
1347 (org-trim contents)
1348 (concat "\n" contents))))))
1351 ;;;; Keyword
1353 (defun org-ascii-keyword (keyword contents info)
1354 "Transcode a KEYWORD element from Org to ASCII.
1355 CONTENTS is nil. INFO is a plist holding contextual
1356 information."
1357 (let ((key (org-element-property :key keyword))
1358 (value (org-element-property :value keyword)))
1359 (cond
1360 ((string= key "ASCII") value)
1361 ((string= key "TOC")
1362 (let ((value (downcase value)))
1363 (cond
1364 ((string-match "\\<headlines\\>" value)
1365 (let ((depth (or (and (string-match "[0-9]+" value)
1366 (string-to-number (match-string 0 value)))
1367 (plist-get info :with-toc))))
1368 (org-ascii--build-toc
1369 info (and (wholenump depth) depth) keyword)))
1370 ((string= "tables" value)
1371 (org-ascii--list-tables keyword info))
1372 ((string= "listings" value)
1373 (org-ascii--list-listings keyword info))))))))
1376 ;;;; Latex Environment
1378 (defun org-ascii-latex-environment (latex-environment contents info)
1379 "Transcode a LATEX-ENVIRONMENT element from Org to ASCII.
1380 CONTENTS is nil. INFO is a plist holding contextual
1381 information."
1382 (when (plist-get info :with-latex)
1383 (org-remove-indentation (org-element-property :value latex-environment))))
1386 ;;;; Latex Fragment
1388 (defun org-ascii-latex-fragment (latex-fragment contents info)
1389 "Transcode a LATEX-FRAGMENT object from Org to ASCII.
1390 CONTENTS is nil. INFO is a plist holding contextual
1391 information."
1392 (when (plist-get info :with-latex)
1393 (org-element-property :value latex-fragment)))
1396 ;;;; Line Break
1398 (defun org-ascii-line-break (line-break contents info)
1399 "Transcode a LINE-BREAK object from Org to ASCII.
1400 CONTENTS is nil. INFO is a plist holding contextual
1401 information." hard-newline)
1404 ;;;; Link
1406 (defun org-ascii-link (link desc info)
1407 "Transcode a LINK object from Org to ASCII.
1409 DESC is the description part of the link, or the empty string.
1410 INFO is a plist holding contextual information."
1411 (let ((raw-link (org-element-property :raw-link link))
1412 (type (org-element-property :type link)))
1413 (cond
1414 ((string= type "coderef")
1415 (let ((ref (org-element-property :path link)))
1416 (format (org-export-get-coderef-format ref desc)
1417 (org-export-resolve-coderef ref info))))
1418 ;; Do not apply a special syntax on radio links. Though, use
1419 ;; transcoded target's contents as output.
1420 ((string= type "radio")
1421 (let ((destination (org-export-resolve-radio-link link info)))
1422 (when destination
1423 (org-export-data (org-element-contents destination) info))))
1424 ;; Do not apply a special syntax on fuzzy links pointing to
1425 ;; targets.
1426 ((string= type "fuzzy")
1427 (let ((destination (org-export-resolve-fuzzy-link link info)))
1428 (if (org-string-nw-p desc) desc
1429 (when destination
1430 (let ((number
1431 (org-export-get-ordinal
1432 destination info nil 'org-ascii--has-caption-p)))
1433 (when number
1434 (if (atom number) (number-to-string number)
1435 (mapconcat 'number-to-string number "."))))))))
1437 (if (not (org-string-nw-p desc)) (format "[%s]" raw-link)
1438 (concat
1439 (format "[%s]" desc)
1440 (unless org-ascii-links-to-notes (format " (%s)" raw-link))))))))
1443 ;;;; Paragraph
1445 (defun org-ascii-paragraph (paragraph contents info)
1446 "Transcode a PARAGRAPH element from Org to ASCII.
1447 CONTENTS is the contents of the paragraph, as a string. INFO is
1448 the plist used as a communication channel."
1449 (let ((contents (if (not (wholenump org-ascii-indented-line-width)) contents
1450 (concat
1451 (make-string org-ascii-indented-line-width ? )
1452 (replace-regexp-in-string "\\`[ \t]+" "" contents)))))
1453 (org-ascii--fill-string
1454 contents (org-ascii--current-text-width paragraph info) info)))
1457 ;;;; Plain List
1459 (defun org-ascii-plain-list (plain-list contents info)
1460 "Transcode a PLAIN-LIST element from Org to ASCII.
1461 CONTENTS is the contents of the list. INFO is a plist holding
1462 contextual information."
1463 contents)
1466 ;;;; Plain Text
1468 (defun org-ascii-plain-text (text info)
1469 "Transcode a TEXT string from Org to ASCII.
1470 INFO is a plist used as a communication channel."
1471 (let ((utf8p (eq (plist-get info :ascii-charset) 'utf-8)))
1472 (when (and utf8p (plist-get info :with-smart-quotes))
1473 (setq text (org-export-activate-smart-quotes text :utf-8 info)))
1474 (if (not (plist-get info :with-special-strings)) text
1475 (setq text (replace-regexp-in-string "\\\\-" "" text))
1476 (if (not utf8p) text
1477 ;; Usual replacements in utf-8 with proper option set.
1478 (replace-regexp-in-string
1479 "\\.\\.\\." "…"
1480 (replace-regexp-in-string
1481 "--" "–"
1482 (replace-regexp-in-string "---" "—" text)))))))
1485 ;;;; Planning
1487 (defun org-ascii-planning (planning contents info)
1488 "Transcode a PLANNING element from Org to ASCII.
1489 CONTENTS is nil. INFO is a plist used as a communication
1490 channel."
1491 (mapconcat
1492 'identity
1493 (delq nil
1494 (list (let ((closed (org-element-property :closed planning)))
1495 (when closed
1496 (concat org-closed-string " "
1497 (org-translate-time
1498 (org-element-property :raw-value closed)))))
1499 (let ((deadline (org-element-property :deadline planning)))
1500 (when deadline
1501 (concat org-deadline-string " "
1502 (org-translate-time
1503 (org-element-property :raw-value deadline)))))
1504 (let ((scheduled (org-element-property :scheduled planning)))
1505 (when scheduled
1506 (concat org-scheduled-string " "
1507 (org-translate-time
1508 (org-element-property :raw-value scheduled)))))))
1509 " "))
1512 ;;;; Quote Block
1514 (defun org-ascii-quote-block (quote-block contents info)
1515 "Transcode a QUOTE-BLOCK element from Org to ASCII.
1516 CONTENTS holds the contents of the block. INFO is a plist
1517 holding contextual information."
1518 (org-ascii--indent-string contents org-ascii-quote-margin))
1521 ;;;; Quote Section
1523 (defun org-ascii-quote-section (quote-section contents info)
1524 "Transcode a QUOTE-SECTION element from Org to ASCII.
1525 CONTENTS is nil. INFO is a plist holding contextual information."
1526 (let ((width (org-ascii--current-text-width quote-section info))
1527 (value
1528 (org-export-data
1529 (org-remove-indentation (org-element-property :value quote-section))
1530 info)))
1531 (org-ascii--indent-string
1532 value
1533 (+ org-ascii-quote-margin
1534 ;; Don't apply inner margin if parent headline is low level.
1535 (let ((headline (org-export-get-parent-headline quote-section)))
1536 (if (org-export-low-level-p headline info) 0
1537 org-ascii-inner-margin))))))
1540 ;;;; Radio Target
1542 (defun org-ascii-radio-target (radio-target contents info)
1543 "Transcode a RADIO-TARGET object from Org to ASCII.
1544 CONTENTS is the contents of the target. INFO is a plist holding
1545 contextual information."
1546 contents)
1549 ;;;; Section
1551 (defun org-ascii-section (section contents info)
1552 "Transcode a SECTION element from Org to ASCII.
1553 CONTENTS is the contents of the section. INFO is a plist holding
1554 contextual information."
1555 (org-ascii--indent-string
1556 (concat
1557 contents
1558 (when org-ascii-links-to-notes
1559 ;; Add list of links at the end of SECTION.
1560 (let ((links (org-ascii--describe-links
1561 (org-ascii--unique-links section info)
1562 (org-ascii--current-text-width section info) info)))
1563 ;; Separate list of links and section contents.
1564 (when (org-string-nw-p links) (concat "\n\n" links)))))
1565 ;; Do not apply inner margin if parent headline is low level.
1566 (let ((headline (org-export-get-parent-headline section)))
1567 (if (or (not headline) (org-export-low-level-p headline info)) 0
1568 org-ascii-inner-margin))))
1571 ;;;; Special Block
1573 (defun org-ascii-special-block (special-block contents info)
1574 "Transcode a SPECIAL-BLOCK element from Org to ASCII.
1575 CONTENTS holds the contents of the block. INFO is a plist
1576 holding contextual information."
1577 contents)
1580 ;;;; Src Block
1582 (defun org-ascii-src-block (src-block contents info)
1583 "Transcode a SRC-BLOCK element from Org to ASCII.
1584 CONTENTS holds the contents of the item. INFO is a plist holding
1585 contextual information."
1586 (let ((caption (org-ascii--build-caption src-block info))
1587 (code (org-export-format-code-default src-block info)))
1588 (if (equal code "") ""
1589 (concat
1590 (when (and caption org-ascii-caption-above) (concat caption "\n"))
1591 (org-ascii--box-string code info)
1592 (when (and caption (not org-ascii-caption-above))
1593 (concat "\n" caption))))))
1596 ;;;; Statistics Cookie
1598 (defun org-ascii-statistics-cookie (statistics-cookie contents info)
1599 "Transcode a STATISTICS-COOKIE object from Org to ASCII.
1600 CONTENTS is nil. INFO is a plist holding contextual information."
1601 (org-element-property :value statistics-cookie))
1604 ;;;; Subscript
1606 (defun org-ascii-subscript (subscript contents info)
1607 "Transcode a SUBSCRIPT object from Org to ASCII.
1608 CONTENTS is the contents of the object. INFO is a plist holding
1609 contextual information."
1610 (if (org-element-property :use-brackets-p subscript)
1611 (format "_{%s}" contents)
1612 (format "_%s" contents)))
1615 ;;;; Superscript
1617 (defun org-ascii-superscript (superscript contents info)
1618 "Transcode a SUPERSCRIPT object from Org to ASCII.
1619 CONTENTS is the contents of the object. INFO is a plist holding
1620 contextual information."
1621 (if (org-element-property :use-brackets-p superscript)
1622 (format "_{%s}" contents)
1623 (format "_%s" contents)))
1626 ;;;; Strike-through
1628 (defun org-ascii-strike-through (strike-through contents info)
1629 "Transcode STRIKE-THROUGH from Org to ASCII.
1630 CONTENTS is text with strike-through markup. INFO is a plist
1631 holding contextual information."
1632 (format "+%s+" contents))
1635 ;;;; Table
1637 (defun org-ascii-table (table contents info)
1638 "Transcode a TABLE element from Org to ASCII.
1639 CONTENTS is the contents of the table. INFO is a plist holding
1640 contextual information."
1641 (let ((caption (org-ascii--build-caption table info)))
1642 (concat
1643 ;; Possibly add a caption string above.
1644 (when (and caption org-ascii-caption-above) (concat caption "\n"))
1645 ;; Insert table. Note: "table.el" tables are left unmodified.
1646 (cond ((eq (org-element-property :type table) 'org) contents)
1647 ((and org-ascii-table-use-ascii-art
1648 (eq (plist-get info :ascii-charset) 'utf-8)
1649 (require 'ascii-art-to-unicode nil t))
1650 (with-temp-buffer
1651 (insert (org-remove-indentation
1652 (org-element-property :value table)))
1653 (goto-char (point-min))
1654 (aa2u)
1655 (goto-char (point-max))
1656 (skip-chars-backward " \r\t\n")
1657 (buffer-substring (point-min) (point))))
1658 (t (org-remove-indentation (org-element-property :value table))))
1659 ;; Possible add a caption string below.
1660 (when (and caption (not org-ascii-caption-above))
1661 (concat "\n" caption)))))
1664 ;;;; Table Cell
1666 (defun org-ascii--table-cell-width (table-cell info)
1667 "Return width of TABLE-CELL.
1669 INFO is a plist used as a communication channel.
1671 Width of a cell is determined either by a width cookie in the
1672 same column as the cell, or by the maximum cell's length in that
1673 column.
1675 When `org-ascii-table-widen-columns' is non-nil, width cookies
1676 are ignored."
1677 (let* ((row (org-export-get-parent table-cell))
1678 (table (org-export-get-parent row))
1679 (col (let ((cells (org-element-contents row)))
1680 (- (length cells) (length (memq table-cell cells)))))
1681 (cache
1682 (or (plist-get info :ascii-table-cell-width-cache)
1683 (plist-get (setq info
1684 (plist-put info :ascii-table-cell-width-cache
1685 (make-hash-table :test 'equal)))
1686 :ascii-table-cell-width-cache)))
1687 (key (cons table col)))
1688 (or (gethash key cache)
1689 (puthash
1691 (or (and (not org-ascii-table-widen-columns)
1692 (org-export-table-cell-width table-cell info))
1693 (let* ((max-width 0))
1694 (org-element-map table 'table-row
1695 (lambda (row)
1696 (setq max-width
1697 (max (length
1698 (org-export-data
1699 (org-element-contents
1700 (elt (org-element-contents row) col))
1701 info))
1702 max-width)))
1703 info)
1704 max-width))
1705 cache))))
1707 (defun org-ascii-table-cell (table-cell contents info)
1708 "Transcode a TABLE-CELL object from Org to ASCII.
1709 CONTENTS is the cell contents. INFO is a plist used as
1710 a communication channel."
1711 ;; Determine column width. When `org-ascii-table-widen-columns'
1712 ;; is nil and some width cookie has set it, use that value.
1713 ;; Otherwise, compute the maximum width among transcoded data of
1714 ;; each cell in the column.
1715 (let ((width (org-ascii--table-cell-width table-cell info)))
1716 ;; When contents are too large, truncate them.
1717 (unless (or org-ascii-table-widen-columns (<= (length contents) width))
1718 (setq contents (concat (substring contents 0 (- width 2)) "=>")))
1719 ;; Align contents correctly within the cell.
1720 (let* ((indent-tabs-mode nil)
1721 (data
1722 (when contents
1723 (org-ascii--justify-string
1724 contents width
1725 (org-export-table-cell-alignment table-cell info)))))
1726 (setq contents (concat data (make-string (- width (length data)) ? ))))
1727 ;; Return cell.
1728 (concat (format " %s " contents)
1729 (when (memq 'right (org-export-table-cell-borders table-cell info))
1730 (if (eq (plist-get info :ascii-charset) 'utf-8) "│" "|")))))
1733 ;;;; Table Row
1735 (defun org-ascii-table-row (table-row contents info)
1736 "Transcode a TABLE-ROW element from Org to ASCII.
1737 CONTENTS is the row contents. INFO is a plist used as
1738 a communication channel."
1739 (when (eq (org-element-property :type table-row) 'standard)
1740 (let ((build-hline
1741 (function
1742 (lambda (lcorner horiz vert rcorner)
1743 (concat
1744 (apply
1745 'concat
1746 (org-element-map table-row 'table-cell
1747 (lambda (cell)
1748 (let ((width (org-ascii--table-cell-width cell info))
1749 (borders (org-export-table-cell-borders cell info)))
1750 (concat
1751 ;; In order to know if CELL starts the row, do
1752 ;; not compare it with the first cell in the
1753 ;; row as there might be a special column.
1754 ;; Instead, compare it with first exportable
1755 ;; cell, obtained with `org-element-map'.
1756 (when (and (memq 'left borders)
1757 (eq (org-element-map table-row 'table-cell
1758 'identity info t)
1759 cell))
1760 lcorner)
1761 (make-string (+ 2 width) (string-to-char horiz))
1762 (cond
1763 ((not (memq 'right borders)) nil)
1764 ((eq (car (last (org-element-contents table-row))) cell)
1765 rcorner)
1766 (t vert)))))
1767 info)) "\n"))))
1768 (utf8p (eq (plist-get info :ascii-charset) 'utf-8))
1769 (borders (org-export-table-cell-borders
1770 (org-element-map table-row 'table-cell 'identity info t)
1771 info)))
1772 (concat (cond
1773 ((and (memq 'top borders) (or utf8p (memq 'above borders)))
1774 (if utf8p (funcall build-hline "┍" "━" "┯" "┑")
1775 (funcall build-hline "+" "-" "+" "+")))
1776 ((memq 'above borders)
1777 (if utf8p (funcall build-hline "├" "─" "┼" "┤")
1778 (funcall build-hline "+" "-" "+" "+"))))
1779 (when (memq 'left borders) (if utf8p "│" "|"))
1780 contents "\n"
1781 (when (and (memq 'bottom borders) (or utf8p (memq 'below borders)))
1782 (if utf8p (funcall build-hline "┕" "━" "┷" "┙")
1783 (funcall build-hline "+" "-" "+" "+")))))))
1786 ;;;; Timestamp
1788 (defun org-ascii-timestamp (timestamp contents info)
1789 "Transcode a TIMESTAMP object from Org to ASCII.
1790 CONTENTS is nil. INFO is a plist holding contextual information."
1791 (org-ascii-plain-text (org-timestamp-translate timestamp) info))
1794 ;;;; Underline
1796 (defun org-ascii-underline (underline contents info)
1797 "Transcode UNDERLINE from Org to ASCII.
1798 CONTENTS is the text with underline markup. INFO is a plist
1799 holding contextual information."
1800 (format "_%s_" contents))
1803 ;;;; Verbatim
1805 (defun org-ascii-verbatim (verbatim contents info)
1806 "Return a VERBATIM object from Org to ASCII.
1807 CONTENTS is nil. INFO is a plist holding contextual information."
1808 (format org-ascii-verbatim-format
1809 (org-element-property :value verbatim)))
1812 ;;;; Verse Block
1814 (defun org-ascii-verse-block (verse-block contents info)
1815 "Transcode a VERSE-BLOCK element from Org to ASCII.
1816 CONTENTS is verse block contents. INFO is a plist holding
1817 contextual information."
1818 (let ((verse-width (org-ascii--current-text-width verse-block info)))
1819 (org-ascii--indent-string
1820 (org-ascii--justify-string contents verse-width 'left)
1821 org-ascii-quote-margin)))
1825 ;;; Filters
1827 (defun org-ascii-filter-headline-blank-lines (headline back-end info)
1828 "Filter controlling number of blank lines after a headline.
1830 HEADLINE is a string representing a transcoded headline.
1831 BACK-END is symbol specifying back-end used for export. INFO is
1832 plist containing the communication channel.
1834 This function only applies to `ascii' back-end. See
1835 `org-ascii-headline-spacing' for information."
1836 (if (not org-ascii-headline-spacing) headline
1837 (let ((blanks (make-string (1+ (cdr org-ascii-headline-spacing)) ?\n)))
1838 (replace-regexp-in-string "\n\\(?:\n[ \t]*\\)*\\'" blanks headline))))
1840 (defun org-ascii-filter-paragraph-spacing (tree back-end info)
1841 "Filter controlling number of blank lines between paragraphs.
1843 TREE is the parse tree. BACK-END is the symbol specifying
1844 back-end used for export. INFO is a plist used as
1845 a communication channel.
1847 See `org-ascii-paragraph-spacing' for information."
1848 (when (wholenump org-ascii-paragraph-spacing)
1849 (org-element-map tree 'paragraph
1850 (lambda (p)
1851 (when (eq (org-element-type (org-export-get-next-element p info))
1852 'paragraph)
1853 (org-element-put-property
1854 p :post-blank org-ascii-paragraph-spacing)))))
1855 tree)
1857 (defun org-ascii-filter-comment-spacing (tree backend info)
1858 "Filter removing blank lines between comments.
1859 TREE is the parse tree. BACK-END is the symbol specifying
1860 back-end used for export. INFO is a plist used as
1861 a communication channel."
1862 (org-element-map tree '(comment comment-block)
1863 (lambda (c)
1864 (when (memq (org-element-type (org-export-get-next-element c info))
1865 '(comment comment-block))
1866 (org-element-put-property c :post-blank 0))))
1867 tree)
1871 ;;; End-user functions
1873 ;;;###autoload
1874 (defun org-ascii-export-as-ascii
1875 (&optional async subtreep visible-only body-only ext-plist)
1876 "Export current buffer to a text buffer.
1878 If narrowing is active in the current buffer, only export its
1879 narrowed part.
1881 If a region is active, export that region.
1883 A non-nil optional argument ASYNC means the process should happen
1884 asynchronously. The resulting buffer should be accessible
1885 through the `org-export-stack' interface.
1887 When optional argument SUBTREEP is non-nil, export the sub-tree
1888 at point, extracting information from the headline properties
1889 first.
1891 When optional argument VISIBLE-ONLY is non-nil, don't export
1892 contents of hidden elements.
1894 When optional argument BODY-ONLY is non-nil, strip title and
1895 table of contents from output.
1897 EXT-PLIST, when provided, is a property list with external
1898 parameters overriding Org default settings, but still inferior to
1899 file-local settings.
1901 Export is done in a buffer named \"*Org ASCII Export*\", which
1902 will be displayed when `org-export-show-temporary-export-buffer'
1903 is non-nil."
1904 (interactive)
1905 (if async
1906 (org-export-async-start
1907 (lambda (output)
1908 (with-current-buffer (get-buffer-create "*Org ASCII Export*")
1909 (erase-buffer)
1910 (insert output)
1911 (goto-char (point-min))
1912 (text-mode)
1913 (org-export-add-to-stack (current-buffer) 'ascii)))
1914 `(org-export-as 'ascii ,subtreep ,visible-only ,body-only
1915 ',ext-plist))
1916 (let ((outbuf (org-export-to-buffer
1917 'ascii "*Org ASCII Export*"
1918 subtreep visible-only body-only ext-plist)))
1919 (with-current-buffer outbuf (text-mode))
1920 (when org-export-show-temporary-export-buffer
1921 (switch-to-buffer-other-window outbuf)))))
1923 ;;;###autoload
1924 (defun org-ascii-export-to-ascii
1925 (&optional async subtreep visible-only body-only ext-plist)
1926 "Export current buffer to a text file.
1928 If narrowing is active in the current buffer, only export its
1929 narrowed part.
1931 If a region is active, export that region.
1933 A non-nil optional argument ASYNC means the process should happen
1934 asynchronously. The resulting file should be accessible through
1935 the `org-export-stack' interface.
1937 When optional argument SUBTREEP is non-nil, export the sub-tree
1938 at point, extracting information from the headline properties
1939 first.
1941 When optional argument VISIBLE-ONLY is non-nil, don't export
1942 contents of hidden elements.
1944 When optional argument BODY-ONLY is non-nil, strip title and
1945 table of contents from output.
1947 EXT-PLIST, when provided, is a property list with external
1948 parameters overriding Org default settings, but still inferior to
1949 file-local settings.
1951 Return output file's name."
1952 (interactive)
1953 (let ((outfile (org-export-output-file-name ".txt" subtreep)))
1954 (if async
1955 (org-export-async-start
1956 (lambda (f) (org-export-add-to-stack f 'ascii))
1957 `(expand-file-name
1958 (org-export-to-file
1959 'ascii ,outfile ,subtreep ,visible-only ,body-only ',ext-plist)))
1960 (org-export-to-file
1961 'ascii outfile subtreep visible-only body-only ext-plist))))
1963 ;;;###autoload
1964 (defun org-ascii-publish-to-ascii (plist filename pub-dir)
1965 "Publish an Org file to ASCII.
1967 FILENAME is the filename of the Org file to be published. PLIST
1968 is the property list for the given project. PUB-DIR is the
1969 publishing directory.
1971 Return output file name."
1972 (org-publish-org-to
1973 'ascii filename ".txt" `(:ascii-charset ascii ,@plist) pub-dir))
1975 ;;;###autoload
1976 (defun org-ascii-publish-to-latin1 (plist filename pub-dir)
1977 "Publish an Org file to Latin-1.
1979 FILENAME is the filename of the Org file to be published. PLIST
1980 is the property list for the given project. PUB-DIR is the
1981 publishing directory.
1983 Return output file name."
1984 (org-publish-org-to
1985 'ascii filename ".txt" `(:ascii-charset latin1 ,@plist) pub-dir))
1987 ;;;###autoload
1988 (defun org-ascii-publish-to-utf8 (plist filename pub-dir)
1989 "Publish an org file to UTF-8.
1991 FILENAME is the filename of the Org file to be published. PLIST
1992 is the property list for the given project. PUB-DIR is the
1993 publishing directory.
1995 Return output file name."
1996 (org-publish-org-to
1997 'ascii filename ".txt" `(:ascii-charset utf-8 ,@plist) pub-dir))
2000 (provide 'ox-ascii)
2002 ;; Local variables:
2003 ;; generated-autoload-file: "org-loaddefs.el"
2004 ;; coding: utf-8-emacs
2005 ;; End:
2007 ;;; ox-ascii.el ends here