summary refs log tree commit diff stats
path: root/lisp/+scratch.el
blob: 5d749c6c83f45499e5bfb4861757e2849adb26a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
;;; +scratch.el --- *scratch* improvements           -*- lexical-binding: t; -*-

;; Copyright (C) 2023  Case Duckworth

;; Author: Case Duckworth <case@acdw.net>
;; Keywords: convenience

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;;

;;; Code:

(defcustom +scratch-save-dir (locate-user-emacs-file "scratch.d")
  "Where to save scratch files."
  :type 'file)

(defcustom +scratch-max-age (* 60 60 24 365)
  "Maximum age of a saved scratch buffer.  Default: one year."
  :type 'natnum)

(defun +scratch@immortal ()
  "Don't kill *scratch* with `kill-buffer'."
  (if (equal (buffer-name) "*scratch*")
      (progn (bury-buffer) nil)
    t))

(defun +scratch-save (time-format)
  "Save *scratch* buffer to `+scratch-save-dir'.
TIME-FORMAT will be used to name the buffer."
  (interactive (list "%FT%H%z"))
  (with-current-buffer (get-scratch-buffer-create)
    (let ((buffer-file-name (expand-file-name (format "%s.%s"
                                                      (format-time-string
                                                       time-format)
                                                      (pcase major-mode
                                                        ('org-mode "org")
                                                        ('emacs-lisp-mode "el")
                                                        (_ "txt")))
                                              +scratch-save-dir)))
      (unless (string-equal (buffer-substring (point-min) (point-max))
                            initial-scratch-message)
        (save-buffer 0)))))

(defun +scratch-save-on-exit ()
  (+scratch-save "%FT%T%z")
  (+scratch-clean))

(defun +scratch-clean ()
  "Clean up saved scratches.
Scratch files older than `+scratch-max-age' will be removed."
  (dolist (f (directory-files +scratch-save-dir))
    (when (and (not (equal f "."))
               (not (equal f ".."))
               (> (time-convert (file-attribute-modification-time
                                 (file-attributes f))
                                'integer)
                  +scratch-max-age))
      (delete-file (expand-file-name f +scratch-save-dir) :trash))))

(provide '+scratch)
;;; +scratch.el ends here