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

Drew
  • 75,699
  • 9
  • 109
  • 225
kevzettler
  • 327
  • 2
  • 13
  • 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 05:23

1 Answers1

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.

Konstantin Morenko
  • 1,407
  • 9
  • 19