ASCII export: Fix bug when exporting links
[org-mode.git] / lisp / org-ascii.el
blob1bc2b7144867fd7d16c7ef0d4761cce0b344d4b6
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.26trans
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)
60 ;;; ASCII export
62 (defvar org-ascii-current-indentation nil) ; For communication
64 ;;;###autoload
65 (defun org-export-as-ascii-to-buffer (arg)
66 "Call `org-export-as-ascii` with output to a temporary buffer.
67 No file is created. The prefix ARG is passed through to `org-export-as-ascii'."
68 (interactive "P")
69 (org-export-as-ascii arg nil nil "*Org ASCII Export*")
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)
249 (switch-to-buffer buffer)
251 (setq lang-words (or (assoc language org-export-language-setup)
252 (assoc "en" org-export-language-setup)))
253 (switch-to-buffer-other-window buffer)
254 (erase-buffer)
255 (fundamental-mode)
256 ;; create local variables for all options, to make sure all called
257 ;; functions get the correct information
258 (mapc (lambda (x)
259 (set (make-local-variable (nth 2 x))
260 (plist-get opt-plist (car x))))
261 org-export-plist-vars)
262 (org-set-local 'org-odd-levels-only odd)
263 (setq umax (if arg (prefix-numeric-value arg)
264 org-export-headline-levels))
265 (setq umax-toc (if (integerp org-export-with-toc)
266 (min org-export-with-toc umax)
267 umax))
269 ;; File header
270 (unless body-only
271 (if title (org-insert-centered title ?=))
272 (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 (insert "\n\n"))
290 (if (and org-export-with-toc (not body-only))
291 (progn
292 (push (concat (nth 3 lang-words) "\n") thetoc)
293 (push (concat (make-string (string-width (nth 3 lang-words)) ?=)
294 "\n") thetoc)
295 (mapc '(lambda (line)
296 (if (string-match org-todo-line-regexp
297 line)
298 ;; This is a headline
299 (progn
300 (setq have-headings t)
301 (setq level (- (match-end 1) (match-beginning 1)
302 level-offset)
303 level (org-tr-level level)
304 txt (match-string 3 line)
305 todo
306 (or (and org-export-mark-todo-in-toc
307 (match-beginning 2)
308 (not (member (match-string 2 line)
309 org-done-keywords)))
310 ; TODO, not DONE
311 (and org-export-mark-todo-in-toc
312 (= level umax-toc)
313 (org-search-todo-below
314 line lines level))))
315 (setq txt (org-html-expand-for-ascii txt))
317 (while (string-match org-bracket-link-regexp txt)
318 (setq txt
319 (replace-match
320 (match-string (if (match-end 2) 3 1) txt)
321 t t txt)))
323 (if (and (memq org-export-with-tags '(not-in-toc nil))
324 (string-match
325 (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$")
326 txt))
327 (setq txt (replace-match "" t t txt)))
328 (if (string-match quote-re0 txt)
329 (setq txt (replace-match "" t t txt)))
331 (if org-export-with-section-numbers
332 (setq txt (concat (org-section-number level)
333 " " txt)))
334 (if (<= level umax-toc)
335 (progn
336 (push
337 (concat
338 (make-string
339 (* (max 0 (- level org-min-level)) 4) ?\ )
340 (format (if todo "%s (*)\n" "%s\n") txt))
341 thetoc)
342 (setq org-last-level level))
343 ))))
344 lines)
345 (setq thetoc (if have-headings (nreverse thetoc) nil))))
347 (org-init-section-numbers)
348 (while (setq line (pop lines))
349 (when (and link-buffer (string-match "^\\*+ " line))
350 (org-export-ascii-push-links (nreverse link-buffer))
351 (setq link-buffer nil))
352 (setq wrap nil)
353 ;; Remove the quoted HTML tags.
354 (setq line (org-html-expand-for-ascii line))
355 ;; Replace links with the description when possible
356 (while (string-match org-bracket-link-regexp line)
357 (setq link (match-string 1 line)
358 desc0 (match-string 3 line)
359 desc (or desc0 (match-string 1 line)))
360 (if (and (> (length link) 8)
361 (equal (substring link 0 8) "coderef:"))
362 (setq line (replace-match
363 (format (org-export-get-coderef-format (substring link 8) desc)
364 (cdr (assoc
365 (substring link 8)
366 org-export-code-refs)))
367 t t line))
368 (setq rpl (concat "["
369 (or (match-string 3 line) (match-string 1 line))
370 "]"))
371 (when (and desc0 (not (equal desc0 link)))
372 (if org-export-ascii-links-to-notes
373 (push (cons desc0 link) link-buffer)
374 (setq rpl (concat rpl " (" link ")")
375 wrap (+ (length line) (- (length (match-string 0 line)))
376 (length desc)))))
377 (setq line (replace-match rpl t t line))))
378 (when custom-times
379 (setq line (org-translate-time line)))
380 (cond
381 ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
382 ;; a Headline
383 (setq first-heading-pos (or first-heading-pos (point)))
384 (setq level (org-tr-level (- (match-end 1) (match-beginning 1)
385 level-offset))
386 txt (match-string 2 line))
387 (org-ascii-level-start level txt umax lines))
389 ((and org-export-with-tables
390 (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
391 (if (not table-open)
392 ;; New table starts
393 (setq table-open t table-buffer nil))
394 ;; Accumulate lines
395 (setq table-buffer (cons line table-buffer))
396 (when (or (not lines)
397 (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
398 (car lines))))
399 (setq table-open nil
400 table-buffer (nreverse table-buffer))
401 (insert (mapconcat
402 (lambda (x)
403 (org-fix-indentation x org-ascii-current-indentation))
404 (org-format-table-ascii table-buffer)
405 "\n") "\n")))
407 (if (string-match "^\\([ \t]*\\)\\([-+*][ \t]+\\)\\(.*?\\)\\( ::\\)" line)
408 (setq line (replace-match "\\1\\3:" t nil line)))
409 (setq line (org-fix-indentation line org-ascii-current-indentation))
410 ;; Remove forced line breaks
411 (if (string-match "\\\\\\\\[ \t]*$" line)
412 (setq line (replace-match "" t t line)))
413 (if (and org-export-with-fixed-width
414 (string-match "^\\([ \t]*\\)\\(:\\( \\|$\\)\\)" line))
415 (setq line (replace-match "\\1" nil nil line))
416 (if wrap (setq line (org-export-ascii-wrap line wrap))))
417 (insert line "\n"))))
419 (org-export-ascii-push-links (nreverse link-buffer))
421 (normal-mode)
423 ;; insert the table of contents
424 (when thetoc
425 (goto-char (point-min))
426 (if (re-search-forward "^[ \t]*\\[TABLE-OF-CONTENTS\\][ \t]*$" nil t)
427 (progn
428 (goto-char (match-beginning 0))
429 (replace-match ""))
430 (goto-char first-heading-pos))
431 (mapc 'insert thetoc)
432 (or (looking-at "[ \t]*\n[ \t]*\n")
433 (insert "\n\n")))
435 ;; Convert whitespace place holders
436 (goto-char (point-min))
437 (let (beg end)
438 (while (setq beg (next-single-property-change (point) 'org-whitespace))
439 (setq end (next-single-property-change beg 'org-whitespace))
440 (goto-char beg)
441 (delete-region beg end)
442 (insert (make-string (- end beg) ?\ ))))
444 ;; remove display and invisible chars
445 (let (beg end)
446 (goto-char (point-min))
447 (while (setq beg (next-single-property-change (point) 'display))
448 (setq end (next-single-property-change beg 'display))
449 (delete-region beg end)
450 (goto-char beg)
451 (insert "=>"))
452 (goto-char (point-min))
453 (while (setq beg (next-single-property-change (point) 'org-cwidth))
454 (setq end (next-single-property-change beg 'org-cwidth))
455 (delete-region beg end)
456 (goto-char beg)))
457 (or to-buffer (save-buffer))
458 (goto-char (point-min))
459 (prog1 (if (eq to-buffer 'string)
460 (prog1 (buffer-substring (point-min) (point-max))
461 (kill-buffer (current-buffer)))
462 (current-buffer))
463 (when hidden
464 (delete-window)))))
466 (defun org-export-ascii-preprocess (parameters)
467 "Do extra work for ASCII export"
468 ;; Put quotes around verbatim text
469 (goto-char (point-min))
470 (while (re-search-forward org-verbatim-re nil t)
471 (goto-char (match-end 2))
472 (backward-delete-char 1) (insert "'")
473 (goto-char (match-beginning 2))
474 (delete-char 1) (insert "`")
475 (goto-char (match-end 2)))
476 ;; Remove target markers
477 (goto-char (point-min))
478 (while (re-search-forward "<<<?\\([^<>]*\\)>>>?\\([ \t]*\\)" nil t)
479 (replace-match "\\1\\2")))
481 (defun org-html-expand-for-ascii (line)
482 "Handle quoted HTML for ASCII export."
483 (if org-export-html-expand
484 (while (string-match "@<[^<>\n]*>" line)
485 ;; We just remove the tags for now.
486 (setq line (replace-match "" nil nil line))))
487 line)
489 (defun org-export-ascii-wrap (line where)
490 "Wrap LINE at or before WHERE."
491 (let ((ind (org-get-indentation line))
492 pos)
493 (catch 'found
494 (loop for i from where downto (/ where 2) do
495 (and (equal (aref line i) ?\ )
496 (setq pos i)
497 (throw 'found t))))
498 (if pos
499 (concat (substring line 0 pos) "\n"
500 (make-string ind ?\ )
501 (substring line (1+ pos)))
502 line)))
504 (defun org-export-ascii-push-links (link-buffer)
505 "Push out links in the buffer."
506 (when link-buffer
507 ;; We still have links to push out.
508 (insert "\n")
509 (let ((ind ""))
510 (save-match-data
511 (if (save-excursion
512 (re-search-backward
513 "^\\(\\([ \t]*\\)\\|\\(\\*+ \\)\\)[^ \t\n]" nil t))
514 (setq ind (or (match-string 2)
515 (make-string (length (match-string 3)) ?\ )))))
516 (mapc (lambda (x) (insert ind "[" (car x) "]: " (cdr x) "\n"))
517 link-buffer))
518 (insert "\n")))
520 (defun org-ascii-level-start (level title umax &optional lines)
521 "Insert a new level in ASCII export."
522 (let (char (n (- level umax 1)) (ind 0))
523 (if (> level umax)
524 (progn
525 (insert (make-string (* 2 n) ?\ )
526 (char-to-string (nth (% n (length org-export-ascii-bullets))
527 org-export-ascii-bullets))
528 " " title "\n")
529 ;; find the indentation of the next non-empty line
530 (catch 'stop
531 (while lines
532 (if (string-match "^\\* " (car lines)) (throw 'stop nil))
533 (if (string-match "^\\([ \t]*\\)\\S-" (car lines))
534 (throw 'stop (setq ind (org-get-indentation (car lines)))))
535 (pop lines)))
536 (setq org-ascii-current-indentation (cons (* 2 (1+ n)) ind)))
537 (if (or (not (equal (char-before) ?\n))
538 (not (equal (char-before (1- (point))) ?\n)))
539 (insert "\n"))
540 (setq char (nth (- umax level) (reverse org-export-ascii-underline)))
541 (unless org-export-with-tags
542 (if (string-match (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
543 (setq title (replace-match "" t t title))))
544 (if org-export-with-section-numbers
545 (setq title (concat (org-section-number level) " " title)))
546 (insert title "\n" (make-string (string-width title) char) "\n")
547 (setq org-ascii-current-indentation '(0 . 0)))))
549 (defun org-insert-centered (s &optional underline)
550 "Insert the string S centered and underline it with character UNDERLINE."
551 (let ((ind (max (/ (- fill-column (string-width s)) 2) 0)))
552 (insert (make-string ind ?\ ) s "\n")
553 (if underline
554 (insert (make-string ind ?\ )
555 (make-string (string-width s) underline)
556 "\n"))))
558 (defvar org-table-colgroup-info nil)
559 (defun org-format-table-ascii (lines)
560 "Format a table for ascii export."
561 (if (stringp lines)
562 (setq lines (org-split-string lines "\n")))
563 (if (not (string-match "^[ \t]*|" (car lines)))
564 ;; Table made by table.el - test for spanning
565 lines
567 ;; A normal org table
568 ;; Get rid of hlines at beginning and end
569 (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
570 (setq lines (nreverse lines))
571 (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
572 (setq lines (nreverse lines))
573 (when org-export-table-remove-special-lines
574 ;; Check if the table has a marking column. If yes remove the
575 ;; column and the special lines
576 (setq lines (org-table-clean-before-export lines)))
577 ;; Get rid of the vertical lines except for grouping
578 (let ((vl (org-colgroup-info-to-vline-list org-table-colgroup-info))
579 rtn line vl1 start)
580 (while (setq line (pop lines))
581 (if (string-match org-table-hline-regexp line)
582 (and (string-match "|\\(.*\\)|" line)
583 (setq line (replace-match " \\1" t nil line)))
584 (setq start 0 vl1 vl)
585 (while (string-match "|" line start)
586 (setq start (match-end 0))
587 (or (pop vl1) (setq line (replace-match " " t t line)))))
588 (push line rtn))
589 (nreverse rtn))))
591 (defun org-colgroup-info-to-vline-list (info)
592 (let (vl new last)
593 (while info
594 (setq last new new (pop info))
595 (if (or (memq last '(:end :startend))
596 (memq new '(:start :startend)))
597 (push t vl)
598 (push nil vl)))
599 (setq vl (nreverse vl))
600 (and vl (setcar vl nil))
601 vl))
603 (provide 'org-ascii)
605 ;; arch-tag: aa96f882-f477-4e13-86f5-70d43e7adf3c
607 ;;; org-ascii.el ends here