help-emacs-windows
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [h-e-w] Why do replace commands sometimes not work?


From: Buchs, Kevin
Subject: Re: [h-e-w] Why do replace commands sometimes not work?
Date: Fri, 25 May 2012 14:41:55 -0500

Mark,

 

(That is how I intended to address my last email!)

 

I agree with the poster who suggested transient mark mode is at fault. If you narrow to region, then there is a pre-existing region defined under transient mark mode. But when you narrowed and then went to the first character the region is ill-defined (it is null). If you simply clear the region at that time, you will be fine. Now, you can’t record that in your macro with C-g, so instead, use 3 ESC presses to clear the region. I just recorded that and tried it and it was happy.

 

Kevin Buchs | Senior Engineer | SPPDG | 507-538-5459 | address@hidden

Mayo Clinic | 200 First Street SW | Rochester, MN 55905 | http://www.mayo.edu/sppdg

 

From: MBR [mailto:address@hidden
Sent: Friday, May 25, 2012 1:51 PM
To: Buchs, Kevin
Cc: address@hidden
Subject: Re: [h-e-w] Why do replace commands sometimes not work?

 

Thanks for the suggestions.  One or the other may prove to be an effective workaround for this particular instance of the problem.  I still have no idea why replace-regex and replace-string occasionally misbehave, or how to reliably reproduce that misbehavior.  And from the responses so far, it looks like nobody else remembers encountering this problem.

As for C-q before the space, in some version or other they added the feature that a regular _expression_ search for a space matched any run of spaces and tabs.  To do a regular _expression_ search for a single space and nothing else, it's necessary to quote the space.  I know that in an insert, the character space inserts as itself.  But my subconscious seems to have decided that the rule is that spaces should be quoted everywhere in a search and replace.  Although I know quoting spaces is not necessary in the replace string, it doesn't do any harm, and I've never gotten around to breaking the habit.

Mark

On 5/25/2012 12:23 PM, Buchs, Kevin wrote:

MBR,
 
I am curious as to why you include the C-q before the space. Space
inserts itself literally. 
 
Try this regexp:  
\(\w\)\([A-Z]\)
 
with this replacement: 
\1 \2
 
and you can skip the step of deleting the first space character at the
beginning of the region and also adjusting extra spaces before each
camelcase word. This replaces every occurrence of a word constituent
followed by a capital letter with a space between the two.
 
If you do this often, try this lisp code:
 
(defun de-hump ()
       "Split up CamelCase words with a space"
       (interactive)
       (replace-regexp "\\(\\w\\)\\([A-Z]\\)" "\\1 \\2" nil (point)
(mark)))
 
Which will do the replacement on the region, so there will be no need to
narrow the buffer.
 
Kevin Buchs | Senior Engineer | SPPDG | 507-538-5459 |
address@hidden
Mayo Clinic | 200 First Street SW | Rochester, MN 55905 |
http://www.mayo.edu/sppdg 
 
 
 

reply via email to

[Prev in Thread] Current Thread [Next in Thread]