;;; From: Francesco Potorti` ;;; Subject: Re: Copying lines from above ;;; Newsgroups: gnu.emacs.sources ;;; Date: 11 Apr 2001 11:15:08 +0200 ;;; Organization: Centro SERRA ;;; Path: news.wam.umd.edu!news.umd.edu!bloom-beacon.mit.edu!howland.erols.net!news-out.worldnet.att.net.MISMATCH!wn3feed!wn4feed!worldnet.att.net!24.0.0.38!newshub2.rdc1.sfba.home.com!news.home.com!enews.sgi.com!newsfeed.nettuno.it!newsfeed.cineca.it!serra.unipi.it!none!not-for-mail Lines: 64 Message-ID: <87y9t7yezn.fsf@pot.cnuce.cnr.it> References: NNTP-Posting-Host: pot.cnuce.cnr.it Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: serra.unipi.it 986980460 18288 146.48.83.182 (11 Apr 2001 09:14:20 GMT) X-Complaints-To: newsmaster NNTP-Posting-Date: Wed, 11 Apr 2001 09:14:20 +0000 (UTC) User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.0.100 Xref: news.wam.umd.edu gnu.emacs.sources:9498 ;;; Ole Laursen writes: ;;; > There is a cool command called copy-from-above-command that will ;;; > copy the entire line to the current line. It is very useful, but it ;;; > only works on the immediately preceding line. What if you want to ;;; > copy a line that is 5 lines above the current one (or 4, or 20)? ;;; > Well, the future has arrived! ;;; Obviously I have my own version: this seems to be a recurring request. ;;; I have used this for many years. ;;; ;;; Addition to 19.30 ;;; ;;; Modified from misc.el distributed with 19.30 ;;; ;;; by "Francesco Potorti` " (defvar copy-from-above-lines -1) (defvar copy-from-above-chars 0) (defvar copy-from-above-inc 0) ;;;###autoload (defun copy-from-above-command (&optional arg) "Copy the previous line incrementing numbers, starting just above point. All integer numbers fetched as part of the line are incremented by ARG. If no argument is given, numbers are left as they are. Successive invocations of this command fetch successively previous lines. The characters copied are inserted in the buffer before point and mark is set to the beginning of the inserted string." (interactive "P") (if (eq last-command 'copy-from-above-command) (progn (delete-char (- copy-from-above-chars)) (setq copy-from-above-lines (1- copy-from-above-lines))) (push-mark) (setq copy-from-above-lines -1) (setq copy-from-above-inc (if arg (prefix-numeric-value arg) 0))) (let ((cc (current-column)) (string "")) (save-excursion (forward-line copy-from-above-lines) (move-to-column cc) ;; If current column winds up in middle of a tab, ;; copy appropriate number of "virtual" space chars. (if (< cc (current-column)) (if (= (preceding-char) ?\t) (setq string (make-string (- (current-column) cc) ?\ )) ;; In middle of ctl char => copy that whole char. (backward-char 1))) (setq string (concat string (buffer-substring (point) (save-excursion (end-of-line) (point)))))) (let ((index 0) (oldstr "") (newstr "")) (while (string-match "[0-9]+" string index) (setq oldstr (match-string 0 string)) (setq newstr (int-to-string (+ copy-from-above-inc (string-to-int oldstr)))) (setq index (+ (match-beginning 0) (length newstr))) (setq string (replace-match newstr nil nil string)))) (setq copy-from-above-chars (length string)) (insert string))) (global-set-key "\M-p" 'copy-from-above-command)