Debian Codegraph @

maint/install-info.el

raw
1 ;;; install-info.el --- install-info in Emacs Lisp
2
3 ;; Copyright (C) 2000 Tetsuo Tsukamoto <[email protected]>
4
5 ;; Author: Tetsuo Tsukamoto <[email protected]>
6 ;; Keywords: docs, help
7
8 ;; This file is not part of GNU Emacs.
9
10 ;; This program is free software: you can redistribute it and/or
11 ;; modify it under the terms of the GNU General Public License as
12 ;; published by the Free Software Foundation, either version 3 of
13 ;; the License, or (at your option) any later version.
14
15 ;; This program is distributed in the hope that it will be
16 ;; useful, but WITHOUT ANY WARRANTY; without even the implied
17 ;; warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
18 ;; PURPOSE. See the GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
25 ;; This program updates info/dir entries, like install-info in GNU texinfo.
26
27 ;;; Code:
28
29 (eval-when-compile
30 (require 'cl-lib))
31
32 (eval-and-compile
33 (ignore-errors
34 (require 'jka-compr)))
35
36 ;; Macros.
37 (defmacro install-info-compressed-name-p (filename)
38 (when (featurep 'jka-compr)
39 `(jka-compr-get-compression-info ,filename)))
40
41 (defmacro install-info-forward-line (n)
42 `(when (< 0 (forward-line ,n))
43 (unless (bolp)
44 (insert "\n"))))
45
46 (put 'install-info-save-point 'lisp-indent-function 0)
47 (defmacro install-info-save-point (&rest forms)
48 `(let ((original-point (point)))
49 (prog1
50 (progn ,@forms)
51 (goto-char original-point))))
52
53 (defmacro install-info-point-at-eol (&optional n)
54 (if (fboundp 'point-at-eol)
55 `(point-at-eol ,n)
56 `(install-info-save-point
57 (if (or (null ,n) (= 1 ,n))
58 (end-of-line)
59 (when (eq (forward-line (- ,n 1)) 0)
60 (end-of-line)))
61 (point))))
62
63 ;; Functions.
64 (defun install-info-skip-blank-lines ()
65 (while (and (eolp) (not (eobp)))
66 (install-info-forward-line 1)))
67
68 (defun install-info-groups (section entry)
69 (let (groups)
70 (dolist (sec section)
71 (setq groups
72 (nconc groups (list (cons sec entry)))))
73 groups))
74
75 (defun install-info-insert-file-contents (file &optional visit beg end replace)
76 (let ((coding-system-for-read 'raw-text))
77 (funcall (if (install-info-compressed-name-p file)
78 #'jka-compr-insert-file-contents
79 #'insert-file-contents)
80 file visit beg end replace)))
81
82 (defun install-info-write-region (start end file &optional append visit)
83 (let ((coding-system-for-write 'raw-text))
84 (funcall (if (install-info-compressed-name-p file)
85 #'jka-compr-write-region
86 #'write-region)
87 start end file append visit)))
88
89 (defun install-info (info-file dir-file &optional entry section delete)
90 "Install or delete dir entries from INFO-FILE in the Info directory file
91 DIR-FILE.
92
93 Optional third arg ENTRY specifies the text inserted as an Info directory
94 entry. ENTRY should have the form of an Info menu item line plus zero or more
95 extra lines starting with whitespace. If you specify more than one entry (i.e.
96 list of entries), they are all added. If you don't specify any entries, they
97 are determined from information in the Info file itself.
98
99 If optional fourth arg SECTION is given, put this file's entries in section
100 SECTION of the directory. If you specify more than one section (i.e. list of
101 sections), all the entries are added in each of the sections. If you don't
102 specify any sections, they are determined from information in the Info file
103 itself.
104
105 If optional fifth arg DELETE is non-nil, delete existing entries for INFO-FILE
106 from DIR-FILE; don't insert any new entries."
107 (interactive "fInfo File: \nFDir File: ")
108 (save-match-data
109 (install-info-1 info-file dir-file entry section delete)))
110
111 (defun install-info-1 (info-file dir-file entry section delete)
112 (let ((buf (get-buffer-create " *install-info-tmp*"))
113 groups)
114 ;;
115 (unless (stringp info-file)
116 (error "%s" "No input file specified"))
117 (setq info-file (expand-file-name info-file))
118 ;;
119 (unless (file-exists-p info-file)
120 (error "No such file or directory for %s" info-file))
121 ;;
122 (unless (stringp dir-file)
123 (error "%s" "No dir file specified"))
124 (setq dir-file (expand-file-name dir-file))
125 ;;
126 (when (stringp entry)
127 (setq entry (list entry)))
128 (when (stringp section)
129 (setq section (list section)))
130 ;;
131 (unless (and entry section)
132 (with-current-buffer buf
133 (setq buffer-read-only nil)
134 (erase-buffer)
135 (install-info-insert-file-contents info-file)))
136 ;;
137 (cond
138 ((and entry section)
139 ;; Both entry and section are given.
140 (unless delete
141 (setq groups (install-info-groups section entry))))
142 (entry
143 ;; Only entry is given. Determine section from the info file.
144 (unless delete
145 (with-current-buffer buf
146 (goto-char (point-min))
147 (while (re-search-forward "^INFO-DIR-SECTION " nil t)
148 (setq section
149 (nconc section
150 (list (buffer-substring
151 (match-end 0)
152 (install-info-point-at-eol)))))))
153 (unless section
154 (setq section (list "Miscellaneous")))
155 (setq groups (install-info-groups section entry))))
156 ((or section delete)
157 ;; Only section is given. Determine entry from the info file.
158 (with-current-buffer buf
159 (goto-char (point-min))
160 (while (re-search-forward "^START-INFO-DIR-ENTRY" nil t)
161 (install-info-forward-line 1)
162 (while (not (looking-at "^END-INFO-DIR-ENTRY"))
163 (let ((start (point))
164 str)
165 (when (eobp)
166 (error
167 "%s"
168 "START-INFO-DIR-ENTRY without matching END-INFO-DIR-ENTRY"))
169 (unless (eolp)
170 (setq str (buffer-substring start (install-info-point-at-eol)))
171 (if (string-match "^\\* " str)
172 (push str entry)
173 (when entry
174 (setcar entry (format "%s\n%s" (car entry) str)))))
175 (install-info-forward-line 1)))))
176 (unless (setq entry (nreverse entry))
177 (error "warning; no info dir entry in %s" info-file))
178 (unless delete
179 (setq groups (install-info-groups section entry))))
180 (t
181 ;; Neither entry nor section is given.
182 (with-current-buffer buf
183 (goto-char (point-min))
184 (while (re-search-forward "^INFO-DIR-SECTION " nil t)
185 (let (section entry)
186 (beginning-of-line)
187 (while (looking-at "^INFO-DIR-SECTION ")
188 ;; Sometimes multiple sections are given for one entry.
189 (setq section
190 (nconc section
191 (list (buffer-substring
192 (match-end 0) (install-info-point-at-eol)))))
193 (install-info-forward-line 1))
194 (install-info-skip-blank-lines)
195 (when (looking-at "^START-INFO-DIR-ENTRY")
196 (install-info-forward-line 1)
197 (while (not (looking-at "^END-INFO-DIR-ENTRY"))
198 (let ((start (point))
199 str)
200 (unless (eolp)
201 (setq str (buffer-substring start
202 (install-info-point-at-eol)))
203 (if (string-match "^\\* " str)
204 (push str entry)
205 ;; Sometimes mutiple lines are used for one entry.
206 (when entry
207 (setcar entry (format "%s\n%s" (car entry) str)))))
208 (install-info-forward-line 1))))
209 (when (and section (setq entry (nreverse entry)))
210 (setq groups
211 (nconc groups
212 (install-info-groups section entry)))))))))
213 ;;
214 (if delete
215 (install-info-delete-entry entry dir-file)
216 (unless groups
217 ;; No section is specified on the info file. Use "Miscellaneous"
218 ;; as the section name.
219 (with-current-buffer buf
220 (goto-char (point-min))
221 (while (re-search-forward "^START-INFO-DIR-ENTRY" nil t)
222 (install-info-forward-line 1)
223 (while (not (looking-at "^END-INFO-DIR-ENTRY"))
224 (let ((start (point))
225 str)
226 (unless (eolp)
227 (setq str (buffer-substring start
228 (install-info-point-at-eol)))
229 (if (string-match "^\\* " str)
230 (push str entry)
231 (when entry
232 (setcar entry (format "%s\n%s" (car entry) str)))))
233 (install-info-forward-line 1)))))
234 (unless (setq entry (nreverse entry))
235 (error "warning; no info dir entry in %s" info-file))
236 (unless section
237 (setq section (list "Miscellaneous")))
238 (setq groups (install-info-groups section entry)))
239 ;;
240 (install-info-add-groups groups dir-file))
241 ;;
242 (kill-buffer buf)))
243
244 (defun install-info-delete-entry (entry dir)
245 ;; Delete all entries given.
246 (setq dir (expand-file-name dir))
247 (let ((buf (get-buffer-create " *install-info-dir*")))
248 (with-current-buffer buf
249 (setq buffer-read-only nil)
250 (erase-buffer)
251 (unless (file-exists-p dir)
252 (error "No such file or directory for %s" dir))
253 (install-info-insert-file-contents dir)
254 (dolist (en entry)
255 (let (start file)
256 ;; XEmacs-style entry is not considered here.
257 (when (string-match "(" en)
258 (setq en (substring en (match-end 0)))
259 (setq file (when (string-match ")" en)
260 (substring en 0 (match-beginning 0)))))
261 (goto-char (point-min))
262 (while (re-search-forward
263 (concat "^\\*.*("
264 (regexp-quote file)
265 "\\(\\.info\\)?)") nil t)
266 (let ((start (match-beginning 0)))
267 (install-info-forward-line 1)
268 (while (not (or (eolp)
269 (looking-at "^\\* ")))
270 (install-info-forward-line 1))
271 (delete-region start (point))))))
272 (install-info-write-region (point-min) (point-max) dir))
273 (kill-buffer buf)))
274
275 (defun install-info-add-groups (groups dir)
276 (setq dir (expand-file-name dir))
277 (let ((buf (get-buffer-create " *install-info-dir*")))
278 (with-current-buffer buf
279 (setq buffer-read-only nil)
280 (erase-buffer)
281 (when (file-exists-p dir)
282 (install-info-insert-file-contents dir))
283 (goto-char (point-min))
284 (unless (let ((case-fold-search t))
285 (and (re-search-forward "^\037" nil t)
286 (re-search-forward "Node:.*Top" nil t)
287 (re-search-forward "^\\* Menu:" nil t)))
288 ;; If the existing dir file is not valid, replace it with a good one.
289 (unless (= 0 (buffer-size))
290 ;; Create a backup file.
291 (install-info-write-region (point-min) (point-max) (concat dir "~")))
292 (erase-buffer)
293 (insert "This is the file .../info/dir, which contains the
294 topmost node of the Info hierarchy, called (dir)Top.
295 The first time you invoke Info you start off looking at this node.
296 \037
297 File: dir, Node: Top This is the top of the INFO tree
298
299 This (the Directory node) gives a menu of major topics.
300 Typing \"q\" exits, \"?\" lists all Info commands, \"d\" returns here,
301 \"h\" gives a primer for first-timers,
302 \"mEmacs<Return>\" visits the Emacs manual, etc.
303
304 In Emacs, you can click mouse button 2 on a menu item or cross reference
305 to select it.
306
307 * Menu:
308 "))
309 (dolist (group (sort groups #'(lambda (g1 g2)
310 (string-lessp (car g1) (car g2)))))
311 (let ((sec (car group))
312 (entry (cdr group)))
313 (goto-char (point-min))
314 (cond
315 ((re-search-forward (concat "^" sec "$") nil t)
316 ;; section exists
317 (install-info-forward-line 1)
318 (let ((sec-start (point)))
319 (install-info-skip-blank-lines)
320 (let ((en-start (point)))
321 (dolist (en entry)
322 (cond
323 ((or (eobp)
324 (not (looking-at "^\\* ")))
325 ;; No entries in the section.
326 (goto-char sec-start)
327 (unless (eolp)
328 (insert "\n")
329 (goto-char sec-start)))
330 (t
331 ;; Delete the old entry.
332 (let ((key (when (string-match ")" en)
333 (substring en 0 (match-beginning 0)))))
334 (while (not
335 (catch 'end
336 (cond
337 ((looking-at
338 (concat "^" (regexp-quote key)
339 "\\(\\.info\\)?)"))
340 (let ((start (point)))
341 (install-info-forward-line 1)
342 (while (looking-at "^[ \t]")
343 ;; Entry has two or more lines.
344 (install-info-forward-line 1))
345 (delete-region start (point))
346 (throw 'end t)))
347 ((eobp)
348 (throw 'end t))
349 ((eolp)
350 (install-info-forward-line 1))
351 ((not (looking-at "^\\(\\*\\|[ \t]\\)"))
352 ;; reaches the next section
353 (throw 'end t))
354 (t
355 (install-info-forward-line 1)))))))
356 (goto-char en-start)
357 ;; Insert the new entry.
358 (while (not
359 (catch 'here
360 (while (not (eolp))
361 (let ((line (buffer-substring
362 (point)
363 (install-info-point-at-eol))))
364 (cond
365 ((not (looking-at "^\\(\\*\\|[ \t]\\)"))
366 ;; reaches the next section
367 (let ((pt (point)))
368 (insert "\n")
369 (goto-char (point)))
370 (throw 'here t))
371 ((or
372 ;; not at the head of an entry
373 (looking-at "^[ \t]")
374 ;; on the way
375 (string-lessp line en))
376 (install-info-forward-line 1))
377 (t
378 (throw 'here t)))))
379 (let ((maybe-here (point)))
380 (install-info-skip-blank-lines)
381 (when (looking-at "^\\(\\*\\|[ \t]\\)")
382 (throw 'here nil))
383 ;; reaches the next section.
384 (goto-char maybe-here)
385 (throw 'here t)))))))
386 (when (and (eobp) (not (bolp)))
387 (install-info-forward-line 1))
388 (insert (format "%s\n" en))
389 (goto-char en-start)))))
390 (t
391 ;; section doesn't exist. Add it at the bottom.
392 (goto-char (point-max))
393 (install-info-forward-line 1)
394 (insert (format "\n%s\n" sec))
395 (dolist (en (sort entry #'string-lessp))
396 (insert (format "%s\n" en)))))))
397 ;;
398 (install-info-write-region (point-min) (point-max) dir))
399 (kill-buffer buf)))
400
401 (provide 'install-info)
402
403 ;; Local Variables:
404 ;; indent-tabs-mode: nil
405 ;; End:
406
407 ;;; install-info.el ends here