org-ascii.el 22 KB

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