unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Joakim Hove <hove@ii.uib.no>
Subject: Re: function to increase numbers in a buffer
Date: Wed, 23 Apr 2003 08:50:08 +0200	[thread overview]
Message-ID: <4ywuhl907z.fsf@skjellgran.ii.uib.no> (raw)
In-Reply-To: b84gt0$ctd$1@news.service.uci.edu


Jordi Burguet Castell <jordi.burguet-castell@cern.ch> writes:

> Hello,
>
> I am trying to write a function that would work like query-replace-regexp, 
> but instead of replacing each match with a fixed string, I want the matched 
> numbers to be increased by 3.

Try this small function:


(defun addnumbers-in-region (p1 p2)
  (interactive "r")
  (save-excursion
    (goto-char p1)
    (let ((delta (string-to-number (read-from-minibuffer "Increase with amount: "))))
      (while (re-search-forward "\\([0-9]+\\)" p2 't)
	(let* ((org-number (string-to-number (match-string 1)))
	       (new-number (+ delta org-number)))
	  (replace-match (format "%s" new-number)))))))
	  

> Or, easier, I have a file that looks like this:
>
> 20      1       23523
> 20      2       23874
> 20      3       23898

(defun add3-to-column3 (p1 p2)
  (interactive "r")
  (save-excursion
    (goto-char p1)
    (while (<= (point) p2)
      (beginning-of-line)
      (re-search-forward "^[ ]*[0-9]+[ ]+[0-9]+[ ]+\\([0-9]+\\)[ ]*$")
      (let ((new-number (+ 3 (string-to-number (match-string 1)))))
	(backward-kill-word 1)
	(insert (format "%4d" new-number)))
      (next-line 1))))

Good - Luck 

Joakim

-- 
Joakim Hove  / hove@ii.uib.no  /  (55 5) 84076

  parent reply	other threads:[~2003-04-23  6:50 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-04-22 13:50 function to increase numbers in a buffer Jordi Burguet Castell
2003-04-23  6:04 ` Harry Putnam
2003-04-23  6:50 ` Joakim Hove [this message]
2003-04-23 12:01 ` Stefan Reichör

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4ywuhl907z.fsf@skjellgran.ii.uib.no \
    --to=hove@ii.uib.no \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).