From mboxrd@z Thu Jan 1 00:00:00 1970 Path: main.gmane.org!not-for-mail From: Joakim Hove Newsgroups: gmane.emacs.help Subject: Re: function to increase numbers in a buffer Date: Wed, 23 Apr 2003 08:50:08 +0200 Organization: University of Bergen Sender: help-gnu-emacs-bounces+gnu-help-gnu-emacs=m.gmane.org@gnu.org Message-ID: <4ywuhl907z.fsf@skjellgran.ii.uib.no> References: NNTP-Posting-Host: main.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: main.gmane.org 1051080580 2240 80.91.224.249 (23 Apr 2003 06:49:40 GMT) X-Complaints-To: usenet@main.gmane.org NNTP-Posting-Date: Wed, 23 Apr 2003 06:49:40 +0000 (UTC) Original-X-From: help-gnu-emacs-bounces+gnu-help-gnu-emacs=m.gmane.org@gnu.org Wed Apr 23 08:49:39 2003 Return-path: Original-Received: from monty-python.gnu.org ([199.232.76.173]) by main.gmane.org with esmtp (Exim 3.35 #1 (Debian)) id 198E4Z-0000Zz-00 for ; Wed, 23 Apr 2003 08:49:39 +0200 Original-Received: from localhost ([127.0.0.1] helo=monty-python.gnu.org) by monty-python.gnu.org with esmtp (Exim 4.10.13) id 198E5C-00076v-08 for gnu-help-gnu-emacs@m.gmane.org; Wed, 23 Apr 2003 02:50:18 -0400 Original-Path: shelby.stanford.edu!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!newsfeed.icl.net!newsfeed.fjserv.net!news.tele.dk!news.tele.dk!small.news.tele.dk!newsfeed1!bredband!uio.no!nntp.uib.no!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 47 Original-NNTP-Posting-Host: skjellgran.ii.uib.no Original-X-Trace: toralf.uib.no 1051080608 13651 129.177.20.52 (23 Apr 2003 06:50:08 GMT) Original-X-Complaints-To: abuse@uib.no Original-NNTP-Posting-Date: 23 Apr 2003 06:50:08 GMT User-Agent: Gnus/5.090017 (Oort Gnus v0.17) Emacs/21.2 (gnu/linux) Cancel-Lock: sha1:kK+CCpo4BB3FpF65fj6CuvMvO9E= Original-Xref: shelby.stanford.edu gnu.emacs.help:112198 Original-To: help-gnu-emacs@gnu.org X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1b5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Help: List-Post: List-Subscribe: , List-Archive: List-Unsubscribe: , Errors-To: help-gnu-emacs-bounces+gnu-help-gnu-emacs=m.gmane.org@gnu.org Xref: main.gmane.org gmane.emacs.help:8697 X-Report-Spam: http://spam.gmane.org/gmane.emacs.help:8697 Jordi Burguet Castell 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