org-ascii.el 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. ;;; org-ascii.el --- ASCII export for Org-mode
  2. ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009
  3. ;; Free Software Foundation, Inc.
  4. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  5. ;; Keywords: outlines, hypermedia, calendar, wp
  6. ;; Homepage: http://orgmode.org
  7. ;; Version: 6.27trans
  8. ;;
  9. ;; This file is part of GNU Emacs.
  10. ;;
  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.
  15. ;; GNU Emacs is distributed in the hope that it will be useful,
  16. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. ;; GNU General Public License for more details.
  19. ;; You should have received a copy of the GNU General Public License
  20. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  21. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  22. ;;
  23. ;;; Commentary:
  24. (require 'org-exp)
  25. (defgroup org-export-ascii nil
  26. "Options specific for ASCII export of Org-mode files."
  27. :tag "Org Export ASCII"
  28. :group 'org-export)
  29. (defcustom org-export-ascii-underline '(?\$ ?\# ?^ ?\~ ?\= ?\-)
  30. "Characters for underlining headings in ASCII export.
  31. In the given sequence, these characters will be used for level 1, 2, ..."
  32. :group 'org-export-ascii
  33. :type '(repeat character))
  34. (defcustom org-export-ascii-bullets '(?* ?+ ?-)
  35. "Bullet characters for headlines converted to lists in ASCII export.
  36. The first character is used for the first lest level generated in this
  37. way, and so on. If there are more levels than characters given here,
  38. the list will be repeated.
  39. Note that plain lists will keep the same bullets as the have in the
  40. Org-mode file."
  41. :group 'org-export-ascii
  42. :type '(repeat character))
  43. (defcustom org-export-ascii-links-to-notes t
  44. "Non-nil means, convert links to notes before the next headline.
  45. When nil, the link will be exported in place. If the line becomes long
  46. in this way, it will be wrapped."
  47. :group 'org-export-ascii
  48. :type 'boolean)
  49. ;;; ASCII export
  50. (defvar org-ascii-current-indentation nil) ; For communication
  51. ;;;###autoload
  52. (defun org-export-as-ascii-to-buffer (arg)
  53. "Call `org-export-as-ascii` with output to a temporary buffer.
  54. No file is created. The prefix ARG is passed through to `org-export-as-ascii'."
  55. (interactive "P")
  56. (org-export-as-ascii arg nil nil "*Org ASCII Export*")
  57. (when org-export-show-temporary-export-buffer
  58. (switch-to-buffer-other-window "*Org ASCII Export*")))
  59. ;;;###autoload
  60. (defun org-replace-region-by-ascii (beg end)
  61. "Assume the current region has org-mode syntax, and convert it to plain ASCII.
  62. This can be used in any buffer. For example, you could write an
  63. itemized list in org-mode syntax in a Mail buffer and then use this
  64. command to convert it."
  65. (interactive "r")
  66. (let (reg ascii buf pop-up-frames)
  67. (save-window-excursion
  68. (if (org-mode-p)
  69. (setq ascii (org-export-region-as-ascii
  70. beg end t 'string))
  71. (setq reg (buffer-substring beg end)
  72. buf (get-buffer-create "*Org tmp*"))
  73. (with-current-buffer buf
  74. (erase-buffer)
  75. (insert reg)
  76. (org-mode)
  77. (setq ascii (org-export-region-as-ascii
  78. (point-min) (point-max) t 'string)))
  79. (kill-buffer buf)))
  80. (delete-region beg end)
  81. (insert ascii)))
  82. ;;;###autoload
  83. (defun org-export-region-as-ascii (beg end &optional body-only buffer)
  84. "Convert region from BEG to END in org-mode buffer to plain ASCII.
  85. If prefix arg BODY-ONLY is set, omit file header, footer, and table of
  86. contents, and only produce the region of converted text, useful for
  87. cut-and-paste operations.
  88. If BUFFER is a buffer or a string, use/create that buffer as a target
  89. of the converted ASCII. If BUFFER is the symbol `string', return the
  90. produced ASCII as a string and leave not buffer behind. For example,
  91. a Lisp program could call this function in the following way:
  92. (setq ascii (org-export-region-as-ascii beg end t 'string))
  93. When called interactively, the output buffer is selected, and shown
  94. in a window. A non-interactive call will only return the buffer."
  95. (interactive "r\nP")
  96. (when (interactive-p)
  97. (setq buffer "*Org ASCII Export*"))
  98. (let ((transient-mark-mode t) (zmacs-regions t)
  99. ext-plist rtn)
  100. (setq ext-plist (plist-put ext-plist :ignore-subree-p t))
  101. (goto-char end)
  102. (set-mark (point)) ;; to activate the region
  103. (goto-char beg)
  104. (setq rtn (org-export-as-ascii
  105. nil nil ext-plist
  106. buffer body-only))
  107. (if (fboundp 'deactivate-mark) (deactivate-mark))
  108. (if (and (interactive-p) (bufferp rtn))
  109. (switch-to-buffer-other-window rtn)
  110. rtn)))
  111. ;;;###autoload
  112. (defun org-export-as-ascii (arg &optional hidden ext-plist
  113. to-buffer body-only pub-dir)
  114. "Export the outline as a pretty ASCII file.
  115. If there is an active region, export only the region.
  116. The prefix ARG specifies how many levels of the outline should become
  117. underlined headlines, default is 3. Lower levels will become bulleted
  118. lists. When HIDDEN is non-nil, don't display the ASCII buffer.
  119. EXT-PLIST is a property list with external parameters overriding
  120. org-mode's default settings, but still inferior to file-local
  121. settings. When TO-BUFFER is non-nil, create a buffer with that
  122. name and export to that buffer. If TO-BUFFER is the symbol
  123. `string', don't leave any buffer behind but just return the
  124. resulting ASCII as a string. When BODY-ONLY is set, don't produce
  125. the file header and footer. When PUB-DIR is set, use this as the
  126. publishing directory."
  127. (interactive "P")
  128. (setq-default org-todo-line-regexp org-todo-line-regexp)
  129. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  130. ext-plist
  131. (org-infile-export-plist)))
  132. (region-p (org-region-active-p))
  133. (rbeg (and region-p (region-beginning)))
  134. (rend (and region-p (region-end)))
  135. (subtree-p
  136. (if (plist-get opt-plist :ignore-subree-p)
  137. nil
  138. (when region-p
  139. (save-excursion
  140. (goto-char rbeg)
  141. (and (org-at-heading-p)
  142. (>= (org-end-of-subtree t t) rend))))))
  143. (level-offset (if subtree-p
  144. (save-excursion
  145. (goto-char rbeg)
  146. (+ (funcall outline-level)
  147. (if org-odd-levels-only 1 0)))
  148. 0))
  149. (opt-plist (setq org-export-opt-plist
  150. (if subtree-p
  151. (org-export-add-subtree-options opt-plist rbeg)
  152. opt-plist)))
  153. (custom-times org-display-custom-times)
  154. (org-ascii-current-indentation '(0 . 0))
  155. (level 0) line txt
  156. (umax nil)
  157. (umax-toc nil)
  158. (case-fold-search nil)
  159. (bfname (buffer-file-name (or (buffer-base-buffer) (current-buffer))))
  160. (filename (if to-buffer
  161. nil
  162. (concat (file-name-as-directory
  163. (or pub-dir
  164. (org-export-directory :ascii opt-plist)))
  165. (file-name-sans-extension
  166. (or (and subtree-p
  167. (org-entry-get (region-beginning)
  168. "EXPORT_FILE_NAME" t))
  169. (file-name-nondirectory bfname)))
  170. ".txt")))
  171. (filename (and filename
  172. (if (equal (file-truename filename)
  173. (file-truename bfname))
  174. (concat filename ".txt")
  175. filename)))
  176. (buffer (if to-buffer
  177. (cond
  178. ((eq to-buffer 'string)
  179. (get-buffer-create "*Org ASCII Export*"))
  180. (t (get-buffer-create to-buffer)))
  181. (find-file-noselect filename)))
  182. (org-levels-open (make-vector org-level-max nil))
  183. (odd org-odd-levels-only)
  184. (date (plist-get opt-plist :date))
  185. (author (plist-get opt-plist :author))
  186. (title (or (and subtree-p (org-export-get-title-from-subtree))
  187. (plist-get opt-plist :title)
  188. (and (not
  189. (plist-get opt-plist :skip-before-1st-heading))
  190. (org-export-grab-title-from-buffer))
  191. (file-name-sans-extension
  192. (file-name-nondirectory bfname))))
  193. (email (plist-get opt-plist :email))
  194. (language (plist-get opt-plist :language))
  195. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  196. (todo nil)
  197. (lang-words nil)
  198. (region
  199. (buffer-substring
  200. (if (org-region-active-p) (region-beginning) (point-min))
  201. (if (org-region-active-p) (region-end) (point-max))))
  202. (lines (org-split-string
  203. (org-export-preprocess-string
  204. region
  205. :for-ascii t
  206. :skip-before-1st-heading
  207. (plist-get opt-plist :skip-before-1st-heading)
  208. :drawers (plist-get opt-plist :drawers)
  209. :tags (plist-get opt-plist :tags)
  210. :priority (plist-get opt-plist :priority)
  211. :footnotes (plist-get opt-plist :footnotes)
  212. :timestamps (plist-get opt-plist :timestamps)
  213. :todo-keywords (plist-get opt-plist :todo-keywords)
  214. :verbatim-multiline t
  215. :select-tags (plist-get opt-plist :select-tags)
  216. :exclude-tags (plist-get opt-plist :exclude-tags)
  217. :archived-trees
  218. (plist-get opt-plist :archived-trees)
  219. :add-text (plist-get opt-plist :text))
  220. "\n"))
  221. thetoc have-headings first-heading-pos
  222. table-open table-buffer link-buffer link desc desc0 rpl wrap)
  223. (let ((inhibit-read-only t))
  224. (org-unmodified
  225. (remove-text-properties (point-min) (point-max)
  226. '(:org-license-to-kill t))))
  227. (setq org-min-level (org-get-min-level lines level-offset))
  228. (setq org-last-level org-min-level)
  229. (org-init-section-numbers)
  230. (setq lang-words (or (assoc language org-export-language-setup)
  231. (assoc "en" org-export-language-setup)))
  232. (set-buffer buffer)
  233. (erase-buffer)
  234. (fundamental-mode)
  235. ;; create local variables for all options, to make sure all called
  236. ;; functions get the correct information
  237. (mapc (lambda (x)
  238. (set (make-local-variable (nth 2 x))
  239. (plist-get opt-plist (car x))))
  240. org-export-plist-vars)
  241. (org-set-local 'org-odd-levels-only odd)
  242. (setq umax (if arg (prefix-numeric-value arg)
  243. org-export-headline-levels))
  244. (setq umax-toc (if (integerp org-export-with-toc)
  245. (min org-export-with-toc umax)
  246. umax))
  247. ;; File header
  248. (unless body-only
  249. (if title (org-insert-centered title ?=))
  250. (insert "\n")
  251. (if (and (or author email)
  252. org-export-author-info)
  253. (insert (concat (nth 1 lang-words) ": " (or author "")
  254. (if email (concat " <" email ">") "")
  255. "\n")))
  256. (cond
  257. ((and date (string-match "%" date))
  258. (setq date (format-time-string date)))
  259. (date)
  260. (t (setq date (format-time-string "%Y-%m-%d %T %Z"))))
  261. (if (and date org-export-time-stamp-file)
  262. (insert (concat (nth 2 lang-words) ": " date"\n")))
  263. (insert "\n\n"))
  264. (if (and org-export-with-toc (not body-only))
  265. (progn
  266. (push (concat (nth 3 lang-words) "\n") thetoc)
  267. (push (concat (make-string (string-width (nth 3 lang-words)) ?=)
  268. "\n") thetoc)
  269. (mapc '(lambda (line)
  270. (if (string-match org-todo-line-regexp
  271. line)
  272. ;; This is a headline
  273. (progn
  274. (setq have-headings t)
  275. (setq level (- (match-end 1) (match-beginning 1)
  276. level-offset)
  277. level (org-tr-level level)
  278. txt (match-string 3 line)
  279. todo
  280. (or (and org-export-mark-todo-in-toc
  281. (match-beginning 2)
  282. (not (member (match-string 2 line)
  283. org-done-keywords)))
  284. ; TODO, not DONE
  285. (and org-export-mark-todo-in-toc
  286. (= level umax-toc)
  287. (org-search-todo-below
  288. line lines level))))
  289. (setq txt (org-html-expand-for-ascii txt))
  290. (while (string-match org-bracket-link-regexp txt)
  291. (setq txt
  292. (replace-match
  293. (match-string (if (match-end 2) 3 1) txt)
  294. t t txt)))
  295. (if (and (memq org-export-with-tags '(not-in-toc nil))
  296. (string-match
  297. (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$")
  298. txt))
  299. (setq txt (replace-match "" t t txt)))
  300. (if (string-match quote-re0 txt)
  301. (setq txt (replace-match "" t t txt)))
  302. (if org-export-with-section-numbers
  303. (setq txt (concat (org-section-number level)
  304. " " txt)))
  305. (if (<= level umax-toc)
  306. (progn
  307. (push
  308. (concat
  309. (make-string
  310. (* (max 0 (- level org-min-level)) 4) ?\ )
  311. (format (if todo "%s (*)\n" "%s\n") txt))
  312. thetoc)
  313. (setq org-last-level level))
  314. ))))
  315. lines)
  316. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  317. (org-init-section-numbers)
  318. (while (setq line (pop lines))
  319. (when (and link-buffer (string-match "^\\*+ " line))
  320. (org-export-ascii-push-links (nreverse link-buffer))
  321. (setq link-buffer nil))
  322. (setq wrap nil)
  323. ;; Remove the quoted HTML tags.
  324. (setq line (org-html-expand-for-ascii line))
  325. ;; Replace links with the description when possible
  326. (while (string-match org-bracket-link-regexp line)
  327. (setq link (match-string 1 line)
  328. desc0 (match-string 3 line)
  329. desc (or desc0 (match-string 1 line)))
  330. (if (and (> (length link) 8)
  331. (equal (substring link 0 8) "coderef:"))
  332. (setq line (replace-match
  333. (format (org-export-get-coderef-format (substring link 8) desc)
  334. (cdr (assoc
  335. (substring link 8)
  336. org-export-code-refs)))
  337. t t line))
  338. (setq rpl (concat "["
  339. (or (match-string 3 line) (match-string 1 line))
  340. "]"))
  341. (when (and desc0 (not (equal desc0 link)))
  342. (if org-export-ascii-links-to-notes
  343. (push (cons desc0 link) link-buffer)
  344. (setq rpl (concat rpl " (" link ")")
  345. wrap (+ (length line) (- (length (match-string 0 line)))
  346. (length desc)))))
  347. (setq line (replace-match rpl t t line))))
  348. (when custom-times
  349. (setq line (org-translate-time line)))
  350. (cond
  351. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  352. ;; a Headline
  353. (setq first-heading-pos (or first-heading-pos (point)))
  354. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)
  355. level-offset))
  356. txt (match-string 2 line))
  357. (org-ascii-level-start level txt umax lines))
  358. ((and org-export-with-tables
  359. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  360. (if (not table-open)
  361. ;; New table starts
  362. (setq table-open t table-buffer nil))
  363. ;; Accumulate lines
  364. (setq table-buffer (cons line table-buffer))
  365. (when (or (not lines)
  366. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  367. (car lines))))
  368. (setq table-open nil
  369. table-buffer (nreverse table-buffer))
  370. (insert (mapconcat
  371. (lambda (x)
  372. (org-fix-indentation x org-ascii-current-indentation))
  373. (org-format-table-ascii table-buffer)
  374. "\n") "\n")))
  375. (t
  376. (if (string-match "^\\([ \t]*\\)\\([-+*][ \t]+\\)\\(.*?\\)\\( ::\\)" line)
  377. (setq line (replace-match "\\1\\3:" t nil line)))
  378. (setq line (org-fix-indentation line org-ascii-current-indentation))
  379. ;; Remove forced line breaks
  380. (if (string-match "\\\\\\\\[ \t]*$" line)
  381. (setq line (replace-match "" t t line)))
  382. (if (and org-export-with-fixed-width
  383. (string-match "^\\([ \t]*\\)\\(:\\( \\|$\\)\\)" line))
  384. (setq line (replace-match "\\1" nil nil line))
  385. (if wrap (setq line (org-export-ascii-wrap line wrap))))
  386. (insert line "\n"))))
  387. (org-export-ascii-push-links (nreverse link-buffer))
  388. (normal-mode)
  389. ;; insert the table of contents
  390. (when thetoc
  391. (goto-char (point-min))
  392. (if (re-search-forward "^[ \t]*\\[TABLE-OF-CONTENTS\\][ \t]*$" nil t)
  393. (progn
  394. (goto-char (match-beginning 0))
  395. (replace-match ""))
  396. (goto-char first-heading-pos))
  397. (mapc 'insert thetoc)
  398. (or (looking-at "[ \t]*\n[ \t]*\n")
  399. (insert "\n\n")))
  400. ;; Convert whitespace place holders
  401. (goto-char (point-min))
  402. (let (beg end)
  403. (while (setq beg (next-single-property-change (point) 'org-whitespace))
  404. (setq end (next-single-property-change beg 'org-whitespace))
  405. (goto-char beg)
  406. (delete-region beg end)
  407. (insert (make-string (- end beg) ?\ ))))
  408. ;; remove display and invisible chars
  409. (let (beg end)
  410. (goto-char (point-min))
  411. (while (setq beg (next-single-property-change (point) 'display))
  412. (setq end (next-single-property-change beg 'display))
  413. (delete-region beg end)
  414. (goto-char beg)
  415. (insert "=>"))
  416. (goto-char (point-min))
  417. (while (setq beg (next-single-property-change (point) 'org-cwidth))
  418. (setq end (next-single-property-change beg 'org-cwidth))
  419. (delete-region beg end)
  420. (goto-char beg)))
  421. (or to-buffer (save-buffer))
  422. (goto-char (point-min))
  423. (or (org-export-push-to-kill-ring "ASCII")
  424. (message "Exporting... done"))
  425. ;; Return the buffer or a string, according to how this function was called
  426. (if (eq to-buffer 'string)
  427. (prog1 (buffer-substring (point-min) (point-max))
  428. (kill-buffer (current-buffer)))
  429. (current-buffer))))
  430. (defun org-export-ascii-preprocess (parameters)
  431. "Do extra work for ASCII export"
  432. ;; Put quotes around verbatim text
  433. (goto-char (point-min))
  434. (while (re-search-forward org-verbatim-re nil t)
  435. (goto-char (match-end 2))
  436. (backward-delete-char 1) (insert "'")
  437. (goto-char (match-beginning 2))
  438. (delete-char 1) (insert "`")
  439. (goto-char (match-end 2)))
  440. ;; Remove target markers
  441. (goto-char (point-min))
  442. (while (re-search-forward "<<<?\\([^<>]*\\)>>>?\\([ \t]*\\)" nil t)
  443. (replace-match "\\1\\2")))
  444. (defun org-html-expand-for-ascii (line)
  445. "Handle quoted HTML for ASCII export."
  446. (if org-export-html-expand
  447. (while (string-match "@<[^<>\n]*>" line)
  448. ;; We just remove the tags for now.
  449. (setq line (replace-match "" nil nil line))))
  450. line)
  451. (defun org-export-ascii-wrap (line where)
  452. "Wrap LINE at or before WHERE."
  453. (let ((ind (org-get-indentation line))
  454. pos)
  455. (catch 'found
  456. (loop for i from where downto (/ where 2) do
  457. (and (equal (aref line i) ?\ )
  458. (setq pos i)
  459. (throw 'found t))))
  460. (if pos
  461. (concat (substring line 0 pos) "\n"
  462. (make-string ind ?\ )
  463. (substring line (1+ pos)))
  464. line)))
  465. (defun org-export-ascii-push-links (link-buffer)
  466. "Push out links in the buffer."
  467. (when link-buffer
  468. ;; We still have links to push out.
  469. (insert "\n")
  470. (let ((ind ""))
  471. (save-match-data
  472. (if (save-excursion
  473. (re-search-backward
  474. "^\\(\\([ \t]*\\)\\|\\(\\*+ \\)\\)[^ \t\n]" nil t))
  475. (setq ind (or (match-string 2)
  476. (make-string (length (match-string 3)) ?\ )))))
  477. (mapc (lambda (x) (insert ind "[" (car x) "]: " (cdr x) "\n"))
  478. link-buffer))
  479. (insert "\n")))
  480. (defun org-ascii-level-start (level title umax &optional lines)
  481. "Insert a new level in ASCII export."
  482. (let (char (n (- level umax 1)) (ind 0))
  483. (if (> level umax)
  484. (progn
  485. (insert (make-string (* 2 n) ?\ )
  486. (char-to-string (nth (% n (length org-export-ascii-bullets))
  487. org-export-ascii-bullets))
  488. " " title "\n")
  489. ;; find the indentation of the next non-empty line
  490. (catch 'stop
  491. (while lines
  492. (if (string-match "^\\* " (car lines)) (throw 'stop nil))
  493. (if (string-match "^\\([ \t]*\\)\\S-" (car lines))
  494. (throw 'stop (setq ind (org-get-indentation (car lines)))))
  495. (pop lines)))
  496. (setq org-ascii-current-indentation (cons (* 2 (1+ n)) ind)))
  497. (if (or (not (equal (char-before) ?\n))
  498. (not (equal (char-before (1- (point))) ?\n)))
  499. (insert "\n"))
  500. (setq char (nth (- umax level) (reverse org-export-ascii-underline)))
  501. (unless org-export-with-tags
  502. (if (string-match (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
  503. (setq title (replace-match "" t t title))))
  504. (if org-export-with-section-numbers
  505. (setq title (concat (org-section-number level) " " title)))
  506. (insert title "\n" (make-string (string-width title) char) "\n")
  507. (setq org-ascii-current-indentation '(0 . 0)))))
  508. (defun org-insert-centered (s &optional underline)
  509. "Insert the string S centered and underline it with character UNDERLINE."
  510. (let ((ind (max (/ (- fill-column (string-width s)) 2) 0)))
  511. (insert (make-string ind ?\ ) s "\n")
  512. (if underline
  513. (insert (make-string ind ?\ )
  514. (make-string (string-width s) underline)
  515. "\n"))))
  516. (defvar org-table-colgroup-info nil)
  517. (defun org-format-table-ascii (lines)
  518. "Format a table for ascii export."
  519. (if (stringp lines)
  520. (setq lines (org-split-string lines "\n")))
  521. (if (not (string-match "^[ \t]*|" (car lines)))
  522. ;; Table made by table.el - test for spanning
  523. lines
  524. ;; A normal org table
  525. ;; Get rid of hlines at beginning and end
  526. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  527. (setq lines (nreverse lines))
  528. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  529. (setq lines (nreverse lines))
  530. (when org-export-table-remove-special-lines
  531. ;; Check if the table has a marking column. If yes remove the
  532. ;; column and the special lines
  533. (setq lines (org-table-clean-before-export lines)))
  534. ;; Get rid of the vertical lines except for grouping
  535. (let ((vl (org-colgroup-info-to-vline-list org-table-colgroup-info))
  536. rtn line vl1 start)
  537. (while (setq line (pop lines))
  538. (if (string-match org-table-hline-regexp line)
  539. (and (string-match "|\\(.*\\)|" line)
  540. (setq line (replace-match " \\1" t nil line)))
  541. (setq start 0 vl1 vl)
  542. (while (string-match "|" line start)
  543. (setq start (match-end 0))
  544. (or (pop vl1) (setq line (replace-match " " t t line)))))
  545. (push line rtn))
  546. (nreverse rtn))))
  547. (defun org-colgroup-info-to-vline-list (info)
  548. (let (vl new last)
  549. (while info
  550. (setq last new new (pop info))
  551. (if (or (memq last '(:end :startend))
  552. (memq new '(:start :startend)))
  553. (push t vl)
  554. (push nil vl)))
  555. (setq vl (nreverse vl))
  556. (and vl (setcar vl nil))
  557. vl))
  558. (provide 'org-ascii)
  559. ;; arch-tag: aa96f882-f477-4e13-86f5-70d43e7adf3c
  560. ;;; org-ascii.el ends here