Skip to content

Instantly share code, notes, and snippets.

@beoliver
Last active April 17, 2024 21:12
Show Gist options
  • Save beoliver/748829cdb7e1e05d4fa2f2ad67e0b3f3 to your computer and use it in GitHub Desktop.
Save beoliver/748829cdb7e1e05d4fa2f2ad67e0b3f3 to your computer and use it in GitHub Desktop.
(defun note ()
"Create and edit a new Markdown note in a temporary buffer.
This function generates a new buffer with a Markdown document based on the current timestamp
in the iCloud directory '~/Library/Mobile Documents/com~apple~CloudDocs/Notes/'.
The buffer is not associated with any file until explicitly saved by the user. When the
function is called interactively, it opens a new buffer for editing the Markdown note.
The Markdown buffer is initialized with a timestamped heading, allowing you to start
writing notes immediately. The buffer remains unsaved until the user chooses to save
it using Emacs save commands (e.g., C-x C-s). Upon saving, the file is created in the
specified notes directory.
Note that the function temporarily modifies Emacs's default-directory within its scope
to operate within the '~/Documents/notes/' directory for buffer operations. After the
function execution completes (whether by saving the buffer or not), the default-directory
is restored to its original value.
Interactively calling this function (e.g., via M-x note) opens a new buffer for editing
a Markdown note with the current timestamp in the specified notes directory. The buffer
remains in Markdown mode for syntax highlighting and editing convenience.
This function is useful for quickly jotting down temporary notes without immediately
creating new files on disk until you decide to save the content.
Keybindings:
- C-x C-s (save-buffer): Save the note to disk.
- C-x k (kill-buffer): Close the note buffer without saving."
(interactive)
(let* ((notes-dir (expand-file-name "~/Library/Mobile Documents/com~apple~CloudDocs/Notes/"))
(file-timestamp (format-time-string "%Y-%m-%d-%H%M%S"))
(filename (concat notes-dir file-timestamp ".md"))
(buffer (get-buffer-create (generate-new-buffer-name (file-name-nondirectory filename))))
(original-default-directory default-directory))
;; Temporarily set default-directory for buffer operations
(unwind-protect
(progn
(switch-to-buffer buffer)
(markdown-mode)
(olivetti-mode)
(setq buffer-file-name filename)
(insert (format "\n%s\n\n" (format-time-string "%Y-%m-%d %H:%M:%S")))
(message "Editing a new note (not yet saved) in %s" buffer-file-name))
;; Restore the original default-directory after function execution
(setq default-directory original-default-directory))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment