org-ascii.el 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  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.26trans
  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. (switch-to-buffer-other-window "*Org ASCII Export*"))
  58. ;;;###autoload
  59. (defun org-replace-region-by-ascii (beg end)
  60. "Assume the current region has org-mode syntax, and convert it to plain ASCII.
  61. This can be used in any buffer. For example, you could write an
  62. itemized list in org-mode syntax in a Mail buffer and then use this
  63. command to convert it."
  64. (interactive "r")
  65. (let (reg ascii buf pop-up-frames)
  66. (save-window-excursion
  67. (if (org-mode-p)
  68. (setq ascii (org-export-region-as-ascii
  69. beg end t 'string))
  70. (setq reg (buffer-substring beg end)
  71. buf (get-buffer-create "*Org tmp*"))
  72. (with-current-buffer buf
  73. (erase-buffer)
  74. (insert reg)
  75. (org-mode)
  76. (setq ascii (org-export-region-as-ascii
  77. (point-min) (point-max) t 'string)))
  78. (kill-buffer buf)))
  79. (delete-region beg end)
  80. (insert ascii)))
  81. ;;;###autoload
  82. (defun org-export-region-as-ascii (beg end &optional body-only buffer)
  83. "Convert region from BEG to END in org-mode buffer to plain ASCII.
  84. If prefix arg BODY-ONLY is set, omit file header, footer, and table of
  85. contents, and only produce the region of converted text, useful for
  86. cut-and-paste operations.
  87. If BUFFER is a buffer or a string, use/create that buffer as a target
  88. of the converted ASCII. If BUFFER is the symbol `string', return the
  89. produced ASCII as a string and leave not buffer behind. For example,
  90. a Lisp program could call this function in the following way:
  91. (setq ascii (org-export-region-as-ascii beg end t 'string))
  92. When called interactively, the output buffer is selected, and shown
  93. in a window. A non-interactive call will only return the buffer."
  94. (interactive "r\nP")
  95. (when (interactive-p)
  96. (setq buffer "*Org ASCII Export*"))
  97. (let ((transient-mark-mode t) (zmacs-regions t)
  98. ext-plist rtn)
  99. (setq ext-plist (plist-put ext-plist :ignore-subree-p t))
  100. (goto-char end)
  101. (set-mark (point)) ;; to activate the region
  102. (goto-char beg)
  103. (setq rtn (org-export-as-ascii
  104. nil nil ext-plist
  105. buffer body-only))
  106. (if (fboundp 'deactivate-mark) (deactivate-mark))
  107. (if (and (interactive-p) (bufferp rtn))
  108. (switch-to-buffer-other-window rtn)
  109. rtn)))
  110. ;;;###autoload
  111. (defun org-export-as-ascii (arg &optional hidden ext-plist
  112. to-buffer body-only pub-dir)
  113. "Export the outline as a pretty ASCII file.
  114. If there is an active region, export only the region.
  115. The prefix ARG specifies how many levels of the outline should become
  116. underlined headlines, default is 3. Lower levels will become bulleted
  117. lists. When HIDDEN is non-nil, don't display the ASCII buffer.
  118. EXT-PLIST is a property list with external parameters overriding
  119. org-mode's default settings, but still inferior to file-local
  120. settings. When TO-BUFFER is non-nil, create a buffer with that
  121. name and export to that buffer. If TO-BUFFER is the symbol
  122. `string', don't leave any buffer behind but just return the
  123. resulting ASCII as a string. When BODY-ONLY is set, don't produce
  124. the file header and footer. When PUB-DIR is set, use this as the
  125. publishing directory."
  126. (interactive "P")
  127. (setq-default org-todo-line-regexp org-todo-line-regexp)
  128. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  129. ext-plist
  130. (org-infile-export-plist)))
  131. (region-p (org-region-active-p))
  132. (rbeg (and region-p (region-beginning)))
  133. (rend (and region-p (region-end)))
  134. (subtree-p
  135. (if (plist-get opt-plist :ignore-subree-p)
  136. nil
  137. (when region-p
  138. (save-excursion
  139. (goto-char rbeg)
  140. (and (org-at-heading-p)
  141. (>= (org-end-of-subtree t t) rend))))))
  142. (level-offset (if subtree-p
  143. (save-excursion
  144. (goto-char rbeg)
  145. (+ (funcall outline-level)
  146. (if org-odd-levels-only 1 0)))
  147. 0))
  148. (opt-plist (setq org-export-opt-plist
  149. (if subtree-p
  150. (org-export-add-subtree-options opt-plist rbeg)
  151. opt-plist)))
  152. (custom-times org-display-custom-times)
  153. (org-ascii-current-indentation '(0 . 0))
  154. (level 0) line txt
  155. (umax nil)
  156. (umax-toc nil)
  157. (case-fold-search nil)
  158. (bfname (buffer-file-name (or (buffer-base-buffer) (current-buffer))))
  159. (filename (if to-buffer
  160. nil
  161. (concat (file-name-as-directory
  162. (or pub-dir
  163. (org-export-directory :ascii opt-plist)))
  164. (file-name-sans-extension
  165. (or (and subtree-p
  166. (org-entry-get (region-beginning)
  167. "EXPORT_FILE_NAME" t))
  168. (file-name-nondirectory bfname)))
  169. ".txt")))
  170. (filename (and filename
  171. (if (equal (file-truename filename)
  172. (file-truename bfname))
  173. (concat filename ".txt")
  174. filename)))
  175. (buffer (if to-buffer
  176. (cond
  177. ((eq to-buffer 'string)
  178. (get-buffer-create "*Org ASCII Export*"))
  179. (t (get-buffer-create to-buffer)))
  180. (find-file-noselect filename)))
  181. (org-levels-open (make-vector org-level-max nil))
  182. (odd org-odd-levels-only)
  183. (date (plist-get opt-plist :date))
  184. (author (plist-get opt-plist :author))
  185. (title (or (and subtree-p (org-export-get-title-from-subtree))
  186. (plist-get opt-plist :title)
  187. (and (not
  188. (plist-get opt-plist :skip-before-1st-heading))
  189. (org-export-grab-title-from-buffer))
  190. (file-name-sans-extension
  191. (file-name-nondirectory bfname))))
  192. (email (plist-get opt-plist :email))
  193. (language (plist-get opt-plist :language))
  194. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  195. (todo nil)
  196. (lang-words nil)
  197. (region
  198. (buffer-substring
  199. (if (org-region-active-p) (region-beginning) (point-min))
  200. (if (org-region-active-p) (region-end) (point-max))))
  201. (lines (org-split-string
  202. (org-export-preprocess-string
  203. region
  204. :for-ascii t
  205. :skip-before-1st-heading
  206. (plist-get opt-plist :skip-before-1st-heading)
  207. :drawers (plist-get opt-plist :drawers)
  208. :tags (plist-get opt-plist :tags)
  209. :priority (plist-get opt-plist :priority)
  210. :footnotes (plist-get opt-plist :footnotes)
  211. :timestamps (plist-get opt-plist :timestamps)
  212. :todo-keywords (plist-get opt-plist :todo-keywords)
  213. :verbatim-multiline t
  214. :select-tags (plist-get opt-plist :select-tags)
  215. :exclude-tags (plist-get opt-plist :exclude-tags)
  216. :archived-trees
  217. (plist-get opt-plist :archived-trees)
  218. :add-text (plist-get opt-plist :text))
  219. "\n"))
  220. thetoc have-headings first-heading-pos
  221. table-open table-buffer link-buffer link desc desc0 rpl wrap)
  222. (let ((inhibit-read-only t))
  223. (org-unmodified
  224. (remove-text-properties (point-min) (point-max)
  225. '(:org-license-to-kill t))))
  226. (setq org-min-level (org-get-min-level lines level-offset))
  227. (setq org-last-level org-min-level)
  228. (org-init-section-numbers)
  229. (switch-to-buffer buffer)
  230. (setq lang-words (or (assoc language org-export-language-setup)
  231. (assoc "en" org-export-language-setup)))
  232. (switch-to-buffer-other-window 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)))
  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. (prog1 (if (eq to-buffer 'string)
  424. (prog1 (buffer-substring (point-min) (point-max))
  425. (kill-buffer (current-buffer)))
  426. (current-buffer))
  427. (when hidden
  428. (delete-window)))))
  429. (defun org-export-ascii-preprocess (parameters)
  430. "Do extra work for ASCII export"
  431. ;; Put quotes around verbatim text
  432. (goto-char (point-min))
  433. (while (re-search-forward org-verbatim-re nil t)
  434. (goto-char (match-end 2))
  435. (backward-delete-char 1) (insert "'")
  436. (goto-char (match-beginning 2))
  437. (delete-char 1) (insert "`")
  438. (goto-char (match-end 2)))
  439. ;; Remove target markers
  440. (goto-char (point-min))
  441. (while (re-search-forward "<<<?\\([^<>]*\\)>>>?\\([ \t]*\\)" nil t)
  442. (replace-match "\\1\\2")))
  443. (defun org-html-expand-for-ascii (line)
  444. "Handle quoted HTML for ASCII export."
  445. (if org-export-html-expand
  446. (while (string-match "@<[^<>\n]*>" line)
  447. ;; We just remove the tags for now.
  448. (setq line (replace-match "" nil nil line))))
  449. line)
  450. (defun org-export-ascii-wrap (line where)
  451. "Wrap LINE at or before WHERE."
  452. (let ((ind (org-get-indentation line))
  453. pos)
  454. (catch 'found
  455. (loop for i from where downto (/ where 2) do
  456. (and (equal (aref line i) ?\ )
  457. (setq pos i)
  458. (throw 'found t))))
  459. (if pos
  460. (concat (substring line 0 pos) "\n"
  461. (make-string ind ?\ )
  462. (substring line (1+ pos)))
  463. line)))
  464. (defun org-export-ascii-push-links (link-buffer)
  465. "Push out links in the buffer."
  466. (when link-buffer
  467. ;; We still have links to push out.
  468. (insert "\n")
  469. (let ((ind ""))
  470. (save-match-data
  471. (if (save-excursion
  472. (re-search-backward
  473. "^\\(\\([ \t]*\\)\\|\\(\\*+ \\)\\)[^ \t\n]" nil t))
  474. (setq ind (or (match-string 2)
  475. (make-string (length (match-string 3)) ?\ )))))
  476. (mapc (lambda (x) (insert ind "[" (car x) "]: " (cdr x) "\n"))
  477. link-buffer))
  478. (insert "\n")))
  479. (defun org-ascii-level-start (level title umax &optional lines)
  480. "Insert a new level in ASCII export."
  481. (let (char (n (- level umax 1)) (ind 0))
  482. (if (> level umax)
  483. (progn
  484. (insert (make-string (* 2 n) ?\ )
  485. (char-to-string (nth (% n (length org-export-ascii-bullets))
  486. org-export-ascii-bullets))
  487. " " title "\n")
  488. ;; find the indentation of the next non-empty line
  489. (catch 'stop
  490. (while lines
  491. (if (string-match "^\\* " (car lines)) (throw 'stop nil))
  492. (if (string-match "^\\([ \t]*\\)\\S-" (car lines))
  493. (throw 'stop (setq ind (org-get-indentation (car lines)))))
  494. (pop lines)))
  495. (setq org-ascii-current-indentation (cons (* 2 (1+ n)) ind)))
  496. (if (or (not (equal (char-before) ?\n))
  497. (not (equal (char-before (1- (point))) ?\n)))
  498. (insert "\n"))
  499. (setq char (nth (- umax level) (reverse org-export-ascii-underline)))
  500. (unless org-export-with-tags
  501. (if (string-match (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
  502. (setq title (replace-match "" t t title))))
  503. (if org-export-with-section-numbers
  504. (setq title (concat (org-section-number level) " " title)))
  505. (insert title "\n" (make-string (string-width title) char) "\n")
  506. (setq org-ascii-current-indentation '(0 . 0)))))
  507. (defun org-insert-centered (s &optional underline)
  508. "Insert the string S centered and underline it with character UNDERLINE."
  509. (let ((ind (max (/ (- fill-column (string-width s)) 2) 0)))
  510. (insert (make-string ind ?\ ) s "\n")
  511. (if underline
  512. (insert (make-string ind ?\ )
  513. (make-string (string-width s) underline)
  514. "\n"))))
  515. (defvar org-table-colgroup-info nil)
  516. (defun org-format-table-ascii (lines)
  517. "Format a table for ascii export."
  518. (if (stringp lines)
  519. (setq lines (org-split-string lines "\n")))
  520. (if (not (string-match "^[ \t]*|" (car lines)))
  521. ;; Table made by table.el - test for spanning
  522. lines
  523. ;; A normal org table
  524. ;; Get rid of hlines at beginning and end
  525. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  526. (setq lines (nreverse lines))
  527. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  528. (setq lines (nreverse lines))
  529. (when org-export-table-remove-special-lines
  530. ;; Check if the table has a marking column. If yes remove the
  531. ;; column and the special lines
  532. (setq lines (org-table-clean-before-export lines)))
  533. ;; Get rid of the vertical lines except for grouping
  534. (let ((vl (org-colgroup-info-to-vline-list org-table-colgroup-info))
  535. rtn line vl1 start)
  536. (while (setq line (pop lines))
  537. (if (string-match org-table-hline-regexp line)
  538. (and (string-match "|\\(.*\\)|" line)
  539. (setq line (replace-match " \\1" t nil line)))
  540. (setq start 0 vl1 vl)
  541. (while (string-match "|" line start)
  542. (setq start (match-end 0))
  543. (or (pop vl1) (setq line (replace-match " " t t line)))))
  544. (push line rtn))
  545. (nreverse rtn))))
  546. (defun org-colgroup-info-to-vline-list (info)
  547. (let (vl new last)
  548. (while info
  549. (setq last new new (pop info))
  550. (if (or (memq last '(:end :startend))
  551. (memq new '(:start :startend)))
  552. (push t vl)
  553. (push nil vl)))
  554. (setq vl (nreverse vl))
  555. (and vl (setcar vl nil))
  556. vl))
  557. (provide 'org-ascii)
  558. ;; arch-tag: aa96f882-f477-4e13-86f5-70d43e7adf3c
  559. ;;; org-ascii.el ends here