From 2d2cab9d97d3e84e5b7158181523fe2c5bbffe85 Mon Sep 17 00:00:00 2001 From: Case Duckworth Date: Sun, 16 Apr 2023 17:06:41 -0500 Subject: uhhh --- lisp/+scratch.el | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 lisp/+scratch.el (limited to 'lisp/+scratch.el') diff --git a/lisp/+scratch.el b/lisp/+scratch.el new file mode 100644 index 0000000..5d749c6 --- /dev/null +++ b/lisp/+scratch.el @@ -0,0 +1,75 @@ +;;; +scratch.el --- *scratch* improvements -*- lexical-binding: t; -*- + +;; Copyright (C) 2023 Case Duckworth + +;; Author: Case Duckworth +;; 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 . + +;;; 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 -- cgit 1.4.1-21-gabe81