[Top][All Lists]

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

Re: Column replace

From: Peter Lee
Subject: Re: Column replace
Date: Wed, 13 Jun 2007 16:01:59 -0500
User-agent: Gnus/5.11 (Gnus v5.11) Emacs/22.1.50 (windows-nt)

>>>> deprecated  writes:

    > Just in case anyone was worried that emacs has only two solutions to
    > this problem, there is a third way ..... :)

Don't know where I got this, but this is what I use:

(defun another-line (num-lines)
  "Copies line, preserving cursor column, and increments any numbers found.
Copies a block of optional NUM-LINES lines.  If no optional argument is given,
then only one line is copied."
  (interactive "p")
  (if (not num-lines) (setq num-lines 0) (setq num-lines (1- num-lines)))
  (let* ((col (current-column))
         (bol (save-excursion (forward-line (- num-lines)) (beginning-of-line) 
         (eol (progn (end-of-line) (point)))
         (line (buffer-substring bol eol)))
    (goto-char bol)
    (while (re-search-forward "[0-9]+" eol 1)
      (let ((num (string-to-number (buffer-substring
                                 (match-beginning 0) (match-end 0)))))
        (replace-match (int-to-string (1+ num))))
      (setq eol (save-excursion (goto-char eol) (end-of-line) (point))))
    (goto-char bol)
    (insert line "\n")
    (move-to-column col)))
(global-set-key (kbd "M-O") 'another-line)

reply via email to

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