Source

dictionary / dictionary.el

Full commit
  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
;; dictionary.el -- an interface to RFC 2229 dictionary server

;; Author: Torsten Hilbrich <Torsten.Hilbrich@gmx.net>
;; Keywords: interface, dictionary
;; $Id$

;; This file 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 file 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.

(require 'cl)
(require 'custom)
(require 'connection)
(require 'link)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Stuff for customizing.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defgroup dictionary nil
  "Client for accessing the dictd server based dictionaries"
  :group 'help
  :group 'hypermedia)
 
(defcustom dictionary-server
  "dict.org"
  "This server is contacted for searching the dictionary"
  :group 'dictionary
  :type 'string)

(defcustom dictionary-port
  2628
  "The port of the dictionary server.
This port is propably always 2628 so there should be no need to modify it."
  :group 'dictionary
  :type 'number)

(defface dictionary-word-entry-face
  '((((type x))
     (:bold t :italic t))
    (((type tty) (class color))
      (:foreground "green"))
    (t
     (:inverse t)))
  "The face that is used for displaying the initial word entry line."
  :group 'dictionary)

(defface dictionary-reference-face
  '((((type x)
      (class color)
      (background dark))
     (:foreground "yellow"))
    (((type tty)
      (class color)
      (background dark))
     (:foreground "brightyellow"))
    (((class color)
      (background light))
     (:foreground "blue"))
    (t
     (:underline t)))

  "The face that is used for displaying a reference word."
  :group 'dictionary)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Buffer local variables for storing the current state
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defvar dictionary-connection 
  nil
  "The current network connection")

(defvar dictionary-window-configuration
  nil
  "The window configuration to be restored upon closing the buffer")

(defvar dictionary-stack
  nil
  "The history buffer for dictionary operations")

(defvar dictionary-potential-stack-contents
  nil
  "The item that will be placed on stack next time")

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Global variables
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defvar dictionary-mode-map
  nil
  "Keymap for dictionary mode")

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Basic function providing startup actions
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defun dictionary-mode ()
  "This is a mode for searching a dictionary server implementing
the protocol defined in RFC 2229.

This is a quick reference to this mode describing the default key bindings:

* q close the dictionary buffer
* h display this help information
* s ask for a new word to search
* d search the word at point
* n or Tab place point to the next link
* p or S-Tab place point to the prev link
* Return or Button2 visit that link
* M-Return or M-Button2 search the word beneath link in all dictionaries
"
  
  (use-local-map dictionary-mode-map)
  (setq major-mode 'dictionary-mode)
  (setq mode-name "Dictionary")

  (make-local-variable 'dictionary-stack)
  (setq dictionary-stack nil)
  (make-local-variable 'dictionary-potential-stack-contents)
  (setq dictionary-potential-stack-contents nil)

  (make-local-hook 'kill-buffer-hook)
  (add-hook 'kill-buffer-hook 'dictionary-close t t))

(defun dictionary (&optional server port)
  "Create a new dictonary buffer and install dictionary-mode"
  (interactive)

  (let ((buffer (generate-new-buffer " *Dictionary buffer*"))
	(window-configuration (current-window-configuration)))
    (switch-to-buffer-other-window buffer)
    (make-local-variable 'dictionary-server)
    (make-local-variable 'dictionary-port)
    
    (if server
	(setq dictionary-server server)
      (setq server dictionary-server))
    (if port
	(setq dictionary-port port)
      (setq port dictionary-port))
    
    (make-local-variable 'dictionary-connection)
    (make-local-variable 'dictionary-window-configuration)
    (setq dictionary-window-configuration window-configuration)
    (dictionary-mode)
    (condition-case message
    (dictionary-check-connection server port)
      (error (progn
	       (dictionary-close)
	       (error "Unable to open connection to %s:%s - %s"
		      server port (nth 1 message)))))))

(unless dictionary-mode-map
  (setq dictionary-mode-map (make-sparse-keymap))

  (define-key dictionary-mode-map "q" 'dictionary-close)
  (define-key dictionary-mode-map "h" 'dictionary-help)
  (define-key dictionary-mode-map "s" 'dictionary-search)
  (define-key dictionary-mode-map "d" 'dictionary-search-word-at-point)
  (define-key dictionary-mode-map "l" 'dictionary-last)

  (define-key dictionary-mode-map [tab] 'dictionary-next-link)
  (define-key dictionary-mode-map "n" 'dictionary-next-link)
  (define-key dictionary-mode-map [(shift tab)] 'dictionary-prev-link)
  (define-key dictionary-mode-map "p" 'dictionary-prev-link)

  (define-key dictionary-mode-map " " 'scroll-up)
  (define-key dictionary-mode-map [(meta space)] 'scroll-down)

  (suppress-keymap dictionary-mode-map))
  
(defun dictionary-check-connection (server port)
  "Check if there is already a connection open"
  (if (not (or dictionary-connection
	       (eq (connection-status dictionary-connection) 'up)))
      (progn
	(connection-close dictionary-connection)
	(setq dictionary-connection
	      (connection-open server port))
	(dictionary-check-initial-reply))))
	
(defun dictionary-mode-p ()
  "Return non-nil if current buffer has dictionary-mode"
  (eq major-mode 'dictionary-mode))

(defun dictionary-ensure-buffer ()
  "If current buffer is not a dictionary buffer, create a new one."
  (unless (dictionary-mode-p)
    (dictionary)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Dealing with closing the buffer
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defun dictionary-close ()
  "Close the current dictionary buffer and its connection"
  (interactive)
  (if (eq major-mode 'dictionary-mode)
      (progn
	(setq major-mode nil)
	(connection-close dictionary-connection)
	(let ((configuration dictionary-window-configuration))
	  (kill-buffer (current-buffer))
	  (set-window-configuration configuration)))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Helpful functions
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defun dictionary-send-command (string)
  "Send the command `string' to the network connection."
  (connection-send-crlf dictionary-connection string))

(defun dictionary-read-reply ()
  "Read the reply line from the server"
  (let ((answer (connection-read-crlf dictionary-connection)))
    (if (string-match "\r" answer)
	(substring answer 0 (match-beginning 0))
      answer)))

(defun dictionary-split-string (string)
  "Split the `string' constiting of space separated words into elements.
This function knows about the special meaning of quotes (\")"
  (let ((list))
    (while (and string (> (length string) 0))
      (let ((search "\\(\\s-+\\)")
	    (start 0))
	(if (= (aref string 0) ?\")
	    (setq search "\\(\"\\)\\s-*"
		  start 1))
      (if (string-match search string start)
	  (progn
	    (setq list (cons (substring string start (- (match-end 1) 1)) list)
		  string (substring string (match-end 0))))
	(setq list (cons string list)
	      string nil))))
    (nreverse list)))

(defun dictionary-read-reply-and-split ()
  "Read the reply, split it into words and return it"
  (let ((answer (make-symbol "reply-data"))
	(reply (dictionary-read-reply)))
    (let ((reply-list (dictionary-split-string reply)))
      (put answer 'reply reply)
      (put answer 'reply-list reply-list)
      (put answer 'reply-code (string-to-number (car reply-list)))
      answer)))

(defmacro dictionary-reply-code (reply)
  "Return the reply code stored in `reply'."
  (list 'get reply ''reply-code))

(defmacro dictionary-reply (reply)
  "Return the string reply stored in `reply'."
  (list 'get reply ''reply))

(defmacro dictionary-reply-list (reply)
  "Return the reply list stored in `reply'."
  (list 'get reply ''reply-list))

(defun dictionary-read-answer ()
  "Read an answer delimited by a . on a single line"
  (let ((answer (connection-read-to-point dictionary-connection))
	(start 0))
    (while (string-match "^\\." answer start)
      (setq answer (replace-match "" t t answer))
      (setq start (match-end 0)))
    (setq start 0)
    (while (string-match "\r\n" answer start)
      (setq answer (replace-match "\n" t t answer))
      (setq start (1- (match-end 0))))
    answer))

(defun dictionary-check-reply (reply code)
  "Check if the reply in `reply' has the `code'."
  (let ((number (dictionary-reply-code reply)))
    (and (numberp number)
	 (= number code))))
    
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Communication functions
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defun dictionary-check-initial-reply ()
  "Read the first reply from server and check it."
  (let ((reply (dictionary-read-reply-and-split)))
    (unless (dictionary-check-reply reply 220)
      (connection-close dictionary-connection)
      (error "Server returned: %s" (dictionary-reply reply)))))

;; Store the current state
(defun dictionary-store-state (word dictionary kind pos window)
  "Stores the current state of operation for later restore"

  (if dictionary-potential-stack-contents
      (push (list (car dictionary-potential-stack-contents)
		  (cdr dictionary-potential-stack-contents)
		  kind pos window)
	    dictionary-stack))
  (setq dictionary-potential-stack-contents (cons word dictionary)))

;; Restore the last state
(defun dictionary-restore-state ()
  "Restore the state just before the last operation"
  (let ((prev (pop dictionary-stack)))
    (unless prev
      (error "Already at begin of history"))
    (let ((word (nth 0 prev))
	  (dictionary (nth 1 prev))
	  (kind (nth 2 prev))
	  (point (nth 3 prev))
	  (window-start (nth 4 prev)))
      (setq dictionary-potential-stack-contents (cons word dictionary))
      (funcall kind word dictionary)
      (set-window-start (selected-window) window-start)
      (goto-char point))))

;; The normal search

(defun dictionary-new-search (args &optional all)
  "Save the current state and start a new search"
  (dictionary-ensure-buffer)
  (let ((word (car args))
	(dictionary (cadr args))
	(pos (point))
	(window (window-start)))
    
    (if all
	(setq dictionary "*"))
    (dictionary-do-search word dictionary)
    (dictionary-store-state word dictionary 'dictionary-do-search
			    pos window)))


(defun dictionary-do-search (word dictionary)
  "The workhorse for doing the search"

  (dictionary-send-command (concat "define \"" dictionary "\" \""
				   word "\""))
  (let ((reply (dictionary-read-reply-and-split)))
    (if (dictionary-check-reply reply 552)
	(error "Word \"%s\" in dictionary \"%s\" not found"
	       word dictionary)
      (unless (dictionary-check-reply reply 150)
	(error "%s" reply))
      (dictionary-display-search-result reply))))

(defun dictionary-display-search-result (reply)
  "This function starts displaying the result starting with the `reply'."
  
  (let* ((number (nth 1 (dictionary-reply-list reply))))
    (toggle-read-only 0)
    (erase-buffer)
    (insert number (if (equal number "1")
		       " definition"
		     " definitions")
	    " found\n\n")
    (setq reply (dictionary-read-reply-and-split))
    (while (dictionary-check-reply reply 151)
      (let* ((reply-list (dictionary-reply-list reply))
	     (database (nth 2 reply-list))
	     (description (nth 3 reply-list))
	     (word (nth 1 reply-list)))
	(dictionary-display-word-entry word database description)
	(setq reply (dictionary-read-answer))
	(dictionary-display-word-definition reply word database)
	(setq reply (dictionary-read-reply-and-split))))
    (goto-char (point-min))
    (set-buffer-modified-p nil)
    (toggle-read-only 1)))

(defun dictionary-display-word-entry (word database description)
  "Insert an explanation for the current definition."
  (let ((start (point)))
    (insert "From " description "[" database "]:\n\n")
    (put-text-property start (point) 'face 'dictionary-word-entry-face)))

(defun dictionary-display-word-definition (reply word database)
  "Insert the definition for the current word"
  (let ((start (point)))
    (insert reply)
    (let ((regexp "\\({+\\)\\([^}]+\\)\\(}+\\)"))
      (goto-char start)
      (while (< (point) (point-max))
	(if (search-forward-regexp regexp nil t)
	    (progn
	      (replace-match "\\2")
	      ;; Compensate for the replacement
	      (let* ((brace-match-length (- (match-end 1)
					    (match-beginning 1)))
		     (match-start (- (match-beginning 2)
				     brace-match-length))
		     (match-end (- (match-end 2)
				   brace-match-length)))
		(dictionary-mark-reference match-start match-end
					   'dictionary-new-search
					   word database)))
	  (goto-char (point-max)))))))

(defun dictionary-mark-reference (start end call displayed-word database)
  "Format the area from `start' to `end' as link calling `call'.
The word is taken from the buffer, the `database' is given as argument."
  (let ((word (buffer-substring-no-properties start end)))
    (while (string-match "\n\\s-*" word)
      (setq word (replace-match " " t t word)))
    (while (string-match "[*\"]" word)
      (setq word (replace-match "" t t word)))
    
    (unless (equal word displayed-word)
      (link-create-link start end 'dictionary-reference-face
			call (list word database)))))


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; User callable commands
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defun dictionary-search (word &optional dictionary)
  "Search the `word' in `dictionary' if given or in all if nil"
  (interactive "sSearch word: ")

  (or dictionary
      (setq dictionary "*"))
  (dictionary-new-search (list word dictionary)))

(defun dictionary-last ()
  "Go to the previous location in the current buffer"
  (interactive)
  
  (unless (dictionary-mode-p)
    (error "Current buffer is no dictionary buffer"))
  (dictionary-restore-state))

(defun dictionary-search-word-at-point ()
  "Search the word placed around the cursor"
  (interactive)

  (dictionary-search (symbol-near-point)))

(defun dictionary-next-link ()
  "Place the cursor to the next link."
  (interactive)
  (let ((pos (link-next-link)))
    (if pos
	(goto-char pos)
      (error "There is no next link"))))

(defun dictionary-prev-link ()
  "Place the cursor to the previous link."
  (interactive)
  (let ((pos (link-prev-link)))
    (if pos
	(goto-char pos)
      (error "There is no previous link"))))

(defun dictionary-help ()
  "Display a little help"
  (interactive)
  (describe-function 'dictionary-mode))