Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6159343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:06:59+00:00 2026-05-23T21:06:59+00:00

In this answer , I use a regular expression search/replace to convert from one

  • 0

In this answer, I use a regular expression search/replace to convert from one type of ruby block to another. However, after the function ruby-flip-containing-block-type is done, the point is moved to the beginning of the replaced text, even though there is a save-excursion around the function.

I even tried saving the point to a register and jumping back to it after the flip. The saved point is relocated to the beginning of my changes. After some google searches, I think the problem lies with the fact that the replace-match call updates the buffer contents at the original point.

Any ideas on how I can preserve/restore the original location of the point in this situation?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T21:07:00+00:00Added an answer on May 23, 2026 at 9:07 pm

    When text is replaced, the old text is removed, and the new text inserted. So let’s say the buffer looks like this, where -!- indicates the position of point:

    abcdefghijklm-!-nopqrstuvwxyz
    

    Suppose you replace jklmnop with jlkMnop. First the jklmnop is removed:

    abcdefghi-!-qrstuvwxyz
    

    and then the jlkMnop is inserted:

    abcdefghi-!-jklMnopqrstuvwxyz
    

    You can see that the effect is as if point was moved.

    The way to preserve point is to be much more careful about how you go about replacing the text. Instead of replacing big blocks (which might well contain point), replace only the small sections that actually change. In the answer you link to, instead of replacing do\(.*\)end with {\1}, replace the do with { and the end with } in separate replacements.


    This doesn’t have to be ugly. Perhaps something like this, using the fifth (subexp) argument to replace-match:

    (when (re-search-forward "\\(do\\).*\\(?:\n.*\\)\\(end\\)" nil t)
      (replace-match "}" t t nil 2)
      (replace-match "{" t t nil 1))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Based on this answer... Using a regular expression to validate an email address Which
This almost has the answer... How do you use a variable in a regular
On this question , there's an answer that states: You can use typedef to
This came up from this answer to a previous question of mine . Is
After reading this answer: best way to pick a random subset from a collection?
In a comment on this answer to another question , someone said that they
I had this answer on another post I asked: I believe the VS designer
I need a regular expression that I can use in VBScript and .NET that
I found this very handy regular expression on regexlib.com, but i'm at a loss
Pretty much all the apps I use on a regular basis implement this 'seemly

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.