home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fresh Fish 2
/
FFMCD02.bin
/
useful
/
lib
/
emacs
/
site-lisp
/
ispell.el
Wrap
Lisp/Scheme
|
1993-09-30
|
18KB
|
542 lines
;; This is the GNU EMACS interface to GNU ISPELL version 4.
;; Copyright (C) 1990, 1993 Free Software Foundation, Inc.
;;
;; This file is part of GNU ISPELL.
;;
;; 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, write to the Free Software
;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
(defvar ispell-have-new-look t
"T if default 'look' program has the -r flag.")
(defvar ispell-enable-tex-parser nil
"T to enable experimental tex parser in ispell for tex buffers.")
(defvar ispell-process nil "The process running ISPELL")
(defvar ispell-next-message nil
"An integer telling where in the *ispell* buffer where
to look for the next message from the ISPELL program.")
;Each marker in this list points to the start of a word that
;ispell thought was bad last time it did the :file command.
;Notice that if the user accepts or inserts a word into his
;private dictionary, then some "good" words will be on the list.
;We would like to deal with this by looking up the words again just before
;presenting them to the user, but that is too slow on machines
;without the select system call. Therefore, see the variable
;ispell-recently-accepted.
(defvar ispell-bad-words nil
"A list of markers corresponding to the output of the ISPELL :file command.")
;list of words that the user has accepted, but that might still
;be on the bad-words list
(defvar ispell-recently-accepted nil)
;t when :dump command needed
(defvar ispell-dump-needed nil)
(defun ispell-flush-bad-words ()
(while ispell-bad-words
(if (markerp (car ispell-bad-words))
(set-marker (car ispell-bad-words) nil))
(setq ispell-bad-words (cdr ispell-bad-words)))
(setq ispell-recently-accepted nil))
(defun kill-ispell ()
"Kill the ispell process. Any changes the your private dictionay
that have not already been dumped will be lost."
(interactive)
(if ispell-process
(delete-process ispell-process))
(setq ispell-process nil)
(ispell-flush-bad-words))
(put 'ispell-startup-error 'error-conditions
'(ispell-startup-error error))
(put 'ispell-startup-error 'error-message
"Problem starting ispell - see buffer *ispell*")
(defun start-ispell ()
"Start an ispell subprocess; check the version; and display the greeting."
(message "Starting ispell ...")
(let ((buf (get-buffer "*ispell*")))
(if buf
(kill-buffer buf)))
(condition-case err
(setq ispell-process (start-process "ispell" "*ispell*" "ispell" "-S"))
(file-error (signal 'ispell-startup-error nil)))
(process-kill-without-query ispell-process)
(buffer-flush-undo (process-buffer ispell-process))
(accept-process-output ispell-process)
(let (last-char)
(save-excursion
(set-buffer (process-buffer ispell-process))
(bury-buffer (current-buffer))
(setq last-char (- (point-max) 1))
(while (not (eq (char-after last-char) ?=))
(cond ((not (eq (process-status ispell-process) 'run))
(kill-ispell)
(signal 'ispell-startup-error nil)))
(accept-process-output ispell-process)
(setq last-char (- (point-max) 1)))
(goto-char (point-min))
(let ((greeting (read (current-buffer))))
(if (not (= (car greeting) 1))
(error "Bad ispell version: wanted 1, got %d" (car greeting)))
(message (car (cdr greeting))))
(delete-region (point-min) last-char))))
;leaves buffer set to *ispell*, point at '='
(defun ispell-sync (intr)
"Make sure ispell is ready for a command."
(if (or (null ispell-process)
(not (eq (process-status ispell-process) 'run)))
(start-ispell))
(if intr
(interrupt-process ispell-process))
(let (last-char)
(set-buffer (process-buffer ispell-process))
(bury-buffer (current-buffer))
(setq last-char (- (point-max) 1))
(while (not (eq (char-after last-char) ?=))
(accept-process-output ispell-process)
(setq last-char (- (point-max) 1)))
(goto-char last-char)))
(defun ispell-cmd (&rest strings)
"Send a command to ispell. Choices are:
word any word is checked for spelling. Result is
nil not found
t spelled ok
list of strings near misses
:file filename scan the named file, and print the file offsets of
any misspelled words
:insert word put word in private dictonary
:accept word don't complain about word any more this session
:dump write out the current private dictionary, if necessary.
:reload reread ~/ispell.words
:tex
:troff
:generic set type of parser to use when scanning whole files
"
(save-excursion
(ispell-sync t)
(set-buffer (process-buffer ispell-process))
(bury-buffer (current-buffer))
(erase-buffer)
(setq ispell-next-message (point-min))
(while strings
(process-send-string ispell-process (car strings))
(setq strings (cdr strings)))
(process-send-string ispell-process "\n")
(accept-process-output ispell-process)
(ispell-sync nil)))
(defun ispell-dump ()
(cond (ispell-dump-needed
(setq ispell-dump-needed nil)
(ispell-cmd ":dump"))))
(defun ispell-insert (word)
(ispell-cmd ":insert " word)
(if ispell-bad-words
(setq ispell-recently-accepted (cons word ispell-recently-accepted)))
(setq ispell-dump-needed t))
(defun ispell-accept (word)
(ispell-cmd ":accept " word)
(if ispell-bad-words
(setq ispell-recently-accepted (cons word ispell-recently-accepted))))
(defun ispell-next-message ()
"Return the next message sent by the ispell subprocess."
(save-excursion
(set-buffer (process-buffer ispell-process))
(bury-buffer (current-buffer))
(save-restriction
(goto-char ispell-next-message)
(narrow-to-region (point)
(progn (forward-sexp 1) (point)))
(setq ispell-next-message (point))
(goto-char (point-min))
(read (current-buffer)))))
(defun ispell-tex-buffer-p ()
(memq major-mode '(plain-TeX-mode LaTeX-mode)))
(defun ispell (&optional buf start end)
"Run ispell over a buffer. (Actually over the buffer's file.)
First the file is scanned for misspelled words, then ispell
enters a loop with the following commands for every misspelled word:
DIGIT Near miss selector. If the misspelled word is 'close' to
some words in the dictionary, they are offered as near misses.
r Replace. Replace the word with a string you type. Each word
of your new string is also checked.
i Insert. Insert this word in your private dictonary (kept in
$HOME/ispell.words)
a Accept. Accept this word for the rest of this editing session,
but don't put it in your private dictonary.
l Lookup. Look for a word in the dictionary by fast binary
search, or search for a regular expression in the dictionary
using grep.
SPACE Accept the word this time, but complain if it is seen again.
q, C-G Leave the command loop. You can come back later with \\[ispell-next]."
(interactive)
(if (null start)
(setq start 0))
(if (null end)
(setq end 0))
(if (null buf)
(setq buf (current-buffer)))
(setq buf (get-buffer buf))
(if (null buf)
(error "Can't find buffer"))
(save-excursion
(set-buffer buf)
(let ((filename buffer-file-name)
(delete-temp nil))
(unwind-protect
(progn
(cond ((null filename)
(setq filename (make-temp-name "/usr/tmp/ispell"))
(setq delete-temp t)
(write-region (point-min) (point-max) filename))
((and (buffer-modified-p buf)
(y-or-n-p (format "Save file %s? " filename)))
(save-buffer)))
(message "Ispell scanning file...")
(if (and ispell-enable-tex-parser
(ispell-tex-buffer-p))
(ispell-cmd ":tex")
(ispell-cmd ":generic"))