org-ascii.el 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  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.29b
  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. (org-install-letbind)
  236. ;; create local variables for all options, to make sure all called
  237. ;; functions get the correct information
  238. (mapc (lambda (x)
  239. (set (make-local-variable (nth 2 x))
  240. (plist-get opt-plist (car x))))
  241. org-export-plist-vars)
  242. (org-set-local 'org-odd-levels-only odd)
  243. (setq umax (if arg (prefix-numeric-value arg)
  244. org-export-headline-levels))
  245. (setq umax-toc (if (integerp org-export-with-toc)
  246. (min org-export-with-toc umax)
  247. umax))
  248. ;; File header
  249. (unless body-only
  250. (if title (org-insert-centered title ?=))
  251. (insert "\n")
  252. (if (and (or author email)
  253. org-export-author-info)
  254. (insert (concat (nth 1 lang-words) ": " (or author "")
  255. (if email (concat " <" email ">") "")
  256. "\n")))
  257. (cond
  258. ((and date (string-match "%" date))
  259. (setq date (format-time-string date)))
  260. (date)
  261. (t (setq date (format-time-string "%Y-%m-%d %T %Z"))))
  262. (if (and date org-export-time-stamp-file)
  263. (insert (concat (nth 2 lang-words) ": " date"\n")))
  264. (insert "\n\n"))
  265. (if (and org-export-with-toc (not body-only))
  266. (progn
  267. (push (concat (nth 3 lang-words) "\n") thetoc)
  268. (push (concat (make-string (string-width (nth 3 lang-words)) ?=)
  269. "\n") thetoc)
  270. (mapc '(lambda (line)
  271. (if (string-match org-todo-line-regexp
  272. line)
  273. ;; This is a headline
  274. (progn
  275. (setq have-headings t)
  276. (setq level (- (match-end 1) (match-beginning 1)
  277. level-offset)
  278. level (org-tr-level level)
  279. txt (match-string 3 line)
  280. todo
  281. (or (and org-export-mark-todo-in-toc
  282. (match-beginning 2)
  283. (not (member (match-string 2 line)
  284. org-done-keywords)))
  285. ; TODO, not DONE
  286. (and org-export-mark-todo-in-toc
  287. (= level umax-toc)
  288. (org-search-todo-below
  289. line lines level))))
  290. (setq txt (org-html-expand-for-ascii txt))
  291. (while (string-match org-bracket-link-regexp txt)
  292. (setq txt
  293. (replace-match
  294. (match-string (if (match-end 2) 3 1) txt)
  295. t t txt)))
  296. (if (and (memq org-export-with-tags '(not-in-toc nil))
  297. (string-match
  298. (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$")
  299. txt))
  300. (setq txt (replace-match "" t t txt)))
  301. (if (string-match quote-re0 txt)
  302. (setq txt (replace-match "" t t txt)))
  303. (if org-export-with-section-numbers
  304. (setq txt (concat (org-section-number level)
  305. " " txt)))
  306. (if (<= level umax-toc)
  307. (progn
  308. (push
  309. (concat
  310. (make-string
  311. (* (max 0 (- level org-min-level)) 4) ?\ )
  312. (format (if todo "%s (*)\n" "%s\n") txt))
  313. thetoc)
  314. (setq org-last-level level))
  315. ))))
  316. lines)
  317. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  318. (org-init-section-numbers)
  319. (while (setq line (pop lines))
  320. (when (and link-buffer (string-match "^\\*+ " line))
  321. (org-export-ascii-push-links (nreverse link-buffer))
  322. (setq link-buffer nil))
  323. (setq wrap nil)
  324. ;; Remove the quoted HTML tags.
  325. (setq line (org-html-expand-for-ascii line))
  326. ;; Replace links with the description when possible
  327. (while (string-match org-bracket-link-regexp line)
  328. (setq link (match-string 1 line)
  329. desc0 (match-string 3 line)
  330. desc (or desc0 (match-string 1 line)))
  331. (if (and (> (length link) 8)
  332. (equal (substring link 0 8) "coderef:"))
  333. (setq line (replace-match
  334. (format (org-export-get-coderef-format (substring link 8) desc)
  335. (cdr (assoc
  336. (substring link 8)
  337. org-export-code-refs)))
  338. t t line))
  339. (setq rpl (concat "["
  340. (or (match-string 3 line) (match-string 1 line))
  341. "]"))
  342. (when (and desc0 (not (equal desc0 link)))
  343. (if org-export-ascii-links-to-notes
  344. (push (cons desc0 link) link-buffer)
  345. (setq rpl (concat rpl " (" link ")")
  346. wrap (+ (length line) (- (length (match-string 0 line)))
  347. (length desc)))))
  348. (setq line (replace-match rpl t t line))))
  349. (when custom-times
  350. (setq line (org-translate-time line)))
  351. (cond
  352. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  353. ;; a Headline
  354. (setq first-heading-pos (or first-heading-pos (point)))
  355. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)
  356. level-offset))
  357. txt (match-string 2 line))
  358. (org-ascii-level-start level txt umax lines))
  359. ((and org-export-with-tables
  360. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  361. (if (not table-open)
  362. ;; New table starts
  363. (setq table-open t table-buffer nil))
  364. ;; Accumulate lines
  365. (setq table-buffer (cons line table-buffer))
  366. (when (or (not lines)
  367. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  368. (car lines))))
  369. (setq table-open nil
  370. table-buffer (nreverse table-buffer))
  371. (insert (mapconcat
  372. (lambda (x)
  373. (org-fix-indentation x org-ascii-current-indentation))
  374. (org-format-table-ascii table-buffer)
  375. "\n") "\n")))
  376. (t
  377. (if (string-match "^\\([ \t]*\\)\\([-+*][ \t]+\\)\\(.*?\\)\\( ::\\)" line)
  378. (setq line (replace-match "\\1\\3:" t nil line)))
  379. (setq line (org-fix-indentation line org-ascii-current-indentation))
  380. ;; Remove forced line breaks
  381. (if (string-match "\\\\\\\\[ \t]*$" line)
  382. (setq line (replace-match "" t t line)))
  383. (if (and org-export-with-fixed-width
  384. (string-match "^\\([ \t]*\\)\\(:\\( \\|$\\)\\)" line))
  385. (setq line (replace-match "\\1" nil nil line))
  386. (if wrap (setq line (org-export-ascii-wrap line wrap))))
  387. (insert line "\n"))))
  388. (org-export-ascii-push-links (nreverse link-buffer))
  389. (normal-mode)
  390. ;; insert the table of contents
  391. (when thetoc
  392. (goto-char (point-min))
  393. (if (re-search-forward "^[ \t]*\\[TABLE-OF-CONTENTS\\][ \t]*$" nil t)
  394. (progn
  395. (goto-char (match-beginning 0))
  396. (replace-match ""))
  397. (goto-char first-heading-pos))
  398. (mapc 'insert thetoc)
  399. (or (looking-at "[ \t]*\n[ \t]*\n")
  400. (insert "\n\n")))
  401. ;; Convert whitespace place holders
  402. (goto-char (point-min))
  403. (let (beg end)
  404. (while (setq beg (next-single-property-change (point) 'org-whitespace))
  405. (setq end (next-single-property-change beg 'org-whitespace))
  406. (goto-char beg)
  407. (delete-region beg end)
  408. (insert (make-string (- end beg) ?\ ))))
  409. ;; remove display and invisible chars
  410. (let (beg end)
  411. (goto-char (point-min))
  412. (while (setq beg (next-single-property-change (point) 'display))
  413. (setq end (next-single-property-change beg 'display))
  414. (delete-region beg end)
  415. (goto-char beg)
  416. (insert "=>"))
  417. (goto-char (point-min))
  418. (while (setq beg (next-single-property-change (point) 'org-cwidth))
  419. (setq end (next-single-property-change beg 'org-cwidth))
  420. (delete-region beg end)
  421. (goto-char beg)))
  422. (or to-buffer (save-buffer))
  423. (goto-char (point-min))
  424. (or (org-export-push-to-kill-ring "ASCII")
  425. (message "Exporting... done"))
  426. ;; Return the buffer or a string, according to how this function was called
  427. (if (eq to-buffer 'string)
  428. (prog1 (buffer-substring (point-min) (point-max))
  429. (kill-buffer (current-buffer)))
  430. (current-buffer))))
  431. (defun org-export-ascii-preprocess (parameters)
  432. "Do extra work for ASCII export"
  433. ;; Put quotes around verbatim text
  434. (goto-char (point-min))
  435. (while (re-search-forward org-verbatim-re nil t)
  436. (goto-char (match-end 2))
  437. (backward-delete-char 1) (insert "'")
  438. (goto-char (match-beginning 2))
  439. (delete-char 1) (insert "`")
  440. (goto-char (match-end 2)))
  441. ;; Remove target markers
  442. (goto-char (point-min))
  443. (while (re-search-forward "<<<?\\([^<>]*\\)>>>?\\([ \t]*\\)" nil t)
  444. (replace-match "\\1\\2")))
  445. (defun org-html-expand-for-ascii (line)
  446. "Handle quoted HTML for ASCII export."
  447. (if org-export-html-expand
  448. (while (string-match "@<[^<>\n]*>" line)
  449. ;; We just remove the tags for now.
  450. (setq line (replace-match "" nil nil line))))
  451. line)
  452. (defun org-export-ascii-wrap (line where)
  453. "Wrap LINE at or before WHERE."
  454. (let ((ind (org-get-indentation line))
  455. pos)
  456. (catch 'found
  457. (loop for i from where downto (/ where 2) do
  458. (and (equal (aref line i) ?\ )
  459. (setq pos i)
  460. (throw 'found t))))
  461. (if pos
  462. (concat (substring line 0 pos) "\n"
  463. (make-string ind ?\ )
  464. (substring line (1+ pos)))
  465. line)))
  466. (defun org-export-ascii-push-links (link-buffer)
  467. "Push out links in the buffer."
  468. (when link-buffer
  469. ;; We still have links to push out.
  470. (insert "\n")
  471. (let ((ind ""))
  472. (save-match-data
  473. (if (save-excursion
  474. (re-search-backward
  475. "^\\(\\([ \t]*\\)\\|\\(\\*+ \\)\\)[^ \t\n]" nil t))
  476. (setq ind (or (match-string 2)
  477. (make-string (length (match-string 3)) ?\ )))))
  478. (mapc (lambda (x) (insert ind "[" (car x) "]: " (cdr x) "\n"))
  479. link-buffer))
  480. (insert "\n")))
  481. (defun org-ascii-level-start (level title umax &optional lines)
  482. "Insert a new level in ASCII export."
  483. (let (char (n (- level umax 1)) (ind 0))
  484. (if (> level umax)
  485. (progn
  486. (insert (make-string (* 2 n) ?\ )
  487. (char-to-string (nth (% n (length org-export-ascii-bullets))
  488. org-export-ascii-bullets))
  489. " " title "\n")
  490. ;; find the indentation of the next non-empty line
  491. (catch 'stop
  492. (while lines
  493. (if (string-match "^\\* " (car lines)) (throw 'stop nil))
  494. (if (string-match "^\\([ \t]*\\)\\S-" (car lines))
  495. (throw 'stop (setq ind (org-get-indentation (car lines)))))
  496. (pop lines)))
  497. (setq org-ascii-current-indentation (cons (* 2 (1+ n)) ind)))
  498. (if (or (not (equal (char-before) ?\n))
  499. (not (equal (char-before (1- (point))) ?\n)))
  500. (insert "\n"))
  501. (setq char (nth (- umax level) (reverse org-export-ascii-underline)))
  502. (unless org-export-with-tags
  503. (if (string-match (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
  504. (setq title (replace-match "" t t title))))
  505. (if org-export-with-section-numbers
  506. (setq title (concat (org-section-number level) " " title)))
  507. (insert title "\n" (make-string (string-width title) char) "\n")
  508. (setq org-ascii-current-indentation '(0 . 0)))))
  509. (defun org-insert-centered (s &optional underline)
  510. "Insert the string S centered and underline it with character UNDERLINE."
  511. (let ((ind (max (/ (- fill-column (string-width s)) 2) 0)))
  512. (insert (make-string ind ?\ ) s "\n")
  513. (if underline
  514. (insert (make-string ind ?\ )
  515. (make-string (string-width s) underline)
  516. "\n"))))
  517. (defvar org-table-colgroup-info nil)
  518. (defun org-format-table-ascii (lines)
  519. "Format a table for ascii export."
  520. (if (stringp lines)
  521. (setq lines (org-split-string lines "\n")))
  522. (if (not (string-match "^[ \t]*|" (car lines)))
  523. ;; Table made by table.el - test for spanning
  524. lines
  525. ;; A normal org table
  526. ;; Get rid of hlines at beginning and end
  527. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  528. (setq lines (nreverse lines))
  529. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  530. (setq lines (nreverse lines))
  531. (when org-export-table-remove-special-lines
  532. ;; Check if the table has a marking column. If yes remove the
  533. ;; column and the special lines
  534. (setq lines (org-table-clean-before-export lines)))
  535. ;; Get rid of the vertical lines except for grouping
  536. (let ((vl (org-colgroup-info-to-vline-list org-table-colgroup-info))
  537. rtn line vl1 start)
  538. (while (setq line (pop lines))
  539. (if (string-match org-table-hline-regexp line)
  540. (and (string-match "|\\(.*\\)|" line)
  541. (setq line (replace-match " \\1" t nil line)))
  542. (setq start 0 vl1 vl)
  543. (while (string-match "|" line start)
  544. (setq start (match-end 0))
  545. (or (pop vl1) (setq line (replace-match " " t t line)))))
  546. (push line rtn))
  547. (nreverse rtn))))
  548. (defun org-colgroup-info-to-vline-list (info)
  549. (let (vl new last)
  550. (while info
  551. (setq last new new (pop info))
  552. (if (or (memq last '(:end :startend))
  553. (memq new '(:start :startend)))
  554. (push t vl)
  555. (push nil vl)))
  556. (setq vl (nreverse vl))
  557. (and vl (setcar vl nil))
  558. vl))
  559. (provide 'org-ascii)
  560. ;; arch-tag: aa96f882-f477-4e13-86f5-70d43e7adf3c
  561. ;;; org-ascii.el ends here