From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Rupert Swarbrick Newsgroups: gmane.emacs.help Subject: Re: How to automatically increment an index array Date: Thu, 26 Jun 2008 12:47:01 +0100 Organization: albasani.net Message-ID: References: NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: ger.gmane.org 1214484057 12198 80.91.229.12 (26 Jun 2008 12:40:57 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Thu, 26 Jun 2008 12:40:57 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Thu Jun 26 14:41:42 2008 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1KBqmx-0002bR-IG for geh-help-gnu-emacs@m.gmane.org; Thu, 26 Jun 2008 14:41:23 +0200 Original-Received: from localhost ([127.0.0.1]:48916 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1KBqm7-0004lm-Pu for geh-help-gnu-emacs@m.gmane.org; Thu, 26 Jun 2008 08:40:31 -0400 Original-Path: news.stanford.edu!newsfeed.stanford.edu!news.tele.dk!news.tele.dk!small.news.tele.dk!newsfeed00.sul.t-online.de!t-online.de!news.albasani.net!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 64 Original-X-Trace: news.albasani.net CS4vi9baEW+Azzd9MCnl2YH0dtd42E2MLLOAgv2pUMeWUgwKf2NSHqduR+2gse3guvUXHGLrHY4wGYnECdtCDGevZ4w0B3nvaW7IK7WUyTfA3n6WT9Kgill1nLGJJfFI Original-X-Complaints-To: abuse@albasani.net Original-NNTP-Posting-Date: Thu, 26 Jun 2008 11:47:02 +0000 (UTC) X-User-ID: nufof+sNsDnGLe8Tc/3PaSDN0m151AAuJ1vg3Q6TI54= Cancel-Lock: sha1:2r+C88j1XZutxNKsAgtcGPfodSY= User-Agent: Gnus/5.110011 (No Gnus v0.11) Emacs/22.2 (gnu/linux) X-NNTP-Posting-Host: Lk86aQvqXsl3tm4jKYyfW7zLLYGwttGhs2Itn8rzdEM= Original-Xref: news.stanford.edu gnu.emacs.help:159738 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:55092 Archived-At: "Francis Moreau" writes: > Hello > > I have this problem: in a buffer, *scratch* for example I have: > > [8735b450] = xxx, > [0x15] = xxx, > [0x16] = xxx, > [0x17] = xxx, > [0x18] = xxx, > [0x19] = xxx, > [0x1a] = xxx, > [0x1b] = xxx, > > After running a 'magic' command I'd like to calculate the new array indexes > as follow: > > [8735b450] = xxx, > [8735b454] = xxx, > [8735b458] = xxx, > [8735b45c] = xxx, > [8735b460] = xxx, > [8735b464] = xxx, > [8735b468] = xxx, > [8735b46c] = xxx, > ... > > Can anybody give me a hint ? > No doubt others will come up with neater versions, but here's a quick hack. Problems with it: it doesn't do any large-scale inspection of the text, so if you had two different sets of these with different base addresses, you'd need to use narrow-to-region. Also there's no error checking, so you should probably look carefully at what happened to the buffer before saving... Rupert (defun do-hex-incrementing () (interactive) (save-excursion (re-search-forward "\\[[0-9a-z]+\\]") (let ((pt) (base)) (setf pt (1- (point))) (search-backward "\[") (setf base (string-to-number (buffer-substring (1+ (point)) pt) 16)) (while (re-search-forward "\\[0x[0-9a-z]+\]" nil t) (search-backward "[0x") (forward-char) (insert (format "%x" (+ (string-to-number (substring (thing-at-point 'word) 2) 16) base))) (setf pt (point)) (search-forward "]") (delete-region pt (1- (point)))))))