From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Barry Margolin Newsgroups: gmane.emacs.help Subject: Re: Why do replace commands sometimes not work? Date: Fri, 25 May 2012 11:36:38 -0400 Organization: A noiseless patient Spider Message-ID: References: <5O-dnbLCNdm5bCPSnZ2dnUVZ5rmdnZ2d@giganews.com> NNTP-Posting-Host: plane.gmane.org X-Trace: dough.gmane.org 1337960414 21225 80.91.229.3 (25 May 2012 15:40:14 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Fri, 25 May 2012 15:40:14 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Fri May 25 17:40:13 2012 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1SXwcf-0004J1-3n for geh-help-gnu-emacs@m.gmane.org; Fri, 25 May 2012 17:40:13 +0200 Original-Received: from localhost ([::1]:50953 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SXwce-0000sm-Ig for geh-help-gnu-emacs@m.gmane.org; Fri, 25 May 2012 11:40:12 -0400 Original-Path: usenet.stanford.edu!goblin1!goblin.stu.neva.ru!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 137 Injection-Info: barmar.motzarella.org; posting-host="ePtxJaRXJPFeIWBMlKfZaA"; logging-data="26774"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18FY/IhIy8UYPKI1Wb5EjIV" User-Agent: MT-NewsWatcher/3.5.3b3 (Intel Mac OS X) Cancel-Lock: sha1:2m/IZ90ZfEuU6yO6gKxGvqvpkt4= Original-Xref: usenet.stanford.edu gnu.emacs.help:192568 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:84973 Archived-At: In article <5O-dnbLCNdm5bCPSnZ2dnUVZ5rmdnZ2d@giganews.com>, "B. T. Raven" wrote: > Die Thu May 24 2012 18:15:40 GMT-0500 (Central Daylight Time) MBR > scripsit: > > > There's a problem I've encountered with Emacs for many years. I never > > reported it because I've been running Emacs 21.3 under Windows, and I > > figured that Emacs users on Windows are probably a very small percentage > > of Emacs users, and that 21.3 is so old that it nobody would be > > interested in debugging the problem. > > > > But then I encountered the same problem with Emacs 23.2.1 running under > > Linux. And a few days ago I finally installed Windows Emacs 23.4.1, and > > it's got the same problem. > > > > The problem: the replace commands, M-x replace-string and M-x > > replace-regexp, sometimes work and sometimes don't. When it doesn't > > work, it often will work if I retype exactly the same command a few times. > > > > My reaction when I first encountered the problem was that I must have > > mistyped the command the first time. But I've encountered it for so > > many years that whenever it fails to work the first time, it's become > > habit for me to be extremely careful in my typing the second and > > subsequent times, and it often fails on those tries too, but eventually > > succeeds. > > > > I particularly notice it when I'm defining a macro [ delimited by C-x ( > > and C-x ) ]. And frequently I have the buffer narrowed to a small > > subset of text that I want to operate on. But I don't know for certain > > that defining a macro or having the buffer narrowed are what cause the > > problem to manifest. > > > > I now have a concrete example of this that proves that it's not due to > > my mistyping. There's a point in the macro where the buffer has been > > narrowed to a portion that contains a symbol in CamelCase. > > > > Note: In case you're unfamiliar with CamelCase, it's a convention > > for variable names originally popularized by the X Window System. > > Earlier conventions for C and C++ used "_" as a word delimiter > > within variable names. Lisp used "-" instead of "_". CamelCase, > > so-called because the capital letters in the middle of the word form > > humps like those on a camel's back, uses capital letters to indicate > > the beginning of a new word. So, the C-style variable name > > find_char_in_string, or Lisp-style variable name > > find-char-in-string, in CamelCase is findCharInString. > > > > The purpose of this part of the macro is to turn CamelCase into > > space-separated words. > > > > M-< ;; Go to beginning of narrowed buffer > > M-x replace-regexp RET > > [A-Z] RET ;; Find any capital letter > > C-q SPC \& RET ;; Replace it with a space followed by itself > > M-< ;; Go to beginning of narrowed buffer > > C-d ;; Delete the unwanted space before the > > first letter > > > > So, if the narrowed portion of the buffer contains: > > > > "JohnJacobJingleheimerschmidt" > > > > after running this portion of the macro, it should contain: > > > > "John Jacob Jingleheimerschmidt" > > > > Instead, when run in Emacs 23, the result is: > > > > "ohnJacobJingleheimerschmidt" > > > > which is exactly what you'd expect if the M-x replace-regexp failed to > > do the replacement that it should have. But since I know that sometimes > > a replace command works the second time after failing to work the first > > time, I modified that portion of the macro to do the replace twice: > > > > M-< ;; Go to beginning of narrowed buffer > > M-x replace-regexp RET > > [A-Z] RET ;; Find any capital letter > > C-q SPC \& RET ;; Replace it with a space followed by itself > > M-< ;; Go to beginning of narrowed buffer > > M-x replace-regexp RET > > [A-Z] RET ;; Find any capital letter > > C-q SPC \& RET ;; Replace it with a space followed by itself > > M-< ;; Go to beginning of narrowed buffer > > C-d ;; Delete the unwanted space before the > > first letter > > > > Now, if the replace were working the first time, applying it again would > > produce the undesired result: > > > > " John Jacob Jingleheimerschmidt" > > > > Instead, it produces: > > > > "John Jacob Jingleheimerschmidt" > > > > Does anybody here have any idea what's going wrong here? > > > > Mark Rosenthal > > mbr@arlsoft.com > > > > P.S. - One further clue: In the older version of Emacs (21.3) I've > > noticed that at those times when the replace fails to work, if I repeat > > the replace command with C-x ESC ESC, the minibuffer shows: > > > > (replace-regexp "[A-Z]" " \\&" nil sss eee) > > > > where sss and eee are integers that are supposed to indicate the > > beginning and end characters of the region to operate on, but when the > > replace has failed, sss and eee specify a small subset of the region. > > > > > I can't reproduce that misbehavior on w32 ver 23.1 > Both in *scratch* (lisp mode) and a junk file in text mode I get: > > John Jacob Jingleheimerschmidt > " John Jacob Jingleheimerschmidt" > " John Jacob Jingleheimerschmidt" > John Jacob Jingleheimerschmidt > John Jacob Jingleheimerschmidt > > where the second and third lines were originally camel-case in quotes. > I did assign the macro to a keychord with C-xC-kb I think the clue is in his P.S. If you have transient-mark-mode enabled, the replace commands restrict themselves to the active region. When I run M-ESC ESC in Emacs 22.2, it doesn't show explicit buffer positions, it show things like (if (and transient-mark-mode mark-active) (region-beginning)). But maybe in the older version it just put the buffer positions in the history. -- Barry Margolin, barmar@alum.mit.edu Arlington, MA *** PLEASE post questions in newsgroups, not directly to me ***