info-xref.el 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. ;;; info-xref.el --- check external references in an Info document
  2. ;; Copyright (C) 2003-2012 Free Software Foundation, Inc.
  3. ;; Author: Kevin Ryde <user42@zip.com.au>
  4. ;; Keywords: docs
  5. ;; Version: 3
  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. ;; This is some simple checking of external cross references in info files,
  19. ;; docstrings and custom-links by attempting to visit the nodes specified.
  20. ;;
  21. ;; `M-x info-xref-check' checks a single info file. See the docstring for
  22. ;; details.
  23. ;;
  24. ;; `M-x info-xref-check-all' checks all info files in Info-directory-list.
  25. ;; This is a good way to check the consistency of the whole system.
  26. ;;
  27. ;; `M-x info-xref-check-all-custom' loads up all defcustom variables and
  28. ;; checks any info references in them.
  29. ;;
  30. ;; `M-x info-xref-docstrings' checks docstring "Info node ..." hyperlinks in
  31. ;; source files (and other files).
  32. ;;; History:
  33. ;; Version 3 - new M-x info-xref-docstrings, use compilation-mode
  34. ;;; Code:
  35. (require 'info)
  36. (eval-when-compile
  37. (require 'cl)) ;; for `incf'
  38. ;;-----------------------------------------------------------------------------
  39. ;; vaguely generic
  40. (defun info-xref-lock-file-p (filename)
  41. "Return non-nil if FILENAME is an Emacs lock file.
  42. A lock file is \".#foo.txt\" etc per `lock-buffer'."
  43. (string-match "\\(\\`\\|\\/\\)\\.#" filename))
  44. (defun info-xref-subfile-p (filename)
  45. "Return t if FILENAME is an info subfile.
  46. If removing the last \"-<NUM>\" from the filename gives a file
  47. which exists, then consider FILENAME a subfile. This is an
  48. imperfect test, probably ought to open up the purported top file
  49. and see what subfiles it says."
  50. (and (string-match "\\`\\(\\([^-]*-\\)*[^-]*\\)-[0-9]+\\(.*\\)\\'" filename)
  51. (file-exists-p (concat (match-string 1 filename)
  52. (match-string 3 filename)))))
  53. (defmacro info-xref-with-file (filename &rest body)
  54. ;; checkdoc-params: (filename body)
  55. "Evaluate BODY in a buffer containing the contents of FILENAME.
  56. If FILENAME is already in a buffer then that's used, otherwise a
  57. temporary buffer.
  58. The current implementation uses `insert-file-contents' rather
  59. than `find-file-noselect' so as not to be held up by queries
  60. about local variables or possible weirdness in a major mode.
  61. `lm-with-file' does a similar thing, but it sets
  62. `emacs-lisp-mode' which is not wanted here."
  63. (declare (debug t) (indent 1))
  64. `(let* ((info-xref-with-file--filename ,filename)
  65. (info-xref-with-file--body (lambda () ,@body))
  66. (info-xref-with-file--existing
  67. (find-buffer-visiting info-xref-with-file--filename)))
  68. (if info-xref-with-file--existing
  69. (with-current-buffer info-xref-with-file--existing
  70. (save-excursion
  71. (funcall info-xref-with-file--body)))
  72. (with-temp-buffer
  73. (insert-file-contents ,filename)
  74. (funcall info-xref-with-file--body)))))
  75. ;;-----------------------------------------------------------------------------
  76. ;; output buffer
  77. (defconst info-xref-output-buffer "*info-xref results*"
  78. "Name of the buffer for info-xref results.")
  79. (defvar info-xref-good 0
  80. "Count of good cross references, during info-xref processing.")
  81. (defvar info-xref-bad 0
  82. "Count of bad cross references, during info-xref processing.")
  83. (defvar info-xref-unavail 0
  84. "Count of unavailable cross references, during info-xref processing.")
  85. (defvar info-xref-output-heading ""
  86. "A heading string, during info-xref processing.
  87. This is shown if there's an error, but not if successful.")
  88. (defvar info-xref-filename nil
  89. "The current buffer's filename, during info-xref processing.
  90. When looking at file contents in a temp buffer there's no
  91. `buffer-file-name', hence this variable.")
  92. (defvar info-xref-xfile-alist nil
  93. "Info files found or not found, during info-xref processing.
  94. Key is \"(foo)\" etc and value nil or t according to whether info
  95. manual \"(foo)\" exists or not. This is used to suppress
  96. duplicate messages about foo not being available. (Duplicates
  97. within one top-level file that is.)")
  98. (defvar info-xref-in-progress nil)
  99. (defmacro info-xref-with-output (&rest body)
  100. "Run BODY with an info-xref output buffer.
  101. This is meant to nest, so you can wrap it around a set of
  102. different info-xref checks and have them write to the one output
  103. buffer created by the outermost `info-xref-with-output', with an
  104. overall good/bad count summary inserted at the very end."
  105. (declare (debug t))
  106. `(save-excursion
  107. (unless info-xref-in-progress
  108. (display-buffer (get-buffer-create info-xref-output-buffer))
  109. (set-buffer info-xref-output-buffer)
  110. (setq buffer-read-only nil)
  111. (fundamental-mode)
  112. (erase-buffer)
  113. (insert ";; info-xref output -*- mode: compilation -*-\n\n")
  114. (compilation-mode)
  115. (setq info-xref-good 0
  116. info-xref-bad 0
  117. info-xref-unavail 0
  118. info-xref-xfile-alist nil))
  119. (let ((info-xref-in-progress t)
  120. (info-xref-output-heading ""))
  121. ,@body)
  122. (unless info-xref-in-progress
  123. (info-xref-output "done, %d good, %d bad, %d unavailable"
  124. info-xref-good info-xref-bad info-xref-unavail))))
  125. (defun info-xref-output (fmt &rest args)
  126. "Emit a `format'-ed message FMT+ARGS to the `info-xref-output-buffer'."
  127. (with-current-buffer info-xref-output-buffer
  128. (save-excursion
  129. (goto-char (point-max))
  130. (let ((inhibit-read-only t))
  131. (insert info-xref-output-heading
  132. (apply 'format fmt args)
  133. "\n")))
  134. (setq info-xref-output-heading "")
  135. ;; all this info-xref can be pretty slow, display now so the user sees
  136. ;; some progress
  137. (sit-for 0)))
  138. (put 'info-xref-output 'byte-compile-format-like t)
  139. (defun info-xref-output-error (fmt &rest args)
  140. "Emit a `format'-ed error FMT+ARGS to the `info-xref-output-buffer'.
  141. The error is attributed to `info-xref-filename' and the current
  142. buffer's line and column of point."
  143. (apply 'info-xref-output
  144. (concat "%s:%s:%s: " fmt)
  145. info-xref-filename
  146. (1+ (count-lines (point-min) (line-beginning-position)))
  147. (1+ (current-column))
  148. args))
  149. (put 'info-xref-output-error 'byte-compile-format-like t)
  150. ;;-----------------------------------------------------------------------------
  151. ;; node checking
  152. ;; When asking Info-goto-node to fork, *info* needs to be the current
  153. ;; buffer, otherwise it seems to clone the current buffer but then do the
  154. ;; goto-node in plain *info*.
  155. ;;
  156. ;; We only fork if *info* already exists, if it doesn't then can create and
  157. ;; destroy just that instead of a new name.
  158. ;;
  159. ;; If Info-goto-node can't find the file, then no new buffer is created. If
  160. ;; it finds the file but not the node, then a buffer is created. Handle
  161. ;; this difference by checking before killing.
  162. ;;
  163. (defun info-xref-goto-node-p (node)
  164. "Return t if it's possible to go to the given NODE."
  165. (let ((oldbuf (current-buffer)))
  166. (save-excursion
  167. (save-window-excursion
  168. (prog1
  169. (condition-case nil
  170. (progn
  171. (Info-goto-node node
  172. (when (get-buffer "*info*")
  173. (set-buffer "*info*")
  174. "xref - temporary"))
  175. t)
  176. (error nil))
  177. (unless (equal (current-buffer) oldbuf)
  178. (kill-buffer)))))))
  179. (defun info-xref-check-node (node)
  180. ;; Collapse spaces as per info.el and `help-make-xrefs'.
  181. ;; Note defcustom :info-link nodes don't get this whitespace collapsing,
  182. ;; they should be the exact node name ready to visit.
  183. ;; `info-xref-check-all-custom' uses `info-xref-goto-node-p' and so
  184. ;; doesn't come through here.
  185. ;;
  186. ;; Could use "[\t\n ]+" but try to avoid uselessly replacing " " with " ".
  187. (setq node (replace-regexp-in-string "[\t\n][\t\n ]*\\| [\t\n ]+" " "
  188. node t t))
  189. (if (not (string-match "\\`([^)]*)" node))
  190. (info-xref-output-error "no `(file)' part at start of node: %s\n" node)
  191. (let ((file (match-string 0 node)))
  192. (if (string-equal "()" file)
  193. (info-xref-output-error "empty filename part: %s" node)
  194. ;; see if the file exists, if haven't looked before
  195. (unless (assoc file info-xref-xfile-alist)
  196. (let ((found (info-xref-goto-node-p file)))
  197. (push (cons file found) info-xref-xfile-alist)
  198. (unless found
  199. (info-xref-output-error "not available to check: %s\n (this reported once per file)" file))))
  200. ;; if the file exists, try the node
  201. (cond ((not (cdr (assoc file info-xref-xfile-alist)))
  202. (incf info-xref-unavail))
  203. ((info-xref-goto-node-p node)
  204. (incf info-xref-good))
  205. (t
  206. (incf info-xref-bad)
  207. (info-xref-output-error "no such node: %s" node)))))))
  208. ;;-----------------------------------------------------------------------------
  209. ;;;###autoload
  210. (defun info-xref-check (filename)
  211. "Check external references in FILENAME, an info document.
  212. Interactively from an `Info-mode' or `texinfo-mode' buffer the
  213. current info file is the default.
  214. Results are shown in a `compilation-mode' buffer. The format is
  215. a bit rough, but there shouldn't be many problems normally. The
  216. file:line:column: is the info document, but of course normally
  217. any correction should be made in the original .texi file.
  218. Finding the right place in the .texi is a manual process.
  219. When a target info file doesn't exist there's obviously no way to
  220. validate node references within it. A message is given for
  221. missing target files once per source document. It could be
  222. simply that you don't have the target installed, or it could be a
  223. mistake in the reference.
  224. Indirect info files are understood, just pass the top-level
  225. foo.info to `info-xref-check' and it traverses all sub-files.
  226. Compressed info files are accepted too as usual for `Info-mode'.
  227. \"makeinfo\" checks references internal to an info document, but
  228. not external references, which makes it rather easy for mistakes
  229. to creep in or node name changes to go unnoticed.
  230. `Info-validate' doesn't check external references either."
  231. (interactive
  232. (list
  233. (let* ((default-filename
  234. (cond ((eq major-mode 'Info-mode)
  235. Info-current-file)
  236. ((eq major-mode 'texinfo-mode)
  237. ;; look for @setfilename like makeinfo.el does
  238. (save-excursion
  239. (goto-char (point-min))
  240. (if (re-search-forward
  241. "^@setfilename[ \t]+\\([^ \t\n]+\\)[ \t]*"
  242. (line-beginning-position 100) t)
  243. (expand-file-name (match-string 1)))))))
  244. (prompt (if default-filename
  245. (format "Info file (%s): " default-filename)
  246. "Info file: ")))
  247. (read-file-name prompt nil default-filename t))))
  248. (info-xref-check-list (list filename)))
  249. ;;;###autoload
  250. (defun info-xref-check-all ()
  251. "Check external references in all info documents in the info path.
  252. `Info-directory-list' and `Info-additional-directory-list' are
  253. the info paths. See `info-xref-check' for how each file is
  254. checked.
  255. The search for \"all\" info files is rather permissive, since
  256. info files don't necessarily have a \".info\" extension and in
  257. particular the Emacs manuals normally don't. If you have a
  258. source code directory in `Info-directory-list' then a lot of
  259. extraneous files might be read. This will be time consuming but
  260. should be harmless."
  261. (interactive)
  262. (info-xref-check-list (info-xref-all-info-files)))
  263. ;; An alternative for getting only top-level files here would be to simply
  264. ;; return all files and have info-xref-check-list not follow "Indirect:".
  265. ;; The current way seems better because it (potentially) gets the proper
  266. ;; top-level filename into the error messages, and suppresses duplicate "not
  267. ;; available" messages for all subfiles of a single document.
  268. (defun info-xref-all-info-files ()
  269. "Return a list of all available info files.
  270. Only top level files are returned, subfiles are excluded.
  271. Since info files don't have to have a .info suffix, all files in
  272. the relevant directories are considered, which might mean a lot
  273. of extraneous things if for instance a source code directory is
  274. in the path."
  275. (info-initialize) ;; establish Info-directory-list
  276. (apply 'nconc
  277. (mapcar
  278. (lambda (dir)
  279. (let ((result nil))
  280. (dolist (name (directory-files
  281. dir
  282. t ;; absolute filenames
  283. "\\`[^.]")) ;; not dotfiles, nor .# lockfiles
  284. (when (and (file-exists-p name) ;; ignore broken symlinks
  285. (not (string-match "\\.te?xi\\'" name)) ;; not .texi
  286. (not (backup-file-name-p name))
  287. (not (file-directory-p name))
  288. (not (info-xref-subfile-p name)))
  289. (push name result)))
  290. (nreverse result)))
  291. (append Info-directory-list Info-additional-directory-list))))
  292. (defun info-xref-check-list (filename-list)
  293. "Check external references in info documents in FILENAME-LIST."
  294. (info-xref-with-output
  295. (dolist (info-xref-filename filename-list)
  296. (setq info-xref-xfile-alist nil)
  297. (let ((info-xref-output-heading
  298. (format "Info file %s\n" info-xref-filename)))
  299. (with-temp-message (format "Looking at %s" info-xref-filename)
  300. (with-temp-buffer
  301. (info-insert-file-contents info-xref-filename)
  302. (goto-char (point-min))
  303. (if (search-forward "\^_\nIndirect:\n" nil t)
  304. (let ((dir (file-name-directory info-xref-filename)))
  305. (while (looking-at "\\(.*\\): [0-9]+\n")
  306. (let ((info-xref-filename
  307. (expand-file-name (match-string 1) dir)))
  308. (with-temp-buffer
  309. (info-insert-file-contents info-xref-filename)
  310. (info-xref-check-buffer)))
  311. (forward-line)))
  312. (info-xref-check-buffer))))))))
  313. (defun info-xref-check-buffer ()
  314. "Check external references in the info file in the current buffer.
  315. This should be the raw file contents, not `Info-mode'."
  316. (goto-char (point-min))
  317. (while (re-search-forward
  318. "\\*[Nn]ote[ \n\t]+[^:]*:[ \n\t]+\\(\\(([^)]*)\\)[^.,]+\\)[.,]"
  319. nil t)
  320. (save-excursion
  321. (goto-char (match-beginning 1)) ;; start of nodename as error position
  322. (info-xref-check-node (match-string 1)))))
  323. (defvar viper-mode) ;; quieten the byte compiler
  324. (defvar gnus-registry-install)
  325. ;;;###autoload
  326. (defun info-xref-check-all-custom ()
  327. "Check info references in all customize groups and variables.
  328. Info references can be in `custom-manual' or `info-link' entries
  329. of the `custom-links' for a variable.
  330. Any `custom-load' autoloads in variables are loaded in order to
  331. get full link information. This will be a lot of Lisp packages
  332. and can take a long time."
  333. (interactive)
  334. (info-xref-with-output
  335. ;; `custom-load-symbol' is not used, since it quietly ignores errors, but
  336. ;; we want to show them since they mean incomplete checking.
  337. ;;
  338. ;; Just one pass through mapatoms is made. There shouldn't be any new
  339. ;; custom-loads setup by packages loaded.
  340. ;;
  341. (info-xref-output "Loading custom-load autoloads ...")
  342. (require 'cus-start)
  343. (require 'cus-load)
  344. ;; These are `setq' rather than `let' since a let would unbind the
  345. ;; variables after viper.el/gnus-registry.el have loaded, defeating the
  346. ;; defvars in those files. Of course it'd be better if those files
  347. ;; didn't make interactive queries on loading at all, to allow for
  348. ;; programmatic loading like here.
  349. (unless (boundp 'viper-mode)
  350. (setq viper-mode nil)) ;; avoid viper.el ask about viperizing
  351. (unless (boundp 'gnus-registry-install)
  352. (setq gnus-registry-install nil)) ;; avoid gnus-registry.el querying
  353. (mapatoms
  354. (lambda (symbol)
  355. (dolist (load (get symbol 'custom-loads))
  356. (cond ((symbolp load)
  357. (condition-case cause (require load)
  358. (error
  359. (info-xref-output "Symbol `%s': cannot require '%s: %s"
  360. symbol load cause))))
  361. ;; skip if previously loaded
  362. ((assoc load load-history))
  363. ((assoc (locate-library load) load-history))
  364. (t
  365. (condition-case err
  366. (load load)
  367. (error
  368. (info-xref-output "Symbol `%s': cannot load \"%s\": %s"
  369. symbol load
  370. (error-message-string err)))))))))
  371. ;; Don't bother to check whether the info file exists as opposed to just
  372. ;; a missing node. If you have the code then you should have the
  373. ;; documentation, so a wrong node name will be the usual fault.
  374. ;;
  375. (info-xref-output "\nChecking custom-links references ...")
  376. (mapatoms
  377. (lambda (symbol)
  378. (dolist (link (get symbol 'custom-links))
  379. (when (memq (car link) '(custom-manual info-link))
  380. ;; skip :tag part of (custom-manual :tag "Foo" "(foo)Node")
  381. (if (eq :tag (cadr link))
  382. (setq link (cddr link)))
  383. (if (info-xref-goto-node-p (cadr link))
  384. (incf info-xref-good)
  385. (incf info-xref-bad)
  386. ;; symbol-file gives nil for preloaded variables, would need
  387. ;; to copy what describe-variable does to show the right place
  388. (info-xref-output "Symbol `%s' (file %s): cannot goto node: %s"
  389. symbol
  390. (symbol-file symbol 'defvar)
  391. (cadr link)))))))))
  392. ;;;###autoload
  393. (defun info-xref-docstrings (filename-list)
  394. ;; checkdoc-params: (filename-list)
  395. "Check docstring info node references in source files.
  396. The given files are searched for docstring hyperlinks like
  397. Info node `(elisp)Documentation Tips'
  398. and those links checked by attempting to visit the target nodes
  399. as per `info-xref-check' does.
  400. Interactively filenames are read as a wildcard pattern like
  401. \"foo*.el\", with the current file as a default. Usually this
  402. will be lisp sources, but anything with such hyperlinks can be
  403. checked, including the Emacs .c sources (or the etc/DOC file of
  404. all builtins).
  405. Because info node hyperlinks are found by a simple regexp search
  406. in the files, the Lisp code checked doesn't have to be loaded,
  407. and links can be in the file commentary or elsewhere too. Even
  408. .elc files can usually be checked successfully if you don't have
  409. the sources handy."
  410. (interactive
  411. (let* ((default (and buffer-file-name
  412. (file-relative-name buffer-file-name)))
  413. (prompt (if default
  414. (format "Filename with wildcards (%s): "
  415. default)
  416. "Filename with wildcards: "))
  417. (pattern (read-file-name prompt nil default))
  418. ;; absolute filenames
  419. (filename-list (file-expand-wildcards pattern t))
  420. newlist)
  421. (setq filename-list
  422. (dolist (file filename-list (nreverse newlist))
  423. (or (info-xref-lock-file-p file)
  424. (file-directory-p file)
  425. (push file newlist))))
  426. (unless filename-list
  427. (error "No files: %S" pattern))
  428. (list filename-list)))
  429. (eval-and-compile
  430. (require 'help-mode)) ;; for `help-xref-info-regexp'
  431. (info-xref-with-output
  432. (dolist (info-xref-filename filename-list)
  433. (setq info-xref-xfile-alist nil) ;; "not found"s once per file
  434. (info-xref-with-file info-xref-filename
  435. (goto-char (point-min))
  436. (while (re-search-forward help-xref-info-regexp nil t)
  437. (let ((node (match-string 2)))
  438. (save-excursion
  439. (goto-char (match-beginning 2)) ;; start of node as error position
  440. ;; skip nodes with "%" as probably `format' strings such as in
  441. ;; info-look.el
  442. (unless (string-match "%" node)
  443. ;; "(emacs)" is the default manual for docstring hyperlinks,
  444. ;; per `help-make-xrefs'
  445. (unless (string-match "\\`(" node)
  446. (setq node (concat "(emacs)" node)))
  447. (info-xref-check-node node)))))))))
  448. (provide 'info-xref)
  449. ;;; info-xref.el ends here