0

I am using following answer to search and replace a word in the entire buffer:

(defun query-replace-region-or-from-top ()   
  (interactive)  
  (progn
    (let ((orig-point (point)))
      (if (use-region-p)
          (call-interactively 'query-replace)
        (save-excursion
          (goto-char (point-min))
          (call-interactively 'query-replace)))
      (message "Back to old point.")
      (goto-char orig-point)))) 

(global-set-key "\C-x\C-r"  'query-replace-region-or-from-top)```

When I apply query-replace-region-or-from-top, for example emacs into emacs_world it also changes EMACS into EMACS_WORLD.

How can I make query-replace-region-or-from-top case-sensitive?

NickD
  • 27,023
  • 3
  • 23
  • 42
alper
  • 1,238
  • 11
  • 30
  • Does this answer your question? [How do I search/replace with case sensitive search?](https://emacs.stackexchange.com/questions/61408/how-do-i-search-replace-with-case-sensitive-search) – Drew Oct 04 '21 at 15:29
  • @Drew yes sir , NickD's answer also helps how can I use it – alper Oct 04 '21 at 15:49
  • The question seems to be a duplicate of that question, in which case it should be deleted. – Drew Oct 04 '21 at 17:04
  • Another answer: https://stackoverflow.com/questions/5346107/emacs-case-sensitive-replace-string/5346418#5346418 // small example is helpful to understand how can I use it under the `defun` function like using `(let ((case-fold-search nil))` – alper Oct 04 '21 at 20:44

1 Answers1

3

You need to let-bind case-fold-search to nil:

(defun query-replace-region-or-from-top ()   
  (interactive)
  (let ((case-fold-search nil))  
    (progn
      ...
      (goto-char orig-point)))))

See the doc string for case-fold-search with C-h v case-fold-search:

case-fold-search is a variable defined in ‘src/buffer.c’.

Its value is t

Automatically becomes buffer-local when set. You can customize this variable. Probably introduced at or before Emacs version 18.

Non-nil if searches and matches should ignore case.

NickD
  • 27,023
  • 3
  • 23
  • 42