[comp.emacs] Followup to: "Real" Vi and Edt emulation packages?

root@ttsi.lonestar.org (System) (04/27/91)

I have been asked to be more specific concerning an earlier posting
that aired a peeve about word handling in Emacs.  Let me illustrate
with a simple example:

Here is a random piece of C code:

#include <X11/Xatom.h>

Starting with the cursor above the line above, and successively moving
forward by word, here is how the cursor moves via emacs, vip-mode emacs,
and vi (X marks the spots):

       emacs                     vip-mode                      vi
#include <X11/Xatom.h>     #include <X11/Xatom.h>    #include <X11/Xatom.h>
        X    X     X X      X        X   X     X     XX       XX  XX    XXX
         
Starting from the beginning of the line and deleting successive words,
here is how the three environments gobble the line (numbers marking 
the text deleted for a given delete-word:


       emacs                     vip-mode                      vi
#include <X11/Xatom.h>     #include <X11/Xatom.h>    #include <X11/Xatom.h>
1111111122222333333445     1222222222333344444455    1222222234555677777890

In the case of emacs and vip-mode, above, the end-of-line and stuff 
from the succeeding line gets deleted as well.  In other words, vi
treats the non-alphanumeric characters as word delimiters as well as
words themselves.  This is just one example pulled out of my hat.
I'm not saying one approach is inherently better than another, just that
I've grown fond of vi's behaviour and want to match it in my emacs
environment.  Please don't flame my taste.

Someone suggested diddling with the syntax table to get the behavior I
want.  I give--what would the character class assignments be to get
the behavior of vi?

-- 
Mark S. Evans                 Tandem Telecommunications Systems Inc.
Phone: 214-516-6201           850 E. Central Parkway
Fax:   214-516-6801           Plano, TX 75074
Mail:  mse@ttsi.lonestar.org

kgallagh@digi.lonestar.org (Kevin Gallagher) (04/28/91)

In article <1991Apr26.213036.10363@ttsi.lonestar.org> root@ttsi.lonestar.org (System) writes:
>[stuff deleted]
>Someone suggested diddling with the syntax table to get the behavior I
>want.  I give--what would the character class assignments be to get
>the behavior of vi?

I think I did, but I did not mean to suggest that mucking with the syntax
table would suffice.  This is because of how forward-word, kill-word, etc.,
are defined.  For example, the description of kill-word is as follows:

	kill-word:
	Kill characters forward until encountering the end of a word.
	With argument, do this that many times.

This is pretty simplistic stuff.  Here's what it does:

	remember the current position of point
	if following character is not a word character
	    then move forward until encountering next word character
	move forward until encountering next non-word character
	kill all characters from remembered position to point

You want functions which have many more conditions under which they are
supposed to terminate.  Which means, to get the behavior you want, you will
have to write your own forward-word, kill-word, etc., so they will stop where
you want them to stop.  It will take some time to write each of the functions
you want, but you should find none of it very difficult to do.  Emacs Lisp has
all the tools needed.

Here's a simple example of an attempt to emulate EDT's WORD command (in the
forward direction):

(defun edt-one-word-forward ()
  "Move forward to first character of next word."
  (interactive)
  (if (eobp)
      (progn
	(beep)
	(message "End of buffer."))
      (progn
	(if (eolp)
	    (forward-char))
	(while (and 
		 (not (eolp))
		 (not (eobp))
		 (eq 119 (char-syntax (following-char))))
	  (forward-char))
	(while (and 
		 (not (eolp))
		 (not (eobp))
		 (not (eq 119 (char-syntax (following-char)))))
	  (forward-char)))))

It is not a 100% EDT.  For example, it does not stop on every tab character
they way EDT does.  But, with an appropriately set up syntax table, its close.
And, with a little work, it could be made to behave exactly like EDT.

If you decide to write your own "better" vi emulation, be sure to consider
using the Emacs functions skip-chars-forward and skip-chars-backward.  They
can be very handy at times.
-- 
----------------------------------------------------------------------------
Kevin Gallagher        kgallagh@digi.lonestar.org OR ...!uunet!digi!kgallagh
DSC Communications Corporation   Addr: MS 152, 1000 Coit Rd, Plano, TX 75075
----------------------------------------------------------------------------