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