summaryrefslogtreecommitdiff
path: root/lisp/backup-before-save/backup-before-save.el
diff options
context:
space:
mode:
authorJohn Turner <jturner.usa@gmail.com>2022-07-03 23:20:44 -0400
committerJohn Turner <jturner.usa@gmail.com>2022-07-03 23:20:44 -0400
commitc284f5b2c4e4ed72a2a060cb91353408f5a2b416 (patch)
tree9902357b42b23071df6b6aa2668aac14e805f6be /lisp/backup-before-save/backup-before-save.el
parent250b3ef0d5c3cbc39129ce632838994a17f57fa2 (diff)
downloademacs.d-c284f5b2c4e4ed72a2a060cb91353408f5a2b416.tar.gz
moved non-config functions into a new lisp directory
We will now put non-config related functions (any elisp libraries or snippets that I write) into a new ".emacs.d/lisp" directory and separate them from the config code in the ".emacs.d/config" directory. During the transition I decided to move and rewrite the logic that adds all of the libraries to load-path. Now this logic is in two top level files (load-config.el load-local-lisp.el). I needed to remove the config-programming-languages module because it conflicted with the new load-path logic (it was mostly useless anyways). The man advice functions are deleted in this commit but they will be added into the new ".emacs.d/lisp" directory soon.
Diffstat (limited to 'lisp/backup-before-save/backup-before-save.el')
-rw-r--r--lisp/backup-before-save/backup-before-save.el54
1 files changed, 54 insertions, 0 deletions
diff --git a/lisp/backup-before-save/backup-before-save.el b/lisp/backup-before-save/backup-before-save.el
new file mode 100644
index 0000000..5277fff
--- /dev/null
+++ b/lisp/backup-before-save/backup-before-save.el
@@ -0,0 +1,54 @@
+(defvar backup-before-save-directory (file-name-concat user-emacs-directory "backups"))
+
+(unless (file-exists-p backup-before-save-directory)
+ (mkdir backup-before-save-directory))
+
+(defun backup-before-save-format-path (path number)
+ (format "%s.~%s~" path number))
+
+(defun backup-before-save-extension (path)
+ (let* ((i (string-search "." (reverse path)))
+ (extension (substring path (- (length path) i))))
+ extension))
+
+(defun backup-before-save-no-extension (path)
+ (let* ((i (string-search "." (reverse path)))
+ (no-extension (substring path 0 (- (length path) (+ i 1)))))
+ no-extension))
+
+(defun backup-before-save-number (path)
+ (let* ((extension (backup-before-save-extension path))
+ (extracted (substring extension 1 (- (length extension) 1)))
+ (as-number (unless (zerop (length extracted))
+ (string-to-number extracted))))
+ as-number))
+
+(defun backup-before-save-shift-path (path)
+ (let* ((without-extension (backup-before-save-no-extension path))
+ (number (backup-before-save-number path))
+ (next-number (+ number 1))
+ (shifted-path (backup-before-save-format-path
+ without-extension
+ next-number)))
+ shifted-path))
+
+(defun backup-before-save-shift-backup (path)
+ (let ((next-path (backup-before-save-shift-path path)))
+ (when (file-exists-p next-path)
+ (backup-before-save-shift-backup next-path))
+ (rename-file path next-path)))
+
+(defun backup-before-save-function ()
+ (unless backup-inhibited
+ (let* ((full-path (buffer-file-name))
+ (base-path (file-name-nondirectory full-path))
+ (backup-full-path (file-name-concat backup-before-save-directory base-path))
+ (backup-full-path-numbered (backup-before-save-format-path backup-full-path 0)))
+ (when (file-exists-p full-path)
+ (when (file-exists-p backup-full-path-numbered)
+ (backup-before-save-shift-backup backup-full-path-numbered))
+ (copy-file full-path backup-full-path-numbered)))))
+
+(add-hook 'before-save-hook 'backup-before-save-function)
+
+(provide 'backup-before-save)