conf-rst.el 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. ;;; Code:
  2. (require 'org-table)
  3. ;; Functions
  4. (defun distopico:orgtbl-to-rst-paddings (table)
  5. (let* ((pruned-table (remove 'hline table))
  6. (size-table (mapcar (lambda (row)
  7. (mapcar #'length row))
  8. pruned-table)))
  9. (apply #'mapcar* #'max size-table)))
  10. (defun distopico:orgtbl-padded-hline (paddings &optional chr)
  11. (let ((chr (or chr ?-)))
  12. (concat (format "+%c" chr)
  13. (mapconcat (lambda (size)
  14. (make-string size chr)) paddings
  15. (format "%c+%c" chr chr))
  16. (format "%c+" chr))))
  17. (defun distopico:orgtbl-to-rst (table params)
  18. "Convert the Orgtbl mode TABLE to ReST."
  19. (let* ((indent (make-string (or (plist-get params :indent) 0) ?\ ))
  20. (paddings (yh/orgtbl-to-rst-paddings table))
  21. (hline (concat indent (yh/orgtbl-padded-hline paddings)))
  22. (hlend (concat indent (yh/orgtbl-padded-hline paddings ?=)))
  23. (lfmt (concat indent "| "
  24. (mapconcat (lambda (size)
  25. (format "%%-%ds" size)) paddings
  26. " | ") " |"))
  27. (hlfmt (concat lfmt "\n" hlend))
  28. (params2
  29. (list
  30. :tstart (concat "\n" hline) :tend (concat hline "\n") :hline hline
  31. :lfmt lfmt :hlfmt hlfmt :skipheadrule t)))
  32. (orgtbl-to-generic table (org-combine-plists params2 params))))
  33. (push `(rst-mode ,(concat
  34. ".. BEGIN RECEIVE ORGTBL %n\n"
  35. "\n"
  36. ".. END RECEIVE ORGTBL %n\n"
  37. "\n"
  38. "..\n"
  39. " #+ORGTBL: SEND %n yh/orgtbl-to-rst :splice nil :skip 0\n"
  40. " | | |\n"))
  41. orgtbl-radio-table-templates)
  42. (defun rst-validate-buffer ()
  43. "Tests to see if buffer is valid reStructured Text."
  44. (interactive)
  45. (if (eq major-mode 'rst-mode) ; only runs in rst-mode
  46. (let ((name (buffer-name))
  47. (filename (buffer-file-name)))
  48. (cond
  49. ((not (file-exists-p "/usr/bin/rst2pseudoxml")) ; check that the program used to process file is present
  50. (error "Docutils programs not available."))
  51. ((not (file-exists-p filename)) ; check that the file of the buffer exists
  52. (error "Buffer '%s' is not visiting a file!" name))
  53. (t ; ok, process the file, producing pseudoxml output
  54. (let* ((pseudoxml (split-string (shell-command-to-string (concat "rst2pseudoxml " filename)) "\n"))
  55. (firstline (car pseudoxml)) ; take first line of output
  56. (lastline (nth (- (length pseudoxml) 2) pseudoxml))) ; take last line of output text
  57. (if (or (string-match "ERROR/" firstline)
  58. (string-match "WARNING/" firstline)
  59. ;; for reasons I don't understand, sometimes the warnings/errors are at the end of output
  60. (string-match "ERROR/" lastline)
  61. (string-match "WARNING/" lastline))
  62. (progn (ding)
  63. (message "There's an problem in this buffer."))
  64. (message "Buffer is valid reStructured Text."))))))))
  65. ;; Hooks
  66. ;; (add-hook 'rst-mode-hook
  67. ;; (lambda()
  68. ;; (add-hook 'after-save-hook 'rst-validate-buffer)
  69. ;; (turn-on-orgtbl)))
  70. (add-hook 'rst-adjust-hook 'rst-toc-update)
  71. (provide 'conf-rst)