0

I've made the following command:

(defun search-backward-lowercase-alphabet ()
  (interactive)
  (search-backward-regexp "[a-z]"))

But when I run it at the end of a line like the following:

abcDEF

It just selects the last letter F, when I actually want to select the c. It doesn't distinguish between uppercase and lowercase at all. How can I fix this?

6
  • 1
    case-fold-search should be nil.
    – abo-abo
    Jan 18, 2016 at 9:17
  • @abo-abo Thanks! Now it works. It seems it doesn't affect the helm or isearch. Is it only for regexp?
    – stacko
    Jan 18, 2016 at 9:23
  • It's for regexp, but isearch and helm might bind case-fold-search at some points.
    – abo-abo
    Jan 18, 2016 at 9:32
  • @abo-abo Would it be a bad idea to start my function by setting case-fold-search nil and end it with setting it 1, if I ever made sure that it actually affects isearch and helm?
    – stacko
    Jan 18, 2016 at 9:44
  • @abo-abo I might delete this comment since it's kind of off-topic, but why is that you don't post this as an answer? I could upvote and choose "this solved my question"?
    – stacko
    Jan 18, 2016 at 9:47

2 Answers 2

3

As suggested by @abo-abo, you should set case-fold-search to nil. However, in order to ensure that it doesn't affect any other part of the system, you can bind in using let, as follows:

(defun search-backward-lowercase-alphabet ()
  (interactive)
  (let ((case-fold-search nil))
    (search-backward-regexp "[a-z]")))
1

Check your setting of case-fold-search:

Documentation: Non-nil if searches and matches should ignore case.

This means you need (setq case-fold-search nil).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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