From c284f5b2c4e4ed72a2a060cb91353408f5a2b416 Mon Sep 17 00:00:00 2001 From: John Turner Date: Sun, 3 Jul 2022 23:20:44 -0400 Subject: [PATCH] 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. --- config/config.el | 54 ------------------- .../files/config-files-backup-on-save-hook.el | 50 ----------------- config/files/config-files.el | 3 -- config/man/config-man-helper-functions.el | 31 ----------- config/man/config-man.el | 5 -- .../c/config-c-functions.el | 4 -- config/programming-languages/c/config-c.el | 5 -- .../config-programming-languages.el | 14 ----- .../shell/config-shell.el | 3 -- init.el | 7 ++- lisp/backup-before-save/backup-before-save.el | 54 +++++++++++++++++++ load-config.el | 52 ++++++++++++++++++ load-local-lisp.el | 4 ++ 13 files changed, 113 insertions(+), 173 deletions(-) delete mode 100644 config/config.el delete mode 100644 config/files/config-files-backup-on-save-hook.el delete mode 100644 config/files/config-files.el delete mode 100644 config/man/config-man-helper-functions.el delete mode 100644 config/man/config-man.el delete mode 100644 config/programming-languages/c/config-c-functions.el delete mode 100644 config/programming-languages/c/config-c.el delete mode 100644 config/programming-languages/config-programming-languages.el delete mode 100644 config/programming-languages/shell/config-shell.el create mode 100644 lisp/backup-before-save/backup-before-save.el create mode 100644 load-config.el create mode 100644 load-local-lisp.el diff --git a/config/config.el b/config/config.el deleted file mode 100644 index 837282c..0000000 --- a/config/config.el +++ /dev/null @@ -1,54 +0,0 @@ -(require 'seq) - -(defvar config-directory (file-name-directory (locate-library "config"))) - -(defun config-add-modules-to-load-path (directory) - (let* ((pattern (file-name-concat directory "*")) - (results (file-expand-wildcards pattern)) - (directories (seq-filter 'file-directory-p results)) - (non-hidden (seq-filter (lambda (d) - (let ((base (file-name-base d))) - (not (string-prefix-p "." base)))) - directories))) - (mapc (lambda (m) - (add-to-list 'load-path m)) - non-hidden))) - -(config-add-modules-to-load-path config-directory) - -(require 'config-dired) -(require 'config-display-buffer) -(require 'config-eldoc) -(require 'config-electric) -(require 'config-files) -(require 'config-flymake) -(require 'config-garbage-collection) -(require 'config-keys) -(require 'config-man) -(require 'config-package) -(require 'config-programming-languages) -(require 'config-project) -(require 'config-recentf) -(require 'config-savehist) -(require 'config-tramp) - -(require 'config-text-mode) -(require 'config-conf-mode) -(require 'config-prog-mode) - -(when (locate-library "company") - (require 'config-company)) - -(when (locate-library "eglot") - (require 'config-eglot)) - -(when (locate-library "flycheck") - (require 'config-flycheck)) - -(when (locate-library "lsp-mode") - (require 'config-lsp-mode)) - -(when (and (>= emacs-major-version 28) (native-comp-available-p)) - (require 'config-native-comp)) - -(provide 'config) diff --git a/config/files/config-files-backup-on-save-hook.el b/config/files/config-files-backup-on-save-hook.el deleted file mode 100644 index 53b96d1..0000000 --- a/config/files/config-files-backup-on-save-hook.el +++ /dev/null @@ -1,50 +0,0 @@ -(defvar config-files-backup-directory (file-name-concat user-emacs-directory "backups")) - -(unless (file-exists-p config-files-backup-directory) - (mkdir config-files-backup-directory)) - -(defun config-files-format-backup-path (path number) - (format "%s.~%s~" path number)) - -(defun config-files-backup-extension (path) - (let* ((i (string-search "." (reverse path))) - (extension (substring path (- (length path) i)))) - extension)) - -(defun config-files-backup-no-extension (path) - (let* ((i (string-search "." (reverse path))) - (no-extension (substring path 0 (- (length path) (+ i 1))))) - no-extension)) - -(defun config-files-backup-number (path) - (let* ((extension (config-files-backup-extension path)) - (extracted (substring extension 1 (- (length extension) 1))) - (as-number (unless (zerop (length extracted)) - (string-to-number extracted)))) - as-number)) - -(defun config-files-shift-path (path) - (let* ((without-extension (config-files-backup-no-extension path)) - (number (config-files-backup-number path)) - (next-number (+ number 1)) - (shifted-path (config-files-format-backup-path - without-extension - next-number))) - shifted-path)) - -(defun config-files-shift-backup (path) - (let ((next-path (config-files-shift-path path))) - (when (file-exists-p next-path) - (config-files-shift-backup next-path)) - (rename-file path next-path))) - -(defun config-files-backup-before-save () - (unless backup-inhibited - (let* ((path (buffer-file-name)) - (backup-path (config-files-format-backup-path path 0))) - (when (file-exists-p path) - (when (file-exists-p backup-path) - (config-files-shift-backup backup-path)) - (copy-file path backup-path))))) - -(add-hook 'before-save-hook 'config-files-backup-before-save) diff --git a/config/files/config-files.el b/config/files/config-files.el deleted file mode 100644 index 9e92ee4..0000000 --- a/config/files/config-files.el +++ /dev/null @@ -1,3 +0,0 @@ -(load "config-files-backup-on-save-hook") - -(provide 'config-files) diff --git a/config/man/config-man-helper-functions.el b/config/man/config-man-helper-functions.el deleted file mode 100644 index a58ba75..0000000 --- a/config/man/config-man-helper-functions.el +++ /dev/null @@ -1,31 +0,0 @@ -(defun config-man-pages () - (let* ((manpath (getenv "MANPATH")) - (directories (split-string manpath ":" t)) - (that-exist (seq-filter 'file-exists-p directories)) - (files (mapcar (lambda (d) - (directory-files-recursively d ".*" nil)) - that-exist)) - (flattened (flatten-list files)) - (pages (mapcar 'file-name-nondirectory flattened)) - (without-second-ext (mapcar (lambda (p) - (let ((extension (file-name-extension p))) - (if (string-match-p "^[0-9+]$" extension) - p - (file-name-sans-extension p)))) - pages))) - without-second-ext)) - -(defun config-man-locate-page (page) - (with-temp-buffer - (let ((exit-code (call-process "man" nil (current-buffer) nil "--where" page))) - (when exit-code - (let* ((output (buffer-string)) - (trimmed (string-trim-right output "\n"))) - trimmed))))) - -(defun config-man-advice (orig &rest args) - (interactive) - (let ((page (or args (list (completing-read "Select page: " (config-man-pages) nil t))))) - (apply orig page))) - -(advice-add 'man :around 'config-man-advice) diff --git a/config/man/config-man.el b/config/man/config-man.el deleted file mode 100644 index 566420e..0000000 --- a/config/man/config-man.el +++ /dev/null @@ -1,5 +0,0 @@ -(setq Man-notify-method 'pushy) - -(load "config-man-helper-functions") - -(provide 'config-man) diff --git a/config/programming-languages/c/config-c-functions.el b/config/programming-languages/c/config-c-functions.el deleted file mode 100644 index b60d38c..0000000 --- a/config/programming-languages/c/config-c-functions.el +++ /dev/null @@ -1,4 +0,0 @@ -(defun config-c-mode-insert-header-guard () - (interactive) - (let ((guard (upcase (format "%s_H" (file-name-base (buffer-file-name)))))) - (insert (format "#ifndef %s\n#define %s\n#endif" guard guard)))) diff --git a/config/programming-languages/c/config-c.el b/config/programming-languages/c/config-c.el deleted file mode 100644 index 3cb6a32..0000000 --- a/config/programming-languages/c/config-c.el +++ /dev/null @@ -1,5 +0,0 @@ -(setq c-default-style "stroustrup" c-basic-offset 4) - -(load "config-c-functions") - -(provide 'config-c) diff --git a/config/programming-languages/config-programming-languages.el b/config/programming-languages/config-programming-languages.el deleted file mode 100644 index 3976cce..0000000 --- a/config/programming-languages/config-programming-languages.el +++ /dev/null @@ -1,14 +0,0 @@ -(defvar config-programming-languages-directory (file-name-directory - (locate-library "config-programming-languages"))) - -(defvar config-programming-languages-modules '(c shell)) - -(mapc (lambda (module) - (let ((module-load-path (file-name-concat config-programming-languages-directory (symbol-name module)))) - (add-to-list 'load-path module-load-path))) - config-programming-languages-modules) - -(require 'config-c) -(require 'config-shell) - -(provide 'config-programming-languages) diff --git a/config/programming-languages/shell/config-shell.el b/config/programming-languages/shell/config-shell.el deleted file mode 100644 index 0c1e82c..0000000 --- a/config/programming-languages/shell/config-shell.el +++ /dev/null @@ -1,3 +0,0 @@ -(setq sh-shell-file "/bin/bash") - -(provide 'config-shell) diff --git a/init.el b/init.el index fafe2a5..bca67cf 100644 --- a/init.el +++ b/init.el @@ -1,7 +1,3 @@ -(add-to-list 'load-path (file-name-concat user-emacs-directory "config")) - -(require 'config) - (setq auth-source-save-behavior nil auto-save-default nil change-major-mode-with-file-name nil @@ -22,6 +18,9 @@ (when (file-exists-p custom-file) (load custom-file)) +(load (file-name-concat user-emacs-directory "load-config")) +(load (file-name-concat user-emacs-directory "load-local-lisp")) + (cua-mode 1) (recentf-mode 1) (savehist-mode 1) 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) diff --git a/load-config.el b/load-config.el new file mode 100644 index 0000000..9e7beb7 --- /dev/null +++ b/load-config.el @@ -0,0 +1,52 @@ +(let ((config-directory (file-name-concat user-emacs-directory "config"))) + (add-to-list 'load-path (file-name-concat config-directory "company")) + (add-to-list 'load-path (file-name-concat config-directory "conf-mode")) + (add-to-list 'load-path (file-name-concat config-directory "dired")) + (add-to-list 'load-path (file-name-concat config-directory "display-buffer")) + (add-to-list 'load-path (file-name-concat config-directory "eglot")) + (add-to-list 'load-path (file-name-concat config-directory "eldoc")) + (add-to-list 'load-path (file-name-concat config-directory "electric")) + (add-to-list 'load-path (file-name-concat config-directory "flycheck")) + (add-to-list 'load-path (file-name-concat config-directory "flymake")) + (add-to-list 'load-path (file-name-concat config-directory "garbage-collection")) + (add-to-list 'load-path (file-name-concat config-directory "keys")) + (add-to-list 'load-path (file-name-concat config-directory "lsp-mode")) + (add-to-list 'load-path (file-name-concat config-directory "native-comp")) + (add-to-list 'load-path (file-name-concat config-directory "package")) + (add-to-list 'load-path (file-name-concat config-directory "prog-mode")) + (add-to-list 'load-path (file-name-concat config-directory "project")) + (add-to-list 'load-path (file-name-concat config-directory "recentf")) + (add-to-list 'load-path (file-name-concat config-directory "savehist")) + (add-to-list 'load-path (file-name-concat config-directory "text-mode")) + (add-to-list 'load-path (file-name-concat config-directory "tramp"))) + +(require 'config-text-mode) +(require 'config-conf-mode) +(require 'config-prog-mode) +(require 'config-dired) +(require 'config-display-buffer) +(require 'config-eldoc) +(require 'config-electric) +(require 'config-flymake) +(require 'config-garbage-collection) +(require 'config-keys) +(require 'config-package) +(require 'config-project) +(require 'config-recentf) +(require 'config-savehist) +(require 'config-tramp) + +(when (locate-library "company") + (require 'config-company)) + +(when (locate-library "eglot") + (require 'config-eglot)) + +(when (locate-library "flycheck") + (require 'config-flycheck)) + +(when (locate-library "lsp-mode") + (require 'config-lsp-mode)) + +(when (and (> emacs-major-version 28) (native-comp-available-p)) + (require 'config-native-comp)) diff --git a/load-local-lisp.el b/load-local-lisp.el new file mode 100644 index 0000000..0e1fba1 --- /dev/null +++ b/load-local-lisp.el @@ -0,0 +1,4 @@ +(let ((local-lisp-directory (file-name-concat user-emacs-directory "lisp"))) + (add-to-list 'load-path (file-name-concat local-lisp-directory "backup-before-save"))) + +(require 'backup-before-save)