documentation fix
[docutils.git] / sandbox / texinfo-writer / tools / rst2texinfo.el
blob00d25863a735040169a4c05d3ed22b5451284ee6
1 ;; Copyright (C) 2010 Jon Waltman
3 ;; Author: Jon Waltman <jonathan.waltman@gmail.com>
4 ;; Keywords: reStructuredText rst reST texinfo info
6 ;; This program is free software; you can redistribute it and/or modify
7 ;; it under the terms of the GNU General Public License as published by
8 ;; the Free Software Foundation, either version 3 of the License, or
9 ;; (at your option) any later version.
11 ;; This program is distributed in the hope that it will be useful,
12 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;; GNU General Public License for more details.
16 ;; You should have received a copy of the GNU General Public License
17 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
19 ;;; Commentary:
21 ;; This package provides the interactive function `rst2texinfo-compile-preview'
22 ;; for quickly viewing the output of running "rst2texinfo" on a buffer
23 ;; visiting a reStructuredText file.
25 ;;; Code:
27 (require 'rst)
28 (require 'info)
30 (defcustom rst2texinfo-run-command "rst2texinfo.py --traceback"
31 "*Shell command used to run `rst2texinfo'"
32 :type 'string
33 :group 'rst2texinfo)
37 (defun rst2texinfo-current-section ()
38 "Return the name of the section containing point, in a rst file."
39 (save-excursion
40 (unless (car (rst-get-decoration))
41 (rst-backward-section)
42 (when (bobp)
43 (rst-forward-section)))
44 (buffer-substring-no-properties (line-beginning-position)
45 (line-end-position))))
47 (defun rst2texinfo-compilation-sentinel (proc msg)
48 (compilation-sentinel proc msg)
49 (when (memq (process-status proc) '(signal exit))
50 (let (node)
51 (if (or (null rst2texinfo-section-name)
52 (string= "" rst2texinfo-section-name))
53 (setq node "Top")
54 (with-temp-buffer
55 (insert-file-contents rst2texinfo-file.texi)
56 (goto-char (point-min))
57 (re-search-forward
58 (concat "^@node \\(" (replace-regexp-in-string
59 "[^[:alpha:]]" "." rst2texinfo-section-name)
60 "\\)") nil t)
61 (setq node (or (match-string 1) "Top"))))
62 (Info-revert-find-node rst2texinfo-file.info node))))
64 (defun rst2texinfo-compile-preview (no-query)
65 "Compile the current reStructuredText file to Info and view the result.
67 The Texinfo source is created by invoking the shell command specified by
68 `rst2texinfo-run-command' with two arguments, the current buffer's filename
69 and the name of the output file. The name of the output file is the same as
70 the buffer's filename but with the `.texi' extension.
72 After the Texinfo file is created, the `makeinfo' program is used to generate
73 the appropriate Info file.
75 Finally, the current buffer is switched to view the resulting Info file.
77 With prefix argument `no-query', the current buffer is automatically saved
78 and no prompts are issued before overwriting existing output files.
80 (interactive "P")
81 (cond ((null buffer-file-name)
82 (error "Buffer not visiting any file"))
83 ((buffer-modified-p)
84 (when (or no-query
85 (y-or-n-p "Buffer modified; do you want to save it? "))
86 (save-buffer))))
87 (let ((src (file-name-sans-extension buffer-file-name)))
88 ;; Not local since needed by `rst2texinfo-compilation-sentinel'.
89 (setq rst2texinfo-file.texi (concat src ".texi"))
90 (setq rst2texinfo-file.info (concat src ".info"))
91 (when (and (not no-query)
92 (file-exists-p rst2texinfo-file.texi))
93 (unless (y-or-n-p (format "File `%s' exists; over write? "
94 rst2texinfo-file.texi))
95 (error "Canceled")))
96 (setq rst2texinfo-section-name (rst2texinfo-current-section))
97 (save-excursion
98 (let* ((command (concat rst2texinfo-run-command
99 " " (shell-quote-argument buffer-file-name)
100 " " (shell-quote-argument rst2texinfo-file.texi)
101 " && makeinfo -v --no-split -o "
102 (shell-quote-argument rst2texinfo-file.info)
103 " " (shell-quote-argument rst2texinfo-file.texi)))
104 (buffer (compilation-start command))
105 (process (get-buffer-process buffer)))
106 (set-process-sentinel process 'rst2texinfo-compilation-sentinel)))))
110 (defadvice info-insert-file-contents (after
111 docutils-info-insert-file-contents
112 activate)
113 "Hack to make `Info-hide-note-references' buffer-local and
114 automatically set to `hide' iff it can be determined that this file
115 was created from a Texinfo file generated by Docutils or Sphinx."
116 (set (make-local-variable 'Info-hide-note-references)
117 (default-value 'Info-hide-note-references))
118 (save-excursion
119 (save-restriction
120 (widen) (goto-char (point-min))
121 (when (re-search-forward
122 "^Generated by \\(Sphinx\\|Docutils\\)"
123 (save-excursion (search-forward "\x1f" nil t)) t)
124 (set (make-local-variable 'Info-hide-note-references)
125 'hide)))))
127 (provide 'rst2texinfo)
128 ;;; rst2texinfo.el ends here