1 ;;; org-ascii.el --- ASCII export for Org-mode
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009
4 ;; Free Software Foundation, Inc.
6 ;; Author: Carsten Dominik <carsten at orgmode dot org>
7 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; Homepage: http://orgmode.org
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
33 (defgroup org-export-ascii nil
34 "Options specific for ASCII export of Org-mode files."
35 :tag
"Org Export ASCII"
38 (defcustom org-export-ascii-underline
'(?\$ ?\
# ?^ ?\~ ?\
= ?\-
)
39 "Characters for underlining headings in ASCII export.
40 In the given sequence, these characters will be used for level 1, 2, ..."
41 :group
'org-export-ascii
42 :type
'(repeat character
))
44 (defcustom org-export-ascii-bullets
'(?
* ?
+ ?-
)
45 "Bullet characters for headlines converted to lists in ASCII export.
46 The first character is used for the first lest level generated in this
47 way, and so on. If there are more levels than characters given here,
48 the list will be repeated.
49 Note that plain lists will keep the same bullets as the have in the
51 :group
'org-export-ascii
52 :type
'(repeat character
))
54 (defcustom org-export-ascii-links-to-notes t
55 "Non-nil means convert links to notes before the next headline.
56 When nil, the link will be exported in place. If the line becomes long
57 in this way, it will be wrapped."
58 :group
'org-export-ascii
61 (defcustom org-export-ascii-table-keep-all-vertical-lines nil
62 "Non-nil means keep all vertical lines in ASCII tables.
63 When nil, vertical lines will be removed except for those needed
65 :group
'org-export-ascii
68 (defcustom org-export-ascii-table-widen-columns t
69 "Non-nil means widen narrowed columns for export.
70 When nil, narrowed columns will look in ASCII export just like in org-mode,
71 i.e. with \"=>\" as ellipsis."
72 :group
'org-export-ascii
77 (defvar org-export-ascii-final-hook nil
78 "Hook run at the end of ASCII export, in the new buffer.")
82 (defvar org-ascii-current-indentation nil
) ; For communication
85 (defun org-export-as-ascii-to-buffer (arg)
86 "Call `org-export-as-ascii` with output to a temporary buffer.
87 No file is created. The prefix ARG is passed through to `org-export-as-ascii'."
89 (org-export-as-ascii arg nil nil
"*Org ASCII Export*")
90 (when org-export-show-temporary-export-buffer
91 (switch-to-buffer-other-window "*Org ASCII Export*")))
94 (defun org-replace-region-by-ascii (beg end
)
95 "Assume the current region has org-mode syntax, and convert it to plain ASCII.
96 This can be used in any buffer. For example, you could write an
97 itemized list in org-mode syntax in a Mail buffer and then use this
98 command to convert it."
100 (let (reg ascii buf pop-up-frames
)
101 (save-window-excursion
103 (setq ascii
(org-export-region-as-ascii
105 (setq reg
(buffer-substring beg end
)
106 buf
(get-buffer-create "*Org tmp*"))
107 (with-current-buffer buf
111 (setq ascii
(org-export-region-as-ascii
112 (point-min) (point-max) t
'string
)))
114 (delete-region beg end
)
118 (defun org-export-region-as-ascii (beg end
&optional body-only buffer
)
119 "Convert region from BEG to END in org-mode buffer to plain ASCII.
120 If prefix arg BODY-ONLY is set, omit file header, footer, and table of
121 contents, and only produce the region of converted text, useful for
122 cut-and-paste operations.
123 If BUFFER is a buffer or a string, use/create that buffer as a target
124 of the converted ASCII. If BUFFER is the symbol `string', return the
125 produced ASCII as a string and leave not buffer behind. For example,
126 a Lisp program could call this function in the following way:
128 (setq ascii (org-export-region-as-ascii beg end t 'string))
130 When called interactively, the output buffer is selected, and shown
131 in a window. A non-interactive call will only return the buffer."
133 (when (interactive-p)
134 (setq buffer
"*Org ASCII Export*"))
135 (let ((transient-mark-mode t
) (zmacs-regions t
)
137 (setq ext-plist
(plist-put ext-plist
:ignore-subtree-p t
))
139 (set-mark (point)) ;; to activate the region
141 (setq rtn
(org-export-as-ascii
144 (if (fboundp 'deactivate-mark
) (deactivate-mark))
145 (if (and (interactive-p) (bufferp rtn
))
146 (switch-to-buffer-other-window rtn
)
150 (defun org-export-as-ascii (arg &optional hidden ext-plist
151 to-buffer body-only pub-dir
)
152 "Export the outline as a pretty ASCII file.
153 If there is an active region, export only the region.
154 The prefix ARG specifies how many levels of the outline should become
155 underlined headlines, default is 3. Lower levels will become bulleted
156 lists. When HIDDEN is non-nil, don't display the ASCII buffer.
157 EXT-PLIST is a property list with external parameters overriding
158 org-mode's default settings, but still inferior to file-local
159 settings. When TO-BUFFER is non-nil, create a buffer with that
160 name and export to that buffer. If TO-BUFFER is the symbol
161 `string', don't leave any buffer behind but just return the
162 resulting ASCII as a string. When BODY-ONLY is set, don't produce
163 the file header and footer. When PUB-DIR is set, use this as the
164 publishing directory."
166 (run-hooks 'org-export-first-hook
)
167 (setq-default org-todo-line-regexp org-todo-line-regexp
)
168 (let* ((opt-plist (org-combine-plists (org-default-export-plist)
170 (org-infile-export-plist)))
171 (region-p (org-region-active-p))
172 (rbeg (and region-p
(region-beginning)))
173 (rend (and region-p
(region-end)))
175 (if (plist-get opt-plist
:ignore-subtree-p
)
180 (and (org-at-heading-p)
181 (>= (org-end-of-subtree t t
) rend
))))))
182 (level-offset (if subtree-p
185 (+ (funcall outline-level
)
186 (if org-odd-levels-only
1 0)))
188 (opt-plist (setq org-export-opt-plist
190 (org-export-add-subtree-options opt-plist rbeg
)
192 ;; The following two are dynamically scoped into other
194 (org-current-export-dir
195 (or pub-dir
(org-export-directory :html opt-plist
)))
196 (org-current-export-file buffer-file-name
)
197 (custom-times org-display-custom-times
)
198 (org-ascii-current-indentation '(0 .
0))
202 (case-fold-search nil
)
203 (bfname (buffer-file-name (or (buffer-base-buffer) (current-buffer))))
204 (filename (if to-buffer
206 (concat (file-name-as-directory
208 (org-export-directory :ascii opt-plist
)))
209 (file-name-sans-extension
211 (org-entry-get (region-beginning)
212 "EXPORT_FILE_NAME" t
))
213 (file-name-nondirectory bfname
)))
215 (filename (and filename
216 (if (equal (file-truename filename
)
217 (file-truename bfname
))
218 (concat filename
".txt")
220 (buffer (if to-buffer
222 ((eq to-buffer
'string
)
223 (get-buffer-create "*Org ASCII Export*"))
224 (t (get-buffer-create to-buffer
)))
225 (find-file-noselect filename
)))
226 (org-levels-open (make-vector org-level-max nil
))
227 (odd org-odd-levels-only
)
228 (date (plist-get opt-plist
:date
))
229 (author (plist-get opt-plist
:author
))
230 (title (or (and subtree-p
(org-export-get-title-from-subtree))
231 (plist-get opt-plist
:title
)
233 (plist-get opt-plist
:skip-before-1st-heading
))
234 (org-export-grab-title-from-buffer))
235 (and (buffer-file-name)
236 (file-name-sans-extension
237 (file-name-nondirectory bfname
)))
239 (email (plist-get opt-plist
:email
))
240 (language (plist-get opt-plist
:language
))
241 (quote-re0 (concat "^[ \t]*" org-quote-string
"\\>"))
246 (if (org-region-active-p) (region-beginning) (point-min))
247 (if (org-region-active-p) (region-end) (point-max))))
248 (lines (org-split-string
249 (org-export-preprocess-string
252 :skip-before-1st-heading
253 (plist-get opt-plist
:skip-before-1st-heading
)
254 :drawers
(plist-get opt-plist
:drawers
)
255 :tags
(plist-get opt-plist
:tags
)
256 :priority
(plist-get opt-plist
:priority
)
257 :footnotes
(plist-get opt-plist
:footnotes
)
258 :timestamps
(plist-get opt-plist
:timestamps
)
259 :todo-keywords
(plist-get opt-plist
:todo-keywords
)
260 :verbatim-multiline t
261 :select-tags
(plist-get opt-plist
:select-tags
)
262 :exclude-tags
(plist-get opt-plist
:exclude-tags
)
264 (plist-get opt-plist
:archived-trees
)
265 :add-text
(plist-get opt-plist
:text
))
267 thetoc have-headings first-heading-pos
268 table-open table-buffer link-buffer link desc desc0 rpl wrap
)
269 (let ((inhibit-read-only t
))
271 (remove-text-properties (point-min) (point-max)
272 '(:org-license-to-kill t
))))
274 (setq org-min-level
(org-get-min-level lines level-offset
))
275 (setq org-last-level org-min-level
)
276 (org-init-section-numbers)
277 (setq lang-words
(or (assoc language org-export-language-setup
)
278 (assoc "en" org-export-language-setup
)))
282 (org-install-letbind)
283 ;; create local variables for all options, to make sure all called
284 ;; functions get the correct information
286 (set (make-local-variable (nth 2 x
))
287 (plist-get opt-plist
(car x
))))
288 org-export-plist-vars
)
289 (org-set-local 'org-odd-levels-only odd
)
290 (setq umax
(if arg
(prefix-numeric-value arg
)
291 org-export-headline-levels
))
292 (setq umax-toc
(if (integerp org-export-with-toc
)
293 (min org-export-with-toc umax
)
298 (when (and title
(not (string= "" title
)))
299 (org-insert-centered title ?
=)
302 (if (and (or author email
)
303 org-export-author-info
)
304 (insert(concat (nth 1 lang-words
) ": " (or author
"")
305 (if (and org-export-email-info
306 email
(string-match "\\S-" email
))
307 (concat " <" email
">") "")
311 ((and date
(string-match "%" date
))
312 (setq date
(format-time-string date
)))
314 (t (setq date
(format-time-string "%Y-%m-%d %T %Z"))))
316 (if (and date org-export-time-stamp-file
)
317 (insert (concat (nth 2 lang-words
) ": " date
"\n")))
319 (unless (= (point) (point-min))
322 (if (and org-export-with-toc
(not body-only
))
324 (push (concat (nth 3 lang-words
) "\n") thetoc
)
325 (push (concat (make-string (string-width (nth 3 lang-words
)) ?
=)
327 (mapc '(lambda (line)
328 (if (string-match org-todo-line-regexp
330 ;; This is a headline
332 (setq have-headings t
)
333 (setq level
(- (match-end 1) (match-beginning 1)
335 level
(org-tr-level level
)
336 txt
(match-string 3 line
)
338 (or (and org-export-mark-todo-in-toc
340 (not (member (match-string 2 line
)
343 (and org-export-mark-todo-in-toc
345 (org-search-todo-below
347 (setq txt
(org-html-expand-for-ascii txt
))
349 (while (string-match org-bracket-link-regexp txt
)
352 (match-string (if (match-end 2) 3 1) txt
)
355 (if (and (memq org-export-with-tags
'(not-in-toc nil
))
357 (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$")
359 (setq txt
(replace-match "" t t txt
)))
360 (if (string-match quote-re0 txt
)
361 (setq txt
(replace-match "" t t txt
)))
363 (if org-export-with-section-numbers
364 (setq txt
(concat (org-section-number level
)
366 (if (<= level umax-toc
)
371 (* (max 0 (- level org-min-level
)) 4) ?\
)
372 (format (if todo
"%s (*)\n" "%s\n") txt
))
374 (setq org-last-level level
))
377 (setq thetoc
(if have-headings
(nreverse thetoc
) nil
))))
379 (org-init-section-numbers)
380 (while (setq line
(pop lines
))
381 (when (and link-buffer
(string-match "^\\*+ " line
))
382 (org-export-ascii-push-links (nreverse link-buffer
))
383 (setq link-buffer nil
))
385 ;; Remove the quoted HTML tags.
386 (setq line
(org-html-expand-for-ascii line
))
387 ;; Replace links with the description when possible
388 (while (string-match org-bracket-link-regexp line
)
389 (setq link
(match-string 1 line
)
390 desc0
(match-string 3 line
)
391 desc
(or desc0
(match-string 1 line
)))
392 (if (and (> (length link
) 8)
393 (equal (substring link
0 8) "coderef:"))
394 (setq line
(replace-match
395 (format (org-export-get-coderef-format (substring link
8) desc
)
398 org-export-code-refs
)))
400 (setq rpl
(concat "["
401 (or (match-string 3 line
) (match-string 1 line
))
403 (when (and desc0
(not (equal desc0 link
)))
404 (if org-export-ascii-links-to-notes
405 (push (cons desc0 link
) link-buffer
)
406 (setq rpl
(concat rpl
" (" link
")")
407 wrap
(+ (length line
) (- (length (match-string 0 line
)))
409 (setq line
(replace-match rpl t t line
))))
411 (setq line
(org-translate-time line
)))
413 ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line
)
415 (setq first-heading-pos
(or first-heading-pos
(point)))
416 (setq level
(org-tr-level (- (match-end 1) (match-beginning 1)
418 txt
(match-string 2 line
))
419 (org-ascii-level-start level txt umax lines
))
421 ((and org-export-with-tables
422 (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line
))
425 (setq table-open t table-buffer nil
))
427 (setq table-buffer
(cons line table-buffer
))
428 (when (or (not lines
)
429 (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
432 table-buffer
(nreverse table-buffer
))
435 (org-fix-indentation x org-ascii-current-indentation
))
436 (org-format-table-ascii table-buffer
)
439 (if (string-match "^\\([ \t]*\\)\\([-+*][ \t]+\\)\\(.*?\\)\\( ::\\)" line
)
440 (setq line
(replace-match "\\1\\3:" t nil line
)))
441 (setq line
(org-fix-indentation line org-ascii-current-indentation
))
442 ;; Remove forced line breaks
443 (if (string-match "\\\\\\\\[ \t]*$" line
)
444 (setq line
(replace-match "" t t line
)))
445 (if (and org-export-with-fixed-width
446 (string-match "^\\([ \t]*\\)\\(:\\( \\|$\\)\\)" line
))
447 (setq line
(replace-match "\\1" nil nil line
))
448 (if wrap
(setq line
(org-export-ascii-wrap line wrap
))))
449 (insert line
"\n"))))
451 (org-export-ascii-push-links (nreverse link-buffer
))
455 ;; insert the table of contents
457 (goto-char (point-min))
458 (if (re-search-forward "^[ \t]*\\[TABLE-OF-CONTENTS\\][ \t]*$" nil t
)
460 (goto-char (match-beginning 0))
462 (goto-char first-heading-pos
))
463 (mapc 'insert thetoc
)
464 (or (looking-at "[ \t]*\n[ \t]*\n")
467 ;; Convert whitespace place holders
468 (goto-char (point-min))
470 (while (setq beg
(next-single-property-change (point) 'org-whitespace
))
471 (setq end
(next-single-property-change beg
'org-whitespace
))
473 (delete-region beg end
)
474 (insert (make-string (- end beg
) ?\
))))
476 ;; remove display and invisible chars
478 (goto-char (point-min))
479 (while (setq beg
(next-single-property-change (point) 'display
))
480 (setq end
(next-single-property-change beg
'display
))
481 (delete-region beg end
)
484 (goto-char (point-min))
485 (while (setq beg
(next-single-property-change (point) 'org-cwidth
))
486 (setq end
(next-single-property-change beg
'org-cwidth
))
487 (delete-region beg end
)
489 (run-hooks 'org-export-ascii-final-hook
)
490 (or to-buffer
(save-buffer))
491 (goto-char (point-min))
492 (or (org-export-push-to-kill-ring "ASCII")
493 (message "Exporting... done"))
494 ;; Return the buffer or a string, according to how this function was called
495 (if (eq to-buffer
'string
)
496 (prog1 (buffer-substring (point-min) (point-max))
497 (kill-buffer (current-buffer)))
500 (defun org-export-ascii-preprocess (parameters)
501 "Do extra work for ASCII export"
503 ;; Realign tables to get rid of narrowing
504 (when org-export-ascii-table-widen-columns
505 (let ((org-table-do-narrow nil
))
506 (goto-char (point-min))
507 (org-table-map-tables
510 (org-table-align))))))
511 ;; Put quotes around verbatim text
512 (goto-char (point-min))
513 (while (re-search-forward org-verbatim-re nil t
)
514 (org-if-unprotected-at (match-beginning 4)
515 (goto-char (match-end 2))
516 (backward-delete-char 1) (insert "'")
517 (goto-char (match-beginning 2))
518 (delete-char 1) (insert "`")
519 (goto-char (match-end 2))))
520 ;; Remove target markers
521 (goto-char (point-min))
522 (while (re-search-forward "<<<?\\([^<>]*\\)>>>?\\([ \t]*\\)" nil t
)
523 (org-if-unprotected-at (match-beginning 1)
524 (replace-match "\\1\\2"))))
526 (defun org-html-expand-for-ascii (line)
527 "Handle quoted HTML for ASCII export."
528 (if org-export-html-expand
529 (while (string-match "@<[^<>\n]*>" line
)
530 ;; We just remove the tags for now.
531 (setq line
(replace-match "" nil nil line
))))
534 (defun org-export-ascii-wrap (line where
)
535 "Wrap LINE at or before WHERE."
536 (let ((ind (org-get-indentation line
))
539 (loop for i from where downto
(/ where
2) do
540 (and (equal (aref line i
) ?\
)
544 (concat (substring line
0 pos
) "\n"
545 (make-string ind ?\
)
546 (substring line
(1+ pos
)))
549 (defun org-export-ascii-push-links (link-buffer)
550 "Push out links in the buffer."
552 ;; We still have links to push out.
558 "^\\(\\([ \t]*\\)\\|\\(\\*+ \\)\\)[^ \t\n]" nil t
))
559 (setq ind
(or (match-string 2)
560 (make-string (length (match-string 3)) ?\
)))))
561 (mapc (lambda (x) (insert ind
"[" (car x
) "]: " (cdr x
) "\n"))
565 (defun org-ascii-level-start (level title umax
&optional lines
)
566 "Insert a new level in ASCII export."
567 (let (char (n (- level umax
1)) (ind 0))
570 (insert (make-string (* 2 n
) ?\
)
571 (char-to-string (nth (% n
(length org-export-ascii-bullets
))
572 org-export-ascii-bullets
))
574 ;; find the indentation of the next non-empty line
577 (if (string-match "^\\* " (car lines
)) (throw 'stop nil
))
578 (if (string-match "^\\([ \t]*\\)\\S-" (car lines
))
579 (throw 'stop
(setq ind
(org-get-indentation (car lines
)))))
581 (setq org-ascii-current-indentation
(cons (* 2 (1+ n
)) ind
)))
582 (if (or (not (equal (char-before) ?
\n))
583 (not (equal (char-before (1- (point))) ?
\n)))
585 (setq char
(nth (- umax level
) (reverse org-export-ascii-underline
)))
586 (unless org-export-with-tags
587 (if (string-match (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title
)
588 (setq title
(replace-match "" t t title
))))
589 (if org-export-with-section-numbers
590 (setq title
(concat (org-section-number level
) " " title
)))
591 (insert title
"\n" (make-string (string-width title
) char
) "\n")
592 (setq org-ascii-current-indentation
'(0 .
0)))))
594 (defun org-insert-centered (s &optional underline
)
595 "Insert the string S centered and underline it with character UNDERLINE."
596 (let ((ind (max (/ (- fill-column
(string-width s
)) 2) 0)))
597 (insert (make-string ind ?\
) s
"\n")
599 (insert (make-string ind ?\
)
600 (make-string (string-width s
) underline
)
603 (defvar org-table-colgroup-info nil
)
604 (defun org-format-table-ascii (lines)
605 "Format a table for ascii export."
607 (setq lines
(org-split-string lines
"\n")))
608 (if (not (string-match "^[ \t]*|" (car lines
)))
609 ;; Table made by table.el - test for spanning
612 ;; A normal org table
613 ;; Get rid of hlines at beginning and end
614 (if (string-match "^[ \t]*|-" (car lines
)) (setq lines
(cdr lines
)))
615 (setq lines
(nreverse lines
))
616 (if (string-match "^[ \t]*|-" (car lines
)) (setq lines
(cdr lines
)))
617 (setq lines
(nreverse lines
))
618 (when org-export-table-remove-special-lines
619 ;; Check if the table has a marking column. If yes remove the
620 ;; column and the special lines
621 (setq lines
(org-table-clean-before-export lines
)))
622 ;; Get rid of the vertical lines except for grouping
623 (if org-export-ascii-table-keep-all-vertical-lines
625 (let ((vl (org-colgroup-info-to-vline-list org-table-colgroup-info
))
627 (while (setq line
(pop lines
))
628 (if (string-match org-table-hline-regexp line
)
629 (and (string-match "|\\(.*\\)|" line
)
630 (setq line
(replace-match " \\1" t nil line
)))
631 (setq start
0 vl1 vl
)
632 (while (string-match "|" line start
)
633 (setq start
(match-end 0))
634 (or (pop vl1
) (setq line
(replace-match " " t t line
)))))
638 (defun org-colgroup-info-to-vline-list (info)
641 (setq last new new
(pop info
))
642 (if (or (memq last
'(:end
:startend
))
643 (memq new
'(:start
:startend
)))
646 (setq vl
(nreverse vl
))
647 (and vl
(setcar vl nil
))
652 ;; arch-tag: aa96f882-f477-4e13-86f5-70d43e7adf3c
653 ;;; org-ascii.el ends here