Release 6.29c
[org-mode/org-tableheadings.git] / lisp / org-ascii.el
blob9e5fd6dda69782e136f674bcf3d33daf1324ae1a
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.29c
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 (if title (org-insert-centered title ?=))
270 (insert "\n")
271 (if (and (or author email)
272 org-export-author-info)
273 (insert (concat (nth 1 lang-words) ": " (or author "")
274 (if email (concat " <" email ">") "")
275 "\n")))
277 (cond
278 ((and date (string-match "%" date))
279 (setq date (format-time-string date)))
280 (date)
281 (t (setq date (format-time-string "%Y-%m-%d %T %Z"))))
283 (if (and date org-export-time-stamp-file)
284 (insert (concat (nth 2 lang-words) ": " date"\n")))
286 (insert "\n\n"))
288 (if (and org-export-with-toc (not body-only))
289 (progn
290 (push (concat (nth 3 lang-words) "\n") thetoc)
291 (push (concat (make-string (string-width (nth 3 lang-words)) ?=)
292 "\n") thetoc)
293 (mapc '(lambda (line)
294 (if (string-match org-todo-line-regexp
295 line)
296 ;; This is a headline
297 (progn
298 (setq have-headings t)
299 (setq level (- (match-end 1) (match-beginning 1)
300 level-offset)
301 level (org-tr-level level)
302 txt (match-string 3 line)
303 todo
304 (or (and org-export-mark-todo-in-toc
305 (match-beginning 2)
306 (not (member (match-string 2 line)
307 org-done-keywords)))
308 ; TODO, not DONE
309 (and org-export-mark-todo-in-toc
310 (= level umax-toc)
311 (org-search-todo-below
312 line lines level))))
313 (setq txt (org-html-expand-for-ascii txt))
315 (while (string-match org-bracket-link-regexp txt)
316 (setq txt
317 (replace-match
318 (match-string (if (match-end 2) 3 1) txt)
319 t t txt)))
321 (if (and (memq org-export-with-tags '(not-in-toc nil))
322 (string-match
323 (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$")
324 txt))
325 (setq txt (replace-match "" t t txt)))
326 (if (string-match quote-re0 txt)
327 (setq txt (replace-match "" t t txt)))
329 (if org-export-with-section-numbers
330 (setq txt (concat (org-section-number level)
331 " " txt)))
332 (if (<= level umax-toc)
333 (progn
334 (push
335 (concat
336 (make-string
337 (* (max 0 (- level org-min-level)) 4) ?\ )
338 (format (if todo "%s (*)\n" "%s\n") txt))
339 thetoc)
340 (setq org-last-level level))
341 ))))
342 lines)
343 (setq thetoc (if have-headings (nreverse thetoc) nil))))
345 (org-init-section-numbers)
346 (while (setq line (pop lines))
347 (when (and link-buffer (string-match "^\\*+ " line))
348 (org-export-ascii-push-links (nreverse link-buffer))
349 (setq link-buffer nil))
350 (setq wrap nil)
351 ;; Remove the quoted HTML tags.
352 (setq line (org-html-expand-for-ascii line))
353 ;; Replace links with the description when possible
354 (while (string-match org-bracket-link-regexp line)
355 (setq link (match-string 1 line)
356 desc0 (match-string 3 line)
357 desc (or desc0 (match-string 1 line)))
358 (if (and (> (length link) 8)
359 (equal (substring link 0 8) "coderef:"))
360 (setq line (replace-match
361 (format (org-export-get-coderef-format (substring link 8) desc)
362 (cdr (assoc
363 (substring link 8)
364 org-export-code-refs)))
365 t t line))
366 (setq rpl (concat "["
367 (or (match-string 3 line) (match-string 1 line))
368 "]"))
369 (when (and desc0 (not (equal desc0 link)))
370 (if org-export-ascii-links-to-notes
371 (push (cons desc0 link) link-buffer)
372 (setq rpl (concat rpl " (" link ")")
373 wrap (+ (length line) (- (length (match-string 0 line)))
374 (length desc)))))
375 (setq line (replace-match rpl t t line))))
376 (when custom-times
377 (setq line (org-translate-time line)))
378 (cond
379 ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
380 ;; a Headline
381 (setq first-heading-pos (or first-heading-pos (point)))
382 (setq level (org-tr-level (- (match-end 1) (match-beginning 1)
383 level-offset))
384 txt (match-string 2 line))
385 (org-ascii-level-start level txt umax lines))
387 ((and org-export-with-tables
388 (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
389 (if (not table-open)
390 ;; New table starts
391 (setq table-open t table-buffer nil))
392 ;; Accumulate lines
393 (setq table-buffer (cons line table-buffer))
394 (when (or (not lines)
395 (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
396 (car lines))))
397 (setq table-open nil
398 table-buffer (nreverse table-buffer))
399 (insert (mapconcat
400 (lambda (x)
401 (org-fix-indentation x org-ascii-current-indentation))
402 (org-format-table-ascii table-buffer)
403 "\n") "\n")))
405 (if (string-match "^\\([ \t]*\\)\\([-+*][ \t]+\\)\\(.*?\\)\\( ::\\)" line)
406 (setq line (replace-match "\\1\\3:" t nil line)))
407 (setq line (org-fix-indentation line org-ascii-current-indentation))
408 ;; Remove forced line breaks
409 (if (string-match "\\\\\\\\[ \t]*$" line)
410 (setq line (replace-match "" t t line)))
411 (if (and org-export-with-fixed-width
412 (string-match "^\\([ \t]*\\)\\(:\\( \\|$\\)\\)" line))
413 (setq line (replace-match "\\1" nil nil line))
414 (if wrap (setq line (org-export-ascii-wrap line wrap))))
415 (insert line "\n"))))
417 (org-export-ascii-push-links (nreverse link-buffer))
419 (normal-mode)
421 ;; insert the table of contents
422 (when thetoc
423 (goto-char (point-min))
424 (if (re-search-forward "^[ \t]*\\[TABLE-OF-CONTENTS\\][ \t]*$" nil t)
425 (progn
426 (goto-char (match-beginning 0))
427 (replace-match ""))
428 (goto-char first-heading-pos))
429 (mapc 'insert thetoc)
430 (or (looking-at "[ \t]*\n[ \t]*\n")
431 (insert "\n\n")))
433 ;; Convert whitespace place holders
434 (goto-char (point-min))
435 (let (beg end)
436 (while (setq beg (next-single-property-change (point) 'org-whitespace))
437 (setq end (next-single-property-change beg 'org-whitespace))
438 (goto-char beg)
439 (delete-region beg end)
440 (insert (make-string (- end beg) ?\ ))))
442 ;; remove display and invisible chars
443 (let (beg end)
444 (goto-char (point-min))
445 (while (setq beg (next-single-property-change (point) 'display))
446 (setq end (next-single-property-change beg 'display))
447 (delete-region beg end)
448 (goto-char beg)
449 (insert "=>"))
450 (goto-char (point-min))
451 (while (setq beg (next-single-property-change (point) 'org-cwidth))
452 (setq end (next-single-property-change beg 'org-cwidth))
453 (delete-region beg end)
454 (goto-char beg)))
455 (or to-buffer (save-buffer))
456 (goto-char (point-min))
457 (or (org-export-push-to-kill-ring "ASCII")
458 (message "Exporting... done"))
459 ;; Return the buffer or a string, according to how this function was called
460 (if (eq to-buffer 'string)
461 (prog1 (buffer-substring (point-min) (point-max))
462 (kill-buffer (current-buffer)))
463 (current-buffer))))
465 (defun org-export-ascii-preprocess (parameters)
466 "Do extra work for ASCII export"
467 ;; Put quotes around verbatim text
468 (goto-char (point-min))
469 (while (re-search-forward org-verbatim-re nil t)
470 (goto-char (match-end 2))
471 (backward-delete-char 1) (insert "'")
472 (goto-char (match-beginning 2))
473 (delete-char 1) (insert "`")
474 (goto-char (match-end 2)))
475 ;; Remove target markers
476 (goto-char (point-min))
477 (while (re-search-forward "<<<?\\([^<>]*\\)>>>?\\([ \t]*\\)" nil t)
478 (replace-match "\\1\\2")))
480 (defun org-html-expand-for-ascii (line)
481 "Handle quoted HTML for ASCII export."
482 (if org-export-html-expand
483 (while (string-match "@<[^<>\n]*>" line)
484 ;; We just remove the tags for now.
485 (setq line (replace-match "" nil nil line))))
486 line)
488 (defun org-export-ascii-wrap (line where)
489 "Wrap LINE at or before WHERE."
490 (let ((ind (org-get-indentation line))
491 pos)
492 (catch 'found
493 (loop for i from where downto (/ where 2) do
494 (and (equal (aref line i) ?\ )
495 (setq pos i)
496 (throw 'found t))))
497 (if pos
498 (concat (substring line 0 pos) "\n"
499 (make-string ind ?\ )
500 (substring line (1+ pos)))
501 line)))
503 (defun org-export-ascii-push-links (link-buffer)
504 "Push out links in the buffer."
505 (when link-buffer
506 ;; We still have links to push out.
507 (insert "\n")
508 (let ((ind ""))
509 (save-match-data
510 (if (save-excursion
511 (re-search-backward
512 "^\\(\\([ \t]*\\)\\|\\(\\*+ \\)\\)[^ \t\n]" nil t))
513 (setq ind (or (match-string 2)
514 (make-string (length (match-string 3)) ?\ )))))
515 (mapc (lambda (x) (insert ind "[" (car x) "]: " (cdr x) "\n"))
516 link-buffer))
517 (insert "\n")))
519 (defun org-ascii-level-start (level title umax &optional lines)
520 "Insert a new level in ASCII export."
521 (let (char (n (- level umax 1)) (ind 0))
522 (if (> level umax)
523 (progn
524 (insert (make-string (* 2 n) ?\ )
525 (char-to-string (nth (% n (length org-export-ascii-bullets))
526 org-export-ascii-bullets))
527 " " title "\n")
528 ;; find the indentation of the next non-empty line
529 (catch 'stop
530 (while lines
531 (if (string-match "^\\* " (car lines)) (throw 'stop nil))
532 (if (string-match "^\\([ \t]*\\)\\S-" (car lines))
533 (throw 'stop (setq ind (org-get-indentation (car lines)))))
534 (pop lines)))
535 (setq org-ascii-current-indentation (cons (* 2 (1+ n)) ind)))
536 (if (or (not (equal (char-before) ?\n))
537 (not (equal (char-before (1- (point))) ?\n)))
538 (insert "\n"))
539 (setq char (nth (- umax level) (reverse org-export-ascii-underline)))
540 (unless org-export-with-tags
541 (if (string-match (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
542 (setq title (replace-match "" t t title))))
543 (if org-export-with-section-numbers
544 (setq title (concat (org-section-number level) " " title)))
545 (insert title "\n" (make-string (string-width title) char) "\n")
546 (setq org-ascii-current-indentation '(0 . 0)))))
548 (defun org-insert-centered (s &optional underline)
549 "Insert the string S centered and underline it with character UNDERLINE."
550 (let ((ind (max (/ (- fill-column (string-width s)) 2) 0)))
551 (insert (make-string ind ?\ ) s "\n")
552 (if underline
553 (insert (make-string ind ?\ )
554 (make-string (string-width s) underline)
555 "\n"))))
557 (defvar org-table-colgroup-info nil)
558 (defun org-format-table-ascii (lines)
559 "Format a table for ascii export."
560 (if (stringp lines)
561 (setq lines (org-split-string lines "\n")))
562 (if (not (string-match "^[ \t]*|" (car lines)))
563 ;; Table made by table.el - test for spanning
564 lines
566 ;; A normal org table
567 ;; Get rid of hlines at beginning and end
568 (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
569 (setq lines (nreverse lines))
570 (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
571 (setq lines (nreverse lines))
572 (when org-export-table-remove-special-lines
573 ;; Check if the table has a marking column. If yes remove the
574 ;; column and the special lines
575 (setq lines (org-table-clean-before-export lines)))
576 ;; Get rid of the vertical lines except for grouping
577 (let ((vl (org-colgroup-info-to-vline-list org-table-colgroup-info))
578 rtn line vl1 start)
579 (while (setq line (pop lines))
580 (if (string-match org-table-hline-regexp line)
581 (and (string-match "|\\(.*\\)|" line)
582 (setq line (replace-match " \\1" t nil line)))
583 (setq start 0 vl1 vl)
584 (while (string-match "|" line start)
585 (setq start (match-end 0))
586 (or (pop vl1) (setq line (replace-match " " t t line)))))
587 (push line rtn))
588 (nreverse rtn))))
590 (defun org-colgroup-info-to-vline-list (info)
591 (let (vl new last)
592 (while info
593 (setq last new new (pop info))
594 (if (or (memq last '(:end :startend))
595 (memq new '(:start :startend)))
596 (push t vl)
597 (push nil vl)))
598 (setq vl (nreverse vl))
599 (and vl (setcar vl nil))
600 vl))
602 (provide 'org-ascii)
604 ;; arch-tag: aa96f882-f477-4e13-86f5-70d43e7adf3c
606 ;;; org-ascii.el ends here