org-e-publish: Fix publishing for files not directly in :base-directory
[org-mode.git] / lisp / org-ascii.el
blob575b830f2c07f7d87472ad4d814b72b04c2b9c31
1 ;;; org-ascii.el --- ASCII export for Org-mode
3 ;; Copyright (C) 2004-2012 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 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
187 nil nil ext-plist
188 buffer body-only))
189 (if (fboundp 'deactivate-mark) (deactivate-mark))
190 (if (and (org-called-interactively-p 'any) (bufferp rtn))
191 (switch-to-buffer-other-window rtn)
192 rtn)))
194 ;;;###autoload
195 (defun org-export-as-ascii (arg &optional hidden ext-plist
196 to-buffer body-only pub-dir)
197 "Export the outline as a pretty ASCII file.
198 If there is an active region, export only the region.
199 The prefix ARG specifies how many levels of the outline should become
200 underlined headlines, default is 3. Lower levels will become bulleted
201 lists. When HIDDEN is non-nil, don't display the ASCII buffer.
202 EXT-PLIST is a property list with external parameters overriding
203 org-mode's default settings, but still inferior to file-local
204 settings. When TO-BUFFER is non-nil, create a buffer with that
205 name and export to that buffer. If TO-BUFFER is the symbol
206 `string', don't leave any buffer behind but just return the
207 resulting ASCII as a string. When BODY-ONLY is set, don't produce
208 the file header and footer. When PUB-DIR is set, use this as the
209 publishing directory."
210 (interactive "P")
211 (run-hooks 'org-export-first-hook)
212 (setq-default org-todo-line-regexp org-todo-line-regexp)
213 (let* ((opt-plist (org-combine-plists (org-default-export-plist)
214 ext-plist
215 (org-infile-export-plist)))
216 (region-p (org-region-active-p))
217 (rbeg (and region-p (region-beginning)))
218 (rend (and region-p (region-end)))
219 (subtree-p
220 (if (plist-get opt-plist :ignore-subtree-p)
222 (when region-p
223 (save-excursion
224 (goto-char rbeg)
225 (and (org-at-heading-p)
226 (>= (org-end-of-subtree t t) rend))))))
227 (level-offset (if subtree-p
228 (save-excursion
229 (goto-char rbeg)
230 (+ (funcall outline-level)
231 (if org-odd-levels-only 1 0)))
233 (opt-plist (setq org-export-opt-plist
234 (if subtree-p
235 (org-export-add-subtree-options opt-plist rbeg)
236 opt-plist)))
237 ;; The following two are dynamically scoped into other
238 ;; routines below.
239 (org-current-export-dir
240 (or pub-dir (org-export-directory :html opt-plist)))
241 (org-current-export-file buffer-file-name)
242 (custom-times org-display-custom-times)
243 (org-ascii-current-indentation '(0 . 0))
244 (level 0) line txt
245 (umax nil)
246 (umax-toc nil)
247 (case-fold-search nil)
248 (bfname (buffer-file-name (or (buffer-base-buffer) (current-buffer))))
249 (filename (if to-buffer
251 (concat (file-name-as-directory
252 (or pub-dir
253 (org-export-directory :ascii opt-plist)))
254 (file-name-sans-extension
255 (or (and subtree-p
256 (org-entry-get (region-beginning)
257 "EXPORT_FILE_NAME" t))
258 (file-name-nondirectory bfname)))
259 ".txt")))
260 (filename (and filename
261 (if (equal (file-truename filename)
262 (file-truename bfname))
263 (concat filename ".txt")
264 filename)))
265 (buffer (if to-buffer
266 (cond
267 ((eq to-buffer 'string)
268 (get-buffer-create "*Org ASCII Export*"))
269 (t (get-buffer-create to-buffer)))
270 (find-file-noselect filename)))
271 (org-levels-open (make-vector org-level-max nil))
272 (odd org-odd-levels-only)
273 (date (plist-get opt-plist :date))
274 (author (plist-get opt-plist :author))
275 (title (or (and subtree-p (org-export-get-title-from-subtree))
276 (plist-get opt-plist :title)
277 (and (not
278 (plist-get opt-plist :skip-before-1st-heading))
279 (org-export-grab-title-from-buffer))
280 (and (buffer-file-name)
281 (file-name-sans-extension
282 (file-name-nondirectory bfname)))
283 "UNTITLED"))
284 (email (plist-get opt-plist :email))
285 (language (plist-get opt-plist :language))
286 (quote-re0 (concat "^\\(" org-quote-string "\\)\\( +\\|[ \t]*$\\)"))
287 (todo nil)
288 (lang-words nil)
289 (region
290 (buffer-substring
291 (if (org-region-active-p) (region-beginning) (point-min))
292 (if (org-region-active-p) (region-end) (point-max))))
293 (org-export-footnotes-seen nil)
294 (org-export-footnotes-data (org-footnote-all-labels 'with-defs))
295 (lines (org-split-string
296 (org-export-preprocess-string
297 region
298 :for-backend 'ascii
299 :skip-before-1st-heading
300 (plist-get opt-plist :skip-before-1st-heading)
301 :drawers (plist-get opt-plist :drawers)
302 :tags (plist-get opt-plist :tags)
303 :priority (plist-get opt-plist :priority)
304 :footnotes (plist-get opt-plist :footnotes)
305 :timestamps (plist-get opt-plist :timestamps)
306 :todo-keywords (plist-get opt-plist :todo-keywords)
307 :tasks (plist-get opt-plist :tasks)
308 :verbatim-multiline t
309 :select-tags (plist-get opt-plist :select-tags)
310 :exclude-tags (plist-get opt-plist :exclude-tags)
311 :archived-trees
312 (plist-get opt-plist :archived-trees)
313 :add-text (plist-get opt-plist :text))
314 "\n"))
315 thetoc have-headings first-heading-pos
316 table-open table-buffer link-buffer link type path desc desc0 rpl wrap fnc)
317 (let ((inhibit-read-only t))
318 (org-unmodified
319 (remove-text-properties (point-min) (point-max)
320 '(:org-license-to-kill t))))
322 (setq org-min-level (org-get-min-level lines level-offset))
323 (setq org-last-level org-min-level)
324 (org-init-section-numbers)
325 (setq lang-words (or (assoc language org-export-language-setup)
326 (assoc "en" org-export-language-setup)))
327 (set-buffer buffer)
328 (erase-buffer)
329 (fundamental-mode)
330 (org-install-letbind)
331 ;; create local variables for all options, to make sure all called
332 ;; functions get the correct information
333 (mapc (lambda (x)
334 (set (make-local-variable (nth 2 x))
335 (plist-get opt-plist (car x))))
336 org-export-plist-vars)
337 (org-set-local 'org-odd-levels-only odd)
338 (setq umax (if arg (prefix-numeric-value arg)
339 org-export-headline-levels))
340 (setq umax-toc (if (integerp org-export-with-toc)
341 (min org-export-with-toc umax)
342 umax))
344 ;; File header
345 (unless body-only
346 (when (and title (not (string= "" title)))
347 (org-insert-centered title ?=)
348 (insert "\n"))
350 (if (and (or author email)
351 org-export-author-info)
352 (insert (concat (nth 1 lang-words) ": " (or author "")
353 (if (and org-export-email-info
354 email (string-match "\\S-" email))
355 (concat " <" email ">") "")
356 "\n")))
358 (cond
359 ((and date (string-match "%" date))
360 (setq date (format-time-string date)))
361 (date)
362 (t (setq date (format-time-string "%Y-%m-%d %T %Z"))))
364 (if (and date org-export-time-stamp-file)
365 (insert (concat (nth 2 lang-words) ": " date"\n")))
367 (unless (= (point) (point-min))
368 (insert "\n\n")))
370 (if (and org-export-with-toc (not body-only))
371 (progn
372 (push (concat (nth 3 lang-words) "\n") thetoc)
373 (push (concat (make-string (string-width (nth 3 lang-words)) ?=)
374 "\n") thetoc)
375 (mapc #'(lambda (line)
376 (if (string-match org-todo-line-regexp
377 line)
378 ;; This is a headline
379 (progn
380 (setq have-headings t)
381 (setq level (- (match-end 1) (match-beginning 1)
382 level-offset)
383 level (org-tr-level level)
384 txt (match-string 3 line)
385 todo
386 (or (and org-export-mark-todo-in-toc
387 (match-beginning 2)
388 (not (member (match-string 2 line)
389 org-done-keywords)))
390 ; TODO, not DONE
391 (and org-export-mark-todo-in-toc
392 (= level umax-toc)
393 (org-search-todo-below
394 line lines level))))
395 (setq txt (org-html-expand-for-ascii txt))
397 (while (string-match org-bracket-link-regexp txt)
398 (setq txt
399 (replace-match
400 (match-string (if (match-end 2) 3 1) txt)
401 t t txt)))
403 (if (and (memq org-export-with-tags '(not-in-toc nil))
404 (string-match
405 (org-re "[ \t]+:[[:alnum:]_@#%:]+:[ \t]*$")
406 txt))
407 (setq txt (replace-match "" t t txt)))
408 (if (string-match quote-re0 txt)
409 (setq txt (replace-match "" t t txt 1)))
411 (if org-export-with-section-numbers
412 (setq txt (concat (org-section-number level)
413 " " txt)))
414 (if (<= level umax-toc)
415 (progn
416 (push
417 (concat
418 (make-string
419 (* (max 0 (- level org-min-level)) 4) ?\ )
420 (format (if todo "%s (*)\n" "%s\n") txt))
421 thetoc)
422 (setq org-last-level level))
423 ))))
424 lines)
425 (setq thetoc (if have-headings (nreverse thetoc) nil))))
427 (org-init-section-numbers)
428 (while (setq line (pop lines))
429 (when (and link-buffer (string-match org-outline-regexp-bol line))
430 (org-export-ascii-push-links (nreverse link-buffer))
431 (setq link-buffer nil))
432 (setq wrap nil)
433 ;; Remove the quoted HTML tags.
434 (setq line (org-html-expand-for-ascii line))
435 ;; Replace links with the description when possible
436 (while (string-match org-bracket-link-analytic-regexp++ line)
437 (setq path (match-string 3 line)
438 link (concat (match-string 1 line) path)
439 type (match-string 2 line)
440 desc0 (match-string 5 line)
441 desc0 (replace-regexp-in-string "\\\\_" "_" desc0)
442 desc (or desc0 link)
443 desc (replace-regexp-in-string "\\\\_" "_" desc))
444 (if (and (> (length link) 8)
445 (equal (substring link 0 8) "coderef:"))
446 (setq line (replace-match
447 (format (org-export-get-coderef-format (substring link 8) desc)
448 (cdr (assoc
449 (substring link 8)
450 org-export-code-refs)))
451 t t line))
452 (setq rpl (concat "[" desc "]"))
453 (if (functionp (setq fnc (nth 2 (assoc type org-link-protocols))))
454 (setq rpl (or (save-match-data
455 (funcall fnc (org-link-unescape path)
456 desc0 'ascii))
457 rpl))
458 (when (and desc0 (not (equal desc0 link)))
459 (if org-export-ascii-links-to-notes
460 (push (cons desc0 link) link-buffer)
461 (setq rpl (concat rpl " (" link ")")
462 wrap (+ (length line) (- (length (match-string 0 line)))
463 (length desc))))))
464 (setq line (replace-match rpl t t line))))
465 (when custom-times
466 (setq line (org-translate-time line)))
467 (cond
468 ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
469 ;; a Headline
470 (setq first-heading-pos (or first-heading-pos (point)))
471 (setq level (org-tr-level (- (match-end 1) (match-beginning 1)
472 level-offset))
473 txt (match-string 2 line))
474 (org-ascii-level-start level txt umax lines))
476 ((and org-export-with-tables
477 (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
478 (if (not table-open)
479 ;; New table starts
480 (setq table-open t table-buffer nil))
481 ;; Accumulate lines
482 (setq table-buffer (cons line table-buffer))
483 (when (or (not lines)
484 (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
485 (car lines))))
486 (setq table-open nil
487 table-buffer (nreverse table-buffer))
488 (insert (mapconcat
489 (lambda (x)
490 (org-fix-indentation x org-ascii-current-indentation))
491 (org-format-table-ascii table-buffer)
492 "\n") "\n")))
494 (if (string-match "^\\([ \t]*\\)\\([-+*][ \t]+\\)\\(.*?\\)\\( ::\\)"
495 line)
496 (setq line (replace-match "\\1\\3:" t nil line)))
497 (setq line (org-fix-indentation line org-ascii-current-indentation))
498 ;; Remove forced line breaks
499 (if (string-match "\\\\\\\\[ \t]*$" line)
500 (setq line (replace-match "" t t line)))
501 (if (and org-export-with-fixed-width
502 (string-match "^\\([ \t]*\\)\\(:\\( \\|$\\)\\)" line))
503 (setq line (replace-match "\\1" nil nil line))
504 (if wrap (setq line (org-export-ascii-wrap line wrap))))
505 (insert line "\n"))))
507 (org-export-ascii-push-links (nreverse link-buffer))
509 (normal-mode)
511 ;; insert the table of contents
512 (when thetoc
513 (goto-char (point-min))
514 (if (re-search-forward "^[ \t]*\\[TABLE-OF-CONTENTS\\][ \t]*$" nil t)
515 (progn
516 (goto-char (match-beginning 0))
517 (replace-match ""))
518 (goto-char first-heading-pos))
519 (mapc 'insert thetoc)
520 (or (looking-at "[ \t]*\n[ \t]*\n")
521 (insert "\n\n")))
523 ;; Convert whitespace place holders
524 (goto-char (point-min))
525 (let (beg end)
526 (while (setq beg (next-single-property-change (point) 'org-whitespace))
527 (setq end (next-single-property-change beg 'org-whitespace))
528 (goto-char beg)
529 (delete-region beg end)
530 (insert (make-string (- end beg) ?\ ))))
532 ;; remove display and invisible chars
533 (let (beg end)
534 (goto-char (point-min))
535 (while (setq beg (next-single-property-change (point) 'display))
536 (setq end (next-single-property-change beg 'display))
537 (delete-region beg end)
538 (goto-char beg)
539 (insert "=>"))
540 (goto-char (point-min))
541 (while (setq beg (next-single-property-change (point) 'org-cwidth))
542 (setq end (next-single-property-change beg 'org-cwidth))
543 (delete-region beg end)
544 (goto-char beg)))
545 (run-hooks 'org-export-ascii-final-hook)
546 (or to-buffer (save-buffer))
547 (goto-char (point-min))
548 (or (org-export-push-to-kill-ring "ASCII")
549 (message "Exporting... done"))
550 ;; Return the buffer or a string, according to how this function was called
551 (if (eq to-buffer 'string)
552 (prog1 (buffer-substring (point-min) (point-max))
553 (kill-buffer (current-buffer)))
554 (current-buffer))))
556 ;;;###autoload
557 (defun org-export-ascii-preprocess (parameters)
558 "Do extra work for ASCII export."
560 ;; Realign tables to get rid of narrowing
561 (when org-export-ascii-table-widen-columns
562 (let ((org-table-do-narrow nil))
563 (goto-char (point-min))
564 (org-ascii-replace-entities)
565 (goto-char (point-min))
566 (org-table-map-tables
567 (lambda () (org-if-unprotected (org-table-align)))
568 'quietly)))
569 ;; Put quotes around verbatim text
570 (goto-char (point-min))
571 (while (re-search-forward org-verbatim-re nil t)
572 (org-if-unprotected-at (match-beginning 4)
573 (goto-char (match-end 2))
574 (backward-delete-char 1) (insert "'")
575 (goto-char (match-beginning 2))
576 (delete-char 1) (insert "`")
577 (goto-char (match-end 2))))
578 ;; Remove target markers
579 (goto-char (point-min))
580 (while (re-search-forward "<<<?\\([^<>]*\\)>>>?\\([ \t]*\\)" nil t)
581 (org-if-unprotected-at (match-beginning 1)
582 (replace-match "\\1\\2")))
583 ;; Remove list start counters
584 (goto-char (point-min))
585 (while (org-list-search-forward
586 "\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*" nil t)
587 (replace-match ""))
588 (remove-text-properties
589 (point-min) (point-max)
590 '(face nil font-lock-fontified nil font-lock-multiline nil line-prefix nil wrap-prefix nil)))
592 (defun org-html-expand-for-ascii (line)
593 "Handle quoted HTML for ASCII export."
594 (if org-export-html-expand
595 (while (string-match "@<[^<>\n]*>" line)
596 ;; We just remove the tags for now.
597 (setq line (replace-match "" nil nil line))))
598 line)
600 (defun org-ascii-replace-entities ()
601 "Replace entities with the ASCII representation."
602 (let (e)
603 (while (re-search-forward "\\\\\\([a-zA-Z]+[0-9]*\\)\\({}\\)?" nil t)
604 (org-if-unprotected-at (match-beginning 1)
605 (setq e (org-entity-get-representation (match-string 1)
606 org-export-ascii-entities))
607 (and e (replace-match e t t))))))
609 (defun org-export-ascii-wrap (line where)
610 "Wrap LINE at or before WHERE."
611 (let ((ind (org-get-indentation line))
612 pos)
613 (catch 'found
614 (loop for i from where downto (/ where 2) do
615 (and (equal (aref line i) ?\ )
616 (setq pos i)
617 (throw 'found t))))
618 (if pos
619 (concat (substring line 0 pos) "\n"
620 (make-string ind ?\ )
621 (substring line (1+ pos)))
622 line)))
624 (defun org-export-ascii-push-links (link-buffer)
625 "Push out links in the buffer."
626 (when link-buffer
627 ;; We still have links to push out.
628 (insert "\n")
629 (let ((ind ""))
630 (save-match-data
631 (if (save-excursion
632 (re-search-backward
633 (concat "^\\(\\([ \t]*\\)\\|\\("
634 org-outline-regexp
635 "\\)\\)[^ \t\n]") nil t))
636 (setq ind (or (match-string 2)
637 (make-string (length (match-string 3)) ?\ )))))
638 (mapc (lambda (x) (insert ind "[" (car x) "]: " (cdr x) "\n"))
639 link-buffer))
640 (insert "\n")))
642 (defun org-ascii-level-start (level title umax &optional lines)
643 "Insert a new level in ASCII export."
644 (let (char (n (- level umax 1)) (ind 0))
645 (if (> level umax)
646 (progn
647 (insert (make-string (* 2 n) ?\ )
648 (char-to-string (nth (% n (length org-export-ascii-bullets))
649 org-export-ascii-bullets))
650 " " title "\n")
651 ;; find the indentation of the next non-empty line
652 (catch 'stop
653 (while lines
654 (if (string-match "^\\* " (car lines)) (throw 'stop nil))
655 (if (string-match "^\\([ \t]*\\)\\S-" (car lines))
656 (throw 'stop (setq ind (org-get-indentation (car lines)))))
657 (pop lines)))
658 (setq org-ascii-current-indentation (cons (* 2 (1+ n)) ind)))
659 (if (or (not (equal (char-before) ?\n))
660 (not (equal (char-before (1- (point))) ?\n)))
661 (insert "\n"))
662 (setq char (or (nth (1- level) org-export-ascii-underline)
663 (car (last org-export-ascii-underline))))
664 (unless org-export-with-tags
665 (if (string-match (org-re "[ \t]+\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$") title)
666 (setq title (replace-match "" t t title))))
667 (if org-export-with-section-numbers
668 (setq title (concat (org-section-number level) " " title)))
669 (insert title "\n" (make-string (string-width title) char) "\n")
670 (setq org-ascii-current-indentation '(0 . 0)))))
672 (defun org-insert-centered (s &optional underline)
673 "Insert the string S centered and underline it with character UNDERLINE."
674 (let ((ind (max (/ (- fill-column (string-width s)) 2) 0)))
675 (insert (make-string ind ?\ ) s "\n")
676 (if underline
677 (insert (make-string ind ?\ )
678 (make-string (string-width s) underline)
679 "\n"))))
681 (defvar org-table-colgroup-info nil)
682 (defun org-format-table-ascii (lines)
683 "Format a table for ascii export."
684 (if (stringp lines)
685 (setq lines (org-split-string lines "\n")))
686 (if (not (string-match "^[ \t]*|" (car lines)))
687 ;; Table made by table.el - test for spanning
688 lines
690 ;; A normal org table
691 ;; Get rid of hlines at beginning and end
692 (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
693 (setq lines (nreverse lines))
694 (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
695 (setq lines (nreverse lines))
696 (when org-export-table-remove-special-lines
697 ;; Check if the table has a marking column. If yes remove the
698 ;; column and the special lines
699 (setq lines (org-table-clean-before-export lines)))
700 ;; Get rid of the vertical lines except for grouping
701 (if org-export-ascii-table-keep-all-vertical-lines
702 lines
703 (let ((vl (org-colgroup-info-to-vline-list org-table-colgroup-info))
704 rtn line vl1 start)
705 (while (setq line (pop lines))
706 (if (string-match org-table-hline-regexp line)
707 (and (string-match "|\\(.*\\)|" line)
708 (setq line (replace-match " \\1" t nil line)))
709 (setq start 0 vl1 vl)
710 (while (string-match "|" line start)
711 (setq start (match-end 0))
712 (or (pop vl1) (setq line (replace-match " " t t line)))))
713 (push line rtn))
714 (nreverse rtn)))))
716 (defun org-colgroup-info-to-vline-list (info)
717 (let (vl new last)
718 (while info
719 (setq last new new (pop info))
720 (if (or (memq last '(:end :startend))
721 (memq new '(:start :startend)))
722 (push t vl)
723 (push nil vl)))
724 (setq vl (nreverse vl))
725 (and vl (setcar vl nil))
726 vl))
728 (provide 'org-ascii)
730 ;; Local variables:
731 ;; generated-autoload-file: "org-loaddefs.el"
732 ;; End:
734 ;;; org-ascii.el ends here