org-ascii.el 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  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.34trans
  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-subtree-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. (run-hooks 'org-export-first-hook)
  140. (setq-default org-todo-line-regexp org-todo-line-regexp)
  141. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  142. ext-plist
  143. (org-infile-export-plist)))
  144. (region-p (org-region-active-p))
  145. (rbeg (and region-p (region-beginning)))
  146. (rend (and region-p (region-end)))
  147. (subtree-p
  148. (if (plist-get opt-plist :ignore-subtree-p)
  149. nil
  150. (when region-p
  151. (save-excursion
  152. (goto-char rbeg)
  153. (and (org-at-heading-p)
  154. (>= (org-end-of-subtree t t) rend))))))
  155. (level-offset (if subtree-p
  156. (save-excursion
  157. (goto-char rbeg)
  158. (+ (funcall outline-level)
  159. (if org-odd-levels-only 1 0)))
  160. 0))
  161. (opt-plist (setq org-export-opt-plist
  162. (if subtree-p
  163. (org-export-add-subtree-options opt-plist rbeg)
  164. opt-plist)))
  165. (custom-times org-display-custom-times)
  166. (org-ascii-current-indentation '(0 . 0))
  167. (level 0) line txt
  168. (umax nil)
  169. (umax-toc nil)
  170. (case-fold-search nil)
  171. (bfname (buffer-file-name (or (buffer-base-buffer) (current-buffer))))
  172. (filename (if to-buffer
  173. nil
  174. (concat (file-name-as-directory
  175. (or pub-dir
  176. (org-export-directory :ascii opt-plist)))
  177. (file-name-sans-extension
  178. (or (and subtree-p
  179. (org-entry-get (region-beginning)
  180. "EXPORT_FILE_NAME" t))
  181. (file-name-nondirectory bfname)))
  182. ".txt")))
  183. (filename (and filename
  184. (if (equal (file-truename filename)
  185. (file-truename bfname))
  186. (concat filename ".txt")
  187. filename)))
  188. (buffer (if to-buffer
  189. (cond
  190. ((eq to-buffer 'string)
  191. (get-buffer-create "*Org ASCII Export*"))
  192. (t (get-buffer-create to-buffer)))
  193. (find-file-noselect filename)))
  194. (org-levels-open (make-vector org-level-max nil))
  195. (odd org-odd-levels-only)
  196. (date (plist-get opt-plist :date))
  197. (author (plist-get opt-plist :author))
  198. (title (or (and subtree-p (org-export-get-title-from-subtree))
  199. (plist-get opt-plist :title)
  200. (and (not
  201. (plist-get opt-plist :skip-before-1st-heading))
  202. (org-export-grab-title-from-buffer))
  203. (file-name-sans-extension
  204. (file-name-nondirectory bfname))))
  205. (email (plist-get opt-plist :email))
  206. (language (plist-get opt-plist :language))
  207. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  208. (todo nil)
  209. (lang-words nil)
  210. (region
  211. (buffer-substring
  212. (if (org-region-active-p) (region-beginning) (point-min))
  213. (if (org-region-active-p) (region-end) (point-max))))
  214. (lines (org-split-string
  215. (org-export-preprocess-string
  216. region
  217. :for-ascii t
  218. :skip-before-1st-heading
  219. (plist-get opt-plist :skip-before-1st-heading)
  220. :drawers (plist-get opt-plist :drawers)
  221. :tags (plist-get opt-plist :tags)
  222. :priority (plist-get opt-plist :priority)
  223. :footnotes (plist-get opt-plist :footnotes)
  224. :timestamps (plist-get opt-plist :timestamps)
  225. :todo-keywords (plist-get opt-plist :todo-keywords)
  226. :verbatim-multiline t
  227. :select-tags (plist-get opt-plist :select-tags)
  228. :exclude-tags (plist-get opt-plist :exclude-tags)
  229. :archived-trees
  230. (plist-get opt-plist :archived-trees)
  231. :add-text (plist-get opt-plist :text))
  232. "\n"))
  233. thetoc have-headings first-heading-pos
  234. table-open table-buffer link-buffer link desc desc0 rpl wrap)
  235. (let ((inhibit-read-only t))
  236. (org-unmodified
  237. (remove-text-properties (point-min) (point-max)
  238. '(:org-license-to-kill t))))
  239. (setq org-min-level (org-get-min-level lines level-offset))
  240. (setq org-last-level org-min-level)
  241. (org-init-section-numbers)
  242. (setq lang-words (or (assoc language org-export-language-setup)
  243. (assoc "en" org-export-language-setup)))
  244. (set-buffer buffer)
  245. (erase-buffer)
  246. (fundamental-mode)
  247. (org-install-letbind)
  248. ;; create local variables for all options, to make sure all called
  249. ;; functions get the correct information
  250. (mapc (lambda (x)
  251. (set (make-local-variable (nth 2 x))
  252. (plist-get opt-plist (car x))))
  253. org-export-plist-vars)
  254. (org-set-local 'org-odd-levels-only odd)
  255. (setq umax (if arg (prefix-numeric-value arg)
  256. org-export-headline-levels))
  257. (setq umax-toc (if (integerp org-export-with-toc)
  258. (min org-export-with-toc umax)
  259. umax))
  260. ;; File header
  261. (unless body-only
  262. (when (and title (not (string= "" title)))
  263. (org-insert-centered title ?=)
  264. (insert "\n"))
  265. (if (and (or author email)
  266. org-export-author-info)
  267. (insert(concat (nth 1 lang-words) ": " (or author "")
  268. (if email (concat " <" email ">") "")
  269. "\n")))
  270. (cond
  271. ((and date (string-match "%" date))
  272. (setq date (format-time-string date)))
  273. (date)
  274. (t (setq date (format-time-string "%Y-%m-%d %T %Z"))))
  275. (if (and date org-export-time-stamp-file)
  276. (insert (concat (nth 2 lang-words) ": " date"\n")))
  277. (unless (= (point) (point-min))
  278. (insert "\n\n")))
  279. (if (and org-export-with-toc (not body-only))
  280. (progn
  281. (push (concat (nth 3 lang-words) "\n") thetoc)
  282. (push (concat (make-string (string-width (nth 3 lang-words)) ?=)
  283. "\n") thetoc)
  284. (mapc '(lambda (line)
  285. (if (string-match org-todo-line-regexp
  286. line)
  287. ;; This is a headline
  288. (progn
  289. (setq have-headings t)
  290. (setq level (- (match-end 1) (match-beginning 1)
  291. level-offset)
  292. level (org-tr-level level)
  293. txt (match-string 3 line)
  294. todo
  295. (or (and org-export-mark-todo-in-toc
  296. (match-beginning 2)
  297. (not (member (match-string 2 line)
  298. org-done-keywords)))
  299. ; TODO, not DONE
  300. (and org-export-mark-todo-in-toc
  301. (= level umax-toc)
  302. (org-search-todo-below
  303. line lines level))))
  304. (setq txt (org-html-expand-for-ascii txt))
  305. (while (string-match org-bracket-link-regexp txt)
  306. (setq txt
  307. (replace-match
  308. (match-string (if (match-end 2) 3 1) txt)
  309. t t txt)))
  310. (if (and (memq org-export-with-tags '(not-in-toc nil))
  311. (string-match
  312. (org-re "[ \t]+:[[:alnum:]_@:]+:[ \t]*$")
  313. txt))
  314. (setq txt (replace-match "" t t txt)))
  315. (if (string-match quote-re0 txt)
  316. (setq txt (replace-match "" t t txt)))
  317. (if org-export-with-section-numbers
  318. (setq txt (concat (org-section-number level)
  319. " " txt)))
  320. (if (<= level umax-toc)
  321. (progn
  322. (push
  323. (concat
  324. (make-string
  325. (* (max 0 (- level org-min-level)) 4) ?\ )
  326. (format (if todo "%s (*)\n" "%s\n") txt))
  327. thetoc)
  328. (setq org-last-level level))
  329. ))))
  330. lines)
  331. (setq thetoc (if have-headings (nreverse thetoc) nil))))
  332. (org-init-section-numbers)
  333. (while (setq line (pop lines))
  334. (when (and link-buffer (string-match "^\\*+ " line))
  335. (org-export-ascii-push-links (nreverse link-buffer))
  336. (setq link-buffer nil))
  337. (setq wrap nil)
  338. ;; Remove the quoted HTML tags.
  339. (setq line (org-html-expand-for-ascii line))
  340. ;; Replace links with the description when possible
  341. (while (string-match org-bracket-link-regexp line)
  342. (setq link (match-string 1 line)
  343. desc0 (match-string 3 line)
  344. desc (or desc0 (match-string 1 line)))
  345. (if (and (> (length link) 8)
  346. (equal (substring link 0 8) "coderef:"))
  347. (setq line (replace-match
  348. (format (org-export-get-coderef-format (substring link 8) desc)
  349. (cdr (assoc
  350. (substring link 8)
  351. org-export-code-refs)))
  352. t t line))
  353. (setq rpl (concat "["
  354. (or (match-string 3 line) (match-string 1 line))
  355. "]"))
  356. (when (and desc0 (not (equal desc0 link)))
  357. (if org-export-ascii-links-to-notes
  358. (push (cons desc0 link) link-buffer)
  359. (setq rpl (concat rpl " (" link ")")
  360. wrap (+ (length line) (- (length (match-string 0 line)))
  361. (length desc)))))
  362. (setq line (replace-match rpl t t line))))
  363. (when custom-times
  364. (setq line (org-translate-time line)))
  365. (cond
  366. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  367. ;; a Headline
  368. (setq first-heading-pos (or first-heading-pos (point)))
  369. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)
  370. level-offset))
  371. txt (match-string 2 line))
  372. (org-ascii-level-start level txt umax lines))
  373. ((and org-export-with-tables
  374. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  375. (if (not table-open)
  376. ;; New table starts
  377. (setq table-open t table-buffer nil))
  378. ;; Accumulate lines
  379. (setq table-buffer (cons line table-buffer))
  380. (when (or (not lines)
  381. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  382. (car lines))))
  383. (setq table-open nil
  384. table-buffer (nreverse table-buffer))
  385. (insert (mapconcat
  386. (lambda (x)
  387. (org-fix-indentation x org-ascii-current-indentation))
  388. (org-format-table-ascii table-buffer)
  389. "\n") "\n")))
  390. (t
  391. (if (string-match "^\\([ \t]*\\)\\([-+*][ \t]+\\)\\(.*?\\)\\( ::\\)" line)
  392. (setq line (replace-match "\\1\\3:" t nil line)))
  393. (setq line (org-fix-indentation line org-ascii-current-indentation))
  394. ;; Remove forced line breaks
  395. (if (string-match "\\\\\\\\[ \t]*$" line)
  396. (setq line (replace-match "" t t line)))
  397. (if (and org-export-with-fixed-width
  398. (string-match "^\\([ \t]*\\)\\(:\\( \\|$\\)\\)" line))
  399. (setq line (replace-match "\\1" nil nil line))
  400. (if wrap (setq line (org-export-ascii-wrap line wrap))))
  401. (insert line "\n"))))
  402. (org-export-ascii-push-links (nreverse link-buffer))
  403. (normal-mode)
  404. ;; insert the table of contents
  405. (when thetoc
  406. (goto-char (point-min))
  407. (if (re-search-forward "^[ \t]*\\[TABLE-OF-CONTENTS\\][ \t]*$" nil t)
  408. (progn
  409. (goto-char (match-beginning 0))
  410. (replace-match ""))
  411. (goto-char first-heading-pos))
  412. (mapc 'insert thetoc)
  413. (or (looking-at "[ \t]*\n[ \t]*\n")
  414. (insert "\n\n")))
  415. ;; Convert whitespace place holders
  416. (goto-char (point-min))
  417. (let (beg end)
  418. (while (setq beg (next-single-property-change (point) 'org-whitespace))
  419. (setq end (next-single-property-change beg 'org-whitespace))
  420. (goto-char beg)
  421. (delete-region beg end)
  422. (insert (make-string (- end beg) ?\ ))))
  423. ;; remove display and invisible chars
  424. (let (beg end)
  425. (goto-char (point-min))
  426. (while (setq beg (next-single-property-change (point) 'display))
  427. (setq end (next-single-property-change beg 'display))
  428. (delete-region beg end)
  429. (goto-char beg)
  430. (insert "=>"))
  431. (goto-char (point-min))
  432. (while (setq beg (next-single-property-change (point) 'org-cwidth))
  433. (setq end (next-single-property-change beg 'org-cwidth))
  434. (delete-region beg end)
  435. (goto-char beg)))
  436. (run-hooks 'org-export-ascii-final-hook)
  437. (or to-buffer (save-buffer))
  438. (goto-char (point-min))
  439. (or (org-export-push-to-kill-ring "ASCII")
  440. (message "Exporting... done"))
  441. ;; Return the buffer or a string, according to how this function was called
  442. (if (eq to-buffer 'string)
  443. (prog1 (buffer-substring (point-min) (point-max))
  444. (kill-buffer (current-buffer)))
  445. (current-buffer))))
  446. (defun org-export-ascii-preprocess (parameters)
  447. "Do extra work for ASCII export"
  448. ;; Put quotes around verbatim text
  449. (goto-char (point-min))
  450. (while (re-search-forward org-verbatim-re nil t)
  451. (org-if-unprotected-at (match-beginning 4)
  452. (goto-char (match-end 2))
  453. (backward-delete-char 1) (insert "'")
  454. (goto-char (match-beginning 2))
  455. (delete-char 1) (insert "`")
  456. (goto-char (match-end 2))))
  457. ;; Remove target markers
  458. (goto-char (point-min))
  459. (while (re-search-forward "<<<?\\([^<>]*\\)>>>?\\([ \t]*\\)" nil t)
  460. (org-if-unprotected-at (match-beginning 1)
  461. (replace-match "\\1\\2"))))
  462. (defun org-html-expand-for-ascii (line)
  463. "Handle quoted HTML for ASCII export."
  464. (if org-export-html-expand
  465. (while (string-match "@<[^<>\n]*>" line)
  466. ;; We just remove the tags for now.
  467. (setq line (replace-match "" nil nil line))))
  468. line)
  469. (defun org-export-ascii-wrap (line where)
  470. "Wrap LINE at or before WHERE."
  471. (let ((ind (org-get-indentation line))
  472. pos)
  473. (catch 'found
  474. (loop for i from where downto (/ where 2) do
  475. (and (equal (aref line i) ?\ )
  476. (setq pos i)
  477. (throw 'found t))))
  478. (if pos
  479. (concat (substring line 0 pos) "\n"
  480. (make-string ind ?\ )
  481. (substring line (1+ pos)))
  482. line)))
  483. (defun org-export-ascii-push-links (link-buffer)
  484. "Push out links in the buffer."
  485. (when link-buffer
  486. ;; We still have links to push out.
  487. (insert "\n")
  488. (let ((ind ""))
  489. (save-match-data
  490. (if (save-excursion
  491. (re-search-backward
  492. "^\\(\\([ \t]*\\)\\|\\(\\*+ \\)\\)[^ \t\n]" nil t))
  493. (setq ind (or (match-string 2)
  494. (make-string (length (match-string 3)) ?\ )))))
  495. (mapc (lambda (x) (insert ind "[" (car x) "]: " (cdr x) "\n"))
  496. link-buffer))
  497. (insert "\n")))
  498. (defun org-ascii-level-start (level title umax &optional lines)
  499. "Insert a new level in ASCII export."
  500. (let (char (n (- level umax 1)) (ind 0))
  501. (if (> level umax)
  502. (progn
  503. (insert (make-string (* 2 n) ?\ )
  504. (char-to-string (nth (% n (length org-export-ascii-bullets))
  505. org-export-ascii-bullets))
  506. " " title "\n")
  507. ;; find the indentation of the next non-empty line
  508. (catch 'stop
  509. (while lines
  510. (if (string-match "^\\* " (car lines)) (throw 'stop nil))
  511. (if (string-match "^\\([ \t]*\\)\\S-" (car lines))
  512. (throw 'stop (setq ind (org-get-indentation (car lines)))))
  513. (pop lines)))
  514. (setq org-ascii-current-indentation (cons (* 2 (1+ n)) ind)))
  515. (if (or (not (equal (char-before) ?\n))
  516. (not (equal (char-before (1- (point))) ?\n)))
  517. (insert "\n"))
  518. (setq char (nth (- umax level) (reverse org-export-ascii-underline)))
  519. (unless org-export-with-tags
  520. (if (string-match (org-re "[ \t]+\\(:[[:alnum:]_@:]+:\\)[ \t]*$") title)
  521. (setq title (replace-match "" t t title))))
  522. (if org-export-with-section-numbers
  523. (setq title (concat (org-section-number level) " " title)))
  524. (insert title "\n" (make-string (string-width title) char) "\n")
  525. (setq org-ascii-current-indentation '(0 . 0)))))
  526. (defun org-insert-centered (s &optional underline)
  527. "Insert the string S centered and underline it with character UNDERLINE."
  528. (let ((ind (max (/ (- fill-column (string-width s)) 2) 0)))
  529. (insert (make-string ind ?\ ) s "\n")
  530. (if underline
  531. (insert (make-string ind ?\ )
  532. (make-string (string-width s) underline)
  533. "\n"))))
  534. (defvar org-table-colgroup-info nil)
  535. (defun org-format-table-ascii (lines)
  536. "Format a table for ascii export."
  537. (if (stringp lines)
  538. (setq lines (org-split-string lines "\n")))
  539. (if (not (string-match "^[ \t]*|" (car lines)))
  540. ;; Table made by table.el - test for spanning
  541. lines
  542. ;; A normal org table
  543. ;; Get rid of hlines at beginning and end
  544. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  545. (setq lines (nreverse lines))
  546. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  547. (setq lines (nreverse lines))
  548. (when org-export-table-remove-special-lines
  549. ;; Check if the table has a marking column. If yes remove the
  550. ;; column and the special lines
  551. (setq lines (org-table-clean-before-export lines)))
  552. ;; Get rid of the vertical lines except for grouping
  553. (if org-export-ascii-table-keep-all-vertical-lines
  554. lines
  555. (let ((vl (org-colgroup-info-to-vline-list org-table-colgroup-info))
  556. rtn line vl1 start)
  557. (while (setq line (pop lines))
  558. (if (string-match org-table-hline-regexp line)
  559. (and (string-match "|\\(.*\\)|" line)
  560. (setq line (replace-match " \\1" t nil line)))
  561. (setq start 0 vl1 vl)
  562. (while (string-match "|" line start)
  563. (setq start (match-end 0))
  564. (or (pop vl1) (setq line (replace-match " " t t line)))))
  565. (push line rtn))
  566. (nreverse rtn)))))
  567. (defun org-colgroup-info-to-vline-list (info)
  568. (let (vl new last)
  569. (while info
  570. (setq last new new (pop info))
  571. (if (or (memq last '(:end :startend))
  572. (memq new '(:start :startend)))
  573. (push t vl)
  574. (push nil vl)))
  575. (setq vl (nreverse vl))
  576. (and vl (setcar vl nil))
  577. vl))
  578. (provide 'org-ascii)
  579. ;; arch-tag: aa96f882-f477-4e13-86f5-70d43e7adf3c
  580. ;;; org-ascii.el ends here