0

I wrote the following command to do a regexp replacement in a region.

(defun camel-case-to-underscore (a b &optional use-dash)
  (interactive "r")
  (save-excursion
    (let ((sep (if use-dash "-" "_"))
          (case-fold-search t))
      (goto-char a)
      (while (re-search-forward "\\([[a-z]\\)\\([A-Z]\\)" b t)
        (replace-match (concat (match-string 1) sep
                               (downcase (match-string 2))))
        (incf b (length sep))))))

However, case-fold-search appears to not be affecting re-search-forward, causing undesired results:

thisIsAnExampleInCamelCase => t_hi_sI_SA_NE_Xa_mp_le_in_ca_me_lC_As_e

What is wrong with my use of case-fold-search?

GNU Emacs 25.2.1 (x86_64-unknown-linux-gnu, GTK+ Version 3.22.10) of 2017-10-09

2

What is wrong with my use of case-fold-search?

You've set it to t instead of nil.

case-fold-search is a variable defined in C source code.
[...]
Non-nil if searches and matches should ignore case.

Hence nil means case-sensitive, and anything else means case-insensitive.

  • yes, thanks. I forgot about that and was interpreting case-fold-search as sommething like case-sensitive-search – erjoalgo Mar 29 '18 at 4:13

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.