1

Trying to make a function that I can call to create a file from a template or open an existing file. Current attempt is as follows:

(defun journal-entry ()
  (interactive)
  (let ((daily-name (format-time-string "%Y-%m-%d)"))
        (journal-path "~/Dropbox (Personal)/journal/"))
    (find-file (concat journal-path daily-name ".md"))
    (insert "JOURNAL" "\n" daily-name)))

Problem with this is that every time I call it. The ( insert ) content gets re added

1
  • How about using something like? (if (file-exists-p name-of-file) do-x do-y) The function in the question has no conditions, so you'll need to familiarize yourself with this like if and cond.
    – lawlist
    Commented Jul 24, 2016 at 5:23

1 Answer 1

4

You could use this version of the function:

(defun journal-entry ()
  (interactive)
  (let* ((daily-name (format-time-string "%Y-%m-%d)"))
     (journal-path "~/Dropbox (Personal)/journal/")
     (journal-file (concat journal-path daily-name ".md")))
     (find-file journal-file)
     (unless (file-exists-p journal-file)
         (insert "JOURNAL" "\n" daily-name))))

Unless like if not, so if the file isn't exist (checked in (file-exists-p journal-file)), the (insert "JOURNAL" "\n" daily-name) will be executed.

I've added creating journal-file because it used in the script twice.

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.