From: kgallagh@digi.lonestar.org (Kevin Gallagher)
Newsgroups: comp.emacs
Subject: Re: Copy word above/Defeating appending kills
Date: 17 Apr 92 16:19:50 GMT
Organization: DSC Communications Corp, Plano, TX

In artilce <KENB.92Apr8084554@atg2.dadd.ti.com> kenb@atg2.dadd.ti.com writes: 
>
>  I'd like to have a function that, on a given line, reproduces the next word
>  on the previous line.
>
>  I've tried writing my own:
>  ...
>  instead of getting one word at a time like I want, I keep getting
>  all of the preceding sentence that I've copied.

You might like the behaviour of the attached file.  The tricky part was to get
it to work correctly when the line above the cursor contains one or more tab
characters.  Also, special tests had to be added so it would behave correctly
on the first line of the buffer.

All you need do is to bind the function edt-duplicate-word to a keystroke of
your choice.

Enjoy!

==============================================================================

;;; File:  duplicate-word.el
;;;
;;;  This code has been lifted from the Beta Version 0.5 of the newly
;;;  enhanced GNU Emacs EDT Emulation package.  It should be self-contained,
;;;  however.  So you should not need the rest of the package for it to work.
;;;  
;;;  In addition to being a much improved EDT emulation, the package also
;;;  contains numerous extensions to EDT.  For example, it contains an
;;;  emulation of the TPU/EVE LEARN command, as well as this duplicate-word
;;;  command.
;;;
;;; Copyright (C) 1986 Free Software Foundation, Inc.
;;; Copyright (C) 1991 Kevin Gallagher
;;;
;;; GNU Emacs is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY.  No author or distributor accepts
;;; RESPONSIBILITY TO anyone for the consequences of using it or for
;;; whether it serves any particular purpose or works at all, unless 
;;; he says so in writing.  Refer to the GNU Emacs General Public
;;; License for full details.
;;;
;;;  Queries about the enhanced EDT emulation should be sent to 
;;;  Kevin Gallagher (kgallagh@digi.lonestar.org OR ...!uunet!digi!kgallagh)
;;;
;;; Everyone is granted permission to copy, modify and redistribute
;;; GNU Emacs, but only under the conditions described in the GNU
;;; Emacs General Public License.  A copy of this license is supposed
;;; to have been given to you along with GNU Emacs so you can know
;;; your rights and responsibilities.  It should be in a file named
;;; COPYING.  Among other things, the Copyright notice and this notice
;;; must be preserved on all copies.
;;;
;;; ====================================================================


(defvar edt-word-entities '(?\t)
  "*Specifies the list of word entity characters.")

(defun edt-one-word-forward ()
  "Move forward to first character of next word."
  (interactive)
  (if (eobp)
      (error "End of buffer"))
  (if (eolp)
      (forward-char)
      (progn
        (if (memq (following-char) edt-word-entities)
            (forward-char)
            (while (and 
                     (not (eolp))
                     (not (eobp))
                     (not (eq ?\  (char-syntax (following-char))))
                     (not (memq (following-char) edt-word-entities)))
              (forward-char)))
        (while (and 
                 (not (eolp))
                 (not (eobp))
                 (eq ?\  (char-syntax (following-char)))
                 (not (memq (following-char) edt-word-entities)))
          (forward-char)))))

;;;
;;; DUPLICATE WORD
;;;

(defun edt-duplicate-word()
  "Duplicate word (or rest of word) found directly above cursor, if any."
  (interactive "*")
  (let ((start (point))
        (start-column (current-column)))
    (forward-line -1)
    (move-to-column start-column)
    (if (and (not (equal start (point)))
             (not (eolp)))
        (progn
          (if (and (equal ?\t (preceding-char))
                   (< start-column (current-column)))
              (backward-char))
          (let ((beg (point)))
            (edt-one-word-forward)
            (setq edt-last-copied-word (buffer-substring beg (point))))
          (forward-line)
          (move-to-column start-column)
          (insert edt-last-copied-word))
        (progn
          (forward-line)
          (move-to-column start-column)
          (error "Nothing to duplicate!")))))

;;; SPECIFYING WORD ENTITIES:
;;;
;;; The variable edt-word-entities is used to emulate EDT's SET ENTITY WORD
;;; command.  It contains a list of characters to be treated as words in
;;; themselves.  If the user does not define edt-word-entities in his/her
;;; .emacs file, then it is set up with the EDT default containing only TAB.
;;; 
;;; The characters are stored in the list by their numerical values, not as
;;; strings.  Emacs supports several ways to specify the numerical value of a
;;; character.  One method is to use the question mark: ?A means the numerical
;;; value for A, ?/ means the numerical value for /, and so on.  Several
;;; unprintable characters have special representations:
;;; 
;;;             ?\b  specifies  BS, C-h
;;;             ?\t  specifies  TAB, C-i
;;;             ?\n  specifies  LFD, C-j
;;;             ?\v  specifies  VTAB, C-k
;;;             ?\f  specifies  FF, C-l
;;;             ?\r  specifies  CR, C-m
;;;             ?\e  specifies  ESC, C-[
;;;             ?\\  specifies  \
;;; 
;;; Here are some examples:
;;; 
;;;      (setq edt-word-entities '(?\t ?- ?/)) ;; Specifies TAB, - , and /
;;;      (setq edt-word-entities '(?\t)        ;; Specifies TAB, the default
;;; 
;;; You can also specify characters by their decimal ascii values:
;;; 
;;;      (setq edt-word-entities '(9 45 47))   ;; Specifies TAB, - , and /
;;; 
-- 
----------------------------------------------------------------------------
Kevin Gallagher        kgallagh@digi.lonestar.org OR ...!uunet!digi!kgallagh
DSC Communications Corporation   Addr: MS 152, 1000 Coit Rd, Plano, TX 75075
----------------------------------------------------------------------------
Xref: sparky comp.emacs:2005 comp.os.os2.misc:11641