unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: MBR <mbr@arlsoft.com>
To: help-gnu-emacs@gnu.org, help-emacs-windows@gnu.org
Subject: Why do replace commands sometimes not work?
Date: Thu, 24 May 2012 19:15:40 -0400	[thread overview]
Message-ID: <4FBEC11C.6030704@arlsoft.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 4670 bytes --]

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 <mailto: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.


[-- Attachment #2: Type: text/html, Size: 7382 bytes --]

             reply	other threads:[~2012-05-24 23:15 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-05-24 23:15 MBR [this message]
     [not found] <mailman.1636.1337901354.855.help-gnu-emacs@gnu.org>
     [not found] ` <5O-dnbLCNdm5bCPSnZ2dnUVZ5rmdnZ2d@giganews.com>
2012-05-25  3:01   ` Why do replace commands sometimes not work? MBR
2012-05-25 12:25     ` Ludwig, Mark
2012-05-25 15:36   ` Barry Margolin

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=4FBEC11C.6030704@arlsoft.com \
    --to=mbr@arlsoft.com \
    --cc=help-emacs-windows@gnu.org \
    --cc=help-gnu-emacs@gnu.org \
    /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).