informat.el 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. ;;; informat.el --- info support functions package for Emacs
  2. ;; Copyright (C) 1986, 2001-2012 Free Software Foundation, Inc.
  3. ;; Maintainer: FSF
  4. ;; Keywords: help
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs 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.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; Nowadays, the Texinfo formatting commands always tagify a buffer
  18. ;; (as does `makeinfo') since @anchor commands need tag tables.
  19. ;;; Code:
  20. (require 'info)
  21. (declare-function texinfo-format-refill "texinfmt" ())
  22. ;; From texinfmt.el
  23. (defvar texinfo-command-start)
  24. (defvar texinfo-command-end)
  25. ;;;###autoload
  26. (defun Info-tagify (&optional input-buffer-name)
  27. "Create or update Info file tag table in current buffer or in a region."
  28. (interactive)
  29. ;; Save and restore point and restrictions.
  30. ;; save-restrictions would not work
  31. ;; because it records the old max relative to the end.
  32. ;; We record it relative to the beginning.
  33. (if input-buffer-name
  34. (message "Tagifying region in %s ..." input-buffer-name)
  35. (message
  36. "Tagifying %s ..." (file-name-nondirectory (buffer-file-name))))
  37. (let ((omin (point-min))
  38. (omax (point-max))
  39. (nomax (= (point-max) (1+ (buffer-size))))
  40. (opoint (point)))
  41. (unwind-protect
  42. (progn
  43. (widen)
  44. (goto-char (point-min))
  45. (if (search-forward "\^_\nIndirect:\n" nil t)
  46. (message
  47. "Cannot tagify split info file. Run this before splitting.")
  48. (let (tag-list
  49. refillp
  50. (case-fold-search t)
  51. (regexp
  52. (concat
  53. "\\("
  54. "\\("
  55. "@anchor" ; match-string 2 matches @anchor
  56. "\\)"
  57. "\\(-no\\|-yes\\)" ; match-string 3 matches -no or -yes
  58. "\\("
  59. "-refill"
  60. "\\)"
  61. "\\("
  62. "{"
  63. "\\)"
  64. "\\("
  65. "[^}]+" ; match-string 6 matches arg to anchor
  66. "\\)"
  67. "\\("
  68. "}"
  69. "\\)"
  70. "\\|"
  71. "\\("
  72. "\n\^_\\(\^L\\)?"
  73. "\\)"
  74. "\\("
  75. "\n\\(File:[ \t]*\\([^,\n\t]*\\)[,\t\n]+[ \t\n]*\\)?"
  76. "Node:[ \t]*"
  77. "\\("
  78. "[^,\n\t]*" ; match-string 13 matches arg to node name
  79. "\\)"
  80. "[,\t\n]"
  81. "\\)"
  82. "\\)"
  83. )))
  84. (while (re-search-forward regexp nil t)
  85. (if (string-equal "@anchor" (match-string 2))
  86. (progn
  87. ;; kludge lest lose match-data
  88. (if (string-equal "-yes" (match-string 3))
  89. (setq refillp t))
  90. (setq tag-list
  91. (cons (list
  92. (concat "Ref: " (match-string 6))
  93. (match-beginning 0))
  94. tag-list))
  95. (if (eq refillp t)
  96. ;; set start and end so texinfo-format-refill works
  97. (let ((texinfo-command-start (match-beginning 0))
  98. (texinfo-command-end (match-end 0)))
  99. (texinfo-format-refill))
  100. (delete-region (match-beginning 0) (match-end 0))))
  101. ;; else this is a Node
  102. (setq tag-list
  103. (cons (list
  104. (concat "Node: " (match-string-no-properties 13))
  105. (1+ (match-beginning 10)))
  106. tag-list))))
  107. (goto-char (point-max))
  108. (forward-line -8)
  109. (let ((buffer-read-only nil))
  110. (if (search-forward "\^_\nEnd tag table\n" nil t)
  111. (let ((end (point)))
  112. (search-backward "\nTag table:\n")
  113. (beginning-of-line)
  114. (delete-region (point) end)))
  115. (goto-char (point-max))
  116. (or (bolp)
  117. (newline))
  118. (insert "\^_\f\nTag table:\n")
  119. (if (eq major-mode 'info-mode)
  120. (move-marker Info-tag-table-marker (point)))
  121. (setq tag-list (nreverse tag-list))
  122. (while tag-list
  123. (insert (car (car tag-list)) ?\177)
  124. (princ (car (cdr (car tag-list))) (current-buffer))
  125. (insert ?\n)
  126. (setq tag-list (cdr tag-list)))
  127. (insert "\^_\nEnd tag table\n")))))
  128. (goto-char opoint)
  129. (narrow-to-region omin (if nomax (1+ (buffer-size))
  130. (min omax (point-max))))))
  131. (if input-buffer-name
  132. (message "Tagifying region in %s done" input-buffer-name)
  133. (message
  134. "Tagifying %s done" (file-name-nondirectory (buffer-file-name)))))
  135. ;;;###autoload
  136. (defcustom Info-split-threshold 262144
  137. "The number of characters by which `Info-split' splits an info file."
  138. :type 'integer
  139. :version "23.1"
  140. :group 'texinfo)
  141. ;;;###autoload
  142. (defun Info-split ()
  143. "Split an info file into an indirect file plus bounded-size subfiles.
  144. Each subfile will be up to the number of characters that
  145. `Info-split-threshold' specifies, plus one node.
  146. To use this command, first visit a large Info file that has a tag
  147. table. The buffer is modified into a (small) indirect info file which
  148. should be saved in place of the original visited file.
  149. The subfiles are written in the same directory the original file is
  150. in, with names generated by appending `-' and a number to the original
  151. file name. The indirect file still functions as an Info file, but it
  152. contains just the tag table and a directory of subfiles."
  153. (interactive)
  154. (if (< (buffer-size) (+ 20000 Info-split-threshold))
  155. (error "This is too small to be worth splitting"))
  156. (goto-char (point-min))
  157. (search-forward "\^_")
  158. (forward-char -1)
  159. (let ((start (point))
  160. (chars-deleted 0)
  161. subfiles
  162. (subfile-number 1)
  163. (case-fold-search t)
  164. (filename (file-name-sans-versions buffer-file-name)))
  165. (goto-char (point-max))
  166. (forward-line -8)
  167. (setq buffer-read-only nil)
  168. (or (search-forward "\^_\nEnd tag table\n" nil t)
  169. (error "Tag table required; use M-x Info-tagify"))
  170. (search-backward "\nTag table:\n")
  171. (if (looking-at "\nTag table:\n\^_")
  172. (error "Tag table is just a skeleton; use M-x Info-tagify"))
  173. (beginning-of-line)
  174. (forward-char 1)
  175. (save-restriction
  176. (narrow-to-region (point-min) (point))
  177. (goto-char (point-min))
  178. (while (< (1+ (point)) (point-max))
  179. (goto-char (min (+ (point) Info-split-threshold) (point-max)))
  180. (search-forward "\^_" nil 'move)
  181. (setq subfiles
  182. (cons (list (+ start chars-deleted)
  183. (concat (file-name-nondirectory filename)
  184. (format "-%d" subfile-number)))
  185. subfiles))
  186. ;; Put a newline at end of split file, to make Unix happier.
  187. (insert "\n")
  188. (write-region (point-min) (point)
  189. (concat filename (format "-%d" subfile-number)))
  190. (delete-region (1- (point)) (point))
  191. ;; Back up over the final ^_.
  192. (forward-char -1)
  193. (setq chars-deleted (+ chars-deleted (- (point) start)))
  194. (delete-region start (point))
  195. (setq subfile-number (1+ subfile-number))))
  196. (while subfiles
  197. (goto-char start)
  198. (insert (nth 1 (car subfiles))
  199. (format ": %d" (1- (car (car subfiles))))
  200. "\n")
  201. (setq subfiles (cdr subfiles)))
  202. (goto-char start)
  203. (insert "\^_\nIndirect:\n")
  204. (search-forward "\nTag Table:\n")
  205. (insert "(Indirect)\n")))
  206. (defvar Info-validate-allnodes)
  207. (defvar Info-validate-thisnode)
  208. (defvar Info-validate-lossages)
  209. ;;;###autoload
  210. (defun Info-validate ()
  211. "Check current buffer for validity as an Info file.
  212. Check that every node pointer points to an existing node."
  213. (interactive)
  214. (save-excursion
  215. (save-restriction
  216. (widen)
  217. (goto-char (point-min))
  218. (if (search-forward "\nTag table:\n(Indirect)\n" nil t)
  219. (error "Don't yet know how to validate indirect info files: \"%s\""
  220. (buffer-name (current-buffer))))
  221. (goto-char (point-min))
  222. (let ((Info-validate-allnodes '(("*")))
  223. (regexp "Node:[ \t]*\\([^,\n\t]*\\)[,\t\n]")
  224. (case-fold-search t)
  225. (tags-losing nil)
  226. (Info-validate-lossages ()))
  227. (while (search-forward "\n\^_" nil t)
  228. (forward-line 1)
  229. (let ((beg (point)))
  230. (forward-line 1)
  231. (if (re-search-backward regexp beg t)
  232. (let ((name (downcase
  233. (buffer-substring-no-properties
  234. (match-beginning 1)
  235. (progn
  236. (goto-char (match-end 1))
  237. (skip-chars-backward " \t")
  238. (point))))))
  239. (if (assoc name Info-validate-allnodes)
  240. (setq Info-validate-lossages
  241. (cons (list name "Duplicate node-name" nil)
  242. Info-validate-lossages))
  243. (setq Info-validate-allnodes
  244. (cons (list name
  245. (progn
  246. (end-of-line)
  247. (and (re-search-backward
  248. "prev[ious]*:" beg t)
  249. (progn
  250. (goto-char (match-end 0))
  251. (downcase
  252. (Info-following-node-name)))))
  253. beg)
  254. Info-validate-allnodes)))))))
  255. (goto-char (point-min))
  256. (while (search-forward "\n\^_" nil t)
  257. (forward-line 1)
  258. (let ((beg (point))
  259. Info-validate-thisnode next)
  260. (forward-line 1)
  261. (if (re-search-backward regexp beg t)
  262. (save-restriction
  263. (let ((md (match-data)))
  264. (search-forward "\n\^_" nil 'move)
  265. (narrow-to-region beg (point))
  266. (set-match-data md))
  267. (setq Info-validate-thisnode (downcase
  268. (buffer-substring-no-properties
  269. (match-beginning 1)
  270. (progn
  271. (goto-char (match-end 1))
  272. (skip-chars-backward " \t")
  273. (point)))))
  274. (end-of-line)
  275. (and (search-backward "next:" nil t)
  276. (setq next (Info-validate-node-name "invalid Next"))
  277. (assoc next Info-validate-allnodes)
  278. (if (equal (car (cdr (assoc next Info-validate-allnodes)))
  279. Info-validate-thisnode)
  280. ;; allow multiple `next' pointers to one node
  281. (let ((tem Info-validate-lossages))
  282. (while tem
  283. (if (and (equal (car (cdr (car tem)))
  284. "should have Previous")
  285. (equal (car (car tem))
  286. next))
  287. (setq Info-validate-lossages
  288. (delq (car tem) Info-validate-lossages)))
  289. (setq tem (cdr tem))))
  290. (setq Info-validate-lossages
  291. (cons (list next
  292. "should have Previous"
  293. Info-validate-thisnode)
  294. Info-validate-lossages))))
  295. (end-of-line)
  296. (if (re-search-backward "prev[ious]*:" nil t)
  297. (Info-validate-node-name "invalid Previous"))
  298. (end-of-line)
  299. (if (search-backward "up:" nil t)
  300. (Info-validate-node-name "invalid Up"))
  301. (if (re-search-forward "\n* Menu:" nil t)
  302. (while (re-search-forward "\n\\* " nil t)
  303. (Info-validate-node-name
  304. (concat "invalid menu item "
  305. (buffer-substring (point)
  306. (save-excursion
  307. (skip-chars-forward "^:")
  308. (point))))
  309. (Info-extract-menu-node-name))))
  310. (goto-char (point-min))
  311. (while (re-search-forward "\\*note[ \n]*[^:\t]*:" nil t)
  312. (goto-char (+ (match-beginning 0) 5))
  313. (skip-chars-forward " \n")
  314. (Info-validate-node-name
  315. (concat "invalid reference "
  316. (buffer-substring (point)
  317. (save-excursion
  318. (skip-chars-forward "^:")
  319. (point))))
  320. (Info-extract-menu-node-name "Bad format cross-reference")))))))
  321. (setq tags-losing (not (Info-validate-tags-table)))
  322. (if (or Info-validate-lossages tags-losing)
  323. (with-output-to-temp-buffer " *problems in info file*"
  324. (while Info-validate-lossages
  325. (princ "In node \"")
  326. (princ (car (car Info-validate-lossages)))
  327. (princ "\", ")
  328. (let ((tem (nth 1 (car Info-validate-lossages))))
  329. (cond ((string-match "\n" tem)
  330. (princ (substring tem 0 (match-beginning 0)))
  331. (princ "..."))
  332. (t
  333. (princ tem))))
  334. (if (nth 2 (car Info-validate-lossages))
  335. (progn
  336. (princ ": ")
  337. (let ((tem (nth 2 (car Info-validate-lossages))))
  338. (cond ((string-match "\n" tem)
  339. (princ (substring tem 0 (match-beginning 0)))
  340. (princ "..."))
  341. (t
  342. (princ tem))))))
  343. (terpri)
  344. (setq Info-validate-lossages (cdr Info-validate-lossages)))
  345. (if tags-losing (princ "\nTags table must be recomputed\n")))
  346. ;; Here if info file is valid.
  347. ;; If we already made a list of problems, clear it out.
  348. (save-excursion
  349. (if (get-buffer " *problems in info file*")
  350. (progn
  351. (set-buffer " *problems in info file*")
  352. (kill-buffer (current-buffer)))))
  353. (message "File appears valid"))))))
  354. (defun Info-validate-node-name (kind &optional name)
  355. (if name
  356. nil
  357. (goto-char (match-end 0))
  358. (skip-chars-forward " \t")
  359. (if (= (following-char) ?\()
  360. nil
  361. (setq name
  362. (buffer-substring-no-properties
  363. (point)
  364. (progn
  365. (skip-chars-forward "^,\t\n")
  366. (skip-chars-backward " ")
  367. (point))))))
  368. (if (null name)
  369. nil
  370. (setq name (downcase name))
  371. (or (and (> (length name) 0) (= (aref name 0) ?\())
  372. (assoc name Info-validate-allnodes)
  373. (setq Info-validate-lossages
  374. (cons (list Info-validate-thisnode kind name)
  375. Info-validate-lossages))))
  376. name)
  377. (defun Info-validate-tags-table ()
  378. (goto-char (point-min))
  379. (if (not (search-forward "\^_\nEnd tag table\n" nil t))
  380. t
  381. (not (catch 'losing
  382. (let* ((end (match-beginning 0))
  383. (start (progn (search-backward "\nTag table:\n")
  384. (1- (match-end 0))))
  385. tem)
  386. (setq tem Info-validate-allnodes)
  387. (while tem
  388. (goto-char start)
  389. (or (equal (car (car tem)) "*")
  390. (search-forward (concat "Node: "
  391. (car (car tem))
  392. "\177")
  393. end t)
  394. (throw 'losing 'x))
  395. (setq tem (cdr tem)))
  396. (goto-char (1+ start))
  397. (while (looking-at ".*Node: \\(.*\\)\177\\([0-9]+\\)$")
  398. (setq tem (downcase (buffer-substring-no-properties
  399. (match-beginning 1)
  400. (match-end 1))))
  401. (setq tem (assoc tem Info-validate-allnodes))
  402. (if (or (not tem)
  403. (< 1000 (progn
  404. (goto-char (match-beginning 2))
  405. (setq tem (- (car (cdr (cdr tem)))
  406. (read (current-buffer))))
  407. (if (> tem 0) tem (- tem)))))
  408. (throw 'losing 'y))
  409. (forward-line 1)))
  410. (if (looking-at "\^_\n")
  411. (forward-line 1))
  412. (or (looking-at "End tag table\n")
  413. (throw 'losing 'z))
  414. nil))))
  415. ;;;###autoload
  416. (defun batch-info-validate ()
  417. "Runs `Info-validate' on the files remaining on the command line.
  418. Must be used only with -batch, and kills Emacs on completion.
  419. Each file will be processed even if an error occurred previously.
  420. For example, invoke \"emacs -batch -f batch-info-validate $info/ ~/*.info\""
  421. (if (not noninteractive)
  422. (error "batch-info-validate may only be used -batch"))
  423. (let ((version-control t)
  424. (auto-save-default nil)
  425. (find-file-run-dired nil)
  426. (kept-old-versions 259259)
  427. (kept-new-versions 259259))
  428. (let ((error 0)
  429. file
  430. (files ()))
  431. (while command-line-args-left
  432. (setq file (expand-file-name (car command-line-args-left)))
  433. (cond ((not (file-exists-p file))
  434. (message ">> %s does not exist!" file)
  435. (setq error 1
  436. command-line-args-left (cdr command-line-args-left)))
  437. ((file-directory-p file)
  438. (setq command-line-args-left (nconc (directory-files file)
  439. (cdr command-line-args-left))))
  440. (t
  441. (setq files (cons file files)
  442. command-line-args-left (cdr command-line-args-left)))))
  443. (while files
  444. (setq file (car files)
  445. files (cdr files))
  446. (let ((lose nil))
  447. (condition-case err
  448. (progn
  449. (if buffer-file-name (kill-buffer (current-buffer)))
  450. (find-file file)
  451. (buffer-disable-undo (current-buffer))
  452. (set-buffer-modified-p nil)
  453. (fundamental-mode)
  454. (let ((case-fold-search nil))
  455. (goto-char (point-max))
  456. (cond ((search-backward "\n\^_\^L\nTag table:\n" nil t)
  457. (message "%s already tagified" file))
  458. ((< (point-max) 30000)
  459. (message "%s too small to bother tagifying" file))
  460. (t
  461. (Info-tagify))))
  462. (let ((loss-name " *problems in info file*"))
  463. (message "Checking validity of info file %s..." file)
  464. (if (get-buffer loss-name)
  465. (kill-buffer loss-name))
  466. (Info-validate)
  467. (if (not (get-buffer loss-name))
  468. nil ;(message "Checking validity of info file %s... OK" file)
  469. (message "----------------------------------------------------------------------")
  470. (message ">> PROBLEMS IN INFO FILE %s" file)
  471. (with-current-buffer loss-name
  472. (princ (buffer-substring-no-properties
  473. (point-min) (point-max))))
  474. (message "----------------------------------------------------------------------")
  475. (setq error 1 lose t)))
  476. (if (and (buffer-modified-p)
  477. (not lose))
  478. (progn (message "Saving modified %s" file)
  479. (save-buffer))))
  480. (error (message ">> Error: %s" (prin1-to-string err))))))
  481. (kill-emacs error))))
  482. (provide 'informat)
  483. ;;; informat.el ends here