emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: yank-handler


From: Andreas Schwab
Subject: Re: yank-handler
Date: Thu, 15 May 2003 18:19:10 +0200
User-agent: Gnus/5.1001 (Gnus v5.10.1) Emacs/21.3.50 (gnu/linux)

Richard Stallman <address@hidden> writes:

|>     If I kill a piece of a hunk and then yank it in some other buffer,
|>     the "[+-! ]" prefix added to each and every line should be removed
|>     (unless I'm yanking back into a diff-mode buffer or unless there's
|>     something like a "@@ -463,4 +463,36 @@" marker, obviously).
|> 
|> I have a bad feeling about this--it is the sort of thing
|> that makes the editor overall less controllable.  I would
|> prefer some sort of feature to copy part of a diff buffer
|> into the kill ring without these prefixes.

I've been using this for years:

(defun delete-columns (start end cols)
  "Delete columns from the beginning of the lines in the region.
Prefix argument gives the number of columns to delete."
  (interactive "*r\np")
  (save-excursion
    (goto-char end)
    (if (bolp) (forward-line -1))
    (move-to-column cols)
    (if (eolp) (indent-to cols))
    (delete-rectangle
     (save-excursion (goto-char start) (beginning-of-line) (point))
     (point))))

(defun copy-diff (&optional reverse)
  "Copy the current hunk of a context diff into the buffer in the next window.
Point must be at the first line to be copied.
Optional arg REVERSE (prefix arguement if interactive) means to
 assume a reversed patch."
  (interactive "P")
  (let (start region)
    (beginning-of-line)
    (setq start (point))
    (if (not (and (looking-at (if reverse "[-!] " "[+!] "))
                  (re-search-forward (if reverse
                                         "\\(^[^-!]\\|\\'\\)"
                                       "\\(^[^+!]\\|\\'\\)") nil t)))
        (error "Not at a context diff marker")
      (beginning-of-line)
      (setq region (buffer-substring start (point)))
      (other-window 1)
      (set-buffer (window-buffer (selected-window)))
      (push-mark)
      (insert region)
      (delete-columns (mark t) (point) 2))))

(defun copy-unidiff (&optional reverse)
  "Copy the current hunk of an unified diff into the buffer in the next window.
Point must be at the first line to be copied.
Optional arg REVERSE (prefix arguement if interactive) means to
 assume a reversed patch."
  (interactive "P")
  (let (start region)
    (beginning-of-line)
    (setq start (point))
    (if (not (and (eq (following-char) (if reverse ?- ?+))
                  (re-search-forward (if reverse
                                         "\\(^[^-]\\|\\'\\)"
                                       "\\(^[^+]\\|\\'\\)") nil t)))
        (error "Not at unified diff marker")
      (beginning-of-line)
      (setq region (buffer-substring start (point)))
      (other-window 1)
      (set-buffer (window-buffer (selected-window)))
      (push-mark)
      (insert region)
      (delete-columns (mark t) (point) 1))))

Andreas.

-- 
Andreas Schwab, SuSE Labs, address@hidden
SuSE Linux AG, Deutschherrnstr. 15-19, D-90429 N├╝rnberg
Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."




reply via email to

[Prev in Thread] Current Thread [Next in Thread]