edict / dui.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
;;; dui.el --- Dictionary user interface

;; Copyright (C) 1998 by Stephen J. Turnbull

;; Author:      Stephen J. Turnbull <turnbull@sk.tsukuba.ac.jp>
;; Keywords:    mule, dictionary
;; Version:     0.6

;;   This file is part of XEmacs.

;;   XEmacs 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.

;;   XEmacs 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 XEmacs; if not, write to the Free Software Foundation,
;;   Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

;;; Commentary:

;; Some code that provides support for dictionary lookup and manipulations
;; (such as inserting definitions and maintaining a private dictionary).
;; Originally written in support of edict.el, by Per Hammarlund
;; <perham@nada.kth.se>, but since generalized.

;;; To do:

;;; Changelog:

;; 1998-03-27  Stephen Turnbull  <turnbull@sk.tsukuba.ac.jp>
;;        (created):  broken out from monolithic edict.el

;;; Code:

;;; Dictionary lookup minor mode (dl-mode)

;; User customization variables

(defvar dl-mode-prefix '[(control ?c) (?\$)]
  "Prefix key sequence for dl-mode command keys.

After loading, change the mode's prefix by using dl-mode-set-prefix;
setq'ing this variable can't work.")

(defvar dl-indicator-string " dl"
  "String indicating activation of dl minor mode in the modeline.

Set to nil to inhibit modeline display.")

;; A convention for modes; here honored by observance, not breach.
;;
(defvar dl-mode-hook nil
  "A normal hook called at the end of the dl-mode activation process.

If you can think of a use for this, you're more clever than I.")

;; Auxiliary customizations

(defvar dl-conflict-warning "Binding conflict: %s -> %s."
  "Format string warning about key sequences with conflicting bindings.

Must contain two `%s' descriptors.  The first formats the key sequence,
the second the description of the existing binding.")

(defvar dl-warn-conflict-verbosity 3
  "Controls verbosity of binding conflict warnings.

0   turns off warnings entirely.
1   issues a warning for each binding conflict (including sub-keymaps).
2   prints a summary message with a count of conflicts (not including
    sub-keymaps, only keys in those maps that have conflicts).
3   adds verbose detail about what is being done.

Each positive level performs all actions of lower levels.")

;; The basic mode conventions.

;; Mode flag and keymaps
;;
(defvar dl-mode nil "Activation flag for dl-mode.")

(defvar dl-mode-submap nil
  "Sub-keymap for dl-mode.

All key sequences are prefixed by the sequence defined in dl-mode-map.")

(defvar dl-mode-map nil
  "Keymap for dl-mode.  Holds the prefix key for dl-mode functions.

Its value will be installed into minor-mode-map-alist.  Prefix cannot
be altered by setq'ing dl-mode-map.  Use dl-mode-set-prefix instead.")

;; Mode toggle
;;
;; The side effect is arguably not a feature :-)
;
;;;###autoload
(defun dl-mode (&optional arg)
  "Minor mode for dictionary lookup, with interfaces to dictionary utilities.

Null ARG toggles the mode, positive ARG turns the mode on, negative ARG
turns the mode off.

When the mode is already off, (dl-mode -1) has the side effect of checking
and reporting any conflicting bindings.

\\{dl-mode-map}"

  (interactive "P")

  ;; dl-warn-binding-conflicts doesn't make sense when the mode is active
  (if (null dl-mode)
      (dl-warn-binding-conflicts dl-mode-map))
  (setq dl-mode (if (null arg) (not dl-mode)
                  (> (prefix-numeric-value arg) 0)))
  (run-hooks dl-mode-hook))

;; Internal mode data

;; Main keymap
;;
(or dl-mode-submap
    (progn
     (define-prefix-command 'dl-mode-submap) ;
     (define-key dl-mode-submap '[ ?s ] 'dui-invoke-search-method)
     (define-key dl-mode-submap '[ ?i ] 'dui-invoke-insert-method)
     ;; Hmm ... I don't think there are any of these :-P
     ;;(define-key dl-mode-submap '[ ?e ] 'dui-invoke-edit-method)
     (define-key dl-mode-submap '[ ?d ] 'dui-describe-method)
     ))

;; Helper functions

;; Set the mode prefix
;;
;; This can't be done simply by setq'ing dl-mode-map; minor-mode-map-alist
;; does not refer to that variable but contains a copy of its value.
;;
(defun dl-mode-set-prefix (key &optional context)
  "Set the prefix key sequence for dl-mode to KEY.

Return the new dl-mode-map.  When called interactively read KEY from
the minibuffer (as a string; keys not bound to `self-insert' must be
quoted with C-q).  If you need more flexibility than ASCII gives, you'll
have to use the `eval-expression' interface.

Example: `\\[dl-mode-set-prefix] C-q C-c $ RET' returns the prefix key
to the default `C-c $'.

Allowed values of CONTEXT:
NIL                substitute a map containing KEY in minor-mode-map-alist.
adding-minor-mode  manipulation of minor-mode-map-alist is done elsewhere."

  ;; Should read key events but I don't know how to make that work.
  (interactive "Key sequence (quote control characters with ^Q): ")

  (setq dl-mode-map (make-sparse-keymap))
  (define-key dl-mode-map key 'dl-mode-submap)
  (cond ((null context)
	 (let ((slot (assq 'dl-mode minor-mode-map-alist)))
	   (setq minor-mode-map-alist
		 (cons (cons 'dl-mode dl-mode-map)
		       (if slot
			   (delete slot minor-mode-map-alist)
			 minor-mode-map-alist)))))
	((equal context 'adding-minor-mode))
	(t (error "Illegal context `%s' in dl-mode-set-prefix." context)))
  dl-mode-map)

;; Deal with binding conflicts
;;
;; Search keymaps for binding conflicts for each key in the mode's keymap.
;; Current implementation searches only active keymaps; it won't tell
;; about inactive keymaps, including those of minor modes that might be
;; invoked later or (worse) major modes already invoked in other buffers.
;;
(defun dl-warn-binding-conflicts (map)
  "Warn about key bindings that will conflict with those in MAP.

Results will be non-sensical if MAP is invoked via a prefix or is
already active.  The current implementation only looks in the active
keymaps.  Maps of inactive minor modes and local maps major modes of
other buffers will not be searched (although the latter will be shadowed
since dl-mode is a global variable)."
  (if (null (featurep 'xemacs))
      ;; `map-keymap' doesn't exist in the FSF's Emacs
      (message "Keymap shadow checking not supported under\n%s"
	       (emacs-version))
    (let ((found 0))
      (if (> dl-warn-conflict-verbosity 1)
	  (progn
	    (message "Checking for conflicting bindings...")
	    (if (> dl-warn-conflict-verbosity 2)
		(message "Examining accessible maps of map:\n    `%s'" map))))
      ;; A map is accessible from itself
      (mapcar (lambda (slot)
		(let ((prefix (car slot))
		      (map (cdr slot)))
		  (if (> dl-warn-conflict-verbosity 2)
		      (message "Examining keys of map:\n    `%s'" map))
		  (map-keymap (lambda (key binding)
				(let* ((key (vconcat prefix (vector key)))
				       (binding (key-binding key)))
				  (if (and binding
					   (> dl-warn-conflict-verbosity 0))
				      (progn
					(if (not (keymapp binding))
					    (setq found (1+ found)))
					(message dl-conflict-warning
						 key binding)))))
			      map)))
	      (accessible-keymaps map))
      (if (> dl-warn-conflict-verbosity 1)
	  (message "Checking for conflicting bindings...done%s"
		   (if (> found 0)
		       (format ".  Found %d." found)
		     "."))))))

;; Register the mode with Emacs
;; `add-minor-mode' doesn't exist in Emacs 20.2  :-(
(or (assq 'dl-mode minor-mode-alist)
    (setq minor-mode-alist
	  (cons (list 'dl-mode dl-indicator-string) minor-mode-alist)))
(dl-mode-set-prefix dl-mode-prefix)

;;; end of dictionary lookup minor mode

(defvar dui-warn-previously-registered-methods-p t
  "Warn about previously registered methods.")

;;     [SJT:  OK, ispell uses M-$, and LEIM and Wnn both use C-\.  I see
;;	all three processes (spell-check, localized input methods, and
;;	dictionary lookup) as being aspects of high-level dictionary-
;;	based natural language input.  I would like to overload the same
;;	mode-toggle for all of them.  I see IMs as being primary (analogous
;;	to a minor mode), while the checking functions are secondary and/or
;;	transient.  Unfortunately, both ispell and LEIM use prefix args to
;;	modify the toggle's behavior.  But all of "C-$", "C-c $", "C-c \",
;;	and "C-c \" are undefined.
;;	  I see the interface as follows.
;;	The main-entry point is the the transient function (ispell-word,
;;	edict-lookup-*), accessed via the un-prefixed key.
;;	The prefixed key would give a short choice menu, in the echo area.
;;	A short-list of defaults would be alternative transient functions,
;;	plus the choices to add or delete from the menu, or to do more
;;	complicated maintenance (eg, customize, once we have an interface.)
;;
;; #### Need to define the call interface for the functions.
(defvar dui-method-history nil
  "List of recently used dictionary methods.")

;; Maybe would like to do something like the following?
;An alist containing elements of the form (METHOD &rest LISTS).
;
;METHOD is a unique string naming the dictionary method.  Each element
;of LISTS is a list of the form (TYPE DESCRIPTION INVOCATION &rest
;ARGS), where TYPE is a symbol (one of 'search, 'insert, or 'edit)
;indicating the context where this invocation is used, DESCRIPTION is a
;string describing this method, INVOCATION is a function to call to
;invoke this method, and the function will be applied to (cons TYPE
;ARGS).
(defvar dui-method-alist nil
  "Registry of dictionary methods and utilities.

An alist containing elements of the form (METHOD TYPE DESCRIPTION
INVOCATION &rest ARGS).

METHOD is a unique string naming the dictionary method.  TYPE is a
symbol (one of 'search, 'insert, or 'edit) indicating the context
where this invocation is used, DESCRIPTION is a string describing this
method, INVOCATION is a function to call to invoke this method, and
the function will be applied to (cons TYPE ARGS).")

;; Method component access functions
(defun dui-get-method-name (slot) (nth 0 slot))
(defun dui-get-method-type (slot) (nth 1 slot))
(defun dui-get-method-description (slot) (nth 2 slot))
(defun dui-get-method-invocation (slot) (nth 3 slot))
(defun dui-get-method-args (slot) (nthcdr 4 slot))

(defvar dui-registration-errors nil
  "String containing description of method registration problems.")

;; Flush any old errors hanging around.
(setq dui-registration-errors nil)

(defun dui-register-method
  (method type invocation description &rest args)
  "Register a dictionary method.

METHOD is a unique string naming the dictionary method.  TYPE
indicates the context in which the method is used (a symbol, one of
'search, 'insert, or 'edit).  INVOCATION is a function to call to
invoke this method, which is applied to ARGS.  DESCRIPTION is a string
describing this method.  The same INVOCATION function may be
registered in different contexts with different descriptions and
argument lists, but must have a different METHOD name in each context.

It may be useful to include METHOD as an element of ARGS to allow the
INVOCATION function to be used by several slightly different methods."
  (if (assoc method dui-method-alist)
      (setq dui-registration-errors
	    (concat dui-registration-errors
		    (format "%s\n" method)))
    (setq dui-method-alist
	  (cons (append (list method type description invocation) args)
		dui-method-alist))))

;; #### should this filter on type?  probably not.
(defun dui-remove-method (method)
  "Remove a dictionary method from the registry."
  (interactive (completing-read "Remove method: " dui-method-alist nil t))
  (setq dui-method-alist
	(delete (assoc method dui-method-alist)
		dui-method-alist)))

(defun dui-filter (type list)
  "Return the sub-list of methods from LIST whose type is TYPE."
  (apply 'append
	 (mapcar #'(lambda (method)
		     (if (eq (dui-get-method-type (assoc method dui-method-alist)) type)
			 (list method)
		       nil))
		 list)))

(defun dui-read-method (type prompt &optional default)
  "Read the name of a dictionary method from the minibuffer.

If DEFAULT is non-nil, use that as the default, substituting it into
PROMPT at the first `%s'.

Signals an error on null input.  The return value is a string."
  (if default (setq prompt (format prompt default)))
  (let* ((completion-ignore-case t)
	 ;; This binding is necessary if dui-method-history
	 ;; is buffer local.  For the name of the variable, see comments
	 ;; in lisp/minibuf.el on `read-from-minibuffer'; it's dynamic
         ;; scope lossage.
	 (naming-this-symbol-simply-history-loses
	  (dui-filter 'search dui-method-history))
	 ;; Ah, bogosity. Oberhasli croaks with wta listp, history.
	 ;; For now leave it in anyway.
	 (method (completing-read prompt dui-method-alist nil t nil
				  'naming-this-symbol-simply-history-loses)))
	 ;;(method (completing-read prompt dui-method-alist nil t)))
    (if (and (> (length method) 0)
	     (eq (dui-get-method-type (assoc method dui-method-alist)) type))
	method
      (error "No valid method was specified"))))

;; #### Make a method for defining additional keymap entries for folks
;;      who want secondary dictionaries available.
(defun dui-invoke-method (type ask)
  "Invoke a dictionary method.

With ASK non-nil, read a method of type TYPE from the minibuffer and
invoke it.

With ASK null, invoke the last selected method, if there is one,
otherwise read from minibuffer and invoke."

  (let* ((default (car (dui-filter type dui-method-history)))
	 (method (if (or ask (not default))
		     (dui-read-method
		      type
		      (if default
			  "Method (default %s): "
			"Method: ")
		      default)  
		   default))
	 (slot (assoc method dui-method-alist))
	 (invocation (dui-get-method-invocation slot))
	 (args (dui-get-method-args slot)))
    (setq dui-method-history
	  (cons method (delete method dui-method-history)))
    (apply invocation args)))

;; #### `dui-invoke-insert-method' and  `dui-invoke-edit-method' probably
;;      don't need defaults or histories.  Instead, they should be part
;;      of the information associated with the search method and they
;;      should be automatically invoked depending on the success or
;;      failure of the search mehtod.  (Insert methods should only be
;;      invoked if the appropriate user variable is set.)
;;;###autoload
(defun dui-invoke-search-method (ask)
  "Invokes a dictionary lookup method.

If ASK is non-nil, reads a method from the minibuffer.  Otherwise invokes the
current default search method.

\\[dui-describe-method] gives help for individual methods."
  (interactive "P")
  (dui-invoke-method 'search ask))

;;;###autoload
(defun dui-invoke-insert-method (ask)
  "Invokes a method to add a dictionary entry.

If ASK is non-nil, reads a method from the minibuffer.  Otherwise invokes the
current default insert method.

\\[dui-describe-method] gives help for individual methods."
  (interactive "P")
  (dui-invoke-method 'insert ask))

;;;###autoload
(defun dui-invoke-edit-method (ask)
  "Invokes a dictionary editing method.

If ASK is non-nil, reads a method from the minibuffer.  Otherwise invokes the
current default edit method.

\\[dui-describe-method] gives help for individual methods."
  (interactive "P")
  (dui-invoke-method 'edit ask))

;;;###autoload
(defun dui-describe-method (method)
  "Shows the docstring for METHOD (a string) in a temporary buffer."
  (interactive (completing-read "Describe method: " dui-method-alist nil t))
  (with-output-to-temp-buffer
      (princ (dui-get-method-description method))))

(defun dui-princ-errors ()
  (if (and dui-warn-previously-registered-methods-p
	   dui-registration-errors)
      (progn
	(princ "Methods are already registered by the following names.
If you wish to register a new method under one of these names, please use
`dui-remove-method' first.
")
	(princ dui-registration-errors)
	(setq dui-registration-errors nil))))

(provide 'dui)

;; load up the default methods
;; must come after the provide call to dui
(require 'dui-registry)

;;; dui.el ends here
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.