-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmi-index.el
337 lines (298 loc) · 11.4 KB
/
mi-index.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
;;; mi-index.el --- Command to make indices of mode-info
;; Copyright (C) 2002,2003 TSUCHIYA Masatoshi <[email protected]>
;; Authors: TSUCHIYA Masatoshi <[email protected]>
;; Keywords: info
;; This file is a part of mode-info.
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program; if not, you can either send email to this
;; program's maintainer or write to: The Free Software Foundation,
;; Inc.; 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
;;; Commentary:
;; This file provides `mode-info-make-all-indices', the command to
;; make indices of mode-info.
;;; Code:
(require 'info)
(require 'mode-info)
(eval-when-compile
(require 'cl))
(defcustom mode-info-read-coding-system
(mode-info-static-if (boundp 'MULE) '*autoconv* 'undecided)
"*Coding system for reading Info files."
:group 'mode-info
:type 'coding-system)
(defcustom mode-info-write-coding-system
(mode-info-static-if (boundp 'MULE) '*iso-2022-jp* 'iso-2022-7bit)
"Coding system for writing index files."
:group 'mode-info
:type 'coding-system)
(defun mode-info-make-all-indices ()
"Make indices of all available Info documents."
(interactive)
(dolist (name mode-info-available-classes)
(condition-case err
(progn
(setq name (car name))
(require (intern (concat "mi-" name)))
(funcall (intern (concat mode-info-prefix name "-make-index"))))
(error
(message (error-message-string err))))))
(defmacro mode-info-target-titles (class)
`(get ,class 'titles))
(defmacro mode-info-entry-regexp (class)
`(get ,class 'entry-regexp))
(defmacro mode-info-entry-pos (class)
`(get ,class 'entry-pos))
(defmacro mode-info-variable-regexp (class)
`(get ,class 'variable-regexp))
(defconst mode-info-variable-regexp "\\(Variable\\|Option\\)"
"Regular expression which match head of variable documents.")
(defun mode-info-make-index (class titles entry-regexp entry-pos
&optional variable-regexp)
(setq class (mode-info-find-class class))
(setf (mode-info-target-titles class)
(delq nil
(mapcar (lambda (x)
(cond
((stringp x)
(and (mode-info-search-info-files x) x))
((listp x)
(catch 'found-title
(dolist (y x)
(and (stringp y)
(mode-info-search-info-files y)
(throw 'found-title y)))))))
titles)))
(setf (mode-info-entry-regexp class) entry-regexp)
(setf (mode-info-entry-pos class) entry-pos)
(setf (mode-info-variable-regexp class)
(or variable-regexp mode-info-variable-regexp))
(unless (and noninteractive
(file-exists-p (mode-info-index-file-name class))
(file-newer-than-file-p
(mode-info-index-file-name class)
(locate-library (concat "mi-" (mode-info-class-name class)))))
(mode-info-make-index-file class)))
(eval-when-compile
(defvar Info-directory-list)
(defvar Info-additional-directory-list))
(defun mode-info-search-info-files (title)
"Return files which have TITLE."
(save-window-excursion
(save-excursion
(info)))
(catch 'found-info-files
(let ((regexp (format "\\`%s\\(\\.info\\)?\\(\\.gz\\)?\\'"
(regexp-quote title))))
(dolist (dir (append Info-directory-list Info-additional-directory-list))
(and (file-directory-p dir)
(directory-files dir nil regexp t)
(throw 'found-info-files
(directory-files
dir t
(format "\\`%s\\(\\.info\\)?\\(-[0-9]+\\)?\\(\\.gz\\)?\\'"
(regexp-quote title)))))))))
(defconst mode-info-node-top-regexp
"\^_\nFile: [^,]*, +Node: +\\([^,\n\t]+\\)[,\n\t]"
"Regular expression which match heads of Info nodes.")
(defconst mode-info-index-node-regexp
"\\(\\`\\| \\)Index\\'"
"Regular expression which match Index node names.")
(mode-info-defgeneric make-index-file (class)
"Make index of functions and variables described in Info.")
(mode-info-defmethod make-index-file ((class mode-info))
(let ((functions (make-symbol "function-alist"))
(variables (make-symbol "variable-alist"))
(found))
(set functions nil)
(set variables nil)
(dolist (title (mode-info-target-titles class))
(with-temp-buffer
(let ((case-fold-search t)
(buffer (current-buffer)))
(dolist (file (mode-info-search-info-files title))
(setq found t)
(with-temp-buffer
(message "Reading %s ..." file)
(let ((coding-system-for-read mode-info-read-coding-system)
(default-process-coding-system
(cons mode-info-read-coding-system
mode-info-read-coding-system)))
(if (string-match "\\.gz\\'" file)
(if (= 0 (call-process "gzip" nil t nil "-dc" file))
(goto-char (point-min))
(error "Can't extract file: %s" file))
(insert-file-contents file)))
(while (re-search-forward mode-info-node-top-regexp nil t)
(let ((nodename (match-string 1)))
(save-restriction
(narrow-to-region (match-beginning 0)
(if (search-forward "\n\^_" nil t)
(1- (point))
(point-max)))
(goto-char (point-min))
(cond
((string-match mode-info-index-node-regexp nodename)
;; Hold index nodes.
(let ((str (buffer-substring (point-min) (point-max))))
(with-current-buffer buffer
(goto-char (point-max))
(insert str))))
((mode-info-entry-regexp class)
(mode-info-process-node class title nodename
functions variables)))
(goto-char (point-max)))))))
;; Process index nodes.
(with-current-buffer buffer
(goto-char (point-min))
(while (re-search-forward mode-info-node-top-regexp nil t)
(let ((nodename (match-string 1)))
(save-restriction
(narrow-to-region (match-beginning 0)
(if (search-forward "\n\^_" nil t)
(1- (point))
(point-max)))
(goto-char (point-min))
(mode-info-process-index-node class title nodename
functions variables)
(goto-char (point-max)))))))))
(when found
(dolist (sym (list functions variables))
(set sym
(sort (symbol-value sym)
(lambda (a b)
(string< (downcase (car a)) (downcase (car b)))))))
(mode-info-write-index-file class functions variables)
(unless noninteractive
(mode-info-load-index class t)))))
(mode-info-defgeneric process-node (class title nodename functions variables)
"Make index for functions and variables described in a node named NODENAME.")
(mode-info-defmethod process-node ((class mode-info) title nodename
functions variables)
(while (re-search-forward (mode-info-entry-regexp class) nil t)
(let ((entry (match-string (mode-info-entry-pos class))))
(mode-info-process-node-1 title
(if (string-match
(mode-info-variable-regexp class)
(match-string 1))
variables
functions)
entry
nodename
(- (count-lines (point-min) (point)) 2)))))
(defun mode-info-process-node-1 (title symbol entry node line)
(setq node (format "(%s)%s" title node))
(let ((item (assoc entry (symbol-value symbol))))
(if item
(unless (member node (cdr item))
(set symbol
(cons (append item (list node line))
(delq item (symbol-value symbol)))))
(set symbol
(cons (list entry node line)
(symbol-value symbol))))))
(defconst mode-info-index-entry-regexp
"^\\* +\\([^\n]+\\):[ \t]*\\([^.\n]*\\)\\.[ \t]*\\([0-9]*\\)"
"Regular expression which match entries in index nodes.")
(mode-info-defgeneric process-index-node (class title nodename
functions variables)
"Make index for functions and variables described in an index node.")
(mode-info-defmethod process-index-node ((class mode-info) title nodename
functions variables)
(cond
((string= nodename "Function Index")
(while (re-search-forward mode-info-index-entry-regexp nil t)
(mode-info-process-index-node-1 title
functions
(match-string 1)
(match-string 2)
(match-string 3))))
((string= nodename "Variable Index")
(while (re-search-forward mode-info-index-entry-regexp nil t)
(mode-info-process-index-node-1 title
variables
(match-string 1)
(match-string 2)
(match-string 3))))))
(defconst mode-info-index-entry-suffix-regexp
"[ \t]*<[0-9]+>\\'"
"Regular expression which match suffixes of entries in index nodes.")
(defun mode-info-process-index-node-1 (title symbol entry node line)
(when (string-match mode-info-index-entry-suffix-regexp entry)
(setq entry (substring entry 0 (match-beginning 0))))
(setq node (format "(%s)%s" title node)
line (if (integerp line) line (string-to-number line)))
(let ((item (assoc entry (symbol-value symbol))))
(if item
(unless (member node (cdr item))
(set symbol
(cons (append item (list node line))
(delq item (symbol-value symbol)))))
(set symbol
(cons (list entry node line)
(symbol-value symbol))))))
(mode-info-defgeneric write-index-file (class functions variables
&optional regexp)
"Write index file.")
(mode-info-defmethod write-index-file ((class mode-info) functions variables
&optional regexp)
(with-temp-buffer
(mode-info-dump-symbols functions variables)
(when regexp
(mode-info-dump-symbols-as-regexp functions variables))
(let ((make-backup-files (if (not noninteractive) make-backup-files)))
(set-visited-file-name (mode-info-index-file-name class) t)
(save-buffer))))
(defun mode-info-dump-symbols (&rest symbols)
(set (mode-info-static-if (>= emacs-major-version 20)
'buffer-file-coding-system
'file-coding-system)
mode-info-write-coding-system)
(insert ";; -*- mode: emacs-lisp; coding: "
(prin1-to-string mode-info-write-coding-system)
"; -*-\n;; This is the index file\n"
";; generated by mode-info-make-index at "
(current-time-string)
".\n\n")
(insert (format "(setq indexer-version \"%s\")\n" mode-info-version))
(dolist (sym symbols)
(insert "(setq "
(symbol-name sym)
"\n '("
(mapconcat (function prin1-to-string)
(symbol-value sym)
"\n ")
"))\n")))
(mode-info-static-if (fboundp 'regexp-opt)
(defun mode-info-regexp-opt (strings &optional paren)
"Return a regexp to match a string in STRINGS."
(with-temp-buffer
(insert (regexp-opt strings paren))
(goto-char (point-min))
(while (search-forward "\\(?:" nil t)
(delete-char -2))
(buffer-string)))
(defun mode-info-regexp-opt (strings &optional paren)
"Return a regexp to match a string in STRINGS."
(let ((open (if paren "\\(" ""))
(close (if paren "\\)" "")))
(concat open (mapconcat 'regexp-quote strings "\\|") close))))
(defun mode-info-dump-symbols-as-regexp (&rest symbols)
(dolist (sym symbols)
(let ((name (if (string-match "-alist\\'" (symbol-name sym))
(concat (substring (symbol-name sym) 0 (match-beginning 0))
"-regexp")
(error "Invalie variable name: %s" sym)))
(value (format "\\b\\(%s\\)\\b"
(mode-info-regexp-opt
(mapcar 'car (symbol-value sym))))))
(insert (format "\(setq %s %s\)\n" name (prin1-to-string value))))))
(provide 'mi-index)
;;; mi-index.el ends here