informat.el 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. ;; Info support functions package for Emacs
  2. ;; Copyright (C) 1986 Free Software Foundation, Inc.
  3. ;; This file is part of GNU Emacs.
  4. ;; GNU Emacs is distributed in the hope that it will be useful,
  5. ;; but WITHOUT ANY WARRANTY. No author or distributor
  6. ;; accepts responsibility to anyone for the consequences of using it
  7. ;; or for whether it serves any particular purpose or works at all,
  8. ;; unless he says so in writing. Refer to the GNU Emacs General Public
  9. ;; License for full details.
  10. ;; Everyone is granted permission to copy, modify and redistribute
  11. ;; GNU Emacs, but only under the conditions described in the
  12. ;; GNU Emacs General Public License. A copy of this license is
  13. ;; supposed to have been given to you along with GNU Emacs so you
  14. ;; can know your rights and responsibilities. It should be in a
  15. ;; file named COPYING. Among other things, the copyright notice
  16. ;; and this notice must be preserved on all copies.
  17. (require 'info)
  18. (defun Info-tagify ()
  19. "Create or update Info-file tag table in current buffer."
  20. (interactive)
  21. ;; Save and restore point and restrictions.
  22. ;; save-restrictions would not work
  23. ;; because it records the old max relative to the end.
  24. ;; We record it relative to the beginning.
  25. (let ((omin (point-min))
  26. (omax (point-max))
  27. (nomax (= (point-max) (1+ (buffer-size))))
  28. (opoint (point)))
  29. (unwind-protect
  30. (progn
  31. (widen)
  32. (goto-char (point-min))
  33. (if (search-forward "\^_\f\nTag table:\n" nil t)
  34. (message "Cannot tagify split info file")
  35. (let ((regexp "Node:[ \t]*\\([^,\n\t]\\)*[,\t\n]")
  36. (case-fold-search t)
  37. list)
  38. (while (search-forward "\n\^_" nil t)
  39. (forward-line 1)
  40. (let ((beg (point)))
  41. (forward-line 1)
  42. (if (re-search-backward regexp beg t)
  43. (setq list
  44. (cons (list (buffer-substring
  45. (match-beginning 1)
  46. (match-end 1))
  47. beg)
  48. list)))))
  49. (goto-char (point-max))
  50. (forward-line -8)
  51. (let ((buffer-read-only nil))
  52. (if (search-forward "\^_\nEnd tag table\n" nil t)
  53. (let ((end (point)))
  54. (search-backward "\nTag table:\n")
  55. (beginning-of-line)
  56. (delete-region (point) end)))
  57. (goto-char (point-max))
  58. (insert "\^_\f\nTag table:\n")
  59. (move-marker Info-tag-table-marker (point))
  60. (setq list (nreverse list))
  61. (while list
  62. (insert "Node: " (car (car list)) ?\177)
  63. (princ (car (cdr (car list))) (current-buffer))
  64. (insert ?\n)
  65. (setq list (cdr list)))
  66. (insert "\^_\nEnd tag table\n")))))
  67. (goto-char opoint)
  68. (narrow-to-region omin (if nomax (1+ (buffer-size))
  69. (min omax (point-max)))))))
  70. (defun Info-split ()
  71. "Split an info file into an indirect file plus bounded-size subfiles.
  72. Each subfile will be up 50000 characters plus one node.
  73. To use this command, first visit a large Info file that has a tag table.
  74. The buffer is modified into a (small) indirect info file
  75. which should be saved in place of the original visited file.
  76. The subfiles are written in the same directory the original file is in,
  77. with names generated by appending `-' and a number to the original file name.
  78. The indirect file still functions as an Info file, but it contains
  79. just the tag table and a directory of subfiles."
  80. (interactive)
  81. (if (< (buffer-size) 70000)
  82. (error "This is too small to be worth splitting"))
  83. (goto-char (point-min))
  84. (search-forward "\^_")
  85. (forward-char -1)
  86. (let ((start (point))
  87. (chars-deleted 0)
  88. subfiles
  89. (subfile-number 1))
  90. (goto-char (point-max))
  91. (forward-line -8)
  92. (setq buffer-read-only nil)
  93. (or (search-forward "\^_\nEnd tag table\n" nil t)
  94. (error "Tag table required in order to split an Info file."))
  95. (search-backward "\nTag table:\n")
  96. (if (looking-at "\nTag table:\n\^_")
  97. (error "Tag table is just a skeleton; use M-x Info-tagify."))
  98. (beginning-of-line)
  99. (forward-char 1)
  100. (save-restriction
  101. (narrow-to-region (point-min) (point))
  102. (goto-char (point-min))
  103. (while (< (1+ (point)) (point-max))
  104. (goto-char (min (+ (point) 50000) (point-max)))
  105. (search-forward "\^_" nil 'move)
  106. (setq subfiles
  107. (cons (list (+ start chars-deleted)
  108. (concat (file-name-nondirectory buffer-file-name)
  109. (format "-%d" subfile-number)))
  110. subfiles))
  111. (write-region (point-min) (point)
  112. (concat buffer-file-name
  113. (format "-%d" subfile-number)))
  114. ;; Back up over the final ^_.
  115. (forward-char -1)
  116. (setq chars-deleted (+ chars-deleted (- (point) start)))
  117. (delete-region start (point))
  118. (setq subfile-number (1+ subfile-number))))
  119. (while subfiles
  120. (goto-char start)
  121. (insert (nth 1 (car subfiles))
  122. (format ": %d" (car (car subfiles)))
  123. "\n")
  124. (setq subfiles (cdr subfiles)))
  125. (goto-char start)
  126. (insert "\^_\nIndirect:\n")
  127. (search-forward "\nTag Table:\n")
  128. (insert "(Indirect)\n")))
  129. (defun Info-validate ()
  130. "Check current buffer for validity as an Info file.
  131. Check that every node pointer points to an existing node."
  132. (interactive)
  133. (save-excursion
  134. (save-restriction
  135. (widen)
  136. (goto-char (point-min))
  137. (if (search-forward "\nTag table:\n(Indirect)\n" nil t)
  138. (error "Don't yet know how to validate indirect info files: \"%s\""
  139. (buffer-name (current-buffer))))
  140. (goto-char (point-min))
  141. (let ((allnodes '(("*")))
  142. (regexp "Node:[ \t]*\\([^,\n\t]*\\)[,\t\n]")
  143. (case-fold-search t)
  144. (tags-losing nil)
  145. (lossages ()))
  146. (while (search-forward "\n\^_" nil t)
  147. (forward-line 1)
  148. (let ((beg (point)))
  149. (forward-line 1)
  150. (if (re-search-backward regexp beg t)
  151. (let ((name (downcase
  152. (buffer-substring
  153. (match-beginning 1)
  154. (progn
  155. (goto-char (match-end 1))
  156. (skip-chars-backward " \t")
  157. (point))))))
  158. (if (assoc name allnodes)
  159. (setq lossages
  160. (cons (list name "Duplicate node-name" nil)
  161. lossages))
  162. (setq allnodes
  163. (cons (list name
  164. (progn
  165. (end-of-line)
  166. (and (re-search-backward
  167. "prev[ious]*:" beg t)
  168. (progn
  169. (goto-char (match-end 0))
  170. (downcase
  171. (Info-following-node-name)))))
  172. beg)
  173. allnodes)))))))
  174. (goto-char (point-min))
  175. (while (search-forward "\n\^_" nil t)
  176. (forward-line 1)
  177. (let ((beg (point))
  178. thisnode next)
  179. (forward-line 1)
  180. (if (re-search-backward regexp beg t)
  181. (save-restriction
  182. (search-forward "\n\^_" nil 'move)
  183. (narrow-to-region beg (point))
  184. (setq thisnode (downcase
  185. (buffer-substring
  186. (match-beginning 1)
  187. (progn
  188. (goto-char (match-end 1))
  189. (skip-chars-backward " \t")
  190. (point)))))
  191. (end-of-line)
  192. (and (search-backward "next:" nil t)
  193. (setq next (Info-validate-node-name "Next"))
  194. (if (equal (car (cdr (assoc next allnodes)))
  195. thisnode)
  196. ;; allow multiple `next' pointers to one node
  197. (let ((tem lossages))
  198. (while tem
  199. (if (and (equal (car (cdr (car tem)))
  200. "Previous-pointer in Next")
  201. (equal (car (cdr (cdr (car tem))))
  202. next))
  203. (setq lossages (delq (car tem) lossages)))
  204. (setq tem (cdr tem))))
  205. (setq lossages
  206. (cons (list thisnode
  207. "Previous-pointer in Next"
  208. next)
  209. lossages))))
  210. (end-of-line)
  211. (if (re-search-backward "prev[ious]*:" nil t)
  212. (Info-validate-node-name "Previous"))
  213. (end-of-line)
  214. (if (search-backward "up:" nil t)
  215. (Info-validate-node-name "Up"))
  216. (if (re-search-forward "\n* Menu:" nil t)
  217. (while (re-search-forward "\n\\* " nil t)
  218. (Info-validate-node-name
  219. (concat "menu item "
  220. (buffer-substring (point)
  221. (save-excursion
  222. (skip-chars-forward "^:")
  223. (point))))
  224. (Info-extract-menu-node-name))))
  225. (goto-char (point-min))
  226. (while (re-search-forward "\\*note[ \n]*[^:\t]*:" nil t)
  227. (goto-char (+ (match-beginning 0) 5))
  228. (skip-chars-forward " \n")
  229. (Info-validate-node-name
  230. (concat "reference "
  231. (buffer-substring (point)
  232. (save-excursion
  233. (skip-chars-forward "^:")
  234. (point))))
  235. (Info-extract-menu-node-name "Bad format cross-reference")))))))
  236. (setq tags-losing (not (Info-validate-tags-table)))
  237. (if (or lossages tags-losing)
  238. (with-output-to-temp-buffer " *problems in info file*"
  239. (while lossages
  240. (princ "In node \"")
  241. (princ (car (car lossages)))
  242. (princ "\", invalid ")
  243. (let ((tem (nth 1 (car lossages))))
  244. (cond ((string-match "\n" tem)
  245. (princ (substring tem 0 (match-beginning 0)))
  246. (princ "..."))
  247. (t
  248. (princ tem))))
  249. (if (nth 2 (car lossages))
  250. (progn
  251. (princ ": ")
  252. (let ((tem (nth 2 (car lossages))))
  253. (cond ((string-match "\n" tem)
  254. (princ (substring tem 0 (match-beginning 0)))
  255. (princ "..."))
  256. (t
  257. (princ tem))))))
  258. (terpri)
  259. (setq lossages (cdr lossages)))
  260. (if tags-losing (princ "\nTags table must be recomputed\n")))
  261. ;; Here if info file is valid.
  262. ;; If we already made a list of problems, clear it out.
  263. (save-excursion
  264. (if (get-buffer " *problems in info file*")
  265. (progn
  266. (set-buffer " *problems in info file*")
  267. (kill-buffer (current-buffer)))))
  268. (message "File appears valid"))))))
  269. (defun Info-validate-node-name (kind &optional name)
  270. (if name
  271. nil
  272. (goto-char (match-end 0))
  273. (skip-chars-forward " \t")
  274. (if (= (following-char) ?\()
  275. nil
  276. (setq name
  277. (buffer-substring
  278. (point)
  279. (progn
  280. (skip-chars-forward "^,\t\n")
  281. (skip-chars-backward " ")
  282. (point))))))
  283. (if (null name)
  284. nil
  285. (setq name (downcase name))
  286. (or (and (> (length name) 0) (= (aref name 0) ?\())
  287. (assoc name allnodes)
  288. (setq lossages
  289. (cons (list thisnode kind name) lossages))))
  290. name)
  291. (defun Info-validate-tags-table ()
  292. (goto-char (point-min))
  293. (if (not (search-forward "\^_\nEnd tag table\n" nil t))
  294. t
  295. (not (catch 'losing
  296. (let* ((end (match-beginning 0))
  297. (start (progn (search-backward "\nTag table:\n")
  298. (1- (match-end 0))))
  299. tem)
  300. (setq tem allnodes)
  301. (while tem
  302. (goto-char start)
  303. (or (equal (car (car tem)) "*")
  304. (search-forward (concat "Node: "
  305. (car (car tem))
  306. "\177")
  307. end t)
  308. (throw 'losing 'x))
  309. (setq tem (cdr tem)))
  310. (goto-char (1+ start))
  311. (while (looking-at ".*Node: \\(.*\\)\177\\([0-9]+\\)$")
  312. (setq tem (downcase (buffer-substring
  313. (match-beginning 1)
  314. (match-end 1))))
  315. (setq tem (assoc tem allnodes))
  316. (if (or (not tem)
  317. (< 1000 (progn
  318. (goto-char (match-beginning 2))
  319. (setq tem (- (car (cdr (cdr tem)))
  320. (read (current-buffer))))
  321. (if (> tem 0) tem (- tem)))))
  322. (throw 'losing 'y)))
  323. (forward-line 1))
  324. (or (looking-at "End tag table\n")
  325. (throw 'losing 'z))
  326. nil))))
  327. (defun batch-info-validate ()
  328. "Runs Info-validate on the files remaining on the command line.
  329. Must be used only with -batch, and kills emacs on completion.
  330. Each file will be processed even if an error occurred previously.
  331. For example, invoke \"emacs -batch -f batch-info-validate $info/ ~/*.info\""
  332. (if (not noninteractive)
  333. (error "batch-info-validate may only be used -batch."))
  334. (let ((version-control t)
  335. (auto-save-default nil)
  336. (find-file-run-dired nil)
  337. (kept-old-versions 259259)
  338. (kept-new-versions 259259))
  339. (let ((error 0)
  340. file
  341. (files ()))
  342. (while command-line-args-left
  343. (setq file (expand-file-name (car command-line-args-left)))
  344. (cond ((not (file-exists-p file))
  345. (message ">> %s does not exist!" file)
  346. (setq error 1
  347. command-line-args-left (cdr command-line-args-left)))
  348. ((file-directory-p file)
  349. (setq command-line-args-left (nconc (directory-files file)
  350. (cdr command-line-args-left))))
  351. (t
  352. (setq files (cons file files)
  353. command-line-args-left (cdr command-line-args-left)))))
  354. (while files
  355. (setq file (car files)
  356. files (cdr files))
  357. (let ((lose nil))
  358. (condition-case err
  359. (progn
  360. (if buffer-file-name (kill-buffer (current-buffer)))
  361. (find-file file)
  362. (buffer-flush-undo (current-buffer))
  363. (set-buffer-modified-p nil)
  364. (fundamental-mode)
  365. (let ((case-fold-search nil))
  366. (goto-char (point-max))
  367. (cond ((search-backward "\n\^_\^L\nTag table:\n" nil t)
  368. (message "%s already tagified" file))
  369. ((< (point-max) 30000)
  370. (message "%s too small to bother tagifying" file))
  371. (t
  372. (message "Tagifying %s..." file)
  373. (Info-tagify)
  374. (message "Tagifying %s...done" file))))
  375. (let ((loss-name " *problems in info file*"))
  376. (message "Checking validity of info file %s..." file)
  377. (if (get-buffer loss-name)
  378. (kill-buffer loss-name))
  379. (Info-validate)
  380. (if (not (get-buffer loss-name))
  381. nil ;(message "Checking validity of info file %s... OK" file)
  382. (message "----------------------------------------------------------------------")
  383. (message ">> PROBLEMS IN INFO FILE %s" file)
  384. (save-excursion
  385. (set-buffer loss-name)
  386. (princ (buffer-substring (point-min) (point-max))))
  387. (message "----------------------------------------------------------------------")
  388. (setq error 1 lose t)))
  389. (if (and (buffer-modified-p)
  390. (not lose))
  391. (progn (message "Saving modified %s" file)
  392. (save-buffer))))
  393. (error (message ">> Error: %s" (prin1-to-string err))))))
  394. (kill-emacs error))))