unified stripping of protective commas -- idempotent org-edit-src-code
[org-mode/org-mode-NeilSmithlineMods.git] / lisp / org-ascii.el
blob154133244806e241a9de5c16fc8fa5a63987e54e
1 ;;; org-ascii.el --- ASCII export for Org-mode
3 ;; Copyright (C) 2004-2011 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 (eq major-mode '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 desc (or desc0 link))
442 (if (and (> (length link) 8)
443 (equal (substring link 0 8) "coderef:"))
444 (setq line (replace-match
445 (format (org-export-get-coderef-format (substring link 8) desc)
446 (cdr (assoc
447 (substring link 8)
448 org-export-code-refs)))
449 t t line))
450 (setq rpl (concat "[" desc "]"))
451 (if (functionp (setq fnc (nth 2 (assoc type org-link-protocols))))
452 (setq rpl (or (save-match-data
453 (funcall fnc (org-link-unescape path)
454 desc0 'ascii))
455 rpl))
456 (when (and desc0 (not (equal desc0 link)))
457 (if org-export-ascii-links-to-notes
458 (push (cons desc0 link) link-buffer)
459 (setq rpl (concat rpl " (" link ")")
460 wrap (+ (length line) (- (length (match-string 0 line)))
461 (length desc))))))
462 (setq line (replace-match rpl t t line))))
463 (when custom-times
464 (setq line (org-translate-time line)))
465 (cond
466 ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
467 ;; a Headline
468 (setq first-heading-pos (or first-heading-pos (point)))
469 (setq level (org-tr-level (- (match-end 1) (match-beginning 1)
470 level-offset))
471 txt (match-string 2 line))
472 (org-ascii-level-start level txt umax lines))
474 ((and org-export-with-tables
475 (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
476 (if (not table-open)
477 ;; New table starts
478 (setq table-open t table-buffer nil))
479 ;; Accumulate lines
480 (setq table-buffer (cons line table-buffer))
481 (when (or (not lines)
482 (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
483 (car lines))))
484 (setq table-open nil
485 table-buffer (nreverse table-buffer))
486 (insert (mapconcat
487 (lambda (x)
488 (org-fix-indentation x org-ascii-current-indentation))
489 (org-format-table-ascii table-buffer)
490 "\n") "\n")))
492 (if (string-match "^\\([ \t]*\\)\\([-+*][ \t]+\\)\\(.*?\\)\\( ::\\)"
493 line)
494 (setq line (replace-match "\\1\\3:" t nil line)))
495 (setq line (org-fix-indentation line org-ascii-current-indentation))
496 ;; Remove forced line breaks
497 (if (string-match "\\\\\\\\[ \t]*$" line)
498 (setq line (replace-match "" t t line)))
499 (if (and org-export-with-fixed-width
500 (string-match "^\\([ \t]*\\)\\(:\\( \\|$\\)\\)" line))
501 (setq line (replace-match "\\1" nil nil line))
502 (if wrap (setq line (org-export-ascii-wrap line wrap))))
503 (insert line "\n"))))
505 (org-export-ascii-push-links (nreverse link-buffer))
507 (normal-mode)
509 ;; insert the table of contents
510 (when thetoc
511 (goto-char (point-min))
512 (if (re-search-forward "^[ \t]*\\[TABLE-OF-CONTENTS\\][ \t]*$" nil t)
513 (progn
514 (goto-char (match-beginning 0))
515 (replace-match ""))
516 (goto-char first-heading-pos))
517 (mapc 'insert thetoc)
518 (or (looking-at "[ \t]*\n[ \t]*\n")
519 (insert "\n\n")))
521 ;; Convert whitespace place holders
522 (goto-char (point-min))
523 (let (beg end)
524 (while (setq beg (next-single-property-change (point) 'org-whitespace))
525 (setq end (next-single-property-change beg 'org-whitespace))
526 (goto-char beg)
527 (delete-region beg end)
528 (insert (make-string (- end beg) ?\ ))))
530 ;; remove display and invisible chars
531 (let (beg end)
532 (goto-char (point-min))
533 (while (setq beg (next-single-property-change (point) 'display))
534 (setq end (next-single-property-change beg 'display))
535 (delete-region beg end)
536 (goto-char beg)
537 (insert "=>"))
538 (goto-char (point-min))
539 (while (setq beg (next-single-property-change (point) 'org-cwidth))
540 (setq end (next-single-property-change beg 'org-cwidth))
541 (delete-region beg end)
542 (goto-char beg)))
543 (run-hooks 'org-export-ascii-final-hook)
544 (or to-buffer (save-buffer))
545 (goto-char (point-min))
546 (or (org-export-push-to-kill-ring "ASCII")
547 (message "Exporting... done"))
548 ;; Return the buffer or a string, according to how this function was called
549 (if (eq to-buffer 'string)
550 (prog1 (buffer-substring (point-min) (point-max))
551 (kill-buffer (current-buffer)))
552 (current-buffer))))
554 (defun org-export-ascii-preprocess (parameters)
555 "Do extra work for ASCII export."
557 ;; Realign tables to get rid of narrowing
558 (when org-export-ascii-table-widen-columns
559 (let ((org-table-do-narrow nil))
560 (goto-char (point-min))
561 (org-ascii-replace-entities)
562 (goto-char (point-min))
563 (org-table-map-tables
564 (lambda () (org-if-unprotected (org-table-align)))
565 'quietly)))
566 ;; Put quotes around verbatim text
567 (goto-char (point-min))
568 (while (re-search-forward org-verbatim-re nil t)
569 (org-if-unprotected-at (match-beginning 4)
570 (goto-char (match-end 2))
571 (backward-delete-char 1) (insert "'")
572 (goto-char (match-beginning 2))
573 (delete-char 1) (insert "`")
574 (goto-char (match-end 2))))
575 ;; Remove target markers
576 (goto-char (point-min))
577 (while (re-search-forward "<<<?\\([^<>]*\\)>>>?\\([ \t]*\\)" nil t)
578 (org-if-unprotected-at (match-beginning 1)
579 (replace-match "\\1\\2")))
580 ;; Remove list start counters
581 (goto-char (point-min))
582 (while (org-list-search-forward
583 "\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*" nil t)
584 (replace-match ""))
585 (remove-text-properties
586 (point-min) (point-max)
587 '(face nil font-lock-fontified nil font-lock-multiline nil line-prefix nil wrap-prefix nil)))
589 (defun org-html-expand-for-ascii (line)
590 "Handle quoted HTML for ASCII export."
591 (if org-export-html-expand
592 (while (string-match "@<[^<>\n]*>" line)
593 ;; We just remove the tags for now.
594 (setq line (replace-match "" nil nil line))))
595 line)
597 (defun org-ascii-replace-entities ()
598 "Replace entities with the ASCII representation."
599 (let (e)
600 (while (re-search-forward "\\\\\\([a-zA-Z]+[0-9]*\\)\\({}\\)?" nil t)
601 (org-if-unprotected-at (match-beginning 1)
602 (setq e (org-entity-get-representation (match-string 1)
603 org-export-ascii-entities))
604 (and e (replace-match e t t))))))
606 (defun org-export-ascii-wrap (line where)
607 "Wrap LINE at or before WHERE."
608 (let ((ind (org-get-indentation line))
609 pos)
610 (catch 'found
611 (loop for i from where downto (/ where 2) do
612 (and (equal (aref line i) ?\ )
613 (setq pos i)
614 (throw 'found t))))
615 (if pos
616 (concat (substring line 0 pos) "\n"
617 (make-string ind ?\ )
618 (substring line (1+ pos)))
619 line)))
621 (defun org-export-ascii-push-links (link-buffer)
622 "Push out links in the buffer."
623 (when link-buffer
624 ;; We still have links to push out.
625 (insert "\n")
626 (let ((ind ""))
627 (save-match-data
628 (if (save-excursion
629 (re-search-backward
630 (concat "^\\(\\([ \t]*\\)\\|\\("
631 org-outline-regexp
632 "\\)\\)[^ \t\n]") nil t))
633 (setq ind (or (match-string 2)
634 (make-string (length (match-string 3)) ?\ )))))
635 (mapc (lambda (x) (insert ind "[" (car x) "]: " (cdr x) "\n"))
636 link-buffer))
637 (insert "\n")))
639 (defun org-ascii-level-start (level title umax &optional lines)
640 "Insert a new level in ASCII export."
641 (let (char (n (- level umax 1)) (ind 0))
642 (if (> level umax)
643 (progn
644 (insert (make-string (* 2 n) ?\ )
645 (char-to-string (nth (% n (length org-export-ascii-bullets))
646 org-export-ascii-bullets))
647 " " title "\n")
648 ;; find the indentation of the next non-empty line
649 (catch 'stop
650 (while lines
651 (if (string-match "^\\* " (car lines)) (throw 'stop nil))
652 (if (string-match "^\\([ \t]*\\)\\S-" (car lines))
653 (throw 'stop (setq ind (org-get-indentation (car lines)))))
654 (pop lines)))
655 (setq org-ascii-current-indentation (cons (* 2 (1+ n)) ind)))
656 (if (or (not (equal (char-before) ?\n))
657 (not (equal (char-before (1- (point))) ?\n)))
658 (insert "\n"))
659 (setq char (or (nth (1- level) org-export-ascii-underline)
660 (car (last org-export-ascii-underline))))
661 (unless org-export-with-tags
662 (if (string-match (org-re "[ \t]+\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$") title)
663 (setq title (replace-match "" t t title))))
664 (if org-export-with-section-numbers
665 (setq title (concat (org-section-number level) " " title)))
666 (insert title "\n" (make-string (string-width title) char) "\n")
667 (setq org-ascii-current-indentation '(0 . 0)))))
669 (defun org-insert-centered (s &optional underline)
670 "Insert the string S centered and underline it with character UNDERLINE."
671 (let ((ind (max (/ (- fill-column (string-width s)) 2) 0)))
672 (insert (make-string ind ?\ ) s "\n")
673 (if underline
674 (insert (make-string ind ?\ )
675 (make-string (string-width s) underline)
676 "\n"))))
678 (defvar org-table-colgroup-info nil)
679 (defun org-format-table-ascii (lines)
680 "Format a table for ascii export."
681 (if (stringp lines)
682 (setq lines (org-split-string lines "\n")))
683 (if (not (string-match "^[ \t]*|" (car lines)))
684 ;; Table made by table.el - test for spanning
685 lines
687 ;; A normal org table
688 ;; Get rid of hlines at beginning and end
689 (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
690 (setq lines (nreverse lines))
691 (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
692 (setq lines (nreverse lines))
693 (when org-export-table-remove-special-lines
694 ;; Check if the table has a marking column. If yes remove the
695 ;; column and the special lines
696 (setq lines (org-table-clean-before-export lines)))
697 ;; Get rid of the vertical lines except for grouping
698 (if org-export-ascii-table-keep-all-vertical-lines
699 lines
700 (let ((vl (org-colgroup-info-to-vline-list org-table-colgroup-info))
701 rtn line vl1 start)
702 (while (setq line (pop lines))
703 (if (string-match org-table-hline-regexp line)
704 (and (string-match "|\\(.*\\)|" line)
705 (setq line (replace-match " \\1" t nil line)))
706 (setq start 0 vl1 vl)
707 (while (string-match "|" line start)
708 (setq start (match-end 0))
709 (or (pop vl1) (setq line (replace-match " " t t line)))))
710 (push line rtn))
711 (nreverse rtn)))))
713 (defun org-colgroup-info-to-vline-list (info)
714 (let (vl new last)
715 (while info
716 (setq last new new (pop info))
717 (if (or (memq last '(:end :startend))
718 (memq new '(:start :startend)))
719 (push t vl)
720 (push nil vl)))
721 (setq vl (nreverse vl))
722 (and vl (setcar vl nil))
723 vl))
725 (provide 'org-ascii)
727 ;;; org-ascii.el ends here