[comp.unix.questions] Spell-checking a buffer in vi. Need help.

rostamia@umbc3.UMBC.EDU (Rouben Rostamian) (03/31/89)

Here is a questions that probably has been asked a hundred times in this
newsgroup.  My apologies to all old-timers.

To spell-check the current buffer in vi I do the following:
  1. 1G       [Go to the top of the current buffer]
  2. "byG     [Yank the current buffer and place it in the named buffer b]
  3. !Gspell  [Run (to the end of buffer) through spell]
              [The current buffer now is replaced by the output of
               spell (i.e., a list of misspelled words]
  4. "bP      [Insert the contents of the original buffer (from 
               the named buffer b) into the present buffer]

The net effect is that I obtain a list of my misspelled words appended to
the original buffer.

How do we automate this procedure?  It seems reasonable to bind the
4-step sequence above to one key, say to ^A, by:

map ^A  1G "byG !Gspell^M "bP

where ^M represents a carriage return.
But somehow this does not work.  For mysterious reasons the buffer b ends 
up empty after the execution, so I only get a list of my misspelled words and a
beep (a warning, I guess, that the buffer b is empty, so "bP cannot execute.)
I can recover my original buffer by pressing u, but then I lose the list
of misspelled words.  I will be thankful for any any explanation of why
this does not work and will welcome suggestions for fixes.

An ugly solution to the problem is redirect to output of the spell to a
temporary file then read in the temporary file into the buffer.  Are
there better solutions for spelling the buffer in vi?

-- 
Rouben Rostamian
Department of Mathematics                      e-mail:
University of Maryland Baltimore Counnty       Rostamian@umbc2.bitnet
Baltimore, MD 21228                            rostamia@umbc3.umbc.edu

dhesi@bsu-cs.UUCP (Rahul Dhesi) (03/31/89)

In article <1856@umbc3.UMBC.EDU> rostamia@umbc3.UMBC.EDU (Rouben Rostamian)
writes:
[questions re spell checking vi buffer]

1.   To just see spelling errors:

    :w ! spell | fmt


2.   To append spelling errors to buffer:

    :$r !spell % | fmt

Definining mapped keys is left as an exercise for the reader.  You may
need multiple levels of quoting.

Actually I keep a shell script around, called spf, that contains:

     #! /bin/sh
     # filters stdin through spell and fmt, so spelling errors are compactly
     # displayed
     exec cat $* | exec /usr/bin/spell | /usr/ucb/fmt

and I just type ":w ! spf" to see spelling errors on the screen.

(I add the "| fmt" so it all fits in three or four lines at most.
Those unfortunate enough to have traded fragmented disks for fmt can
just omit the "| fmt" from the above commands.)
-- 
Rahul Dhesi         UUCP:  <backbones>!{iuvax,pur-ee}!bsu-cs!dhesi
                    ARPA:  dhesi@bsu-cs.bsu.edu

maart@cs.vu.nl (Maarten Litmaath) (04/01/89)

rostamia@umbc3.UMBC.EDU (Rouben Rostamian) writes:
\To spell-check the current buffer in vi I do the following:

[deleted]

\The net effect is that I obtain a list of my misspelled words appended to
\the original buffer.

\How do we automate this procedure?  It seems reasonable to bind the
\4-step sequence above to one key, say to ^A, by:

\map ^A  1G "byG !Gspell^M "bP

\where ^M represents a carriage return.
\But somehow this does not work.
\...

Well, I haven't figured out (yet) why this doesn't work (the `!' seems to be
the cause of trouble: map ^A 1G"byG works fine), but I've got a workaround:

	map	^A	:w^M:$r!spell %^M

However, undoing a `^A' reveals another bug: the last line of the original
buffer is doubled! Further undoing leads to `interesting' effects too...
Puzzle for the reader: Who is the connection between vi and csh?
Hint: check recent postings on csh quirks.
Disclaimer: vi's my editor, csh's my shell (sh's for shell scripts).
-- 
 Modeless editors and strong typing:   |Maarten Litmaath @ VU Amsterdam:
   both for people with weak memories. |maart@cs.vu.nl, mcvax!botter!maart

wyle@inf.ethz.ch (Mitchell Wyle) (04/02/89)

>To spell-check the current buffer in vi I do the following:

>map ^A  1G "byG !Gspell^M "bP

>temporary file then read in the temporary file into the buffer.  Are
>there better solutions for spelling the buffer in vi?
>rostamia@umbc3.umbc.edu


In my .exrc, I have a macro:  map ;sp !}spellcheck

and spellcheck is simply:
#!/bin/sh
#       A sh script to allow spelling checks either in vi or stand alone
#       Usage: spell_check < file or within vi,
#               !}spell_check           or
#               :(addressed lines)!spell_check
#
# The wrongly spelt words will be surrounded by ###bad-word### 
#       Badri Lokanathan, 30 September 1988
###############################################################
doc=/tmp/splchk.$$
scr=/tmp/sedscr.$$

trap 'rm -f $doc $scr; exit 1' 1 2 15
cat > $doc
spell < $doc |
  sed -e 's/^/s;\\(\\.*\\)\\(/
          s/$/\\)\\(\\.*\\);\\1###\\2###\\3;g/' > $scr
#
#       sed -e 's/^/s;\\([      ][      ]*\\)\\(/
#               s/$/\\)\\([     ][      ]*\\);\\1###\\2###\\3;g/' > $scr
#
sed -f $scr $doc
rm -f $doc $scr

I admit it's only for PARAGRAPHS, and not a buffer, but it runs fast.

-- 
-Mitchell F. Wyle                         wyle@ethz.uucp
Institut fuer Informationssysteme         wyle@inf.ethz.ch
ETH Zentrum / 8092 Zurich, Switzerland    +41 1 256 5237