org-ascii.el 22 KB

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