[comp.emacs] Copy From Above?

gph@hpsemc.HP.COM (Old run-down actor) (02/16/89)

Can anyone tell me how I can get gnuemacs to (with one keystroke) copy
the character on the line above the cursor to the current cursor
position?

   For example, my screen looks like this:


            This is the first  line of text.
            This is the second line of text.
            This is the third  line of text.
            _


   My cursor is sitting where the "_" character is shown.  After typing
in the keystroke mapped to the functionality I want, you should see:

            This is the first  line of text.
            This is the second line of text.
            This is the third  line of text.
            T_

    If I repeat it multiple times or use cntl-u (say 5) and then the
keystroke, you should see:

            This is the first  line of text.
            This is the second line of text.
            This is the third  line of text.
            This i_

    Any help would be appreciated.


Paul Houtz
HP Technology Access Center
10670 N. Tantau Avenue
Cupertino, Ca 95014
(408) 725-3864
hplabs!hpda!hpsemc!gph 
gph%hpsemc@hplabs.HP.COM

mesard@bbn.com (Wayne Mesard) (02/16/89)

In article <690026@hpsemc.HP.COM> gph@hpsemc.HP.COM (Old run-down actor) writes:
>Can anyone tell me how I can get gnuemacs to (with one keystroke) copy
>the character on the line above the cursor to the current cursor
>position?

>    If I repeat it multiple times or use cntl-u (say 5) and then the
>keystroke, you should see:
> [deleted]

For simple things like this, all you need is keyboard macros.
Read about them in the manual (section 28.3 of the fifth edition), and
look at the documentation for the function start-kbd-macro [which is
generally bound to Ctrl-X (  ].

In your case you would type the following:

 Ctrl-X (          ; Start the macro.
 Ctrl-P            ; Move up a line.
 Ctrl-U 1 Ctrl-D   ; Delete one char and put it on the kill-ring.
 Ctrl-Y            ; Yank it back.
 Ctrl-N            ; Move back to original line.
 Ctrl-Y            ; Put a copy of the killed char here.
 Ctrl-X )          ; Done defining macro.

Now your command can be executed by Ctrl-X E (call-last-kbd-macro).
And Ctrl-U <n> Ctrl-X E will do the right thing, too.  To bind it to
another key, you must first name it with M-x name-last-kbd-macro.  After
naming it you can insert it in, e.g., your ~/.emacs file, so it will be
available in later Emacs sessions.  To do this use insert-kbd-macro.

Gosh, and you didn't have to type a single lisp expression.  Whattaneditor!

[[Hypothesis of the day: It is possible to build an arbitrarily complex
Turing machine using Emacs keyboard macros :-) ]]

[[Followups refuting the previous sentence (after all its got a
net.insurance.policy (i.e. a smiley)), or showing a way to do the macro
in only six keystrokes to /dev/null.]]



-- 
unsigned *Wayne_Mesard();
MESARD@BBN.COM           
BBN, Cambridge, MA       

pratap@hpcllcm.HP.COM (Pratap Subrahmanyam) (02/17/89)

If you don't have to press the constraint of "one keystroke" then I guess 
you could write a small macro, bind it to some key, and there you have it.


-- pratap.

gaynor@athos.rutgers.edu (Silver) (02/17/89)

Hmph, it appears my original posting didn't make it out, or I used reply
instead of follow-up, or whatever.  C'est la vie.

;; (define-key global-map "\C-\\" 'insert-from-above-command)

(defun insert-from-above-command (n)
"Insert the character at the same column from the previous non-blank line at
the point.  Prefix argument specifies maximum number of characters to copy, as
less will be copied if that previous line is too short."
  (interactive "p")
  (let* ((cc (current-column))
	 pt)
    (insert (save-excursion
	      (beginning-of-line)
	      (and (bobp) (error "Beginning of buffer"))
	      (skip-chars-backward "\ \t\n")
	      ;; Characters which take up more than 1 column of display are
	      ;; tricky.
	      (if (<= cc (move-to-column cc))
		(buffer-substring
		  (setq pt (point))
		  (progn (skip-chars-forward "^\n" (+ (point) n))
			 ;; 1+ to grab the newline if not n chars
			 (if (< (point) (+ pt n))
			   (1+ (point))
			   (point)))))))))

Regards, [Ag] gaynor@rutgers.edu

mgh@intelob.biin.com (Mike Heavin) (02/17/89)

Paul Houtz writes:

Can anyone tell me how I can get gnuemacs to (with one keystroke) copy
the character on the line above the cursor to the current cursor
position?

   For example, my screen looks like this:


            This is the first  line of text.
            This is the second line of text.
            This is the third  line of text.
            _


   My cursor is sitting where the "_" character is shown.  After typing
in the keystroke mapped to the functionality I want, you should see:

            This is the first  line of text.
            This is the second line of text.
            This is the third  line of text.
            T_

    If I repeat it multiple times or use cntl-u (say 5) and then the
keystroke, you should see:

            This is the first  line of text.
            This is the second line of text.
            This is the third  line of text.
            This i_

    Any help would be appreciated.

--------------------------
how about the following:

(defun copy-char-above-cursor ()
"Copy the character immediately above the cursor onto the current line."
  (interactive)
  (point-to-register 1)
  (setq temp-column (current-column))
  (previous-line 1)
  (beginning-of-line)
  (forward-char temp-column)
  (setq temp-start-region (dot-marker))
  (forward-char 1)
  (setq temp-end-region (dot-marker))
  (copy-to-register 0 temp-start-region temp-end-region)
  (register-to-point 1)
  (insert-register 0 1)
)

merlyn@intelob.intel.com (Randal L. Schwartz @ Stonehenge) (02/18/89)

In article <MGH.89Feb17085945@intelob.biin.com>, mgh@intelob (Mike Heavin) writes:
| how about the following:
| 
| (defun copy-char-above-cursor ()
| "Copy the character immediately above the cursor onto the current line."
|   (interactive)
|   (point-to-register 1)
|   (setq temp-column (current-column))
|   (previous-line 1)
|   (beginning-of-line)
|   (forward-char temp-column)
|   (setq temp-start-region (dot-marker))
|   (forward-char 1)
|   (setq temp-end-region (dot-marker))
|   (copy-to-register 0 temp-start-region temp-end-region)
|   (register-to-point 1)
|   (insert-register 0 1)
| )
| 

Which is probably vaguely equivalent to (without all the smoke and mirrors):

(defun copy-char-above-cursor ()
  "Copy the character immediately above the cursor onto the current line."
  (interactive)
  (insert (save-excursion
	    (move-to-column (prog1 (current-column)
			      (forward-line -1)))
	    (char-after (point)))))

[warning... untested code... compile at your own risk]

"Variables?  What are variables?"
-- 
Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095
on contract to BiiN (for now :-), Hillsboro, Oregon, USA.
ARPA: <@intel-iwarp.arpa:merlyn@intelob> (fastest!)
MX-Internet: <merlyn@intelob.intel.com> UUCP: ...[!uunet]!tektronix!biin!merlyn
Standard disclaimer: I *am* my employer!
Cute quote: "Welcome to Oregon... home of the California Raisins!"