info-xref.el 22 KB

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