Release 6.35c
[org-mode.git] / lisp / org-ascii.el
blob4f90b5990b0b32de7b90425f8825e04c86a0309b
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: TAG=6.35c
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)
30 (eval-when-compile
31 (require 'cl))
33 (defgroup org-export-ascii nil
34 "Options specific for ASCII export of Org-mode files."
35 :tag "Org Export ASCII"
36 :group 'org-export)
38 (defcustom org-export-ascii-underline '(?\$ ?\# ?^ ?\~ ?\= ?\-)
39 "Characters for underlining headings in ASCII export.
40 In the given sequence, these characters will be used for level 1, 2, ..."
41 :group 'org-export-ascii
42 :type '(repeat character))
44 (defcustom org-export-ascii-bullets '(?* ?+ ?-)
45 "Bullet characters for headlines converted to lists in ASCII export.
46 The first character is used for the first lest level generated in this
47 way, and so on. If there are more levels than characters given here,
48 the list will be repeated.
49 Note that plain lists will keep the same bullets as the have in the
50 Org-mode file."
51 :group 'org-export-ascii
52 :type '(repeat character))
54 (defcustom org-export-ascii-links-to-notes t
55 "Non-nil means convert links to notes before the next headline.
56 When nil, the link will be exported in place. If the line becomes long
57 in this way, it will be wrapped."
58 :group 'org-export-ascii
59 :type 'boolean)
61 (defcustom org-export-ascii-table-keep-all-vertical-lines nil
62 "Non-nil means keep all vertical lines in ASCII tables.
63 When nil, vertical lines will be removed except for those needed
64 for column grouping."
65 :group 'org-export-ascii
66 :type 'boolean)
68 (defcustom org-export-ascii-table-widen-columns t
69 "Non-nil means widen narrowed columns for export.
70 When nil, narrowed columns will look in ASCII export just like in org-mode,
71 i.e. with \"=>\" as ellipsis."
72 :group 'org-export-ascii
73 :type 'boolean)
75 (defvar org-export-ascii-entities 'ascii
76 "The ascii representation to be used during ascii export.
77 Possible values are:
79 ascii Only use plain ASCII characters
80 latin1 Include Latin-1 character
81 utf8 Use all UTF-8 characters")
83 ;;; Hooks
85 (defvar org-export-ascii-final-hook nil
86 "Hook run at the end of ASCII export, in the new buffer.")
88 ;;; ASCII export
90 (defvar org-ascii-current-indentation nil) ; For communication
92 ;;;###autoload
93 (defun org-export-as-latin1 (&rest args)
94 "Like `org-export-as-ascii', use latin1 encoding for special symbols."
95 (interactive)
96 (org-export-as-encoding 'org-export-as-ascii (interactive-p)
97 'latin1 args))
99 ;;;###autoload
100 (defun org-export-as-latin1-to-buffer (&rest args)
101 "Like `org-export-as-ascii-to-buffer', use latin1 encoding for symbols."
102 (interactive)
103 (org-export-as-encoding 'org-export-as-ascii-to-buffer (interactive-p)
104 'latin1 args))
106 ;;;###autoload
107 (defun org-export-as-utf8 (&rest args)
108 "Like `org-export-as-ascii', use use encoding for special symbols."
109 (interactive)
110 (org-export-as-encoding 'org-export-as-ascii (interactive-p)
111 'utf8 args))
113 ;;;###autoload
114 (defun org-export-as-utf8-to-buffer (&rest args)
115 "Like `org-export-as-ascii-to-buffer', use utf8 encoding for symbols."
116 (interactive)
117 (org-export-as-encoding 'org-export-as-ascii-to-buffer (interactive-p)
118 'utf8 args))
120 (defun org-export-as-encoding (command interactivep encoding &rest args)
121 (let ((org-export-ascii-entities encoding))
122 (if interactivep
123 (call-interactively command)
124 (apply command args))))
127 ;;;###autoload
128 (defun org-export-as-ascii-to-buffer (arg)
129 "Call `org-export-as-ascii` with output to a temporary buffer.
130 No file is created. The prefix ARG is passed through to `org-export-as-ascii'."
131 (interactive "P")
132 (org-export-as-ascii arg nil nil "*Org ASCII Export*")
133 (when org-export-show-temporary-export-buffer
134 (switch-to-buffer-other-window "*Org ASCII Export*")))
136 ;;;###autoload
137 (defun org-replace-region-by-ascii (beg end)
138 "Assume the current region has org-mode syntax, and convert it to plain ASCII.
139 This can be used in any buffer. For example, you could write an
140 itemized list in org-mode syntax in a Mail buffer and then use this
141 command to convert it."
142 (interactive "r")
143 (let (reg ascii buf pop-up-frames)
144 (save-window-excursion
145 (if (org-mode-p)
146 (setq ascii (org-export-region-as-ascii
147 beg end t 'string))
148 (setq reg (buffer-substring beg end)
149 buf (get-buffer-create "*Org tmp*"))
150 (with-current-buffer buf
151 (erase-buffer)
152 (insert reg)
153 (org-mode)
154 (setq ascii (org-export-region-as-ascii
155 (point-min) (point-max) t 'string)))
156 (kill-buffer buf)))
157 (delete-region beg end)
158 (insert ascii)))
160 ;;;###autoload
161 (defun org-export-region-as-ascii (beg end &optional body-only buffer)
162 "Convert region from BEG to END in org-mode buffer to plain ASCII.
163 If prefix arg BODY-ONLY is set, omit file header, footer, and table of
164 contents, and only produce the region of converted text, useful for
165 cut-and-paste operations.
166 If BUFFER is a buffer or a string, use/create that buffer as a target
167 of the converted ASCII. If BUFFER is the symbol `string', return the
168 produced ASCII as a string and leave not buffer behind. For example,
169 a Lisp program could call this function in the following way:
171 (setq ascii (org-export-region-as-ascii beg end t 'string))
173 When called interactively, the output buffer is selected, and shown
174 in a window. A non-interactive call will only return the buffer."
175 (interactive "r\nP")
176 (when (interactive-p)
177 (setq buffer "*Org ASCII Export*"))
178 (let ((transient-mark-mode t) (zmacs-regions t)
179 ext-plist rtn)
180 (setq ext-plist (plist-put ext-plist :ignore-subtree-p t))
181 (goto-char end)
182 (set-mark (point)) ;; to activate the region
183 (goto-char beg)
184 (setq rtn (org-export-as-ascii
185 nil nil ext-plist
186 buffer body-only))
187 (if (fboundp 'deactivate-mark) (deactivate-mark))
188 (if (and (interactive-p) (bufferp rtn))
189 (switch-to-buffer-other-window rtn)
190 rtn)))
192 ;;;###autoload
193 (defun org-export-as-ascii (arg &optional hidden ext-plist
194 to-buffer body-only pub-dir)
195 "Export the outline as a pretty ASCII file.
196 If there is an active region, export only the region.
197 The prefix ARG specifies how many levels of the outline should become
198 underlined headlines, default is 3. Lower levels will become bulleted
199 lists. When HIDDEN is non-nil, don't display the ASCII buffer.
200 EXT-PLIST is a property list with external parameters overriding
201 org-mode's default settings, but still inferior to file-local
202 settings. When TO-BUFFER is non-nil, create a buffer with that
203 name and export to that buffer. If TO-BUFFER is the symbol
204 `string', don't leave any buffer behind but just return the
205 resulting ASCII as a string. When BODY-ONLY is set, don't produce
206 the file header and footer. When PUB-DIR is set, use this as the
207 publishing directory."
208 (interactive "P")
209 (run-hooks 'org-export-first-hook)
210 (setq-default org-todo-line-regexp org-todo-line-regexp)
211 (let* ((opt-plist (org-combine-plists (org-default-export-plist)
212 ext-plist
213 (org-infile-export-plist)))
214 (region-p (org-region-active-p))
215 (rbeg (and region-p (region-beginning)))
216 (rend (and region-p (region-end)))
217 (subtree-p
218 (if (plist-get opt-plist :ignore-subtree-p)
220 (when region-p
221 (save-excursion
222 (goto-char rbeg)
223 (and (org-at-heading-p)
224 (>= (org-end-of-subtree t t) rend))))))
225 (level-offset (if subtree-p
226 (save-excursion
227 (goto-char rbeg)
228 (+ (funcall outline-level)
229 (if org-odd-levels-only 1 0)))
231 (opt-plist (setq org-export-opt-plist
232 (if subtree-p
233 (org-export-add-subtree-options opt-plist rbeg)
234 opt-plist)))
235 ;; The following two are dynamically scoped into other
236 ;; routines below.
237 (org-current-export-dir
238 (or pub-dir (org-export-directory :html opt-plist)))
239 (org-current-export-file buffer-file-name)
240 (custom-times org-display-custom-times)
241 (org-ascii-current-indentation '(0 . 0))
242 (level 0) line txt
243 (umax nil)
244 (umax-toc nil)
245 (case-fold-search nil)
246 (bfname (buffer-file-name (or (buffer-base-buffer) (current-buffer))))
247 (filename (if to-buffer
249 (concat (file-name-as-directory
250 (or pub-dir
251 (org-export-directory :ascii opt-plist)))
252 (file-name-sans-extension
253 (or (and subtree-p
254 (org-entry-get (region-beginning)
255 "EXPORT_FILE_NAME" t))
256 (file-name-nondirectory bfname)))
257 ".txt")))
258 (filename (and filename
259 (if (equal (file-truename filename)
260 (file-truename bfname))
261 (concat filename ".txt")
262 filename)))
263 (buffer (if to-buffer
264 (cond
265 ((eq to-buffer 'string)
266 (get-buffer-create "*Org ASCII Export*"))
267 (t (get-buffer-create to-buffer)))
268 (find-file-noselect filename)))
269 (org-levels-open (make-vector org-level-max nil))
270 (odd org-odd-levels-only)
271 (date (plist-get opt-plist :date))
272 (author (plist-get opt-plist :author))
273 (title (or (and subtree-p (org-export-get-title-from-subtree))
274 (plist-get opt-plist :title)
275 (and (not
276 (plist-get opt-plist :skip-before-1st-heading))
277 (org-export-grab-title-from-buffer))
278 (and (buffer-file-name)
279 (file-name-sans-extension
280 (file-name-nondirectory bfname)))
281 "UNTITLED"))
282 (email (plist-get opt-plist :email))
283 (language (plist-get opt-plist :language))
284 (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
285 (todo nil)
286 (lang-words nil)
287 (region
288 (buffer-substring
289 (if (org-region-active-p) (region-beginning) (point-min))
290 (if (org-region-active-p) (region-end) (point-max))))
291 (lines (org-split-string
292 (org-export-preprocess-string
293 region
294 :for-ascii t
295 :skip-before-1st-heading
296 (plist-get opt-plist :skip-before-1st-heading)
297 :drawers (plist-get opt-plist :drawers)
298 :tags (plist-get opt-plist :tags)
299 :priority (plist-get opt-plist :priority)
300 :footnotes (plist-get opt-plist :footnotes)
301 :timestamps (plist-get opt-plist :timestamps)
302 :todo-keywords (plist-get opt-plist :todo-keywords)
303 :verbatim-multiline t
304 :select-tags (plist-get opt-plist :select-tags)
305 :exclude-tags (plist-get opt-plist :exclude-tags)
306 :archived-trees
307 (plist-get opt-plist :archived-trees)
308 :add-text (plist-get opt-plist :text))
309 "\n"))
310 thetoc have-headings first-heading-pos
311 table-open table-buffer link-buffer link desc desc0 rpl wrap)
312 (let ((inhibit-read-only t))
313 (org-unmodified
314 (remove-text-properties (point-min) (point-max)
315 '(:org-license-to-kill t))))
317 (setq org-min-level (org-get-min-level lines level-offset))
318 (setq org-last-level org-min-level)
319 (org-init-section-numbers)
320 (setq lang-words (or (assoc language org-export-language-setup)
321 (assoc "en" org-export-language-setup)))
322 (set-buffer buffer)
323 (erase-buffer)
324 (fundamental-mode)
325 (org-install-letbind)
326 ;; create local variables for all options, to make sure all called
327 ;; functions get the correct information
328 (mapc (lambda (x)
329 (set (make-local-variable (nth 2 x))
330 (plist-get opt-plist (car x))))
331 org-export-plist-vars)
332 (org-set-local 'org-odd-levels-only odd)
333 (setq umax (if arg (prefix-numeric-value arg)
334 org-export-headline-levels))
335 (setq umax-toc (if (integerp org-export-with-toc)
336 (min org-export-with-toc umax)
337 umax))
339 ;; File header
340 (unless body-only
341 (when (and title (not (string= "" title)))
342 (org-insert-centered title ?=)
343 (insert "\n"))
345 (if (and (or author email)
346 org-export-author-info)
347 (insert(concat (nth 1 lang-words) ": " (or author "")
348 (if (and org-export-email-info
349 email (string-match "\\S-" email))
350 (concat " <" email ">") "")
351 "\n")))
353 (cond
354 ((and date (string-match "%" date))
355 (setq date (format-time-string date)))
356 (date)
357 (t (setq date (format-time-string "%Y-%m-%d %T %Z"))))
359 (if (and date org-export-time-stamp-file)
360 (insert (concat (nth 2 lang-words) ": " date"\n")))
362 (unless (= (point) (point-min))
363 (insert "\n\n")))
365 (if (and org-export-with-toc (not body-only))
366 (progn
367 (push (concat (nth 3 lang-words) "\n") thetoc)
368 (push (concat (make-string (string-width (nth 3 lang-words)) ?=)
369 "\n") thetoc)
370 (mapc '(lambda (line)
371 (if (string-match org-todo-line-regexp
372 line)
373 ;; This is a headline
374 (progn
375 (setq have-headings t)
376 (setq level (- (match-end 1) (match-beginning 1)
377 level-offset)
378 level (org-tr-level level)
379 txt (match-string 3 line)
380 todo
381 (or (and org-export-mark-todo-in-toc
382 (match-beginning 2)
383 (not (member (match-string 2 line)
384 org-done-keywords)))
385 ; TODO, not DONE
386 (and org-export-mark-todo-in-toc
387 (= level umax-toc)
388 (org-search-todo-below
389 line lines level))))
390 (setq txt (org-html-expand-for-ascii txt))
392 (while (string-match org-bracket-link-regexp txt)
393 (setq txt
394 (replace-match
395 (match-string (if (match-end 2) 3 1) txt)
396 t t txt)))
398 (if (and (memq org-export-with-tags '(not-in-toc nil))
399 (string-match
400 (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$")
401 txt))
402 (setq txt (replace-match "" t t txt)))
403 (if (string-match quote-re0 txt)
404 (setq txt (replace-match "" t t txt)))
406 (if org-export-with-section-numbers
407 (setq txt (concat (org-section-number level)
408 " " txt)))
409 (if (<= level umax-toc)
410 (progn
411 (push
412 (concat
413 (make-string
414 (* (max 0 (- level org-min-level)) 4) ?\ )
415 (format (if todo "%s (*)\n" "%s\n") txt))
416 thetoc)
417 (setq org-last-level level))
418 ))))
419 lines)
420 (setq thetoc (if have-headings (nreverse thetoc) nil))))
422 (org-init-section-numbers)
423 (while (setq line (pop lines))
424 (when (and link-buffer (string-match "^\\*+ " line))
425 (org-export-ascii-push-links (nreverse link-buffer))
426 (setq link-buffer nil))
427 (setq wrap nil)
428 ;; Remove the quoted HTML tags.
429 (setq line (org-html-expand-for-ascii line))
430 ;; Replace links with the description when possible
431 (while (string-match org-bracket-link-regexp line)
432 (setq link (match-string 1 line)
433 desc0 (match-string 3 line)
434 desc (or desc0 (match-string 1 line)))
435 (if (and (> (length link) 8)
436 (equal (substring link 0 8) "coderef:"))
437 (setq line (replace-match
438 (format (org-export-get-coderef-format (substring link 8) desc)
439 (cdr (assoc
440 (substring link 8)
441 org-export-code-refs)))
442 t t line))
443 (setq rpl (concat "["
444 (or (match-string 3 line) (match-string 1 line))
445 "]"))
446 (when (and desc0 (not (equal desc0 link)))
447 (if org-export-ascii-links-to-notes
448 (push (cons desc0 link) link-buffer)
449 (setq rpl (concat rpl " (" link ")")
450 wrap (+ (length line) (- (length (match-string 0 line)))
451 (length desc)))))
452 (setq line (replace-match rpl t t line))))
453 (when custom-times
454 (setq line (org-translate-time line)))
455 (cond
456 ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
457 ;; a Headline
458 (setq first-heading-pos (or first-heading-pos (point)))
459 (setq level (org-tr-level (- (match-end 1) (match-beginning 1)
460 level-offset))
461 txt (match-string 2 line))
462 (org-ascii-level-start level txt umax lines))
464 ((and org-export-with-tables
465 (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
466 (if (not table-open)
467 ;; New table starts
468 (setq table-open t table-buffer nil))
469 ;; Accumulate lines
470 (setq table-buffer (cons line table-buffer))
471 (when (or (not lines)
472 (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
473 (car lines))))
474 (setq table-open nil
475 table-buffer (nreverse table-buffer))
476 (insert (mapconcat
477 (lambda (x)
478 (org-fix-indentation x org-ascii-current-indentation))
479 (org-format-table-ascii table-buffer)
480 "\n") "\n")))
482 (if (string-match "^\\([ \t]*\\)\\([-+*][ \t]+\\)\\(.*?\\)\\( ::\\)" line)
483 (setq line (replace-match "\\1\\3:" t nil line)))
484 (setq line (org-fix-indentation line org-ascii-current-indentation))
485 ;; Remove forced line breaks
486 (if (string-match "\\\\\\\\[ \t]*$" line)
487 (setq line (replace-match "" t t line)))
488 (if (and org-export-with-fixed-width
489 (string-match "^\\([ \t]*\\)\\(:\\( \\|$\\)\\)" line))
490 (setq line (replace-match "\\1" nil nil line))
491 (if wrap (setq line (org-export-ascii-wrap line wrap))))
492 (insert line "\n"))))
494 (org-export-ascii-push-links (nreverse link-buffer))
496 (normal-mode)
498 ;; insert the table of contents
499 (when thetoc
500 (goto-char (point-min))
501 (if (re-search-forward "^[ \t]*\\[TABLE-OF-CONTENTS\\][ \t]*$" nil t)
502 (progn
503 (goto-char (match-beginning 0))
504 (replace-match ""))
505 (goto-char first-heading-pos))
506 (mapc 'insert thetoc)
507 (or (looking-at "[ \t]*\n[ \t]*\n")
508 (insert "\n\n")))
510 ;; Convert whitespace place holders
511 (goto-char (point-min))
512 (let (beg end)
513 (while (setq beg (next-single-property-change (point) 'org-whitespace))
514 (setq end (next-single-property-change beg 'org-whitespace))
515 (goto-char beg)
516 (delete-region beg end)
517 (insert (make-string (- end beg) ?\ ))))
519 ;; remove display and invisible chars
520 (let (beg end)
521 (goto-char (point-min))
522 (while (setq beg (next-single-property-change (point) 'display))
523 (setq end (next-single-property-change beg 'display))
524 (delete-region beg end)
525 (goto-char beg)
526 (insert "=>"))
527 (goto-char (point-min))
528 (while (setq beg (next-single-property-change (point) 'org-cwidth))
529 (setq end (next-single-property-change beg 'org-cwidth))
530 (delete-region beg end)
531 (goto-char beg)))
532 (run-hooks 'org-export-ascii-final-hook)
533 (or to-buffer (save-buffer))
534 (goto-char (point-min))
535 (or (org-export-push-to-kill-ring "ASCII")
536 (message "Exporting... done"))
537 ;; Return the buffer or a string, according to how this function was called
538 (if (eq to-buffer 'string)
539 (prog1 (buffer-substring (point-min) (point-max))
540 (kill-buffer (current-buffer)))
541 (current-buffer))))
543 (defun org-export-ascii-preprocess (parameters)
544 "Do extra work for ASCII export"
546 ;; Realign tables to get rid of narrowing
547 (when org-export-ascii-table-widen-columns
548 (let ((org-table-do-narrow nil))
549 (goto-char (point-min))
550 (org-ascii-replace-entities)
551 (goto-char (point-min))
552 (org-table-map-tables
553 (lambda ()
554 (org-if-unprotected
555 (org-table-align))))))
556 ;; Put quotes around verbatim text
557 (goto-char (point-min))
558 (while (re-search-forward org-verbatim-re nil t)
559 (org-if-unprotected-at (match-beginning 4)
560 (goto-char (match-end 2))
561 (backward-delete-char 1) (insert "'")
562 (goto-char (match-beginning 2))
563 (delete-char 1) (insert "`")
564 (goto-char (match-end 2))))
565 ;; Remove target markers
566 (goto-char (point-min))
567 (while (re-search-forward "<<<?\\([^<>]*\\)>>>?\\([ \t]*\\)" nil t)
568 (org-if-unprotected-at (match-beginning 1)
569 (replace-match "\\1\\2"))))
571 (defun org-html-expand-for-ascii (line)
572 "Handle quoted HTML for ASCII export."
573 (if org-export-html-expand
574 (while (string-match "@<[^<>\n]*>" line)
575 ;; We just remove the tags for now.
576 (setq line (replace-match "" nil nil line))))
577 line)
579 (defun org-ascii-replace-entities ()
580 "Replace entities with the ASCII representation."
581 (let (e)
582 (while (re-search-forward "\\\\\\([a-zA-Z]+[0-9]*\\)" nil t)
583 (org-if-unprotected-at (match-beginning 1)
584 (setq e (org-entity-get-representation (match-string 1)
585 org-export-ascii-entities))
586 (and e (replace-match e t t))))))
588 (defun org-export-ascii-wrap (line where)
589 "Wrap LINE at or before WHERE."
590 (let ((ind (org-get-indentation line))
591 pos)
592 (catch 'found
593 (loop for i from where downto (/ where 2) do
594 (and (equal (aref line i) ?\ )
595 (setq pos i)
596 (throw 'found t))))
597 (if pos
598 (concat (substring line 0 pos) "\n"
599 (make-string ind ?\ )
600 (substring line (1+ pos)))
601 line)))
603 (defun org-export-ascii-push-links (link-buffer)
604 "Push out links in the buffer."
605 (when link-buffer
606 ;; We still have links to push out.
607 (insert "\n")
608 (let ((ind ""))
609 (save-match-data
610 (if (save-excursion
611 (re-search-backward
612 "^\\(\\([ \t]*\\)\\|\\(\\*+ \\)\\)[^ \t\n]" nil t))
613 (setq ind (or (match-string 2)
614 (make-string (length (match-string 3)) ?\ )))))
615 (mapc (lambda (x) (insert ind "[" (car x) "]: " (cdr x) "\n"))
616 link-buffer))
617 (insert "\n")))
619 (defun org-ascii-level-start (level title umax &optional lines)
620 "Insert a new level in ASCII export."
621 (let (char (n (- level umax 1)) (ind 0))
622 (if (> level umax)
623 (progn
624 (insert (make-string (* 2 n) ?\ )
625 (char-to-string (nth (% n (length org-export-ascii-bullets))
626 org-export-ascii-bullets))
627 " " title "\n")
628 ;; find the indentation of the next non-empty line
629 (catch 'stop
630 (while lines
631 (if (string-match "^\\* " (car lines)) (throw 'stop nil))
632 (if (string-match "^\\([ \t]*\\)\\S-" (car lines))
633 (throw 'stop (setq ind (org-get-indentation (car lines)))))
634 (pop lines)))
635 (setq org-ascii-current-indentation (cons (* 2 (1+ n)) ind)))
636 (if (or (not (equal (char-before) ?\n))
637 (not (equal (char-before (1- (point))) ?\n)))
638 (insert "\n"))
639 (setq char (nth (- umax level) (reverse org-export-ascii-underline)))
640 (unless org-export-with-tags
641 (if (string-match (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
642 (setq title (replace-match "" t t title))))
643 (if org-export-with-section-numbers
644 (setq title (concat (org-section-number level) " " title)))
645 (insert title "\n" (make-string (string-width title) char) "\n")
646 (setq org-ascii-current-indentation '(0 . 0)))))
648 (defun org-insert-centered (s &optional underline)
649 "Insert the string S centered and underline it with character UNDERLINE."
650 (let ((ind (max (/ (- fill-column (string-width s)) 2) 0)))
651 (insert (make-string ind ?\ ) s "\n")
652 (if underline
653 (insert (make-string ind ?\ )
654 (make-string (string-width s) underline)
655 "\n"))))
657 (defvar org-table-colgroup-info nil)
658 (defun org-format-table-ascii (lines)
659 "Format a table for ascii export."
660 (if (stringp lines)
661 (setq lines (org-split-string lines "\n")))
662 (if (not (string-match "^[ \t]*|" (car lines)))
663 ;; Table made by table.el - test for spanning
664 lines
666 ;; A normal org table
667 ;; Get rid of hlines at beginning and end
668 (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
669 (setq lines (nreverse lines))
670 (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
671 (setq lines (nreverse lines))
672 (when org-export-table-remove-special-lines
673 ;; Check if the table has a marking column. If yes remove the
674 ;; column and the special lines
675 (setq lines (org-table-clean-before-export lines)))
676 ;; Get rid of the vertical lines except for grouping
677 (if org-export-ascii-table-keep-all-vertical-lines
678 lines
679 (let ((vl (org-colgroup-info-to-vline-list org-table-colgroup-info))
680 rtn line vl1 start)
681 (while (setq line (pop lines))
682 (if (string-match org-table-hline-regexp line)
683 (and (string-match "|\\(.*\\)|" line)
684 (setq line (replace-match " \\1" t nil line)))
685 (setq start 0 vl1 vl)
686 (while (string-match "|" line start)
687 (setq start (match-end 0))
688 (or (pop vl1) (setq line (replace-match " " t t line)))))
689 (push line rtn))
690 (nreverse rtn)))))
692 (defun org-colgroup-info-to-vline-list (info)
693 (let (vl new last)
694 (while info
695 (setq last new new (pop info))
696 (if (or (memq last '(:end :startend))
697 (memq new '(:start :startend)))
698 (push t vl)
699 (push nil vl)))
700 (setq vl (nreverse vl))
701 (and vl (setcar vl nil))
702 vl))
704 (provide 'org-ascii)
706 ;; arch-tag: aa96f882-f477-4e13-86f5-70d43e7adf3c
707 ;;; org-ascii.el ends here