0

I want to create an Emacs lisp function which

  1. composes a path of a diary file which has the format <some directory>/t<timestamp>.org and
  2. determines whether or not such file exists.

I wrote this:

(defun open-diary ()
  (interactive)
  ;; TODO: Find out whether or not file-name exists
  (let ((file-name (format-time-string "/some-dir/diary-files/t%Y%m%d.org"))
        (diary-file-exists (file-exists-p file-name)))
    (message diary-file-exists)))

When I run M-x open-diary I get the error Symbol's value as variable is void: file-name.

How can I fix it, i. e. make sure that (file-exists-p file-name) sees the file-name variable defined above?

3
  • 3
    let bindings occur in parallel. You want let* instead (or even when-let, but lets get this working first :-)
    – rpluim
    May 16, 2023 at 8:07
  • You should make that into a proper answer so that it can be accepted.
    – db48x
    May 16, 2023 at 9:22
  • emacs.stackexchange.com/tags/elisp/info
    – Drew
    May 16, 2023 at 13:22

1 Answer 1

1

First, lispers read code by indentation, not by counting parens, so your code was hard to read (I fixed that).

Second, let binds variables in parallel, while let* does it sequentially (see Local Variables).

Thus you need to use the latter:

(defun open-diary ()
  (interactive)
  ;; TODO: Find out whether or not file-name exists
  (let* ((file-name (format-time-string "/some-dir/diary-files/t%Y%m%d.org"))
         (diary-file-exists (file-exists-p file-name)))
    (message "%s exists? %s" file-name diary-file-exists)))

Your Answer

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

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