makeinfo.el 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. ;;; makeinfo.el --- run makeinfo conveniently
  2. ;; Copyright (C) 1991, 1993, 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: Robert J. Chassell
  4. ;; Maintainer: FSF
  5. ;; Keywords: docs convenience
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs 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.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;;; The Texinfo mode `makeinfo' related commands are:
  19. ;; makeinfo-region to run makeinfo on the current region.
  20. ;; makeinfo-buffer to run makeinfo on the current buffer, or
  21. ;; with optional prefix arg, on current region
  22. ;; kill-compilation to kill currently running makeinfo job
  23. ;; makeinfo-recenter-makeinfo-buffer to redisplay *compilation* buffer
  24. ;;; Keybindings (defined in `texinfo.el')
  25. ;; makeinfo bindings
  26. ; (define-key texinfo-mode-map "\C-c\C-m\C-r" 'makeinfo-region)
  27. ; (define-key texinfo-mode-map "\C-c\C-m\C-b" 'makeinfo-buffer)
  28. ; (define-key texinfo-mode-map "\C-c\C-m\C-k" 'kill-compilation)
  29. ; (define-key texinfo-mode-map "\C-c\C-m\C-l"
  30. ; 'makeinfo-recenter-compilation-buffer)
  31. ;;; Code:
  32. ;;; Variables used by `makeinfo'
  33. (require 'compile)
  34. (require 'info)
  35. (defvar tex-end-of-header)
  36. (defvar tex-start-of-header)
  37. (defgroup makeinfo nil
  38. "Run makeinfo conveniently."
  39. :group 'docs)
  40. (defcustom makeinfo-run-command "makeinfo"
  41. "Command used to run `makeinfo' subjob.
  42. The name of the file is appended to this string, separated by a space."
  43. :type 'string
  44. :group 'makeinfo)
  45. (defcustom makeinfo-options "--fill-column=70"
  46. "String containing options for running `makeinfo'.
  47. Do not include `--footnote-style' or `--paragraph-indent';
  48. the proper way to specify those is with the Texinfo commands
  49. `@footnotestyle` and `@paragraphindent'."
  50. :type 'string
  51. :group 'makeinfo)
  52. (require 'texinfo)
  53. (defvar makeinfo-compilation-process nil
  54. "Process that runs `makeinfo'. Should start out nil.")
  55. (defvar makeinfo-temp-file nil
  56. "Temporary file name used for text being sent as input to `makeinfo'.")
  57. (defvar makeinfo-output-file-name nil
  58. "Info file name used for text output by `makeinfo'.")
  59. (defvar makeinfo-output-node-name nil
  60. "Node name to visit in output file, for `makeinfo-buffer'.")
  61. ;;; The `makeinfo' function definitions
  62. (defun makeinfo-region (region-beginning region-end)
  63. "Make Info file from region of current Texinfo file, and switch to it.
  64. This command does not offer the `next-error' feature since it would
  65. apply to a temporary file, not the original; use the `makeinfo-buffer'
  66. command to gain use of `next-error'."
  67. (interactive "r")
  68. (let (filename-or-header
  69. filename-or-header-beginning
  70. filename-or-header-end)
  71. ;; Cannot use `let' for makeinfo-temp-file or
  72. ;; makeinfo-output-file-name since `makeinfo-compilation-sentinel'
  73. ;; needs them.
  74. (setq makeinfo-temp-file
  75. (concat
  76. (make-temp-file
  77. (substring (buffer-file-name)
  78. 0
  79. (or (string-match "\\.tex" (buffer-file-name))
  80. (length (buffer-file-name)))))
  81. ".texinfo"))
  82. (save-excursion
  83. (save-restriction
  84. (widen)
  85. (goto-char (point-min))
  86. (let ((search-end (save-excursion (forward-line 100) (point))))
  87. ;; Find and record the Info filename,
  88. ;; or else explain that a filename is needed.
  89. (if (re-search-forward
  90. "^@setfilename[ \t]+\\([^ \t\n]+\\)[ \t]*"
  91. search-end t)
  92. (setq makeinfo-output-file-name
  93. (buffer-substring (match-beginning 1) (match-end 1)))
  94. (error
  95. "The texinfo file needs a line saying: @setfilename <name>"))
  96. ;; Find header and specify its beginning and end.
  97. (goto-char (point-min))
  98. (if (and
  99. (prog1
  100. (search-forward tex-start-of-header search-end t)
  101. (beginning-of-line)
  102. ;; Mark beginning of header.
  103. (setq filename-or-header-beginning (point)))
  104. (prog1
  105. (search-forward tex-end-of-header nil t)
  106. (beginning-of-line)
  107. ;; Mark end of header
  108. (setq filename-or-header-end (point))))
  109. ;; Insert the header into the temporary file.
  110. (write-region
  111. (min filename-or-header-beginning region-beginning)
  112. filename-or-header-end
  113. makeinfo-temp-file nil nil)
  114. ;; Else no header; insert @filename line into temporary file.
  115. (goto-char (point-min))
  116. (search-forward "@setfilename" search-end t)
  117. (beginning-of-line)
  118. (setq filename-or-header-beginning (point))
  119. (forward-line 1)
  120. (setq filename-or-header-end (point))
  121. (write-region
  122. (min filename-or-header-beginning region-beginning)
  123. filename-or-header-end
  124. makeinfo-temp-file nil nil))
  125. ;; Insert the region into the file.
  126. (write-region
  127. (max region-beginning filename-or-header-end)
  128. region-end
  129. makeinfo-temp-file t nil)
  130. ;; Run the `makeinfo-compile' command in the *compilation* buffer
  131. (save-excursion
  132. (makeinfo-compile
  133. (concat makeinfo-run-command
  134. " "
  135. makeinfo-options
  136. " "
  137. makeinfo-temp-file)
  138. t
  139. 'makeinfo-compilation-sentinel-region)))))))
  140. (defun makeinfo-next-error (arg reset)
  141. "This function is used to disable `next-error' if the user has
  142. used `makeinfo-region'. Since the compilation process is used on
  143. a temporary file in that case, calling `next-error' would give
  144. nonsensical results."
  145. (error "Use `makeinfo-buffer' to gain use of the `next-error' command"))
  146. ;; Actually run makeinfo. COMMAND is the command to run. If
  147. ;; DISABLE-ERRORS is non-nil, disable `next-error' by setting
  148. ;; `next-error-function' to `makeinfo-next-error' in the compilation
  149. ;; buffer.
  150. (defun makeinfo-compile (command disable-errors sentinel)
  151. (let ((buffer (compilation-start command)))
  152. (with-current-buffer buffer
  153. (setq next-error-function
  154. (if disable-errors
  155. 'makeinfo-next-error
  156. 'compilation-next-error-function)))
  157. (set-process-sentinel (get-buffer-process buffer) sentinel)))
  158. ;; Delete makeinfo-temp-file after processing is finished,
  159. ;; and visit Info file.
  160. ;; This function is called when the compilation process changes state.
  161. ;; Based on `compilation-sentinel' in compile.el
  162. (defun makeinfo-compilation-sentinel-region (proc msg)
  163. "Sentinel for `makeinfo-compile' run from `makeinfo-region'."
  164. (compilation-sentinel proc msg)
  165. (when (memq (process-status proc) '(signal exit))
  166. (if (file-exists-p makeinfo-temp-file)
  167. (delete-file makeinfo-temp-file))
  168. ;; Always use the version on disk.
  169. (let ((buffer (get-file-buffer makeinfo-output-file-name)))
  170. (if buffer
  171. (with-current-buffer buffer
  172. (revert-buffer t t))
  173. (setq buffer (find-file-noselect makeinfo-output-file-name)))
  174. (if (window-dedicated-p (selected-window))
  175. (switch-to-buffer-other-window buffer)
  176. (switch-to-buffer buffer)))
  177. (goto-char (point-min))))
  178. (defun makeinfo-current-node ()
  179. "Return the name of the node containing point, in a texinfo file."
  180. (save-excursion
  181. (end-of-line) ; in case point is at the start of an @node line
  182. (if (re-search-backward "^@node\\s-+\\([^,\n]+\\)" (point-min) t)
  183. (match-string 1)
  184. "Top")))
  185. (defun makeinfo-buffer ()
  186. "Make Info file from current buffer.
  187. Use the \\[next-error] command to move to the next error
  188. \(if there are errors\)."
  189. (interactive)
  190. (cond ((null buffer-file-name)
  191. (error "Buffer not visiting any file"))
  192. ((buffer-modified-p)
  193. (if (y-or-n-p "Buffer modified; do you want to save it? ")
  194. (save-buffer))))
  195. ;; Find and record the Info filename,
  196. ;; or else explain that a filename is needed.
  197. (save-excursion
  198. (goto-char (point-min))
  199. (let ((search-end (save-excursion (forward-line 100) (point))))
  200. (if (re-search-forward
  201. "^@setfilename[ \t]+\\([^ \t\n]+\\)[ \t]*"
  202. search-end t)
  203. (setq makeinfo-output-file-name
  204. (expand-file-name
  205. (buffer-substring (match-beginning 1) (match-end 1))))
  206. (error
  207. "The texinfo file needs a line saying: @setfilename <name>"))))
  208. (setq makeinfo-output-node-name (makeinfo-current-node))
  209. (save-excursion
  210. (makeinfo-compile
  211. (concat makeinfo-run-command " " makeinfo-options
  212. " " buffer-file-name)
  213. nil
  214. 'makeinfo-compilation-sentinel-buffer)))
  215. (defun makeinfo-compilation-sentinel-buffer (proc msg)
  216. "Sentinel for `makeinfo-compile' run from `makeinfo-buffer'."
  217. (compilation-sentinel proc msg)
  218. (when (memq (process-status proc) '(signal exit))
  219. (when (file-exists-p makeinfo-output-file-name)
  220. (Info-revert-find-node
  221. makeinfo-output-file-name makeinfo-output-node-name))))
  222. (defun makeinfo-recenter-compilation-buffer (linenum)
  223. "Redisplay `*compilation*' buffer so most recent output can be seen.
  224. The last line of the buffer is displayed on
  225. line LINE of the window, or centered if LINE is nil."
  226. (interactive "P")
  227. (let ((makeinfo-buffer (get-buffer "*compilation*"))
  228. (old-buffer (current-buffer)))
  229. (if (null makeinfo-buffer)
  230. (message "No *compilation* buffer")
  231. (pop-to-buffer makeinfo-buffer)
  232. (bury-buffer makeinfo-buffer)
  233. (goto-char (point-max))
  234. (recenter (if linenum
  235. (prefix-numeric-value linenum)
  236. (/ (window-height) 2)))
  237. (pop-to-buffer old-buffer)
  238. )))
  239. ;;; Place `provide' at end of file.
  240. (provide 'makeinfo)
  241. ;;; makeinfo.el ends here