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

  • 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 Jul 24 '16 at 5:23
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, privacy policy and cookie policy

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