Merge branch 'master' of git+ssh://repo.or.cz/srv/git/org-mode
[org-mode/org-tableheadings.git] / lisp / org-ascii.el
blobe8be487d64820addb4e8c5e3b9e6a04fb6870361
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.28trans
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 ;; create local variables for all options, to make sure all called
254 ;; functions get the correct information
255 (mapc (lambda (x)
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)
264 umax))
266 ;; File header
267 (unless body-only
268 (if title (org-insert-centered title ?=))
269 (insert "\n")
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 ">") "")
274 "\n")))
276 (cond
277 ((and date (string-match "%" date))
278 (setq date (format-time-string date)))
279 (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")))
285 (insert "\n\n"))
287 (if (and org-export-with-toc (not body-only))
288 (progn
289 (push (concat (nth 3 lang-words) "\n") thetoc)
290 (push (concat (make-string (string-width (nth 3 lang-words)) ?=)
291 "\n") thetoc)
292 (mapc '(lambda (line)
293 (if (string-match org-todo-line-regexp
294 line)
295 ;; This is a headline
296 (progn
297 (setq have-headings t)
298 (setq level (- (match-end 1) (match-beginning 1)
299 level-offset)
300 level (org-tr-level level)
301 txt (match-string 3 line)
302 todo
303 (or (and org-export-mark-todo-in-toc
304 (match-beginning 2)
305 (not (member (match-string 2 line)
306 org-done-keywords)))
307 ; TODO, not DONE
308 (and org-export-mark-todo-in-toc
309 (= level umax-toc)
310 (org-search-todo-below
311 line lines level))))
312 (setq txt (org-html-expand-for-ascii txt))
314 (while (string-match org-bracket-link-regexp txt)
315 (setq txt
316 (replace-match
317 (match-string (if (match-end 2) 3 1) txt)
318 t t txt)))
320 (if (and (memq org-export-with-tags '(not-in-toc nil))
321 (string-match
322 (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$")
323 txt))
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)
330 " " txt)))
331 (if (<= level umax-toc)
332 (progn
333 (push
334 (concat
335 (make-string
336 (* (max 0 (- level org-min-level)) 4) ?\ )
337 (format (if todo "%s (*)\n" "%s\n") txt))
338 thetoc)
339 (setq org-last-level level))
340 ))))
341 lines)
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))
349 (setq wrap 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)
361 (cdr (assoc
362 (substring link 8)
363 org-export-code-refs)))
364 t t line))
365 (setq rpl (concat "["
366 (or (match-string 3 line) (match-string 1 line))
367 "]"))
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)))
373 (length desc)))))
374 (setq line (replace-match rpl t t line))))
375 (when custom-times
376 (setq line (org-translate-time line)))
377 (cond
378 ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
379 ;; a Headline
380 (setq first-heading-pos (or first-heading-pos (point)))
381 (setq level (org-tr-level (- (match-end 1) (match-beginning 1)
382 level-offset))
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))
388 (if (not table-open)
389 ;; New table starts
390 (setq table-open t table-buffer nil))
391 ;; Accumulate lines
392 (setq table-buffer (cons line table-buffer))
393 (when (or (not lines)
394 (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
395 (car lines))))
396 (setq table-open nil
397 table-buffer (nreverse table-buffer))
398 (insert (mapconcat
399 (lambda (x)
400 (org-fix-indentation x org-ascii-current-indentation))
401 (org-format-table-ascii table-buffer)
402 "\n") "\n")))
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))
418 (normal-mode)
420 ;; insert the table of contents
421 (when thetoc
422 (goto-char (point-min))
423 (if (re-search-forward "^[ \t]*\\[TABLE-OF-CONTENTS\\][ \t]*$" nil t)
424 (progn
425 (goto-char (match-beginning 0))
426 (replace-match ""))
427 (goto-char first-heading-pos))
428 (mapc 'insert thetoc)
429 (or (looking-at "[ \t]*\n[ \t]*\n")
430 (insert "\n\n")))
432 ;; Convert whitespace place holders
433 (goto-char (point-min))
434 (let (beg end)
435 (while (setq beg (next-single-property-change (point) 'org-whitespace))
436 (setq end (next-single-property-change beg 'org-whitespace))
437 (goto-char beg)
438 (delete-region beg end)
439 (insert (make-string (- end beg) ?\ ))))
441 ;; remove display and invisible chars
442 (let (beg end)
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)
447 (goto-char beg)
448 (insert "=>"))
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)
453 (goto-char beg)))
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)))
462 (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))))
485 line)
487 (defun org-export-ascii-wrap (line where)
488 "Wrap LINE at or before WHERE."
489 (let ((ind (org-get-indentation line))
490 pos)
491 (catch 'found
492 (loop for i from where downto (/ where 2) do
493 (and (equal (aref line i) ?\ )
494 (setq pos i)
495 (throw 'found t))))
496 (if pos
497 (concat (substring line 0 pos) "\n"
498 (make-string ind ?\ )
499 (substring line (1+ pos)))
500 line)))
502 (defun org-export-ascii-push-links (link-buffer)
503 "Push out links in the buffer."
504 (when link-buffer
505 ;; We still have links to push out.
506 (insert "\n")
507 (let ((ind ""))
508 (save-match-data
509 (if (save-excursion
510 (re-search-backward
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"))
515 link-buffer))
516 (insert "\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))
521 (if (> level umax)
522 (progn
523 (insert (make-string (* 2 n) ?\ )
524 (char-to-string (nth (% n (length org-export-ascii-bullets))
525 org-export-ascii-bullets))
526 " " title "\n")
527 ;; find the indentation of the next non-empty line
528 (catch 'stop
529 (while lines
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)))))
533 (pop 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)))
537 (insert "\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")
551 (if underline
552 (insert (make-string ind ?\ )
553 (make-string (string-width s) underline)
554 "\n"))))
556 (defvar org-table-colgroup-info nil)
557 (defun org-format-table-ascii (lines)
558 "Format a table for ascii export."
559 (if (stringp lines)
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
563 lines
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))
577 rtn line vl1 start)
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)))))
586 (push line rtn))
587 (nreverse rtn))))
589 (defun org-colgroup-info-to-vline-list (info)
590 (let (vl new last)
591 (while info
592 (setq last new new (pop info))
593 (if (or (memq last '(:end :startend))
594 (memq new '(:start :startend)))
595 (push t vl)
596 (push nil vl)))
597 (setq vl (nreverse vl))
598 (and vl (setcar vl nil))
599 vl))
601 (provide 'org-ascii)
603 ;; arch-tag: aa96f882-f477-4e13-86f5-70d43e7adf3c
605 ;;; org-ascii.el ends here