Release 6.30d
[org-mode.git] / lisp / org-ascii.el
bloba4b81afdbf92b96ea059b14bcbe0f53fd2b1c0fc
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
9 ;; Version: 6.30d
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
27 ;;; Commentary:
29 (require 'org-exp)
31 (defgroup org-export-ascii nil
32 "Options specific for ASCII export of Org-mode files."
33 :tag "Org Export ASCII"
34 :group 'org-export)
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
48 Org-mode file."
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
57 :type 'boolean)
59 ;;; ASCII export
61 (defvar org-ascii-current-indentation nil) ; For communication
63 ;;;###autoload
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'."
67 (interactive "P")
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*")))
72 ;;;###autoload
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."
78 (interactive "r")
79 (let (reg ascii buf pop-up-frames)
80 (save-window-excursion
81 (if (org-mode-p)
82 (setq ascii (org-export-region-as-ascii
83 beg end t 'string))
84 (setq reg (buffer-substring beg end)
85 buf (get-buffer-create "*Org tmp*"))
86 (with-current-buffer buf
87 (erase-buffer)
88 (insert reg)
89 (org-mode)
90 (setq ascii (org-export-region-as-ascii
91 (point-min) (point-max) t 'string)))
92 (kill-buffer buf)))
93 (delete-region beg end)
94 (insert ascii)))
96 ;;;###autoload
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."
111 (interactive "r\nP")
112 (when (interactive-p)
113 (setq buffer "*Org ASCII Export*"))
114 (let ((transient-mark-mode t) (zmacs-regions t)
115 ext-plist rtn)
116 (setq ext-plist (plist-put ext-plist :ignore-subree-p t))
117 (goto-char end)
118 (set-mark (point)) ;; to activate the region
119 (goto-char beg)
120 (setq rtn (org-export-as-ascii
121 nil nil ext-plist
122 buffer body-only))
123 (if (fboundp 'deactivate-mark) (deactivate-mark))
124 (if (and (interactive-p) (bufferp rtn))
125 (switch-to-buffer-other-window rtn)
126 rtn)))
128 ;;;###autoload
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."
144 (interactive "P")
145 (setq-default org-todo-line-regexp org-todo-line-regexp)
146 (let* ((opt-plist (org-combine-plists (org-default-export-plist)
147 ext-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)))
152 (subtree-p
153 (if (plist-get opt-plist :ignore-subree-p)
155 (when region-p
156 (save-excursion
157 (goto-char rbeg)
158 (and (org-at-heading-p)
159 (>= (org-end-of-subtree t t) rend))))))
160 (level-offset (if subtree-p
161 (save-excursion
162 (goto-char rbeg)
163 (+ (funcall outline-level)
164 (if org-odd-levels-only 1 0)))
166 (opt-plist (setq org-export-opt-plist
167 (if subtree-p
168 (org-export-add-subtree-options opt-plist rbeg)
169 opt-plist)))
170 (custom-times org-display-custom-times)
171 (org-ascii-current-indentation '(0 . 0))
172 (level 0) line txt
173 (umax nil)
174 (umax-toc nil)
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
180 (or pub-dir
181 (org-export-directory :ascii opt-plist)))
182 (file-name-sans-extension
183 (or (and subtree-p
184 (org-entry-get (region-beginning)
185 "EXPORT_FILE_NAME" t))
186 (file-name-nondirectory bfname)))
187 ".txt")))
188 (filename (and filename
189 (if (equal (file-truename filename)
190 (file-truename bfname))
191 (concat filename ".txt")
192 filename)))
193 (buffer (if to-buffer
194 (cond
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)
205 (and (not
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 "\\>"))
213 (todo nil)
214 (lang-words nil)
215 (region
216 (buffer-substring
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
221 region
222 :for-ascii t
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)
234 :archived-trees
235 (plist-get opt-plist :archived-trees)
236 :add-text (plist-get opt-plist :text))
237 "\n"))
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))
241 (org-unmodified
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)))
250 (set-buffer buffer)
251 (erase-buffer)
252 (fundamental-mode)
253 (org-install-letbind)
254 ;; create local variables for all options, to make sure all called
255 ;; functions get the correct information
256 (mapc (lambda (x)
257 (set (make-local-variable (nth 2 x))
258 (plist-get opt-plist (car x))))
259 org-export-plist-vars)
260 (org-set-local 'org-odd-levels-only odd)
261 (setq umax (if arg (prefix-numeric-value arg)
262 org-export-headline-levels))
263 (setq umax-toc (if (integerp org-export-with-toc)
264 (min org-export-with-toc umax)
265 umax))
267 ;; File header
268 (unless body-only
269 (when (and title (not (string= "" title)))
270 (org-insert-centered title ?=)
271 (insert "\n"))
273 (if (and (or author email)
274 org-export-author-info)
275 (insert(concat (nth 1 lang-words) ": " (or author "")
276 (if email (concat " <" email ">") "")
277 "\n")))
279 (cond
280 ((and date (string-match "%" date))
281 (setq date (format-time-string date)))
282 (date)
283 (t (setq date (format-time-string "%Y-%m-%d %T %Z"))))
285 (if (and date org-export-time-stamp-file)
286 (insert (concat (nth 2 lang-words) ": " date"\n")))
288 (unless (= (point) (point-min))
289 (insert "\n\n")))
291 (if (and org-export-with-toc (not body-only))
292 (progn
293 (push (concat (nth 3 lang-words) "\n") thetoc)
294 (push (concat (make-string (string-width (nth 3 lang-words)) ?=)
295 "\n") thetoc)
296 (mapc '(lambda (line)
297 (if (string-match org-todo-line-regexp
298 line)
299 ;; This is a headline
300 (progn
301 (setq have-headings t)
302 (setq level (- (match-end 1) (match-beginning 1)
303 level-offset)
304 level (org-tr-level level)
305 txt (match-string 3 line)
306 todo
307 (or (and org-export-mark-todo-in-toc
308 (match-beginning 2)
309 (not (member (match-string 2 line)
310 org-done-keywords)))
311 ; TODO, not DONE
312 (and org-export-mark-todo-in-toc
313 (= level umax-toc)
314 (org-search-todo-below
315 line lines level))))
316 (setq txt (org-html-expand-for-ascii txt))
318 (while (string-match org-bracket-link-regexp txt)
319 (setq txt
320 (replace-match
321 (match-string (if (match-end 2) 3 1) txt)
322 t t txt)))
324 (if (and (memq org-export-with-tags '(not-in-toc nil))
325 (string-match
326 (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$")
327 txt))
328 (setq txt (replace-match "" t t txt)))
329 (if (string-match quote-re0 txt)
330 (setq txt (replace-match "" t t txt)))
332 (if org-export-with-section-numbers
333 (setq txt (concat (org-section-number level)
334 " " txt)))
335 (if (<= level umax-toc)
336 (progn
337 (push
338 (concat
339 (make-string
340 (* (max 0 (- level org-min-level)) 4) ?\ )
341 (format (if todo "%s (*)\n" "%s\n") txt))
342 thetoc)
343 (setq org-last-level level))
344 ))))
345 lines)
346 (setq thetoc (if have-headings (nreverse thetoc) nil))))
348 (org-init-section-numbers)
349 (while (setq line (pop lines))
350 (when (and link-buffer (string-match "^\\*+ " line))
351 (org-export-ascii-push-links (nreverse link-buffer))
352 (setq link-buffer nil))
353 (setq wrap nil)
354 ;; Remove the quoted HTML tags.
355 (setq line (org-html-expand-for-ascii line))
356 ;; Replace links with the description when possible
357 (while (string-match org-bracket-link-regexp line)
358 (setq link (match-string 1 line)
359 desc0 (match-string 3 line)
360 desc (or desc0 (match-string 1 line)))
361 (if (and (> (length link) 8)
362 (equal (substring link 0 8) "coderef:"))
363 (setq line (replace-match
364 (format (org-export-get-coderef-format (substring link 8) desc)
365 (cdr (assoc
366 (substring link 8)
367 org-export-code-refs)))
368 t t line))
369 (setq rpl (concat "["
370 (or (match-string 3 line) (match-string 1 line))
371 "]"))
372 (when (and desc0 (not (equal desc0 link)))
373 (if org-export-ascii-links-to-notes
374 (push (cons desc0 link) link-buffer)
375 (setq rpl (concat rpl " (" link ")")
376 wrap (+ (length line) (- (length (match-string 0 line)))
377 (length desc)))))
378 (setq line (replace-match rpl t t line))))
379 (when custom-times
380 (setq line (org-translate-time line)))
381 (cond
382 ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
383 ;; a Headline
384 (setq first-heading-pos (or first-heading-pos (point)))
385 (setq level (org-tr-level (- (match-end 1) (match-beginning 1)
386 level-offset))
387 txt (match-string 2 line))
388 (org-ascii-level-start level txt umax lines))
390 ((and org-export-with-tables
391 (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
392 (if (not table-open)
393 ;; New table starts
394 (setq table-open t table-buffer nil))
395 ;; Accumulate lines
396 (setq table-buffer (cons line table-buffer))
397 (when (or (not lines)
398 (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
399 (car lines))))
400 (setq table-open nil
401 table-buffer (nreverse table-buffer))
402 (insert (mapconcat
403 (lambda (x)
404 (org-fix-indentation x org-ascii-current-indentation))
405 (org-format-table-ascii table-buffer)
406 "\n") "\n")))
408 (if (string-match "^\\([ \t]*\\)\\([-+*][ \t]+\\)\\(.*?\\)\\( ::\\)" line)
409 (setq line (replace-match "\\1\\3:" t nil line)))
410 (setq line (org-fix-indentation line org-ascii-current-indentation))
411 ;; Remove forced line breaks
412 (if (string-match "\\\\\\\\[ \t]*$" line)
413 (setq line (replace-match "" t t line)))
414 (if (and org-export-with-fixed-width
415 (string-match "^\\([ \t]*\\)\\(:\\( \\|$\\)\\)" line))
416 (setq line (replace-match "\\1" nil nil line))
417 (if wrap (setq line (org-export-ascii-wrap line wrap))))
418 (insert line "\n"))))
420 (org-export-ascii-push-links (nreverse link-buffer))
422 (normal-mode)
424 ;; insert the table of contents
425 (when thetoc
426 (goto-char (point-min))
427 (if (re-search-forward "^[ \t]*\\[TABLE-OF-CONTENTS\\][ \t]*$" nil t)
428 (progn
429 (goto-char (match-beginning 0))
430 (replace-match ""))
431 (goto-char first-heading-pos))
432 (mapc 'insert thetoc)
433 (or (looking-at "[ \t]*\n[ \t]*\n")
434 (insert "\n\n")))
436 ;; Convert whitespace place holders
437 (goto-char (point-min))
438 (let (beg end)
439 (while (setq beg (next-single-property-change (point) 'org-whitespace))
440 (setq end (next-single-property-change beg 'org-whitespace))
441 (goto-char beg)
442 (delete-region beg end)
443 (insert (make-string (- end beg) ?\ ))))
445 ;; remove display and invisible chars
446 (let (beg end)
447 (goto-char (point-min))
448 (while (setq beg (next-single-property-change (point) 'display))
449 (setq end (next-single-property-change beg 'display))
450 (delete-region beg end)
451 (goto-char beg)
452 (insert "=>"))
453 (goto-char (point-min))
454 (while (setq beg (next-single-property-change (point) 'org-cwidth))
455 (setq end (next-single-property-change beg 'org-cwidth))
456 (delete-region beg end)
457 (goto-char beg)))
458 (or to-buffer (save-buffer))
459 (goto-char (point-min))
460 (or (org-export-push-to-kill-ring "ASCII")
461 (message "Exporting... done"))
462 ;; Return the buffer or a string, according to how this function was called
463 (if (eq to-buffer 'string)
464 (prog1 (buffer-substring (point-min) (point-max))
465 (kill-buffer (current-buffer)))
466 (current-buffer))))
468 (defun org-export-ascii-preprocess (parameters)
469 "Do extra work for ASCII export"
470 ;; Put quotes around verbatim text
471 (goto-char (point-min))
472 (while (re-search-forward org-verbatim-re nil t)
473 (goto-char (match-end 2))
474 (backward-delete-char 1) (insert "'")
475 (goto-char (match-beginning 2))
476 (delete-char 1) (insert "`")
477 (goto-char (match-end 2)))
478 ;; Remove target markers
479 (goto-char (point-min))
480 (while (re-search-forward "<<<?\\([^<>]*\\)>>>?\\([ \t]*\\)" nil t)
481 (replace-match "\\1\\2")))
483 (defun org-html-expand-for-ascii (line)
484 "Handle quoted HTML for ASCII export."
485 (if org-export-html-expand
486 (while (string-match "@<[^<>\n]*>" line)
487 ;; We just remove the tags for now.
488 (setq line (replace-match "" nil nil line))))
489 line)
491 (defun org-export-ascii-wrap (line where)
492 "Wrap LINE at or before WHERE."
493 (let ((ind (org-get-indentation line))
494 pos)
495 (catch 'found
496 (loop for i from where downto (/ where 2) do
497 (and (equal (aref line i) ?\ )
498 (setq pos i)
499 (throw 'found t))))
500 (if pos
501 (concat (substring line 0 pos) "\n"
502 (make-string ind ?\ )
503 (substring line (1+ pos)))
504 line)))
506 (defun org-export-ascii-push-links (link-buffer)
507 "Push out links in the buffer."
508 (when link-buffer
509 ;; We still have links to push out.
510 (insert "\n")
511 (let ((ind ""))
512 (save-match-data
513 (if (save-excursion
514 (re-search-backward
515 "^\\(\\([ \t]*\\)\\|\\(\\*+ \\)\\)[^ \t\n]" nil t))
516 (setq ind (or (match-string 2)
517 (make-string (length (match-string 3)) ?\ )))))
518 (mapc (lambda (x) (insert ind "[" (car x) "]: " (cdr x) "\n"))
519 link-buffer))
520 (insert "\n")))
522 (defun org-ascii-level-start (level title umax &optional lines)
523 "Insert a new level in ASCII export."
524 (let (char (n (- level umax 1)) (ind 0))
525 (if (> level umax)
526 (progn
527 (insert (make-string (* 2 n) ?\ )
528 (char-to-string (nth (% n (length org-export-ascii-bullets))
529 org-export-ascii-bullets))
530 " " title "\n")
531 ;; find the indentation of the next non-empty line
532 (catch 'stop
533 (while lines
534 (if (string-match "^\\* " (car lines)) (throw 'stop nil))
535 (if (string-match "^\\([ \t]*\\)\\S-" (car lines))
536 (throw 'stop (setq ind (org-get-indentation (car lines)))))
537 (pop lines)))
538 (setq org-ascii-current-indentation (cons (* 2 (1+ n)) ind)))
539 (if (or (not (equal (char-before) ?\n))
540 (not (equal (char-before (1- (point))) ?\n)))
541 (insert "\n"))
542 (setq char (nth (- umax level) (reverse org-export-ascii-underline)))
543 (unless org-export-with-tags
544 (if (string-match (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
545 (setq title (replace-match "" t t title))))
546 (if org-export-with-section-numbers
547 (setq title (concat (org-section-number level) " " title)))
548 (insert title "\n" (make-string (string-width title) char) "\n")
549 (setq org-ascii-current-indentation '(0 . 0)))))
551 (defun org-insert-centered (s &optional underline)
552 "Insert the string S centered and underline it with character UNDERLINE."
553 (let ((ind (max (/ (- fill-column (string-width s)) 2) 0)))
554 (insert (make-string ind ?\ ) s "\n")
555 (if underline
556 (insert (make-string ind ?\ )
557 (make-string (string-width s) underline)
558 "\n"))))
560 (defvar org-table-colgroup-info nil)
561 (defun org-format-table-ascii (lines)
562 "Format a table for ascii export."
563 (if (stringp lines)
564 (setq lines (org-split-string lines "\n")))
565 (if (not (string-match "^[ \t]*|" (car lines)))
566 ;; Table made by table.el - test for spanning
567 lines
569 ;; A normal org table
570 ;; Get rid of hlines at beginning and end
571 (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
572 (setq lines (nreverse lines))
573 (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
574 (setq lines (nreverse lines))
575 (when org-export-table-remove-special-lines
576 ;; Check if the table has a marking column. If yes remove the
577 ;; column and the special lines
578 (setq lines (org-table-clean-before-export lines)))
579 ;; Get rid of the vertical lines except for grouping
580 (let ((vl (org-colgroup-info-to-vline-list org-table-colgroup-info))
581 rtn line vl1 start)
582 (while (setq line (pop lines))
583 (if (string-match org-table-hline-regexp line)
584 (and (string-match "|\\(.*\\)|" line)
585 (setq line (replace-match " \\1" t nil line)))
586 (setq start 0 vl1 vl)
587 (while (string-match "|" line start)
588 (setq start (match-end 0))
589 (or (pop vl1) (setq line (replace-match " " t t line)))))
590 (push line rtn))
591 (nreverse rtn))))
593 (defun org-colgroup-info-to-vline-list (info)
594 (let (vl new last)
595 (while info
596 (setq last new new (pop info))
597 (if (or (memq last '(:end :startend))
598 (memq new '(:start :startend)))
599 (push t vl)
600 (push nil vl)))
601 (setq vl (nreverse vl))
602 (and vl (setcar vl nil))
603 vl))
605 (provide 'org-ascii)
607 ;; arch-tag: aa96f882-f477-4e13-86f5-70d43e7adf3c
608 ;;; org-ascii.el ends here