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 (custom-times org-display-custom-times
)
193 (org-ascii-current-indentation '(0 .
0))
197 (case-fold-search nil
)
198 (bfname (buffer-file-name (or (buffer-base-buffer) (current-buffer))))
199 (filename (if to-buffer
201 (concat (file-name-as-directory
203 (org-export-directory :ascii opt-plist
)))
204 (file-name-sans-extension
206 (org-entry-get (region-beginning)
207 "EXPORT_FILE_NAME" t
))
208 (file-name-nondirectory bfname
)))
210 (filename (and filename
211 (if (equal (file-truename filename
)
212 (file-truename bfname
))
213 (concat filename
".txt")
215 (buffer (if to-buffer
217 ((eq to-buffer
'string
)
218 (get-buffer-create "*Org ASCII Export*"))
219 (t (get-buffer-create to-buffer
)))
220 (find-file-noselect filename
)))
221 (org-levels-open (make-vector org-level-max nil
))
222 (odd org-odd-levels-only
)
223 (date (plist-get opt-plist
:date
))
224 (author (plist-get opt-plist
:author
))
225 (title (or (and subtree-p
(org-export-get-title-from-subtree))
226 (plist-get opt-plist
:title
)
228 (plist-get opt-plist
:skip-before-1st-heading
))
229 (org-export-grab-title-from-buffer))
230 (and (buffer-file-name)
231 (file-name-sans-extension
232 (file-name-nondirectory bfname
)))
234 (email (plist-get opt-plist
:email
))
235 (language (plist-get opt-plist
:language
))
236 (quote-re0 (concat "^[ \t]*" org-quote-string
"\\>"))
241 (if (org-region-active-p) (region-beginning) (point-min))
242 (if (org-region-active-p) (region-end) (point-max))))
243 (lines (org-split-string
244 (org-export-preprocess-string
247 :skip-before-1st-heading
248 (plist-get opt-plist
:skip-before-1st-heading
)
249 :drawers
(plist-get opt-plist
:drawers
)
250 :tags
(plist-get opt-plist
:tags
)
251 :priority
(plist-get opt-plist
:priority
)
252 :footnotes
(plist-get opt-plist
:footnotes
)
253 :timestamps
(plist-get opt-plist
:timestamps
)
254 :todo-keywords
(plist-get opt-plist
:todo-keywords
)
255 :verbatim-multiline t
256 :select-tags
(plist-get opt-plist
:select-tags
)
257 :exclude-tags
(plist-get opt-plist
:exclude-tags
)
259 (plist-get opt-plist
:archived-trees
)
260 :add-text
(plist-get opt-plist
:text
))
262 thetoc have-headings first-heading-pos
263 table-open table-buffer link-buffer link desc desc0 rpl wrap
)
264 (let ((inhibit-read-only t
))
266 (remove-text-properties (point-min) (point-max)
267 '(:org-license-to-kill t
))))
269 (setq org-min-level
(org-get-min-level lines level-offset
))
270 (setq org-last-level org-min-level
)
271 (org-init-section-numbers)
272 (setq lang-words
(or (assoc language org-export-language-setup
)
273 (assoc "en" org-export-language-setup
)))
277 (org-install-letbind)
278 ;; create local variables for all options, to make sure all called
279 ;; functions get the correct information
281 (set (make-local-variable (nth 2 x
))
282 (plist-get opt-plist
(car x
))))
283 org-export-plist-vars
)
284 (org-set-local 'org-odd-levels-only odd
)
285 (setq umax
(if arg
(prefix-numeric-value arg
)
286 org-export-headline-levels
))
287 (setq umax-toc
(if (integerp org-export-with-toc
)
288 (min org-export-with-toc umax
)
293 (when (and title
(not (string= "" title
)))
294 (org-insert-centered title ?
=)
297 (if (and (or author email
)
298 org-export-author-info
)
299 (insert(concat (nth 1 lang-words
) ": " (or author
"")
300 (if (and org-export-email-info
301 email
(string-match "\\S-" email
))
302 (concat " <" email
">") "")
306 ((and date
(string-match "%" date
))
307 (setq date
(format-time-string date
)))
309 (t (setq date
(format-time-string "%Y-%m-%d %T %Z"))))
311 (if (and date org-export-time-stamp-file
)
312 (insert (concat (nth 2 lang-words
) ": " date
"\n")))
314 (unless (= (point) (point-min))
317 (if (and org-export-with-toc
(not body-only
))
319 (push (concat (nth 3 lang-words
) "\n") thetoc
)
320 (push (concat (make-string (string-width (nth 3 lang-words
)) ?
=)
322 (mapc '(lambda (line)
323 (if (string-match org-todo-line-regexp
325 ;; This is a headline
327 (setq have-headings t
)
328 (setq level
(- (match-end 1) (match-beginning 1)
330 level
(org-tr-level level
)
331 txt
(match-string 3 line
)
333 (or (and org-export-mark-todo-in-toc
335 (not (member (match-string 2 line
)
338 (and org-export-mark-todo-in-toc
340 (org-search-todo-below
342 (setq txt
(org-html-expand-for-ascii txt
))
344 (while (string-match org-bracket-link-regexp txt
)
347 (match-string (if (match-end 2) 3 1) txt
)
350 (if (and (memq org-export-with-tags
'(not-in-toc nil
))
352 (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$")
354 (setq txt
(replace-match "" t t txt
)))
355 (if (string-match quote-re0 txt
)
356 (setq txt
(replace-match "" t t txt
)))
358 (if org-export-with-section-numbers
359 (setq txt
(concat (org-section-number level
)
361 (if (<= level umax-toc
)
366 (* (max 0 (- level org-min-level
)) 4) ?\
)
367 (format (if todo
"%s (*)\n" "%s\n") txt
))
369 (setq org-last-level level
))
372 (setq thetoc
(if have-headings
(nreverse thetoc
) nil
))))
374 (org-init-section-numbers)
375 (while (setq line
(pop lines
))
376 (when (and link-buffer
(string-match "^\\*+ " line
))
377 (org-export-ascii-push-links (nreverse link-buffer
))
378 (setq link-buffer nil
))
380 ;; Remove the quoted HTML tags.
381 (setq line
(org-html-expand-for-ascii line
))
382 ;; Replace links with the description when possible
383 (while (string-match org-bracket-link-regexp line
)
384 (setq link
(match-string 1 line
)
385 desc0
(match-string 3 line
)
386 desc
(or desc0
(match-string 1 line
)))
387 (if (and (> (length link
) 8)
388 (equal (substring link
0 8) "coderef:"))
389 (setq line
(replace-match
390 (format (org-export-get-coderef-format (substring link
8) desc
)
393 org-export-code-refs
)))
395 (setq rpl
(concat "["
396 (or (match-string 3 line
) (match-string 1 line
))
398 (when (and desc0
(not (equal desc0 link
)))
399 (if org-export-ascii-links-to-notes
400 (push (cons desc0 link
) link-buffer
)
401 (setq rpl
(concat rpl
" (" link
")")
402 wrap
(+ (length line
) (- (length (match-string 0 line
)))
404 (setq line
(replace-match rpl t t line
))))
406 (setq line
(org-translate-time line
)))
408 ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line
)
410 (setq first-heading-pos
(or first-heading-pos
(point)))
411 (setq level
(org-tr-level (- (match-end 1) (match-beginning 1)
413 txt
(match-string 2 line
))
414 (org-ascii-level-start level txt umax lines
))
416 ((and org-export-with-tables
417 (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line
))
420 (setq table-open t table-buffer nil
))
422 (setq table-buffer
(cons line table-buffer
))
423 (when (or (not lines
)
424 (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
427 table-buffer
(nreverse table-buffer
))
430 (org-fix-indentation x org-ascii-current-indentation
))
431 (org-format-table-ascii table-buffer
)
434 (if (string-match "^\\([ \t]*\\)\\([-+*][ \t]+\\)\\(.*?\\)\\( ::\\)" line
)
435 (setq line
(replace-match "\\1\\3:" t nil line
)))
436 (setq line
(org-fix-indentation line org-ascii-current-indentation
))
437 ;; Remove forced line breaks
438 (if (string-match "\\\\\\\\[ \t]*$" line
)
439 (setq line
(replace-match "" t t line
)))
440 (if (and org-export-with-fixed-width
441 (string-match "^\\([ \t]*\\)\\(:\\( \\|$\\)\\)" line
))
442 (setq line
(replace-match "\\1" nil nil line
))
443 (if wrap
(setq line
(org-export-ascii-wrap line wrap
))))
444 (insert line
"\n"))))
446 (org-export-ascii-push-links (nreverse link-buffer
))
450 ;; insert the table of contents
452 (goto-char (point-min))
453 (if (re-search-forward "^[ \t]*\\[TABLE-OF-CONTENTS\\][ \t]*$" nil t
)
455 (goto-char (match-beginning 0))
457 (goto-char first-heading-pos
))
458 (mapc 'insert thetoc
)
459 (or (looking-at "[ \t]*\n[ \t]*\n")
462 ;; Convert whitespace place holders
463 (goto-char (point-min))
465 (while (setq beg
(next-single-property-change (point) 'org-whitespace
))
466 (setq end
(next-single-property-change beg
'org-whitespace
))
468 (delete-region beg end
)
469 (insert (make-string (- end beg
) ?\
))))
471 ;; remove display and invisible chars
473 (goto-char (point-min))
474 (while (setq beg
(next-single-property-change (point) 'display
))
475 (setq end
(next-single-property-change beg
'display
))
476 (delete-region beg end
)
479 (goto-char (point-min))
480 (while (setq beg
(next-single-property-change (point) 'org-cwidth
))
481 (setq end
(next-single-property-change beg
'org-cwidth
))
482 (delete-region beg end
)
484 (run-hooks 'org-export-ascii-final-hook
)
485 (or to-buffer
(save-buffer))
486 (goto-char (point-min))
487 (or (org-export-push-to-kill-ring "ASCII")
488 (message "Exporting... done"))
489 ;; Return the buffer or a string, according to how this function was called
490 (if (eq to-buffer
'string
)
491 (prog1 (buffer-substring (point-min) (point-max))
492 (kill-buffer (current-buffer)))
495 (defun org-export-ascii-preprocess (parameters)
496 "Do extra work for ASCII export"
498 ;; Realign tables to get rid of narrowing
499 (when org-export-ascii-table-widen-columns
500 (let ((org-table-do-narrow nil
))
501 (goto-char (point-min))
502 (org-table-map-tables
505 (org-table-align))))))
506 ;; Put quotes around verbatim text
507 (goto-char (point-min))
508 (while (re-search-forward org-verbatim-re nil t
)
509 (org-if-unprotected-at (match-beginning 4)
510 (goto-char (match-end 2))
511 (backward-delete-char 1) (insert "'")
512 (goto-char (match-beginning 2))
513 (delete-char 1) (insert "`")
514 (goto-char (match-end 2))))
515 ;; Remove target markers
516 (goto-char (point-min))
517 (while (re-search-forward "<<<?\\([^<>]*\\)>>>?\\([ \t]*\\)" nil t
)
518 (org-if-unprotected-at (match-beginning 1)
519 (replace-match "\\1\\2"))))
521 (defun org-html-expand-for-ascii (line)
522 "Handle quoted HTML for ASCII export."
523 (if org-export-html-expand
524 (while (string-match "@<[^<>\n]*>" line
)
525 ;; We just remove the tags for now.
526 (setq line
(replace-match "" nil nil line
))))
529 (defun org-export-ascii-wrap (line where
)
530 "Wrap LINE at or before WHERE."
531 (let ((ind (org-get-indentation line
))
534 (loop for i from where downto
(/ where
2) do
535 (and (equal (aref line i
) ?\
)
539 (concat (substring line
0 pos
) "\n"
540 (make-string ind ?\
)
541 (substring line
(1+ pos
)))
544 (defun org-export-ascii-push-links (link-buffer)
545 "Push out links in the buffer."
547 ;; We still have links to push out.
553 "^\\(\\([ \t]*\\)\\|\\(\\*+ \\)\\)[^ \t\n]" nil t
))
554 (setq ind
(or (match-string 2)
555 (make-string (length (match-string 3)) ?\
)))))
556 (mapc (lambda (x) (insert ind
"[" (car x
) "]: " (cdr x
) "\n"))
560 (defun org-ascii-level-start (level title umax
&optional lines
)
561 "Insert a new level in ASCII export."
562 (let (char (n (- level umax
1)) (ind 0))
565 (insert (make-string (* 2 n
) ?\
)
566 (char-to-string (nth (% n
(length org-export-ascii-bullets
))
567 org-export-ascii-bullets
))
569 ;; find the indentation of the next non-empty line
572 (if (string-match "^\\* " (car lines
)) (throw 'stop nil
))
573 (if (string-match "^\\([ \t]*\\)\\S-" (car lines
))
574 (throw 'stop
(setq ind
(org-get-indentation (car lines
)))))
576 (setq org-ascii-current-indentation
(cons (* 2 (1+ n
)) ind
)))
577 (if (or (not (equal (char-before) ?
\n))
578 (not (equal (char-before (1- (point))) ?
\n)))
580 (setq char
(nth (- umax level
) (reverse org-export-ascii-underline
)))
581 (unless org-export-with-tags
582 (if (string-match (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title
)
583 (setq title
(replace-match "" t t title
))))
584 (if org-export-with-section-numbers
585 (setq title
(concat (org-section-number level
) " " title
)))
586 (insert title
"\n" (make-string (string-width title
) char
) "\n")
587 (setq org-ascii-current-indentation
'(0 .
0)))))
589 (defun org-insert-centered (s &optional underline
)
590 "Insert the string S centered and underline it with character UNDERLINE."
591 (let ((ind (max (/ (- fill-column
(string-width s
)) 2) 0)))
592 (insert (make-string ind ?\
) s
"\n")
594 (insert (make-string ind ?\
)
595 (make-string (string-width s
) underline
)
598 (defvar org-table-colgroup-info nil
)
599 (defun org-format-table-ascii (lines)
600 "Format a table for ascii export."
602 (setq lines
(org-split-string lines
"\n")))
603 (if (not (string-match "^[ \t]*|" (car lines
)))
604 ;; Table made by table.el - test for spanning
607 ;; A normal org table
608 ;; Get rid of hlines at beginning and end
609 (if (string-match "^[ \t]*|-" (car lines
)) (setq lines
(cdr lines
)))
610 (setq lines
(nreverse lines
))
611 (if (string-match "^[ \t]*|-" (car lines
)) (setq lines
(cdr lines
)))
612 (setq lines
(nreverse lines
))
613 (when org-export-table-remove-special-lines
614 ;; Check if the table has a marking column. If yes remove the
615 ;; column and the special lines
616 (setq lines
(org-table-clean-before-export lines
)))
617 ;; Get rid of the vertical lines except for grouping
618 (if org-export-ascii-table-keep-all-vertical-lines
620 (let ((vl (org-colgroup-info-to-vline-list org-table-colgroup-info
))
622 (while (setq line
(pop lines
))
623 (if (string-match org-table-hline-regexp line
)
624 (and (string-match "|\\(.*\\)|" line
)
625 (setq line
(replace-match " \\1" t nil line
)))
626 (setq start
0 vl1 vl
)
627 (while (string-match "|" line start
)
628 (setq start
(match-end 0))
629 (or (pop vl1
) (setq line
(replace-match " " t t line
)))))
633 (defun org-colgroup-info-to-vline-list (info)
636 (setq last new new
(pop info
))
637 (if (or (memq last
'(:end
:startend
))
638 (memq new
'(:start
:startend
)))
641 (setq vl
(nreverse vl
))
642 (and vl
(setcar vl nil
))
647 ;; arch-tag: aa96f882-f477-4e13-86f5-70d43e7adf3c
648 ;;; org-ascii.el ends here