1

Running Spacemacs v.0.200.13 on emacs 25.2.2 on Kubuntu 18.04.

Consider the following code:

  (defun xx-ll ()
    (interactive)
    (save-excursion
      (goto-char (point-min))
      (xx-replace-regexp-and-return
        "^[[:blank:]]\*LLA[[:space:]]\*\\(<[ou]l class=\"nicelist\"\\)"
        "\\1 type=\"A\"")
      (xx-replace-regexp-and-return
        "^[[:blank:]]\*LLI[[:space:]]\*\\(<[ou]l class=\"nicelist\"\\)"
        "\\1 type=\"I\"")
      (xx-replace-regexp-and-return
        "^[[:blank:]]\*LLa[[:space:]]\*\\(<[ou]l class=\"nicelist\"\\)"
        "\\1 type=\"a\"")
      (xx-replace-regexp-and-return
        "^[[:blank:]]\*LLi[[:space:]]\*\\(<[ou]l class=\"nicelist\"\\)"
        "\\1 type=\"i\"")))

  (defun xx-replace-regexp-and-return (from to)
    (save-excursion
      (while (re-search-forward from nil t)
        (replace-match to 1))))

The 1 in replace-to is to preserve case of replacement string. All goes well except that LLa and LLi leads to replacement by type="A" and type="I" respectively.

How do I set this right? Also, if I need to make any changes to any initialization variables, I will like to make them ONLY for this function.

  • 1
    What is the value of case-fold-search when you invoke re-search-forward? Do you see the same behavior when you remove the first two calls to xx-replace-regexp-and-return (those for LLA and LLI)? If so, try binding case-fold-search to t in the defun of xx-replace-regexp-and-return. – Drew Jun 9 '18 at 16:58
  • @Drew Thanks. In emacs, case-fold-search is set to t. When I remove the first two calls, LLA and LLa both convert to type="a" and LLI and LLi both convert to type="i" So, it is different from previously. – deshmukh Jun 10 '18 at 1:36
  • @Drew Thanks for pointing me to the missing piece of the puzzle. Read up the documentation. Should have done it earlier. Here is the solution that works. Added (setq case-fold-search nil) to xx-replace-regexp-and-return and all is well. – deshmukh Jun 10 '18 at 2:00
  • 1. You can bind, instead of assigning, case-fold-search in your function. 2. Please consider posting an answer, with the solution you found. You can accept your own answer. (Comments can be deleted at any time. Answers in comments don't help much, and they don't count.) – Drew Jun 10 '18 at 14:46
  • 1
    You did bind it (good). It's a global variable, so if you had just used (setq case-fold-search nil) that would have assigned it a new (global) value, which would have remained in effect when your function finished. Instead, you bound the value locally, within the scope of the function. – Drew Jun 11 '18 at 13:57
1

The following change to xx-replace-regexp-and-return solves the problem:

  (defun xx-replace-regexp-and-return (from to)
    (let ((case-fold-search nil)))
    (save-excursion
      (while (re-search-forward from nil t)
        (replace-match to 1))))

The only change needed was to add (let ((case-fold-search nil))). This comes from the documentation of the variable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.