org-ascii.el 21 KB

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