zap until-condition docs
[emacs.git] / lisp / org / org-ascii.el
blobc5a4b3775e8e616e3497dca818aa32c529f78a62
1 ;;; org-ascii.el --- ASCII export for Org-mode
3 ;; Copyright (C) 2004-2013 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
8 ;;
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
25 ;;; Commentary:
27 ;;; Code:
29 (require 'org-exp)
31 (eval-when-compile
32 (require 'cl))
34 (defgroup org-export-ascii nil
35 "Options specific for ASCII export of Org-mode files."
36 :tag "Org Export ASCII"
37 :group 'org-export)
39 (defcustom org-export-ascii-underline '(?\= ?\- ?\~ ?\^ ?\. ?\# ?\$)
40 "Characters for underlining headings in ASCII export.
41 In the given sequence, these characters will be used for level 1, 2, ..."
42 :group 'org-export-ascii
43 :type '(repeat character))
45 (defcustom org-export-ascii-bullets '(?* ?+ ?-)
46 "Bullet characters for headlines converted to lists in ASCII export.
47 The first character is used for the first lest level generated in this
48 way, and so on. If there are more levels than characters given here,
49 the list will be repeated.
50 Note that plain lists will keep the same bullets as the have in the
51 Org-mode file."
52 :group 'org-export-ascii
53 :type '(repeat character))
55 (defcustom org-export-ascii-links-to-notes t
56 "Non-nil means convert links to notes before the next headline.
57 When nil, the link will be exported in place. If the line becomes long
58 in this way, it will be wrapped."
59 :group 'org-export-ascii
60 :type 'boolean)
62 (defcustom org-export-ascii-table-keep-all-vertical-lines nil
63 "Non-nil means keep all vertical lines in ASCII tables.
64 When nil, vertical lines will be removed except for those needed
65 for column grouping."
66 :group 'org-export-ascii
67 :type 'boolean)
69 (defcustom org-export-ascii-table-widen-columns t
70 "Non-nil means widen narrowed columns for export.
71 When nil, narrowed columns will look in ASCII export just like in org-mode,
72 i.e. with \"=>\" as ellipsis."
73 :group 'org-export-ascii
74 :type 'boolean)
76 (defvar org-export-ascii-entities 'ascii
77 "The ascii representation to be used during ascii export.
78 Possible values are:
80 ascii Only use plain ASCII characters
81 latin1 Include Latin-1 character
82 utf8 Use all UTF-8 characters")
84 ;;; Hooks
86 (defvar org-export-ascii-final-hook nil
87 "Hook run at the end of ASCII export, in the new buffer.")
89 ;;; ASCII export
91 (defvar org-ascii-current-indentation nil) ; For communication
93 ;;;###autoload
94 (defun org-export-as-latin1 (&rest args)
95 "Like `org-export-as-ascii', use latin1 encoding for special symbols."
96 (interactive)
97 (org-export-as-encoding 'org-export-as-ascii (org-called-interactively-p 'any)
98 'latin1 args))
100 ;;;###autoload
101 (defun org-export-as-latin1-to-buffer (&rest args)
102 "Like `org-export-as-ascii-to-buffer', use latin1 encoding for symbols."
103 (interactive)
104 (org-export-as-encoding 'org-export-as-ascii-to-buffer
105 (org-called-interactively-p 'any) 'latin1 args))
107 ;;;###autoload
108 (defun org-export-as-utf8 (&rest args)
109 "Like `org-export-as-ascii', use encoding for special symbols."
110 (interactive)
111 (org-export-as-encoding 'org-export-as-ascii
112 (org-called-interactively-p 'any)
113 'utf8 args))
115 ;;;###autoload
116 (defun org-export-as-utf8-to-buffer (&rest args)
117 "Like `org-export-as-ascii-to-buffer', use utf8 encoding for symbols."
118 (interactive)
119 (org-export-as-encoding 'org-export-as-ascii-to-buffer
120 (org-called-interactively-p 'any) 'utf8 args))
122 (defun org-export-as-encoding (command interactivep encoding &rest args)
123 (let ((org-export-ascii-entities encoding))
124 (if interactivep
125 (call-interactively command)
126 (apply command args))))
129 ;;;###autoload
130 (defun org-export-as-ascii-to-buffer (arg)
131 "Call `org-export-as-ascii` with output to a temporary buffer.
132 No file is created. The prefix ARG is passed through to `org-export-as-ascii'."
133 (interactive "P")
134 (org-export-as-ascii arg nil "*Org ASCII Export*")
135 (when org-export-show-temporary-export-buffer
136 (switch-to-buffer-other-window "*Org ASCII Export*")))
138 ;;;###autoload
139 (defun org-replace-region-by-ascii (beg end)
140 "Assume the current region has org-mode syntax, and convert it to plain ASCII.
141 This can be used in any buffer. For example, you could write an
142 itemized list in org-mode syntax in a Mail buffer and then use this
143 command to convert it."
144 (interactive "r")
145 (let (reg ascii buf pop-up-frames)
146 (save-window-excursion
147 (if (derived-mode-p 'org-mode)
148 (setq ascii (org-export-region-as-ascii
149 beg end t 'string))
150 (setq reg (buffer-substring beg end)
151 buf (get-buffer-create "*Org tmp*"))
152 (with-current-buffer buf
153 (erase-buffer)
154 (insert reg)
155 (org-mode)
156 (setq ascii (org-export-region-as-ascii
157 (point-min) (point-max) t 'string)))
158 (kill-buffer buf)))
159 (delete-region beg end)
160 (insert ascii)))
162 ;;;###autoload
163 (defun org-export-region-as-ascii (beg end &optional body-only buffer)
164 "Convert region from BEG to END in org-mode buffer to plain ASCII.
165 If prefix arg BODY-ONLY is set, omit file header, footer, and table of
166 contents, and only produce the region of converted text, useful for
167 cut-and-paste operations.
168 If BUFFER is a buffer or a string, use/create that buffer as a target
169 of the converted ASCII. If BUFFER is the symbol `string', return the
170 produced ASCII as a string and leave not buffer behind. For example,
171 a Lisp program could call this function in the following way:
173 (setq ascii (org-export-region-as-ascii beg end t 'string))
175 When called interactively, the output buffer is selected, and shown
176 in a window. A non-interactive call will only return the buffer."
177 (interactive "r\nP")
178 (when (org-called-interactively-p 'any)
179 (setq buffer "*Org ASCII Export*"))
180 (let ((transient-mark-mode t) (zmacs-regions t)
181 ext-plist rtn)
182 (setq ext-plist (plist-put ext-plist :ignore-subtree-p t))
183 (goto-char end)
184 (set-mark (point)) ;; to activate the region
185 (goto-char beg)
186 (setq rtn (org-export-as-ascii nil ext-plist buffer body-only))
187 (if (fboundp 'deactivate-mark) (deactivate-mark))
188 (if (and (org-called-interactively-p 'any) (bufferp rtn))
189 (switch-to-buffer-other-window rtn)
190 rtn)))
192 ;;;###autoload
193 (defun org-export-as-ascii (arg &optional ext-plist to-buffer body-only pub-dir)
194 "Export the outline as a pretty ASCII file.
195 If there is an active region, export only the region.
196 The prefix ARG specifies how many levels of the outline should become
197 underlined headlines, default is 3. Lower levels will become bulleted
198 lists. EXT-PLIST is a property list with external parameters overriding
199 org-mode's default settings, but still inferior to file-local
200 settings. When TO-BUFFER is non-nil, create a buffer with that
201 name and export to that buffer. If TO-BUFFER is the symbol
202 `string', don't leave any buffer behind but just return the
203 resulting ASCII as a string. When BODY-ONLY is set, don't produce
204 the file header and footer. When PUB-DIR is set, use this as the
205 publishing directory."
206 (interactive "P")
207 (run-hooks 'org-export-first-hook)
208 (setq-default org-todo-line-regexp org-todo-line-regexp)
209 (let* ((opt-plist (org-combine-plists (org-default-export-plist)
210 ext-plist
211 (org-infile-export-plist)))
212 (region-p (org-region-active-p))
213 (rbeg (and region-p (region-beginning)))
214 (rend (and region-p (region-end)))
215 (subtree-p
216 (if (plist-get opt-plist :ignore-subtree-p)
218 (when region-p
219 (save-excursion
220 (goto-char rbeg)
221 (and (org-at-heading-p)
222 (>= (org-end-of-subtree t t) rend))))))
223 (level-offset (if subtree-p
224 (save-excursion
225 (goto-char rbeg)
226 (+ (funcall outline-level)
227 (if org-odd-levels-only 1 0)))
229 (opt-plist (setq org-export-opt-plist
230 (if subtree-p
231 (org-export-add-subtree-options opt-plist rbeg)
232 opt-plist)))
233 ;; The following two are dynamically scoped into other
234 ;; routines below.
235 (org-current-export-dir
236 (or pub-dir (org-export-directory :html opt-plist)))
237 (org-current-export-file buffer-file-name)
238 (custom-times org-display-custom-times)
239 (org-ascii-current-indentation '(0 . 0))
240 (level 0) line txt
241 (umax nil)
242 (umax-toc nil)
243 (case-fold-search nil)
244 (bfname (buffer-file-name (or (buffer-base-buffer) (current-buffer))))
245 (filename (if to-buffer
247 (concat (file-name-as-directory
248 (or pub-dir
249 (org-export-directory :ascii opt-plist)))
250 (file-name-sans-extension
251 (or (and subtree-p
252 (org-entry-get (region-beginning)
253 "EXPORT_FILE_NAME" t))
254 (file-name-nondirectory bfname)))
255 ".txt")))
256 (filename (and filename
257 (if (equal (file-truename filename)
258 (file-truename bfname))
259 (concat filename ".txt")
260 filename)))
261 (buffer (if to-buffer
262 (cond
263 ((eq to-buffer 'string)
264 (get-buffer-create "*Org ASCII Export*"))
265 (t (get-buffer-create to-buffer)))
266 (find-file-noselect filename)))
267 (org-levels-open (make-vector org-level-max nil))
268 (odd org-odd-levels-only)
269 (date (plist-get opt-plist :date))
270 (author (plist-get opt-plist :author))
271 (title (or (and subtree-p (org-export-get-title-from-subtree))
272 (plist-get opt-plist :title)
273 (and (not
274 (plist-get opt-plist :skip-before-1st-heading))
275 (org-export-grab-title-from-buffer))
276 (and (buffer-file-name)
277 (file-name-sans-extension
278 (file-name-nondirectory bfname)))
279 "UNTITLED"))
280 (email (plist-get opt-plist :email))
281 (language (plist-get opt-plist :language))
282 (quote-re0 (concat "^\\(" org-quote-string "\\)\\( +\\|[ \t]*$\\)"))
283 (todo nil)
284 (lang-words nil)
285 (region
286 (buffer-substring
287 (if (org-region-active-p) (region-beginning) (point-min))
288 (if (org-region-active-p) (region-end) (point-max))))
289 (org-export-footnotes-seen nil)
290 (org-export-footnotes-data (org-footnote-all-labels 'with-defs))
291 (lines (org-split-string
292 (org-export-preprocess-string
293 region
294 :for-backend 'ascii
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 :tasks (plist-get opt-plist :tasks)
304 :verbatim-multiline t
305 :select-tags (plist-get opt-plist :select-tags)
306 :exclude-tags (plist-get opt-plist :exclude-tags)
307 :archived-trees
308 (plist-get opt-plist :archived-trees)
309 :add-text (plist-get opt-plist :text))
310 "\n"))
311 thetoc have-headings first-heading-pos
312 table-open table-buffer link-buffer link type path desc desc0 rpl wrap fnc)
313 (let ((inhibit-read-only t))
314 (org-unmodified
315 (remove-text-properties (point-min) (point-max)
316 '(:org-license-to-kill t))))
318 (setq org-min-level (org-get-min-level lines level-offset))
319 (setq org-last-level org-min-level)
320 (org-init-section-numbers)
321 (setq lang-words (or (assoc language org-export-language-setup)
322 (assoc "en" org-export-language-setup)))
323 (set-buffer buffer)
324 (erase-buffer)
325 (fundamental-mode)
326 (org-install-letbind)
327 ;; create local variables for all options, to make sure all called
328 ;; functions get the correct information
329 (mapc (lambda (x)
330 (set (make-local-variable (nth 2 x))
331 (plist-get opt-plist (car x))))
332 org-export-plist-vars)
333 (org-set-local 'org-odd-levels-only odd)
334 (setq umax (if arg (prefix-numeric-value arg)
335 org-export-headline-levels))
336 (setq umax-toc (if (integerp org-export-with-toc)
337 (min org-export-with-toc umax)
338 umax))
340 ;; File header
341 (unless body-only
342 (when (and title (not (string= "" title)))
343 (org-insert-centered title ?=)
344 (insert "\n"))
346 (if (and (or author email)
347 org-export-author-info)
348 (insert (concat (nth 1 lang-words) ": " (or author "")
349 (if (and org-export-email-info
350 email (string-match "\\S-" email))
351 (concat " <" email ">") "")
352 "\n")))
354 (cond
355 ((and date (string-match "%" date))
356 (setq date (format-time-string date)))
357 (date)
358 (t (setq date (format-time-string "%Y-%m-%d %T %Z"))))
360 (if (and date org-export-time-stamp-file)
361 (insert (concat (nth 2 lang-words) ": " date"\n")))
363 (unless (= (point) (point-min))
364 (insert "\n\n")))
366 (if (and org-export-with-toc (not body-only))
367 (progn
368 (push (concat (nth 3 lang-words) "\n") thetoc)
369 (push (concat (make-string (string-width (nth 3 lang-words)) ?=)
370 "\n") thetoc)
371 (mapc #'(lambda (line)
372 (if (string-match org-todo-line-regexp
373 line)
374 ;; This is a headline
375 (progn
376 (setq have-headings t)
377 (setq level (- (match-end 1) (match-beginning 1)
378 level-offset)
379 level (org-tr-level level)
380 txt (match-string 3 line)
381 todo
382 (or (and org-export-mark-todo-in-toc
383 (match-beginning 2)
384 (not (member (match-string 2 line)
385 org-done-keywords)))
386 ; TODO, not DONE
387 (and org-export-mark-todo-in-toc
388 (= level umax-toc)
389 (org-search-todo-below
390 line lines level))))
391 (setq txt (org-html-expand-for-ascii txt))
393 (while (string-match org-bracket-link-regexp txt)
394 (setq txt
395 (replace-match
396 (match-string (if (match-end 2) 3 1) txt)
397 t t txt)))
399 (if (and (memq org-export-with-tags '(not-in-toc nil))
400 (string-match
401 (org-re "[ \t]+:[[:alnum:]_@#%:]+:[ \t]*$")
402 txt))
403 (setq txt (replace-match "" t t txt)))
404 (if (string-match quote-re0 txt)
405 (setq txt (replace-match "" t t txt 1)))
407 (if org-export-with-section-numbers
408 (setq txt (concat (org-section-number level)
409 " " txt)))
410 (if (<= level umax-toc)
411 (progn
412 (push
413 (concat
414 (make-string
415 (* (max 0 (- level org-min-level)) 4) ?\ )
416 (format (if todo "%s (*)\n" "%s\n") txt))
417 thetoc)
418 (setq org-last-level level))
419 ))))
420 lines)
421 (setq thetoc (if have-headings (nreverse thetoc) nil))))
423 (org-init-section-numbers)
424 (while (setq line (pop lines))
425 (when (and link-buffer (string-match org-outline-regexp-bol line))
426 (org-export-ascii-push-links (nreverse link-buffer))
427 (setq link-buffer nil))
428 (setq wrap nil)
429 ;; Remove the quoted HTML tags.
430 (setq line (org-html-expand-for-ascii line))
431 ;; Replace links with the description when possible
432 (while (string-match org-bracket-link-analytic-regexp++ line)
433 (setq path (match-string 3 line)
434 link (concat (match-string 1 line) path)
435 type (match-string 2 line)
436 desc0 (match-string 5 line)
437 desc0 (replace-regexp-in-string "\\\\_" "_" desc0)
438 desc (or desc0 link)
439 desc (replace-regexp-in-string "\\\\_" "_" desc))
440 (if (and (> (length link) 8)
441 (equal (substring link 0 8) "coderef:"))
442 (setq line (replace-match
443 (format (org-export-get-coderef-format (substring link 8) desc)
444 (cdr (assoc
445 (substring link 8)
446 org-export-code-refs)))
447 t t line))
448 (setq rpl (concat "[" desc "]"))
449 (if (functionp (setq fnc (nth 2 (assoc type org-link-protocols))))
450 (setq rpl (or (save-match-data
451 (funcall fnc (org-link-unescape path)
452 desc0 'ascii))
453 rpl))
454 (when (and desc0 (not (equal desc0 link)))
455 (if org-export-ascii-links-to-notes
456 (push (cons desc0 link) link-buffer)
457 (setq rpl (concat rpl " (" link ")")
458 wrap (+ (length line) (- (length (match-string 0 line)))
459 (length desc))))))
460 (setq line (replace-match rpl t t line))))
461 (when custom-times
462 (setq line (org-translate-time line)))
463 (cond
464 ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
465 ;; a Headline
466 (setq first-heading-pos (or first-heading-pos (point)))
467 (setq level (org-tr-level (- (match-end 1) (match-beginning 1)
468 level-offset))
469 txt (match-string 2 line))
470 (org-ascii-level-start level txt umax lines))
472 ((and org-export-with-tables
473 (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
474 (if (not table-open)
475 ;; New table starts
476 (setq table-open t table-buffer nil))
477 ;; Accumulate lines
478 (setq table-buffer (cons line table-buffer))
479 (when (or (not lines)
480 (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
481 (car lines))))
482 (setq table-open nil
483 table-buffer (nreverse table-buffer))
484 (insert (mapconcat
485 (lambda (x)
486 (org-fix-indentation x org-ascii-current-indentation))
487 (org-format-table-ascii table-buffer)
488 "\n") "\n")))
490 (if (string-match "^\\([ \t]*\\)\\([-+*][ \t]+\\)\\(.*?\\)\\( ::\\)"
491 line)
492 (setq line (replace-match "\\1\\3:" t nil line)))
493 (setq line (org-fix-indentation line org-ascii-current-indentation))
494 ;; Remove forced line breaks
495 (if (string-match "\\\\\\\\[ \t]*$" line)
496 (setq line (replace-match "" t t line)))
497 (if (and org-export-with-fixed-width
498 (string-match "^\\([ \t]*\\)\\(:\\( \\|$\\)\\)" line))
499 (setq line (replace-match "\\1" nil nil line))
500 (if wrap (setq line (org-export-ascii-wrap line wrap))))
501 (insert line "\n"))))
503 (org-export-ascii-push-links (nreverse link-buffer))
505 (normal-mode)
507 ;; insert the table of contents
508 (when thetoc
509 (goto-char (point-min))
510 (if (re-search-forward "^[ \t]*\\[TABLE-OF-CONTENTS\\][ \t]*$" nil t)
511 (progn
512 (goto-char (match-beginning 0))
513 (replace-match ""))
514 (goto-char first-heading-pos))
515 (mapc 'insert thetoc)
516 (or (looking-at "[ \t]*\n[ \t]*\n")
517 (insert "\n\n")))
519 ;; Convert whitespace place holders
520 (goto-char (point-min))
521 (let (beg end)
522 (while (setq beg (next-single-property-change (point) 'org-whitespace))
523 (setq end (next-single-property-change beg 'org-whitespace))
524 (goto-char beg)
525 (delete-region beg end)
526 (insert (make-string (- end beg) ?\ ))))
528 ;; remove display and invisible chars
529 (let (beg end)
530 (goto-char (point-min))
531 (while (setq beg (next-single-property-change (point) 'display))
532 (setq end (next-single-property-change beg 'display))
533 (delete-region beg end)
534 (goto-char beg)
535 (insert "=>"))
536 (goto-char (point-min))
537 (while (setq beg (next-single-property-change (point) 'org-cwidth))
538 (setq end (next-single-property-change beg 'org-cwidth))
539 (delete-region beg end)
540 (goto-char beg)))
541 (run-hooks 'org-export-ascii-final-hook)
542 (or to-buffer (save-buffer))
543 (goto-char (point-min))
544 (or (org-export-push-to-kill-ring "ASCII")
545 (message "Exporting... done"))
546 ;; Return the buffer or a string, according to how this function was called
547 (if (eq to-buffer 'string)
548 (prog1 (buffer-substring (point-min) (point-max))
549 (kill-buffer (current-buffer)))
550 (current-buffer))))
552 ;;;###autoload
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 (concat "^\\(\\([ \t]*\\)\\|\\("
630 org-outline-regexp
631 "\\)\\)[^ \t\n]") nil t))
632 (setq ind (or (match-string 2)
633 (make-string (length (match-string 3)) ?\ )))))
634 (mapc (lambda (x) (insert ind "[" (car x) "]: " (cdr x) "\n"))
635 link-buffer))
636 (insert "\n")))
638 (defun org-ascii-level-start (level title umax &optional lines)
639 "Insert a new level in ASCII export."
640 (let (char (n (- level umax 1)) (ind 0))
641 (if (> level umax)
642 (progn
643 (insert (make-string (* 2 n) ?\ )
644 (char-to-string (nth (% n (length org-export-ascii-bullets))
645 org-export-ascii-bullets))
646 " " title "\n")
647 ;; find the indentation of the next non-empty line
648 (catch 'stop
649 (while lines
650 (if (string-match "^\\* " (car lines)) (throw 'stop nil))
651 (if (string-match "^\\([ \t]*\\)\\S-" (car lines))
652 (throw 'stop (setq ind (org-get-indentation (car lines)))))
653 (pop lines)))
654 (setq org-ascii-current-indentation (cons (* 2 (1+ n)) ind)))
655 (if (or (not (equal (char-before) ?\n))
656 (not (equal (char-before (1- (point))) ?\n)))
657 (insert "\n"))
658 (setq char (or (nth (1- level) org-export-ascii-underline)
659 (car (last org-export-ascii-underline))))
660 (unless org-export-with-tags
661 (if (string-match (org-re "[ \t]+\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$") title)
662 (setq title (replace-match "" t t title))))
663 (if org-export-with-section-numbers
664 (setq title (concat (org-section-number level) " " title)))
665 (insert title "\n" (make-string (string-width title) char) "\n")
666 (setq org-ascii-current-indentation '(0 . 0)))))
668 (defun org-insert-centered (s &optional underline)
669 "Insert the string S centered and underline it with character UNDERLINE."
670 (let ((ind (max (/ (- fill-column (string-width s)) 2) 0)))
671 (insert (make-string ind ?\ ) s "\n")
672 (if underline
673 (insert (make-string ind ?\ )
674 (make-string (string-width s) underline)
675 "\n"))))
677 (defvar org-table-colgroup-info nil)
678 (defun org-format-table-ascii (lines)
679 "Format a table for ascii export."
680 (if (stringp lines)
681 (setq lines (org-split-string lines "\n")))
682 (if (not (string-match "^[ \t]*|" (car lines)))
683 ;; Table made by table.el - test for spanning
684 lines
686 ;; A normal org table
687 ;; Get rid of hlines at beginning and end
688 (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
689 (setq lines (nreverse lines))
690 (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
691 (setq lines (nreverse lines))
692 (when org-export-table-remove-special-lines
693 ;; Check if the table has a marking column. If yes remove the
694 ;; column and the special lines
695 (setq lines (org-table-clean-before-export lines)))
696 ;; Get rid of the vertical lines except for grouping
697 (if org-export-ascii-table-keep-all-vertical-lines
698 lines
699 (let ((vl (org-colgroup-info-to-vline-list org-table-colgroup-info))
700 rtn line vl1 start)
701 (while (setq line (pop lines))
702 (if (string-match org-table-hline-regexp line)
703 (and (string-match "|\\(.*\\)|" line)
704 (setq line (replace-match " \\1" t nil line)))
705 (setq start 0 vl1 vl)
706 (while (string-match "|" line start)
707 (setq start (match-end 0))
708 (or (pop vl1) (setq line (replace-match " " t t line)))))
709 (push line rtn))
710 (nreverse rtn)))))
712 (defun org-colgroup-info-to-vline-list (info)
713 (let (vl new last)
714 (while info
715 (setq last new new (pop info))
716 (if (or (memq last '(:end :startend))
717 (memq new '(:start :startend)))
718 (push t vl)
719 (push nil vl)))
720 (setq vl (nreverse vl))
721 (and vl (setcar vl nil))
722 vl))
724 (provide 'org-ascii)
726 ;; Local variables:
727 ;; generated-autoload-file: "org-loaddefs.el"
728 ;; End:
730 ;;; org-ascii.el ends here