From: mjn@xc_con.jet.uk (mark neale)
Newsgroups: comp.emacs,gnu.emacs.sources
Subject: Re: Copy word above/Defeating appending kills
Date: 16 Apr 92 07:41:02 GMT
	<SHARMAN.92Apr15001322@sharman.Software.Mitel.COM>
Organization: Joint European Torus


>   I'd like to have a function that, on a given line, reproduces the next word
>   on the previous line.

I missed the first half of this thread, but here's something I wrote a
while ago and provides a general solution to your problem. It allows
you to copy characters from the next/previous line up to and including some
specified character. For what you want to do, you would copy up to the
next space.

The idea for this was taken from an editor I used to use before I
found emacs. This editor had this bound to C-P, so for historical
reasons I still use this.

This code is available under the usual GNU licence. You are free to do
with it what you will.

---------C U T   H E R E-----------------------------------------

(defvar copy-to-eol-char ?\C-p
  "*character to type to copy up to the end of the previous/next line
in copy-from-previous-line")

(defun copy-from-previous-line(search &optional arg)
  "Copy text from previous line to CHAR (optional prefix arg means from next)
Copies text from the last/next non-blank line, from the current point up to
and including the character typed. If the character typed is
`copy-to-eol-char' the remainder of the line is copied. If
the character typed is \\[newline] then the remainder of the line is
copied and the cursor put at the start of the next line. Otherwise
text is copied up to and including CHAR if found. If not
found nothing will be copied

With prefix-arg, copy from next non-blank line"

  (interactive "*cCopy to character :\nP")
  (let*
      ((len (- (save-excursion (end-of-line) (point))
	       (point)))
       (nskip (current-column))
       eol slen
       (sol (save-excursion
	      (if (if arg
		      (save-excursion (forward-line 1) (eobp))
		    (save-excursion (beginning-of-line) (bobp)))
		  (error "No %s line in buffer" (if arg "next" "previous")))
	      ;;
	      ;; search for last/next non-blank line
	      ;;
	      (while (and (zerop (forward-line (if arg 1 -1)))
			  (zerop (save-excursion
				   (end-of-line) (current-column)))))
	      ;;
	      ;; now at previous non-blank line
	      ;; find end and length of search line
	      (end-of-line)
	      (setq eol (point))
	      (setq slen (current-column))
	      ;; go to start and return point position
	      (beginning-of-line)
	      (point)))
       ;;
       ;; find start pos to search from as point value
       ;; start pos is column which is min of nskip and
       ;; length of search line
       (spos (save-excursion
	       (goto-char sol)
	       (move-to-column (min slen nskip))
	       (point)))
       ;;
       ;; if search character is copy-to-eol-char or RET,
       ;; get remainder or line, else search for requested character
       ;;
       (epos
	(save-excursion
	  (goto-char spos)
	  (if (or (eq search copy-to-eol-char) (eq search ?\r))
	      (end-of-line)
	    (search-forward (char-to-string search) eol))
	  (point)))
       ;;
       ;; spos is at start position, epos is at found character
       ;; if character was not found, epos will be at spos
       ;; return found string, or null string
       ;;
       (txt (buffer-substring spos epos))
       (txtlen (min len
		    (- (save-excursion (goto-char epos) (current-column))
		       (save-excursion (goto-char spos) (current-column))))))

    (if overwrite-mode		    ; in overwrite mode, delete text
	(save-excursion
	  (move-to-column (+ (current-column) txtlen))
	  (backward-delete-char-untabify txtlen)))
    (insert txt)		    ; insert text
    (if (eq search ?\r)		    ; if RET, throw new line
	(newline))))
-- 
      #                   Mark Neale (mjn@jet.uk)
      #  ######   #####   Juggling with the parameters of Fusion.
      #  #          #
      #  #####      #     Joint European Torus, Culham Lab. OX14 3EA, UK
#     #  #          #     +44 235 528822 x4948
 #####   ######     #     Any opinion expressed above is.
- Disclaimer: Please note that the above is a personal view and should not 
  be construed as an official comment from the JET project.
Newsgroups: comp.emacs