[comp.emacs] spell-lookup-word-in-buffer

tim@banyan.UUCP (Tim Henrion) (01/04/88)

I posted this once before and nobody answered me so I'll try
once more. Does anybody out there have the source for the
spell-lookup-word-in-buffer function for GNU emacs that
somebody posted a few months ago? Any help would be greatly
appreciated.

----------------------------------------------------------------------------

Tim Henrion
Communications Software Engineer	UUCP:  ...!necntc!banyan!tim
Banyan Systems, Inc.			VOICE: (617) 898-1116
115 Flanders Road
Westboro, MA 01581

	   "UNIX?  What do you expect from the phone company..." :-)

ansley@sunybcs.uucp (William Ansley) (01/08/88)

In article <231@banyan.UUCP> tim@banyan.UUCP (Tim Henrion) writes:
>I posted this once before and nobody answered me so I'll try
>once more. Does anybody out there have the source for the
>spell-lookup-word-in-buffer function for GNU emacs that
>somebody posted a few months ago? Any help would be greatly
>appreciated.
>
[...]

Sorry to post this, but when I tried to use e-mail, it bounced right back to
me.

Here's the whole package as I got it off the net.  I think it's pretty well
documented.  I've been using it for quite some time now and find it to be very
useful.  There are one or two improvements that could be made (most notably
automatically giving the string from the start of the word being checked to
point to ispell when it is called in the lookup function) but I haven't had
the time (and I'm not sure I have the expertise) to make them.

By the way, I've been using the file web2 rather than words, since I am
working on a BSD 4.3 system.  It's great to have some way to access this much
larger dictionary, since it seems impossible to make UNIX's spell use it.

------------------------------cut here------------------------------

;;;
;;; spell-dict.el - Access to on-line spelling dictionary.  
;;;
;;; Author:	John W. Peterson
;;;		Computer Science Dept.
;;;		University of Utah
;;; Date:	16-Dec-86
;;;
;;;    ESC-?   Look up words starting with what you typed in the buffer and
;;;            let you search for a word to replace it with.
;;;

(defvar *dictionary-file* "/usr/dict/words"
  "*File used for the spelling dictionary.  On 4.3bsd systems, try
using \"/usr/dict/web2\" for a larger selection.  Apollo users may
want to try \"/sys/dict\".")

(defvar *max-dictionary-height* 15 "*Maximum number of lines the 
*Spelling Dictionary* window will take on the screen")

(defun spell-lookup-word-in-buffer ()
  "Look up words in the dictionary that start with the letters from point
to the beginning of the word in the current buffer.  A window pops up and 
puts you in I-search mode.  Search for the word you want (you don't have to
type the whole thing) and then hit ESC.  That word is then inserted into
your buffer, replacing the word you had partially typed.  Type ^G to abort."  

  (interactive)
  (let* ((cur-buf (current-buffer))
	 (cur-win (selected-window))
	 (one-window (eq cur-win (next-window)))
	 num-lines target-height lookup-str str-beg str-end word-end word
	 in-word replace-str)
    ;;
    ;; Get the string to look up
    ;;
    (setq str-end (point))
    (save-excursion
      (setq in-word (looking-at "\\w"))	;Flag if cursor is inside lookup word
      (backward-word 1)
      (setq str-beg (point))
      (setq lookup-str (buffer-substring str-beg str-end))
      ;; You can look up just part of a word, but need to replace all of it.
      (forward-word 1)
      (setq replace-str (buffer-substring str-beg (point))))
    ;;
    ;; The purist would probably use Electric-pop-up-window here.  I couldn't
    ;; get it to work to my liking.
    ;;
    (save-window-excursion
      (if one-window
	  ;; It would be nice to have something here that would force the
	  ;; spelling dictionary window to the bottom.
	  nil
	(other-window 1))		;Don't nuke current window
      (with-output-to-temp-buffer "*Spelling Dictionary*"
	(switch-to-buffer "*Spelling Dictionary*" t)
	(call-process "look" nil t nil "-df" lookup-str *dictionary-file*)
	(fill-region (point-min) (point-max) nil)
	(setq num-lines (count-lines (point-min) (point-max)))
	; Stolen from Electric-pop-up-window...
	(setq target-height
	      (min (max (1+ num-lines) window-min-height)
		   (min
		    (save-window-excursion	; Find Max screen size
		      (delete-other-windows)
		      (1- (window-height (selected-window))))
		    *max-dictionary-height*)))
	)
      ;;
      ;; Window hacking must be done outside the scope of 
      ;; with-output-to-temp-buffer
      ;;
      (select-window (get-buffer-window "*Spelling Dictionary*"))
      (if (< target-height num-lines)
	  (setq mode-name "*MORE*")
	(setq mode-name "All"))
      (shrink-window (- (window-height) target-height))
      (isearch-forward)			;Allow them to look for word
      (cond
       ((not (= (point-min) (point)))	;Search found something (no abort)
	(if (not (looking-at "\\Sw"))	;Find entire thing, copy to word
	    (forward-word 1))
	(setq word-end (point))		
	(backward-word 1)
	(setq word (buffer-substring (point) word-end))
	(switch-to-buffer cur-buf)
	(select-window cur-win)		;Kill partial in buffer and replace
	(if in-word			
	    (forward-word 1))		;Replace the whole thing
	;;
	;; Use search/replace to fix word so capitolization/case is preserved
	;;
	(search-backward replace-str str-beg)
	(replace-match word))))))      

(global-set-key "\e?" 'spell-lookup-word-in-buffer)

------------------------------cut here------------------------------


William H. Ansley, Computer Science Graduate Student   
uucp:	  ..!{ames,boulder,decvax,rutgers}!sunybcs!ansley
internet: ansley@cs.buffalo.edu         bitnet:	  ansley@sunybcs.bitnet
[or: ansley%cs.buffalo.edu@relay.cs.net, or:  ansley@buffalo.csnet]