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
70 (defvar org-export-ascii-final-hook nil
71 "Hook run at the end of ASCII export, in the new buffer.")
75 (defvar org-ascii-current-indentation nil
) ; For communication
78 (defun org-export-as-ascii-to-buffer (arg)
79 "Call `org-export-as-ascii` with output to a temporary buffer.
80 No file is created. The prefix ARG is passed through to `org-export-as-ascii'."
82 (org-export-as-ascii arg nil nil
"*Org ASCII Export*")
83 (when org-export-show-temporary-export-buffer
84 (switch-to-buffer-other-window "*Org ASCII Export*")))
87 (defun org-replace-region-by-ascii (beg end
)
88 "Assume the current region has org-mode syntax, and convert it to plain ASCII.
89 This can be used in any buffer. For example, you could write an
90 itemized list in org-mode syntax in a Mail buffer and then use this
91 command to convert it."
93 (let (reg ascii buf pop-up-frames
)
94 (save-window-excursion
96 (setq ascii
(org-export-region-as-ascii
98 (setq reg
(buffer-substring beg end
)
99 buf
(get-buffer-create "*Org tmp*"))
100 (with-current-buffer buf
104 (setq ascii
(org-export-region-as-ascii
105 (point-min) (point-max) t
'string
)))
107 (delete-region beg end
)
111 (defun org-export-region-as-ascii (beg end
&optional body-only buffer
)
112 "Convert region from BEG to END in org-mode buffer to plain ASCII.
113 If prefix arg BODY-ONLY is set, omit file header, footer, and table of
114 contents, and only produce the region of converted text, useful for
115 cut-and-paste operations.
116 If BUFFER is a buffer or a string, use/create that buffer as a target
117 of the converted ASCII. If BUFFER is the symbol `string', return the
118 produced ASCII as a string and leave not buffer behind. For example,
119 a Lisp program could call this function in the following way:
121 (setq ascii (org-export-region-as-ascii beg end t 'string))
123 When called interactively, the output buffer is selected, and shown
124 in a window. A non-interactive call will only return the buffer."
126 (when (interactive-p)
127 (setq buffer
"*Org ASCII Export*"))
128 (let ((transient-mark-mode t
) (zmacs-regions t
)
130 (setq ext-plist
(plist-put ext-plist
:ignore-subtree-p t
))
132 (set-mark (point)) ;; to activate the region
134 (setq rtn
(org-export-as-ascii
137 (if (fboundp 'deactivate-mark
) (deactivate-mark))
138 (if (and (interactive-p) (bufferp rtn
))
139 (switch-to-buffer-other-window rtn
)
143 (defun org-export-as-ascii (arg &optional hidden ext-plist
144 to-buffer body-only pub-dir
)
145 "Export the outline as a pretty ASCII file.
146 If there is an active region, export only the region.
147 The prefix ARG specifies how many levels of the outline should become
148 underlined headlines, default is 3. Lower levels will become bulleted
149 lists. When HIDDEN is non-nil, don't display the ASCII buffer.
150 EXT-PLIST is a property list with external parameters overriding
151 org-mode's default settings, but still inferior to file-local
152 settings. When TO-BUFFER is non-nil, create a buffer with that
153 name and export to that buffer. If TO-BUFFER is the symbol
154 `string', don't leave any buffer behind but just return the
155 resulting ASCII as a string. When BODY-ONLY is set, don't produce
156 the file header and footer. When PUB-DIR is set, use this as the
157 publishing directory."
159 (run-hooks 'org-export-first-hook
)
160 (setq-default org-todo-line-regexp org-todo-line-regexp
)
161 (let* ((opt-plist (org-combine-plists (org-default-export-plist)
163 (org-infile-export-plist)))
164 (region-p (org-region-active-p))
165 (rbeg (and region-p
(region-beginning)))
166 (rend (and region-p
(region-end)))
168 (if (plist-get opt-plist
:ignore-subtree-p
)
173 (and (org-at-heading-p)
174 (>= (org-end-of-subtree t t
) rend
))))))
175 (level-offset (if subtree-p
178 (+ (funcall outline-level
)
179 (if org-odd-levels-only
1 0)))
181 (opt-plist (setq org-export-opt-plist
183 (org-export-add-subtree-options opt-plist rbeg
)
185 (custom-times org-display-custom-times
)
186 (org-ascii-current-indentation '(0 .
0))
190 (case-fold-search nil
)
191 (bfname (buffer-file-name (or (buffer-base-buffer) (current-buffer))))
192 (filename (if to-buffer
194 (concat (file-name-as-directory
196 (org-export-directory :ascii opt-plist
)))
197 (file-name-sans-extension
199 (org-entry-get (region-beginning)
200 "EXPORT_FILE_NAME" t
))
201 (file-name-nondirectory bfname
)))
203 (filename (and filename
204 (if (equal (file-truename filename
)
205 (file-truename bfname
))
206 (concat filename
".txt")
208 (buffer (if to-buffer
210 ((eq to-buffer
'string
)
211 (get-buffer-create "*Org ASCII Export*"))
212 (t (get-buffer-create to-buffer
)))
213 (find-file-noselect filename
)))
214 (org-levels-open (make-vector org-level-max nil
))
215 (odd org-odd-levels-only
)
216 (date (plist-get opt-plist
:date
))
217 (author (plist-get opt-plist
:author
))
218 (title (or (and subtree-p
(org-export-get-title-from-subtree))
219 (plist-get opt-plist
:title
)
221 (plist-get opt-plist
:skip-before-1st-heading
))
222 (org-export-grab-title-from-buffer))
223 (file-name-sans-extension
224 (file-name-nondirectory bfname
))))
225 (email (plist-get opt-plist
:email
))
226 (language (plist-get opt-plist
:language
))
227 (quote-re0 (concat "^[ \t]*" org-quote-string
"\\>"))
232 (if (org-region-active-p) (region-beginning) (point-min))
233 (if (org-region-active-p) (region-end) (point-max))))
234 (lines (org-split-string
235 (org-export-preprocess-string
238 :skip-before-1st-heading
239 (plist-get opt-plist
:skip-before-1st-heading
)
240 :drawers
(plist-get opt-plist
:drawers
)
241 :tags
(plist-get opt-plist
:tags
)
242 :priority
(plist-get opt-plist
:priority
)
243 :footnotes
(plist-get opt-plist
:footnotes
)
244 :timestamps
(plist-get opt-plist
:timestamps
)
245 :todo-keywords
(plist-get opt-plist
:todo-keywords
)
246 :verbatim-multiline t
247 :select-tags
(plist-get opt-plist
:select-tags
)
248 :exclude-tags
(plist-get opt-plist
:exclude-tags
)
250 (plist-get opt-plist
:archived-trees
)
251 :add-text
(plist-get opt-plist
:text
))
253 thetoc have-headings first-heading-pos
254 table-open table-buffer link-buffer link desc desc0 rpl wrap
)
255 (let ((inhibit-read-only t
))
257 (remove-text-properties (point-min) (point-max)
258 '(:org-license-to-kill t
))))
260 (setq org-min-level
(org-get-min-level lines level-offset
))
261 (setq org-last-level org-min-level
)
262 (org-init-section-numbers)
263 (setq lang-words
(or (assoc language org-export-language-setup
)
264 (assoc "en" org-export-language-setup
)))
268 (org-install-letbind)
269 ;; create local variables for all options, to make sure all called
270 ;; functions get the correct information
272 (set (make-local-variable (nth 2 x
))
273 (plist-get opt-plist
(car x
))))
274 org-export-plist-vars
)
275 (org-set-local 'org-odd-levels-only odd
)
276 (setq umax
(if arg
(prefix-numeric-value arg
)
277 org-export-headline-levels
))
278 (setq umax-toc
(if (integerp org-export-with-toc
)
279 (min org-export-with-toc umax
)
284 (when (and title
(not (string= "" title
)))
285 (org-insert-centered title ?
=)
288 (if (and (or author email
)
289 org-export-author-info
)
290 (insert(concat (nth 1 lang-words
) ": " (or author
"")
291 (if email
(concat " <" email
">") "")
295 ((and date
(string-match "%" date
))
296 (setq date
(format-time-string date
)))
298 (t (setq date
(format-time-string "%Y-%m-%d %T %Z"))))
300 (if (and date org-export-time-stamp-file
)
301 (insert (concat (nth 2 lang-words
) ": " date
"\n")))
303 (unless (= (point) (point-min))
306 (if (and org-export-with-toc
(not body-only
))
308 (push (concat (nth 3 lang-words
) "\n") thetoc
)
309 (push (concat (make-string (string-width (nth 3 lang-words
)) ?
=)
311 (mapc '(lambda (line)
312 (if (string-match org-todo-line-regexp
314 ;; This is a headline
316 (setq have-headings t
)
317 (setq level
(- (match-end 1) (match-beginning 1)
319 level
(org-tr-level level
)
320 txt
(match-string 3 line
)
322 (or (and org-export-mark-todo-in-toc
324 (not (member (match-string 2 line
)
327 (and org-export-mark-todo-in-toc
329 (org-search-todo-below
331 (setq txt
(org-html-expand-for-ascii txt
))
333 (while (string-match org-bracket-link-regexp txt
)
336 (match-string (if (match-end 2) 3 1) txt
)
339 (if (and (memq org-export-with-tags
'(not-in-toc nil
))
341 (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$")
343 (setq txt
(replace-match "" t t txt
)))
344 (if (string-match quote-re0 txt
)
345 (setq txt
(replace-match "" t t txt
)))
347 (if org-export-with-section-numbers
348 (setq txt
(concat (org-section-number level
)
350 (if (<= level umax-toc
)
355 (* (max 0 (- level org-min-level
)) 4) ?\
)
356 (format (if todo
"%s (*)\n" "%s\n") txt
))
358 (setq org-last-level level
))
361 (setq thetoc
(if have-headings
(nreverse thetoc
) nil
))))
363 (org-init-section-numbers)
364 (while (setq line
(pop lines
))
365 (when (and link-buffer
(string-match "^\\*+ " line
))
366 (org-export-ascii-push-links (nreverse link-buffer
))
367 (setq link-buffer nil
))
369 ;; Remove the quoted HTML tags.
370 (setq line
(org-html-expand-for-ascii line
))
371 ;; Replace links with the description when possible
372 (while (string-match org-bracket-link-regexp line
)
373 (setq link
(match-string 1 line
)
374 desc0
(match-string 3 line
)
375 desc
(or desc0
(match-string 1 line
)))
376 (if (and (> (length link
) 8)
377 (equal (substring link
0 8) "coderef:"))
378 (setq line
(replace-match
379 (format (org-export-get-coderef-format (substring link
8) desc
)
382 org-export-code-refs
)))
384 (setq rpl
(concat "["
385 (or (match-string 3 line
) (match-string 1 line
))
387 (when (and desc0
(not (equal desc0 link
)))
388 (if org-export-ascii-links-to-notes
389 (push (cons desc0 link
) link-buffer
)
390 (setq rpl
(concat rpl
" (" link
")")
391 wrap
(+ (length line
) (- (length (match-string 0 line
)))
393 (setq line
(replace-match rpl t t line
))))
395 (setq line
(org-translate-time line
)))
397 ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line
)
399 (setq first-heading-pos
(or first-heading-pos
(point)))
400 (setq level
(org-tr-level (- (match-end 1) (match-beginning 1)
402 txt
(match-string 2 line
))
403 (org-ascii-level-start level txt umax lines
))
405 ((and org-export-with-tables
406 (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line
))
409 (setq table-open t table-buffer nil
))
411 (setq table-buffer
(cons line table-buffer
))
412 (when (or (not lines
)
413 (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
416 table-buffer
(nreverse table-buffer
))
419 (org-fix-indentation x org-ascii-current-indentation
))
420 (org-format-table-ascii table-buffer
)
423 (if (string-match "^\\([ \t]*\\)\\([-+*][ \t]+\\)\\(.*?\\)\\( ::\\)" line
)
424 (setq line
(replace-match "\\1\\3:" t nil line
)))
425 (setq line
(org-fix-indentation line org-ascii-current-indentation
))
426 ;; Remove forced line breaks
427 (if (string-match "\\\\\\\\[ \t]*$" line
)
428 (setq line
(replace-match "" t t line
)))
429 (if (and org-export-with-fixed-width
430 (string-match "^\\([ \t]*\\)\\(:\\( \\|$\\)\\)" line
))
431 (setq line
(replace-match "\\1" nil nil line
))
432 (if wrap
(setq line
(org-export-ascii-wrap line wrap
))))
433 (insert line
"\n"))))
435 (org-export-ascii-push-links (nreverse link-buffer
))
439 ;; insert the table of contents
441 (goto-char (point-min))
442 (if (re-search-forward "^[ \t]*\\[TABLE-OF-CONTENTS\\][ \t]*$" nil t
)
444 (goto-char (match-beginning 0))
446 (goto-char first-heading-pos
))
447 (mapc 'insert thetoc
)
448 (or (looking-at "[ \t]*\n[ \t]*\n")
451 ;; Convert whitespace place holders
452 (goto-char (point-min))
454 (while (setq beg
(next-single-property-change (point) 'org-whitespace
))
455 (setq end
(next-single-property-change beg
'org-whitespace
))
457 (delete-region beg end
)
458 (insert (make-string (- end beg
) ?\
))))
460 ;; remove display and invisible chars
462 (goto-char (point-min))
463 (while (setq beg
(next-single-property-change (point) 'display
))
464 (setq end
(next-single-property-change beg
'display
))
465 (delete-region beg end
)
468 (goto-char (point-min))
469 (while (setq beg
(next-single-property-change (point) 'org-cwidth
))
470 (setq end
(next-single-property-change beg
'org-cwidth
))
471 (delete-region beg end
)
473 (run-hooks 'org-export-ascii-final-hook
)
474 (or to-buffer
(save-buffer))
475 (goto-char (point-min))
476 (or (org-export-push-to-kill-ring "ASCII")
477 (message "Exporting... done"))
478 ;; Return the buffer or a string, according to how this function was called
479 (if (eq to-buffer
'string
)
480 (prog1 (buffer-substring (point-min) (point-max))
481 (kill-buffer (current-buffer)))
484 (defun org-export-ascii-preprocess (parameters)
485 "Do extra work for ASCII export"
486 ;; Put quotes around verbatim text
487 (goto-char (point-min))
488 (while (re-search-forward org-verbatim-re nil t
)
489 (org-if-unprotected-at (match-beginning 4)
490 (goto-char (match-end 2))
491 (backward-delete-char 1) (insert "'")
492 (goto-char (match-beginning 2))
493 (delete-char 1) (insert "`")
494 (goto-char (match-end 2))))
495 ;; Remove target markers
496 (goto-char (point-min))
497 (while (re-search-forward "<<<?\\([^<>]*\\)>>>?\\([ \t]*\\)" nil t
)
498 (org-if-unprotected-at (match-beginning 1)
499 (replace-match "\\1\\2"))))
501 (defun org-html-expand-for-ascii (line)
502 "Handle quoted HTML for ASCII export."
503 (if org-export-html-expand
504 (while (string-match "@<[^<>\n]*>" line
)
505 ;; We just remove the tags for now.
506 (setq line
(replace-match "" nil nil line
))))
509 (defun org-export-ascii-wrap (line where
)
510 "Wrap LINE at or before WHERE."
511 (let ((ind (org-get-indentation line
))
514 (loop for i from where downto
(/ where
2) do
515 (and (equal (aref line i
) ?\
)
519 (concat (substring line
0 pos
) "\n"
520 (make-string ind ?\
)
521 (substring line
(1+ pos
)))
524 (defun org-export-ascii-push-links (link-buffer)
525 "Push out links in the buffer."
527 ;; We still have links to push out.
533 "^\\(\\([ \t]*\\)\\|\\(\\*+ \\)\\)[^ \t\n]" nil t
))
534 (setq ind
(or (match-string 2)
535 (make-string (length (match-string 3)) ?\
)))))
536 (mapc (lambda (x) (insert ind
"[" (car x
) "]: " (cdr x
) "\n"))
540 (defun org-ascii-level-start (level title umax
&optional lines
)
541 "Insert a new level in ASCII export."
542 (let (char (n (- level umax
1)) (ind 0))
545 (insert (make-string (* 2 n
) ?\
)
546 (char-to-string (nth (% n
(length org-export-ascii-bullets
))
547 org-export-ascii-bullets
))
549 ;; find the indentation of the next non-empty line
552 (if (string-match "^\\* " (car lines
)) (throw 'stop nil
))
553 (if (string-match "^\\([ \t]*\\)\\S-" (car lines
))
554 (throw 'stop
(setq ind
(org-get-indentation (car lines
)))))
556 (setq org-ascii-current-indentation
(cons (* 2 (1+ n
)) ind
)))
557 (if (or (not (equal (char-before) ?
\n))
558 (not (equal (char-before (1- (point))) ?
\n)))
560 (setq char
(nth (- umax level
) (reverse org-export-ascii-underline
)))
561 (unless org-export-with-tags
562 (if (string-match (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title
)
563 (setq title
(replace-match "" t t title
))))
564 (if org-export-with-section-numbers
565 (setq title
(concat (org-section-number level
) " " title
)))
566 (insert title
"\n" (make-string (string-width title
) char
) "\n")
567 (setq org-ascii-current-indentation
'(0 .
0)))))
569 (defun org-insert-centered (s &optional underline
)
570 "Insert the string S centered and underline it with character UNDERLINE."
571 (let ((ind (max (/ (- fill-column
(string-width s
)) 2) 0)))
572 (insert (make-string ind ?\
) s
"\n")
574 (insert (make-string ind ?\
)
575 (make-string (string-width s
) underline
)
578 (defvar org-table-colgroup-info nil
)
579 (defun org-format-table-ascii (lines)
580 "Format a table for ascii export."
582 (setq lines
(org-split-string lines
"\n")))
583 (if (not (string-match "^[ \t]*|" (car lines
)))
584 ;; Table made by table.el - test for spanning
587 ;; A normal org table
588 ;; Get rid of hlines at beginning and end
589 (if (string-match "^[ \t]*|-" (car lines
)) (setq lines
(cdr lines
)))
590 (setq lines
(nreverse lines
))
591 (if (string-match "^[ \t]*|-" (car lines
)) (setq lines
(cdr lines
)))
592 (setq lines
(nreverse lines
))
593 (when org-export-table-remove-special-lines
594 ;; Check if the table has a marking column. If yes remove the
595 ;; column and the special lines
596 (setq lines
(org-table-clean-before-export lines
)))
597 ;; Get rid of the vertical lines except for grouping
598 (if org-export-ascii-table-keep-all-vertical-lines
600 (let ((vl (org-colgroup-info-to-vline-list org-table-colgroup-info
))
602 (while (setq line
(pop lines
))
603 (if (string-match org-table-hline-regexp line
)
604 (and (string-match "|\\(.*\\)|" line
)
605 (setq line
(replace-match " \\1" t nil line
)))
606 (setq start
0 vl1 vl
)
607 (while (string-match "|" line start
)
608 (setq start
(match-end 0))
609 (or (pop vl1
) (setq line
(replace-match " " t t line
)))))
613 (defun org-colgroup-info-to-vline-list (info)
616 (setq last new new
(pop info
))
617 (if (or (memq last
'(:end
:startend
))
618 (memq new
'(:start
:startend
)))
621 (setq vl
(nreverse vl
))
622 (and vl
(setcar vl nil
))
627 ;; arch-tag: aa96f882-f477-4e13-86f5-70d43e7adf3c
628 ;;; org-ascii.el ends here