org-ascii.el 21 KB

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