Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (defvar mwn/kill-ring-file
- "~/.emacs.d/kill-ring"
- "Path of the file used to save the contents of `kill-ring' between sessions.")
- (defun mwn/get-file-buffer-status (file-name)
- "Return a data structure associated with the FILE-NAME and boolean value
- indicating if the buffer was open before the function call. Creates a
- new buffer if needed. First value is the buffer associated with
- `kill-ring' content. Second value is t if the buffer was already open,
- nil if it is a new buffer."
- (let ((has-buffer (not (null (get-file-buffer file-name)))))
- (cl-values (find-file-noselect file-name t)
- has-buffer)))
- (defun mwn/save-kill-ring ()
- "Stores the contents of the `kill-ring' to a file for later retrieval."
- (interactive)
- (cl-multiple-value-bind
- (buffer open)
- (mwn/get-file-buffer-status mwn/kill-ring-file)
- (with-current-buffer buffer
- (erase-buffer)
- (print kill-ring buffer)
- (save-buffer))
- (unless open
- (kill-buffer buffer))))
- (defun mwn/restore-kill-ring ()
- "Reads in the contents of the kill ring file, if any, and sets the value of `kill-ring'."
- (interactive)
- (cl-multiple-value-bind
- (buffer open)
- (mwn/get-file-buffer-status mwn/kill-ring-file)
- (with-current-buffer buffer
- (unless (zerop (buffer-size))
- (goto-char 1)
- (setq kill-ring (read (current-buffer)))))
- (unless open
- (kill-buffer buffer))))
- ;; Automatically preserve the kill-ring when terminating the current emacs process.
- (add-hook 'kill-emacs-hook #'mwn/save-kill-ring)
- (defun mwn/no-kill-ring-terminate (&optional restart)
- "Core function for killing/restarting emacs. This version will NOT save the
- `kill-ring' in the defined kill-ring preservation file."
- (let ((old-hook (cl-copy-list kill-emacs-hook)))
- (remove-hook 'kill-emacs-hook #'mwn/save-kill-ring)
- (unless (save-buffers-kill-emacs nil restart)
- (setq kill-emacs-hook old-hook))))
- (defun mwn/kill-emacs-no-kill-ring ()
- "End the current emacs session w/o saving the `kill-ring'."
- (interactive)
- (mwn/no-kill-ring-terminate))
- (defun mwn/restart-emacs-no-kill-ring ()
- "Restart the current emacs session w/o saving the `kill-ring'."
- (interactive)
- (mwn/no-kill-ring-terminate t))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement