Define a local variable for a function

I work (joyfully) while working on the Introduction to Emacs Lisp Program and solved the first 8.7 Exercise Search . It states

Write an interactive function that searches for a string. If the search finds a line, leave a dot after it and display a message that says "Found!".

My decision

(defun test-search (string)
  "Searches for STRING in document.
Displays message 'Found!' or 'Not found...'"
  (interactive "sEnter search word: ")
  (save-excursion
    (beginning-of-buffer)
    (setq found (search-forward string nil t nil))) 
  (if found
      (progn
        (goto-char found)
        (message "Found!"))
    (message "Not found...")))

How to make foundlocal to function? I know that the operator letdefines a local variable. However, I only want to move the point if found string. It’s not clear to me how to foundlocally, but not point in beginning-of-buffer, if stringnot found. Is the letright team for this situation?

+2
1

, let - , , , , .

:

(defun test-search (string)
   "Searches for STRING in document.
Displays message 'Found!' or 'Not found...'"
   (interactive "sEnter search word: ")
   (let ((found (save-excursion
                  (goto-char (point-min))
                  (search-forward string nil t nil))))
     (if found
       (progn
         (goto-char found)
         (message "Found!"))
       (message "Not found..."))))

: phils '.

0

Source: https://habr.com/ru/post/1674316/


All Articles