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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
31 (defgroup org-export-ascii nil
32 "Options specific for ASCII export of Org-mode files."
33 :tag
"Org Export ASCII"
36 (defcustom org-export-ascii-underline
'(?\$ ?\
# ?^ ?\~ ?\
= ?\-
)
37 "Characters for underlining headings in ASCII export.
38 In the given sequence, these characters will be used for level 1, 2, ..."
39 :group
'org-export-ascii
40 :type
'(repeat character
))
42 (defcustom org-export-ascii-bullets
'(?
* ?
+ ?-
)
43 "Bullet characters for headlines converted to lists in ASCII export.
44 The first character is used for the first lest level generated in this
45 way, and so on. If there are more levels than characters given here,
46 the list will be repeated.
47 Note that plain lists will keep the same bullets as the have in the
49 :group
'org-export-ascii
50 :type
'(repeat character
))
52 (defcustom org-export-ascii-links-to-notes t
53 "Non-nil means, convert links to notes before the next headline.
54 When nil, the link will be exported in place. If the line becomes long
55 in this way, it will be wrapped."
56 :group
'org-export-ascii
61 (defvar org-ascii-current-indentation nil
) ; For communication
64 (defun org-export-as-ascii-to-buffer (arg)
65 "Call `org-export-as-ascii` with output to a temporary buffer.
66 No file is created. The prefix ARG is passed through to `org-export-as-ascii'."
68 (org-export-as-ascii arg nil nil
"*Org ASCII Export*")
69 (when org-export-show-temporary-export-buffer
70 (switch-to-buffer-other-window "*Org ASCII Export*")))
73 (defun org-replace-region-by-ascii (beg end
)
74 "Assume the current region has org-mode syntax, and convert it to plain ASCII.
75 This can be used in any buffer. For example, you could write an
76 itemized list in org-mode syntax in a Mail buffer and then use this
77 command to convert it."
79 (let (reg ascii buf pop-up-frames
)
80 (save-window-excursion
82 (setq ascii
(org-export-region-as-ascii
84 (setq reg
(buffer-substring beg end
)
85 buf
(get-buffer-create "*Org tmp*"))
86 (with-current-buffer buf
90 (setq ascii
(org-export-region-as-ascii
91 (point-min) (point-max) t
'string
)))
93 (delete-region beg end
)
97 (defun org-export-region-as-ascii (beg end
&optional body-only buffer
)
98 "Convert region from BEG to END in org-mode buffer to plain ASCII.
99 If prefix arg BODY-ONLY is set, omit file header, footer, and table of
100 contents, and only produce the region of converted text, useful for
101 cut-and-paste operations.
102 If BUFFER is a buffer or a string, use/create that buffer as a target
103 of the converted ASCII. If BUFFER is the symbol `string', return the
104 produced ASCII as a string and leave not buffer behind. For example,
105 a Lisp program could call this function in the following way:
107 (setq ascii (org-export-region-as-ascii beg end t 'string))
109 When called interactively, the output buffer is selected, and shown
110 in a window. A non-interactive call will only return the buffer."
112 (when (interactive-p)
113 (setq buffer
"*Org ASCII Export*"))
114 (let ((transient-mark-mode t
) (zmacs-regions t
)
116 (setq ext-plist
(plist-put ext-plist
:ignore-subree-p t
))
118 (set-mark (point)) ;; to activate the region
120 (setq rtn
(org-export-as-ascii
123 (if (fboundp 'deactivate-mark
) (deactivate-mark))
124 (if (and (interactive-p) (bufferp rtn
))
125 (switch-to-buffer-other-window rtn
)
129 (defun org-export-as-ascii (arg &optional hidden ext-plist
130 to-buffer body-only pub-dir
)
131 "Export the outline as a pretty ASCII file.
132 If there is an active region, export only the region.
133 The prefix ARG specifies how many levels of the outline should become
134 underlined headlines, default is 3. Lower levels will become bulleted
135 lists. When HIDDEN is non-nil, don't display the ASCII buffer.
136 EXT-PLIST is a property list with external parameters overriding
137 org-mode's default settings, but still inferior to file-local
138 settings. When TO-BUFFER is non-nil, create a buffer with that
139 name and export to that buffer. If TO-BUFFER is the symbol
140 `string', don't leave any buffer behind but just return the
141 resulting ASCII as a string. When BODY-ONLY is set, don't produce
142 the file header and footer. When PUB-DIR is set, use this as the
143 publishing directory."
145 (setq-default org-todo-line-regexp org-todo-line-regexp
)
146 (let* ((opt-plist (org-combine-plists (org-default-export-plist)
148 (org-infile-export-plist)))
149 (region-p (org-region-active-p))
150 (rbeg (and region-p
(region-beginning)))
151 (rend (and region-p
(region-end)))
153 (if (plist-get opt-plist
:ignore-subree-p
)
158 (and (org-at-heading-p)
159 (>= (org-end-of-subtree t t
) rend
))))))
160 (level-offset (if subtree-p
163 (+ (funcall outline-level
)
164 (if org-odd-levels-only
1 0)))
166 (opt-plist (setq org-export-opt-plist
168 (org-export-add-subtree-options opt-plist rbeg
)
170 (custom-times org-display-custom-times
)
171 (org-ascii-current-indentation '(0 .
0))
175 (case-fold-search nil
)
176 (bfname (buffer-file-name (or (buffer-base-buffer) (current-buffer))))
177 (filename (if to-buffer
179 (concat (file-name-as-directory
181 (org-export-directory :ascii opt-plist
)))
182 (file-name-sans-extension
184 (org-entry-get (region-beginning)
185 "EXPORT_FILE_NAME" t
))
186 (file-name-nondirectory bfname
)))
188 (filename (and filename
189 (if (equal (file-truename filename
)
190 (file-truename bfname
))
191 (concat filename
".txt")
193 (buffer (if to-buffer
195 ((eq to-buffer
'string
)
196 (get-buffer-create "*Org ASCII Export*"))
197 (t (get-buffer-create to-buffer
)))
198 (find-file-noselect filename
)))
199 (org-levels-open (make-vector org-level-max nil
))
200 (odd org-odd-levels-only
)
201 (date (plist-get opt-plist
:date
))
202 (author (plist-get opt-plist
:author
))
203 (title (or (and subtree-p
(org-export-get-title-from-subtree))
204 (plist-get opt-plist
:title
)
206 (plist-get opt-plist
:skip-before-1st-heading
))
207 (org-export-grab-title-from-buffer))
208 (file-name-sans-extension
209 (file-name-nondirectory bfname
))))
210 (email (plist-get opt-plist
:email
))
211 (language (plist-get opt-plist
:language
))
212 (quote-re0 (concat "^[ \t]*" org-quote-string
"\\>"))
217 (if (org-region-active-p) (region-beginning) (point-min))
218 (if (org-region-active-p) (region-end) (point-max))))
219 (lines (org-split-string
220 (org-export-preprocess-string
223 :skip-before-1st-heading
224 (plist-get opt-plist
:skip-before-1st-heading
)
225 :drawers
(plist-get opt-plist
:drawers
)
226 :tags
(plist-get opt-plist
:tags
)
227 :priority
(plist-get opt-plist
:priority
)
228 :footnotes
(plist-get opt-plist
:footnotes
)
229 :timestamps
(plist-get opt-plist
:timestamps
)
230 :todo-keywords
(plist-get opt-plist
:todo-keywords
)
231 :verbatim-multiline t
232 :select-tags
(plist-get opt-plist
:select-tags
)
233 :exclude-tags
(plist-get opt-plist
:exclude-tags
)
235 (plist-get opt-plist
:archived-trees
)
236 :add-text
(plist-get opt-plist
:text
))
238 thetoc have-headings first-heading-pos
239 table-open table-buffer link-buffer link desc desc0 rpl wrap
)
240 (let ((inhibit-read-only t
))
242 (remove-text-properties (point-min) (point-max)
243 '(:org-license-to-kill t
))))
245 (setq org-min-level
(org-get-min-level lines level-offset
))
246 (setq org-last-level org-min-level
)
247 (org-init-section-numbers)
248 (setq lang-words
(or (assoc language org-export-language-setup
)
249 (assoc "en" org-export-language-setup
)))
253 ;; create local variables for all options, to make sure all called
254 ;; functions get the correct information
256 (set (make-local-variable (nth 2 x
))
257 (plist-get opt-plist
(car x
))))
258 org-export-plist-vars
)
259 (org-set-local 'org-odd-levels-only odd
)
260 (setq umax
(if arg
(prefix-numeric-value arg
)
261 org-export-headline-levels
))
262 (setq umax-toc
(if (integerp org-export-with-toc
)
263 (min org-export-with-toc umax
)
268 (if title
(org-insert-centered title ?
=))
270 (if (and (or author email
)
271 org-export-author-info
)
272 (insert (concat (nth 1 lang-words
) ": " (or author
"")
273 (if email
(concat " <" email
">") "")
277 ((and date
(string-match "%" date
))
278 (setq date
(format-time-string date
)))
280 (t (setq date
(format-time-string "%Y-%m-%d %T %Z"))))
282 (if (and date org-export-time-stamp-file
)
283 (insert (concat (nth 2 lang-words
) ": " date
"\n")))
287 (if (and org-export-with-toc
(not body-only
))
289 (push (concat (nth 3 lang-words
) "\n") thetoc
)
290 (push (concat (make-string (string-width (nth 3 lang-words
)) ?
=)
292 (mapc '(lambda (line)
293 (if (string-match org-todo-line-regexp
295 ;; This is a headline
297 (setq have-headings t
)
298 (setq level
(- (match-end 1) (match-beginning 1)
300 level
(org-tr-level level
)
301 txt
(match-string 3 line
)
303 (or (and org-export-mark-todo-in-toc
305 (not (member (match-string 2 line
)
308 (and org-export-mark-todo-in-toc
310 (org-search-todo-below
312 (setq txt
(org-html-expand-for-ascii txt
))
314 (while (string-match org-bracket-link-regexp txt
)
317 (match-string (if (match-end 2) 3 1) txt
)
320 (if (and (memq org-export-with-tags
'(not-in-toc nil
))
322 (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$")
324 (setq txt
(replace-match "" t t txt
)))
325 (if (string-match quote-re0 txt
)
326 (setq txt
(replace-match "" t t txt
)))
328 (if org-export-with-section-numbers
329 (setq txt
(concat (org-section-number level
)
331 (if (<= level umax-toc
)
336 (* (max 0 (- level org-min-level
)) 4) ?\
)
337 (format (if todo
"%s (*)\n" "%s\n") txt
))
339 (setq org-last-level level
))
342 (setq thetoc
(if have-headings
(nreverse thetoc
) nil
))))
344 (org-init-section-numbers)
345 (while (setq line
(pop lines
))
346 (when (and link-buffer
(string-match "^\\*+ " line
))
347 (org-export-ascii-push-links (nreverse link-buffer
))
348 (setq link-buffer nil
))
350 ;; Remove the quoted HTML tags.
351 (setq line
(org-html-expand-for-ascii line
))
352 ;; Replace links with the description when possible
353 (while (string-match org-bracket-link-regexp line
)
354 (setq link
(match-string 1 line
)
355 desc0
(match-string 3 line
)
356 desc
(or desc0
(match-string 1 line
)))
357 (if (and (> (length link
) 8)
358 (equal (substring link
0 8) "coderef:"))
359 (setq line
(replace-match
360 (format (org-export-get-coderef-format (substring link
8) desc
)
363 org-export-code-refs
)))
365 (setq rpl
(concat "["
366 (or (match-string 3 line
) (match-string 1 line
))
368 (when (and desc0
(not (equal desc0 link
)))
369 (if org-export-ascii-links-to-notes
370 (push (cons desc0 link
) link-buffer
)
371 (setq rpl
(concat rpl
" (" link
")")
372 wrap
(+ (length line
) (- (length (match-string 0 line
)))
374 (setq line
(replace-match rpl t t line
))))
376 (setq line
(org-translate-time line
)))
378 ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line
)
380 (setq first-heading-pos
(or first-heading-pos
(point)))
381 (setq level
(org-tr-level (- (match-end 1) (match-beginning 1)
383 txt
(match-string 2 line
))
384 (org-ascii-level-start level txt umax lines
))
386 ((and org-export-with-tables
387 (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line
))
390 (setq table-open t table-buffer nil
))
392 (setq table-buffer
(cons line table-buffer
))
393 (when (or (not lines
)
394 (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
397 table-buffer
(nreverse table-buffer
))
400 (org-fix-indentation x org-ascii-current-indentation
))
401 (org-format-table-ascii table-buffer
)
404 (if (string-match "^\\([ \t]*\\)\\([-+*][ \t]+\\)\\(.*?\\)\\( ::\\)" line
)
405 (setq line
(replace-match "\\1\\3:" t nil line
)))
406 (setq line
(org-fix-indentation line org-ascii-current-indentation
))
407 ;; Remove forced line breaks
408 (if (string-match "\\\\\\\\[ \t]*$" line
)
409 (setq line
(replace-match "" t t line
)))
410 (if (and org-export-with-fixed-width
411 (string-match "^\\([ \t]*\\)\\(:\\( \\|$\\)\\)" line
))
412 (setq line
(replace-match "\\1" nil nil line
))
413 (if wrap
(setq line
(org-export-ascii-wrap line wrap
))))
414 (insert line
"\n"))))
416 (org-export-ascii-push-links (nreverse link-buffer
))
420 ;; insert the table of contents
422 (goto-char (point-min))
423 (if (re-search-forward "^[ \t]*\\[TABLE-OF-CONTENTS\\][ \t]*$" nil t
)
425 (goto-char (match-beginning 0))
427 (goto-char first-heading-pos
))
428 (mapc 'insert thetoc
)
429 (or (looking-at "[ \t]*\n[ \t]*\n")
432 ;; Convert whitespace place holders
433 (goto-char (point-min))
435 (while (setq beg
(next-single-property-change (point) 'org-whitespace
))
436 (setq end
(next-single-property-change beg
'org-whitespace
))
438 (delete-region beg end
)
439 (insert (make-string (- end beg
) ?\
))))
441 ;; remove display and invisible chars
443 (goto-char (point-min))
444 (while (setq beg
(next-single-property-change (point) 'display
))
445 (setq end
(next-single-property-change beg
'display
))
446 (delete-region beg end
)
449 (goto-char (point-min))
450 (while (setq beg
(next-single-property-change (point) 'org-cwidth
))
451 (setq end
(next-single-property-change beg
'org-cwidth
))
452 (delete-region beg end
)
454 (or to-buffer
(save-buffer))
455 (goto-char (point-min))
456 (or (org-export-push-to-kill-ring "ASCII")
457 (message "Exporting... done"))
458 ;; Return the buffer or a string, according to how this function was called
459 (if (eq to-buffer
'string
)
460 (prog1 (buffer-substring (point-min) (point-max))
461 (kill-buffer (current-buffer)))
464 (defun org-export-ascii-preprocess (parameters)
465 "Do extra work for ASCII export"
466 ;; Put quotes around verbatim text
467 (goto-char (point-min))
468 (while (re-search-forward org-verbatim-re nil t
)
469 (goto-char (match-end 2))
470 (backward-delete-char 1) (insert "'")
471 (goto-char (match-beginning 2))
472 (delete-char 1) (insert "`")
473 (goto-char (match-end 2)))
474 ;; Remove target markers
475 (goto-char (point-min))
476 (while (re-search-forward "<<<?\\([^<>]*\\)>>>?\\([ \t]*\\)" nil t
)
477 (replace-match "\\1\\2")))
479 (defun org-html-expand-for-ascii (line)
480 "Handle quoted HTML for ASCII export."
481 (if org-export-html-expand
482 (while (string-match "@<[^<>\n]*>" line
)
483 ;; We just remove the tags for now.
484 (setq line
(replace-match "" nil nil line
))))
487 (defun org-export-ascii-wrap (line where
)
488 "Wrap LINE at or before WHERE."
489 (let ((ind (org-get-indentation line
))
492 (loop for i from where downto
(/ where
2) do
493 (and (equal (aref line i
) ?\
)
497 (concat (substring line
0 pos
) "\n"
498 (make-string ind ?\
)
499 (substring line
(1+ pos
)))
502 (defun org-export-ascii-push-links (link-buffer)
503 "Push out links in the buffer."
505 ;; We still have links to push out.
511 "^\\(\\([ \t]*\\)\\|\\(\\*+ \\)\\)[^ \t\n]" nil t
))
512 (setq ind
(or (match-string 2)
513 (make-string (length (match-string 3)) ?\
)))))
514 (mapc (lambda (x) (insert ind
"[" (car x
) "]: " (cdr x
) "\n"))
518 (defun org-ascii-level-start (level title umax
&optional lines
)
519 "Insert a new level in ASCII export."
520 (let (char (n (- level umax
1)) (ind 0))
523 (insert (make-string (* 2 n
) ?\
)
524 (char-to-string (nth (% n
(length org-export-ascii-bullets
))
525 org-export-ascii-bullets
))
527 ;; find the indentation of the next non-empty line
530 (if (string-match "^\\* " (car lines
)) (throw 'stop nil
))
531 (if (string-match "^\\([ \t]*\\)\\S-" (car lines
))
532 (throw 'stop
(setq ind
(org-get-indentation (car lines
)))))
534 (setq org-ascii-current-indentation
(cons (* 2 (1+ n
)) ind
)))
535 (if (or (not (equal (char-before) ?
\n))
536 (not (equal (char-before (1- (point))) ?
\n)))
538 (setq char
(nth (- umax level
) (reverse org-export-ascii-underline
)))
539 (unless org-export-with-tags
540 (if (string-match (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title
)
541 (setq title
(replace-match "" t t title
))))
542 (if org-export-with-section-numbers
543 (setq title
(concat (org-section-number level
) " " title
)))
544 (insert title
"\n" (make-string (string-width title
) char
) "\n")
545 (setq org-ascii-current-indentation
'(0 .
0)))))
547 (defun org-insert-centered (s &optional underline
)
548 "Insert the string S centered and underline it with character UNDERLINE."
549 (let ((ind (max (/ (- fill-column
(string-width s
)) 2) 0)))
550 (insert (make-string ind ?\
) s
"\n")
552 (insert (make-string ind ?\
)
553 (make-string (string-width s
) underline
)
556 (defvar org-table-colgroup-info nil
)
557 (defun org-format-table-ascii (lines)
558 "Format a table for ascii export."
560 (setq lines
(org-split-string lines
"\n")))
561 (if (not (string-match "^[ \t]*|" (car lines
)))
562 ;; Table made by table.el - test for spanning
565 ;; A normal org table
566 ;; Get rid of hlines at beginning and end
567 (if (string-match "^[ \t]*|-" (car lines
)) (setq lines
(cdr lines
)))
568 (setq lines
(nreverse lines
))
569 (if (string-match "^[ \t]*|-" (car lines
)) (setq lines
(cdr lines
)))
570 (setq lines
(nreverse lines
))
571 (when org-export-table-remove-special-lines
572 ;; Check if the table has a marking column. If yes remove the
573 ;; column and the special lines
574 (setq lines
(org-table-clean-before-export lines
)))
575 ;; Get rid of the vertical lines except for grouping
576 (let ((vl (org-colgroup-info-to-vline-list org-table-colgroup-info
))
578 (while (setq line
(pop lines
))
579 (if (string-match org-table-hline-regexp line
)
580 (and (string-match "|\\(.*\\)|" line
)
581 (setq line
(replace-match " \\1" t nil line
)))
582 (setq start
0 vl1 vl
)
583 (while (string-match "|" line start
)
584 (setq start
(match-end 0))
585 (or (pop vl1
) (setq line
(replace-match " " t t line
)))))
589 (defun org-colgroup-info-to-vline-list (info)
592 (setq last new new
(pop info
))
593 (if (or (memq last
'(:end
:startend
))
594 (memq new
'(:start
:startend
)))
597 (setq vl
(nreverse vl
))
598 (and vl
(setcar vl nil
))
603 ;; arch-tag: aa96f882-f477-4e13-86f5-70d43e7adf3c
605 ;;; org-ascii.el ends here