-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiman.el
552 lines (463 loc) · 20.5 KB
/
iman.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
;;; iman.el --- call man & Info viewers with completion
;; Copyright (C) 2000 Yuji 'bmonkey' Minejima <[email protected]>
;; Author: Yuji Minejima <[email protected]>
;; $Revision: 2.22 $
;; Keywords: local, help
;; This file is NOT part of GNU Emacs.
;; 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 GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
;; This program contains part of the code and the documentation of the
;; following program which is distributed under GPL:
;;
;; thingatpt.el --- Get the `thing' at point
;; Copyright (C) 1991,92,93,94,95,96,97,1998 Free Software Foundation, Inc.
;; Author: Mike Williams <[email protected]>
;;
;; Thanks to the author for writing an excellent program.
;;; Commentary:
;; Call the viewers of Unix manual pages and GNU Info with completion.
;;
;; This package provides the `iman' command.
;; The `iman' command reads the name of a Unix manual page or an item of
;; GNU Info top directory in the minibuffer with completion, and shows
;; that page.
;;
;; The job of actually showing the page is done by calling the standard
;; viewers.
;;; Requirements:
;; Tested with FSF Emacs 20.6.2 and XEmacs 21.1.9 on Debian GNU/Linux.
;;
;; You need a man(1) program. You probably have one already if you
;; run a Unix like system.
;;; Enhancements:
;; completing-help.el (>= version 3.3)
;; With this packege, short descriptions on completions is displayed
;; by pressing <TAB> (XEmacs only) or `?' which looks like:
;; -----------------------------------------------------------------
;; Click mouse-2 on a completion to select it.
;; In this buffer, type RET to select the completion near point.
;;
;; Possible completions are:
;; find(1) - search for files in a directory hierarchy
;; findaffix(1) - Interactive spelling checking
;; finding files: (find). Listing and operating on files that mat$
;; findoidjoins(1) - list joins on oids between PostgreSQL tabl$
;; findsuper(8) - No manpage for this program, utility or fu$
;; -----------------------------------------------------------------
;;
;; You can get completing-help.el at
;; http://homepage1.nifty.com/bmonkey/emacs/elisp/completing-help.el
;;; Install:
;; 1: Put this file in one of the directories listed in `load-path'.
;; You can see the contents of `load-path' by entering
;; `M-x customize-option <RET> load-path'.
;;
;; 2: Enter `M-x byte-compile-file <RET>
;; <DIR-YOU-PUT-THIS-FILE-IN>/iman.el <RET>'
;; to speed up the execution of this package.
;;
;; 3: Put the following lines in your .emacs file.
;;
;; (autoload 'iman "iman"
;; "Call the viewers of man pages and GNU Info with completion."
;; t nil)
;;
;; If you want to bind the `iman' command to some key sequence,
;; add the following line, too:
;; (global-set-key "\C-cm" 'iman) ; `control c', then `m' calls `iman'
;; You should change "\C-cm" to the key sequence you want.
;;
;; 4: Restart Emacs or enter `M-x load-library <RET> iman'.
;;; Usage:
;; * Invoke the command by `M-x iman' or the key sequence you bound to
;; `iman'.
;;
;; * In the minibuffer, the key sequences described in
;; "(emacs)Completion Commands" should work as usual.
;; Here's some of the key sequences you probably use most.
;;
;; <TAB> Completion.
;; <SPACE> Word completion.
;; You can use this key to enter ' ', '(', and '-'.
;; The feature of entering '(' by this key is specific to
;; this package.
;; M-C-v Scroll the help window from within the minibuffer.
;; M-v Go into the help window ("*Completions*" buffer).
;; Inside the help window, press <RET> to select an item,
;; or just move around then go back into the minibuffer
;; (C-x o).
;;; Change Log:
;; Version 2.22 (29 Sep 2000)
;; * Changed to support completing-help.el (>= version 3.3)
;; * Cleaned up some code.
;; * Added `iman-ignore-case'.
;; Version 2.16 (27 May 2000):
;; * Changed the name from "bm-iman.el" to "iman.el".
;; * Changed to use `completing-help.el', if available, to display
;; the short descriptions of man pages.
;; * Default man page names recognition mechanism is improved.
;;; Code:
;;; customization
(eval-when-compile
(defvar current-prefix-arg)
(defvar quit-flag)
(defvar unread-command-events)
(defvar minibuffer-completion-table)
(defvar minibuffer-completion-predicate)
(defvar minibuffer-local-must-match-map)
(defvar completion-auto-help))
(defgroup iman nil
"Call the viewers of Unix manual pages and the GNU Info with completion.
This package provides the `iman' command which reads, in the
minibuffer with completion, the name of a Unix manual page or
an item in the top directory of the GNU Info documentation tree,
and then shows that page.
The job of actually showing the page is done by calling the standard
viewers."
:group 'help)
(defcustom iman-ignore-case nil
"Non-nil means case is not considered significant in completion."
:type 'boolean
:group 'iman)
(defcustom iman-index-obarray-bucket-number 2047
"The number of buckets of an obarray which contains the index.
\"(elisp)Creating Symbols\" recommends this value to be a prime number
or a number which is one less than a power of two."
:type 'integer
:group 'iman
:link '(info-link "(elisp)Creating Symbols"))
;;; Variables
(defvar iman-index-obarray nil
"Obarray to store the Man page and GNU Info index.")
(defvar iman-history nil
"History list for `iman' command.")
(defvar iman-predicate nil
"Predicate function for `completing-read'.")
(eval-and-compile
(autoload 'manual-entry "man")
(autoload 'Info-directory "info")
(autoload 'Info-menu "info"))
;;;###autoload
(defun iman (item)
"Call the viewers of Unix manual pages and the GNU Info with completion.
With prefix argument (e.g. `C-u M-x iman'), rebuild the index."
(interactive (list (progn
(when (or current-prefix-arg
(not (vectorp iman-index-obarray)))
(iman-make-index))
(iman-minibuffer-read iman-predicate
nil
'iman-history))))
(unless (string= item "")
(string-match "\\`\\([^(]*\\)(?\\([^)]*\\)" item) ; always match
(let ((name (match-string 1 item))
(section (match-string 2 item)))
(cond
((string= section "") ; Info
(Info-directory)
(Info-menu item))
(t ; man
(funcall 'manual-entry (if (featurep 'xemacs)
item
(concat section " " name))))))))
(defvar iman-completion-commands
'(minibuffer-complete minibuffer-complete-word ; standard commands
MComplete-complete MComplete-complete-word ; mcomplete.el
PC-complete PC-complete-word ; complete.el
IMan-minibuffer-complete-word)
"List of minibuffer completion commands.")
(eval-when-compile
(defvar mcomplete-complete-word-high-priority-strings) ; for mcomplete.el
(defvar completing-help-commands)) ; for completing-help.el
(defun iman-minibuffer-read (&optional pred init history)
"Read the name of a man page or an item of Info top directory."
(let* ((mcomplete-complete-word-high-priority-strings '(" " "(" "-"))
(completing-help-commands (if (boundp 'completing-help-commands)
(append iman-completion-commands
completing-help-commands)
iman-completion-commands))
(completion-ignore-case iman-ignore-case)
(table iman-index-obarray)
(must-match t)
(default (iman-name-at-point))
(prompt (format "iman%s: " (if default
(format " (default %s)" default)
"")))
(minibuffer-local-must-match-map (copy-keymap
minibuffer-local-must-match-map)))
(define-key minibuffer-local-must-match-map " "
'IMan-minibuffer-complete-word)
(iman-completing-read prompt table pred must-match init history default)))
(defun iman-completing-read (prompt table
&optional pred must-match init history default
inherit-input-method)
"Compatibility function for FSF Emacs and XEmacs."
(let* ((common-args (list prompt table pred must-match init history))
(args (append common-args (unless (featurep 'xemacs)
(list default inherit-input-method))))
(input (apply 'completing-read args)))
(or (and (string= input "") default) ; default handling
input)))
;;; Man spec or Info menu item at point
(defun iman-name-at-point ()
"Return a string denoting a man spec. or a GNU Info directory item at point."
(or (iman-Man-spec-at-point)
(let* ((case-fold-search t)
(name (iman-symbol-name-at-point))
name-re)
(when name
(setq name-re (concat "\\`" (regexp-quote name) "\\((\\|\\'\\)"))
(catch 'found
(mapatoms
#'(lambda (entry-symbol)
(when (string-match name-re (symbol-name entry-symbol))
(throw 'found (symbol-name entry-symbol))))
iman-index-obarray))))))
(defun iman-symbol-name-at-point ()
"Return a symbol name at point. Return nil if none is found.
Ripped off out of Mike Williams's thingatpt.el"
(save-excursion
(save-match-data
(let* ((orig-pt (point))
(bol (prog2 (beginning-of-line) (point) (goto-char orig-pt)))
(eol (prog2 (end-of-line) (point) (goto-char orig-pt)))
(sym-beg (progn (skip-syntax-backward "w_" bol) (point)))
(sym-end (progn (skip-syntax-forward "w_" eol) (point))))
(unless (= sym-beg sym-end)
(buffer-substring sym-beg sym-end))))))
(defun iman-Man-spec-at-point ()
"Return a man spec. at point. Return nil if none is found."
(save-excursion
(save-match-data
(let* ((orig-pt (point))
(bol (prog2 (beginning-of-line) (point) (goto-char orig-pt)))
(eol (prog2 (end-of-line) (point) (goto-char orig-pt)))
(rpar (when (or (and (char-before)
(char-equal (char-before) ?\)))
(search-forward ")" eol t))
(point))) ; "`ls (1)-!-'"
(lpar (and rpar
(search-backward "(" bol t)))
; "`ls -!-(1)'"
(sec (when (looking-at "([1-9][a-zA-Z0-9]*)")
(buffer-substring lpar rpar)))
(name-end (when sec
(when (and (char-before)
(char-equal (char-before) ?\ ))
(backward-char))
(point)))) ; "`ls-!- (1)'"
(when sec
(when (re-search-backward "\\s-" bol "MOVE-ON-FAIL") ; \s=whitespace
(forward-char)) ; "-!-`ls (1)'"
(when (catch 'found
(while (and (<= (point) (min orig-pt lpar)))
(when (intern-soft
(concat (buffer-substring (point) name-end) sec)
iman-index-obarray)
(throw 'found t))
(forward-char))) ; "`-!-ls (1)'" gotcha!
(concat (buffer-substring (point) name-end) sec)))))))
;;; Index ------------------------------------------------------------
(defcustom iman-merge-Info-menu t
"Non-nil means merging the GNU Info top directory into the index."
:type 'boolean
:group 'iman)
;;;###autoload
(defun iman-make-index ()
"Make the index of man pages and Info directory items."
(interactive)
(setq iman-index-obarray (make-vector iman-index-obarray-bucket-number 0))
(message "Making iman index...")
(let ((ok nil))
(unwind-protect
(progn
(iman-make-Man-index iman-index-obarray)
(when iman-merge-Info-menu
(iman-make-Info-menu-index iman-index-obarray))
(setq ok t))
(or ok (setq iman-index-obarray nil))))
(message nil))
;;; Man index.
(defcustom iman-Man-index-command-and-args '("man" "-k" "")
"List of a program name and its options to dump the index of man pages.
Note these values DON'T go through shell expansions.
These values are used in a `call-process' call and should get the same
output as if you enter \"man -k ''\" in a shell session, which should
print out something like the following:
-------------------------------------------------------------------
~$ man -k ''
. (1) [builtins] - bash built-in commands, see bash(1)
.ldaprc (5) [ldaprc] - ldap configuration file
.netrc (5) [netrc] - user configuration for ftp
2a_ctrl (4) - (unknown)
2b_romkana (4) - (unknown)
822-date (1) - Print date and time in RFC822 format
.......
-------------------------------------------------------------------"
:type '(repeat string)
:group 'iman)
(defvar iman-Man-index-regexp
"^\\([^ (]+\\) *\\(([^ )]+)\\)\\(.*\\)$"
"Regexp to recognize an entry of the man page index.
(setq entry \"alias (1) [builtins] - bash built-in commands, see bash(1)\")
(string-match iman-Man-index-regexp entry)
(match-string 1 entry) => \"alias\"
(match-string 2 entry) => \"(1)\"
(match-string 2 entry) => \"[builtins] - bash built-in commands, see bash(1)\"
")
;; This is just a note
;; 1 2 3 4
;; alias (1) [builtins] - bash built-in ...
;;"^\\([^ (]+\\) *\\(([^ )]+)\\) *\\(\\[[^]]+\\]\\)? *- \\(.*\\)$"
(defvar iman-Man-index-buffer-name
"*iman-man-index*"
"Name of the buffer to store the man page index temporarily.")
(defun iman-make-Man-index (index-obarray)
"Intern symbols whose names are the man page names, in INDEX-OBARRAY.
Each symbol has a description string as its value."
(when (get-buffer iman-Man-index-buffer-name)
(kill-buffer iman-Man-index-buffer-name))
(let* ((buffer (get-buffer-create iman-Man-index-buffer-name))
(program (car iman-Man-index-command-and-args))
(args (cdr iman-Man-index-command-and-args))
(exit-status (apply 'call-process
program
nil ; < /dev/null
buffer ; > current temporary buffer
nil ; no redisplay
args)))
(when (not (zerop exit-status))
(switch-to-buffer-other-window buffer)
(goto-char (point-max))
(error "%s program returned error exit status %s" program exit-status))
(with-current-buffer buffer
(goto-char (point-min))
(while (re-search-forward iman-Man-index-regexp nil t)
(set (intern (concat (match-string 1) (match-string 2)) index-obarray)
(match-string 3))))
(kill-buffer iman-Man-index-buffer-name)))
;;; GNU Info index.
(defvar iman-Info-menu-item-regexp
"^\\* \\([^:]+\\):\\(.*\\)$"
"Regexp to recognize a menu item of the GNU Info node.
(setq item \"* Binutils: (binutils). The GNU binary utilities.\")
(string-match iman-Info-menu-item-regexp item)
(match-string 1 item) => \"Binutils\"
(match-string 2 item) => \" (binutils). The GNU binary utilities.\"")
(eval-and-compile
(autoload 'Info-mode "info")
(autoload 'Info-find-node "info"))
(defun iman-make-Info-menu-index (index-obarray &optional file node)
"Intern in INDEX-OBARRAY symbols of the Info FILE NODE's menu items.
Each symbol has a description string as its value."
(unless file (setq file "dir"))
(unless node (setq node "top"))
(let* (name description)
(save-excursion
(save-window-excursion
(with-temp-buffer
(Info-mode)
(Info-find-node file node)
(goto-char (point-min))
(while (re-search-forward iman-Info-menu-item-regexp nil t)
(setq name (downcase (match-string 1))
description (concat " " (match-string 2)))
(set-text-properties 0 (length name) nil name)
(set-text-properties 0 (length description) nil description)
(set (intern name index-obarray) description)))))))
;;; minibuffer commands
(defun iman-minibuffer-message (str &optional sec)
"Display STR at the end of the minibuffer for SEC (default 2) seconds.
The minibuffer must be the current buffer.
Stop displaying when the next input event arrives.
Work almost the same as `minibuffer-message'."
(unless sec (setq sec 2))
(let ((buffer-undo-list t) ; prevent undo recording
(pt-max (point-max))
(inhibit-quit t))
(save-excursion
(message nil)
(goto-char (point-max))
(insert str))
(sit-for sec)
(delete-region pt-max (point-max))
(when quit-flag
(let ((quit-char (if (fboundp 'current-input-mode)
(nth 3 (current-input-mode))
?\^G))
(char-to-event (if (fboundp 'character-to-event)
'character-to-event
'identity)))
(cond
((boundp 'unread-command-events)
(setq unread-command-events (list (funcall char-to-event quit-char))
quit-flag nil))
(t
(error "mcomplete-message: %S is not bound."
'unread-command-events)))))))
(defun IMan-minibuffer-complete-word ()
"`minibuffer-complete-word' for iman package.
You can enter `(' by hitting <SPACE>. This is useful when there're
possible completions like ls(1), lsattr(1), lsdic(1), and lsearch(3)."
(interactive)
(let* ((buf-str (buffer-string))
(completion (try-completion buf-str
minibuffer-completion-table
minibuffer-completion-predicate))
(suffix (when (stringp completion)
(substring completion (length buf-str)))))
(cond
((null completion)
(iman-minibuffer-message " [No match]") nil)
((eq completion t)
(iman-minibuffer-message " [Sole completion]") nil)
((string= suffix "")
(let ((strings '(" " "(" "-")))
(unless (catch 'inserted
(while strings
(when (try-completion (concat (buffer-string)
(car strings))
minibuffer-completion-table
minibuffer-completion-predicate)
(goto-char (point-max))
(insert (car strings))
(throw 'inserted t))
(setq strings (cdr strings))))
(if completion-auto-help
(minibuffer-completion-help)
(iman-minibuffer-message " [Next char not unique]")))))
((string-match "\\`\\sw*\\Sw?" suffix)
(goto-char (point-max))
(insert (match-string 0 suffix))
t)
(t (error "IMan-minibuffer-complete-word: logical error")))))
;;; completing-help support
(defun iman-completing-help-p ()
(eq minibuffer-completion-table iman-index-obarray))
(defvar iman-completing-help-group
'(:predicate iman-completing-help-p
:get completing-help-user-obarray-get-info
:info-head ""
:info-tail "")
"")
(if (featurep 'completing-help)
(add-to-list 'completing-help-groups 'iman-completing-help-group)
(add-hook 'completing-help-load-hook
#'(lambda () (add-to-list 'completing-help-groups
'iman-completing-help-group))))
;;; Hook
(defvar iman-load-hook nil
"Hook to run at the end of loading iman.")
(provide 'iman)
(run-hooks 'iman-load-hook)
;;; iman.el ends here