From 4d73f659f5c3469c63b63073ca50a4696bf92bb1 Mon Sep 17 00:00:00 2001 From: Henrik Lissner Date: Fri, 26 Jul 2019 22:05:26 +0200 Subject: [PATCH] Refactor out nested hook defuns & refactor core.el Moves file-handling config to core-editor.el. --- core/core-editor.el | 40 ++++++++++++++++++++--- core/core.el | 77 +++++++++++++++------------------------------ 2 files changed, 62 insertions(+), 55 deletions(-) diff --git a/core/core-editor.el b/core/core-editor.el index b042ae6b8..c131c0f7b 100644 --- a/core/core-editor.el +++ b/core/core-editor.el @@ -10,6 +10,38 @@ indentation settings or not. This should be set by editorconfig if it successfully sets indent_style/indent_size.") +;; +;;; File handling + +;; Resolve symlinks when opening files, so that any operations are conducted +;; from the file's true directory (like `find-file'). +(setq find-file-visit-truename t) + +;; Disable the warning "X and Y are the same file". It's fine to ignore this +;; warning as it will redirect you to the existing buffer anyway. +(setq find-file-suppress-same-file-warnings t) + +;; Create missing directories when we open a file that doesn't exist under a +;; directory tree that may not exist. +(add-hook! 'find-file-not-found-functions + (defun doom-create-missing-directories-h () + "Automatically create missing directories when creating new files." + (let ((parent-directory (file-name-directory buffer-file-name))) + (when (and (not (file-exists-p parent-directory)) + (y-or-n-p (format "Directory `%s' does not exist! Create it?" parent-directory))) + (make-directory parent-directory t))))) + +;; Don't autosave files or create lock/history/backup files. The +;; editor doesn't need to hold our hands so much. We'll rely on git +;; and our own good fortune instead. Fingers crossed! +(setq auto-save-default nil + create-lockfiles nil + make-backup-files nil + ;; But have a place to store them in case we do use them... + auto-save-list-file-name (concat doom-cache-dir "autosave") + backup-directory-alist `(("." . ,(concat doom-cache-dir "backup/")))) + + ;; ;;; Formatting @@ -45,7 +77,7 @@ successfully sets indent_style/indent_size.") (setq save-interprogram-paste-before-kill t) ;; Fix the clipboard in terminal or daemon Emacs (non-GUI) -(add-hook 'tty-setup-hook +(add-hook! 'tty-setup-hook (defun doom-init-clipboard-in-tty-emacs-h () (unless (getenv "SSH_CONNECTION") (cond (IS-MAC @@ -131,7 +163,7 @@ successfully sets indent_style/indent_size.") ;; Return nil for `write-file-functions' nil)) - (add-hook 'dired-mode-hook + (add-hook! 'dired-mode-hook (defun doom--recentf-add-dired-directory-h () "Add dired directory to recentf file list." (recentf-add-file default-directory))) @@ -152,7 +184,7 @@ successfully sets indent_style/indent_size.") savehist-additional-variables '(kill-ring search-ring regexp-search-ring)) (savehist-mode +1) - (add-hook 'kill-emacs-hook + (add-hook! 'kill-emacs-hook (defun doom-unpropertize-kill-ring-h () "Remove text properties from `kill-ring' for a smaller savehist file." (setq kill-ring (cl-loop for item in kill-ring @@ -344,7 +376,7 @@ successfully sets indent_style/indent_size.") (dolist (key '(:unmatched-expression :no-matching-tag)) (setf (cdr (assq key sp-message-alist)) nil)) - (add-hook 'minibuffer-setup-hook + (add-hook! 'minibuffer-setup-hook (defun doom-init-smartparens-in-minibuffer-maybe-h () "Enable `smartparens-mode' in the minibuffer, during `eval-expression' or `evil-ex'." diff --git a/core/core.el b/core/core.el index 8056ee421..f4c40ccb6 100644 --- a/core/core.el +++ b/core/core.el @@ -158,16 +158,6 @@ users).") ;; slightly, from 0.5s: (setq idle-update-delay 1) -;; Don't autosave files or create lock/history/backup files. The -;; editor doesn't need to hold our hands so much. We'll rely on git -;; and our own good fortune instead. Fingers crossed! -(setq auto-save-default nil - create-lockfiles nil - make-backup-files nil - ;; But have a place to store them in case we do use them... - auto-save-list-file-name (concat doom-cache-dir "autosave") - backup-directory-alist `(("." . ,(concat doom-cache-dir "backup/")))) - ;; Emacs is a huge security vulnerability, what with all the dependencies it ;; pulls in from all corners of the globe. Let's at least try to be more ;; discerning. @@ -184,25 +174,6 @@ users).") (setq auth-sources (list (expand-file-name "authinfo.gpg" doom-etc-dir) "~/.authinfo.gpg")) - -;; Resolve symlinks when opening files, so that any operations are conducted -;; from the file's true directory (like `find-file'). -(setq find-file-visit-truename t) - -;; Disable the warning "X and Y are the same file". It's fine to ignore this -;; warning as it will redirect you to the existing buffer anyway. -(setq find-file-suppress-same-file-warnings t) - -;; Create missing directories when we open a file that doesn't exist under a -;; directory tree that may not exist. -(add-hook 'find-file-not-found-functions - (defun doom-create-missing-directories-h () - "Automatically create missing directories when creating new files." - (let ((parent-directory (file-name-directory buffer-file-name))) - (when (and (not (file-exists-p parent-directory)) - (y-or-n-p (format "Directory `%s' does not exist! Create it?" parent-directory))) - (make-directory parent-directory t))))) - ;; Emacs on Windows frequently confuses HOME (C:\Users\) and APPDATA, ;; causing `abbreviate-home-dir' to produce incorrect paths. (when IS-WINDOWS @@ -273,9 +244,11 @@ users).") ;; This is consulted on every `require', `load' and various path/io functions. ;; You get a minor speed up by nooping this. (setq file-name-handler-alist nil) -(add-hook 'emacs-startup-hook - (defun doom-restore-file-name-handler-alist-h () - (setq file-name-handler-alist doom--initial-file-name-handler-alist))) + +(defun doom-restore-file-name-handler-alist-h () + (setq file-name-handler-alist doom--initial-file-name-handler-alist)) + +(add-hook 'emacs-startup-hook #'doom-restore-file-name-handler-alist-h) ;; To speed up minibuffer commands (like helm and ivy), we defer garbage ;; collection while the minibuffer is active. @@ -307,20 +280,21 @@ users).") ;; File+dir local variables are initialized after the major mode and its hooks ;; have run. If you want hook functions to be aware of these customizations, add ;; them to MODE-local-vars-hook instead. -(add-hook 'hack-local-variables-hook - (defun doom-run-local-var-hooks-h () - "Run MODE-local-vars-hook after local variables are initialized." - (run-hook-wrapped (intern-soft (format "%s-local-vars-hook" major-mode)) - #'doom-try-run-hook))) +(defun doom-run-local-var-hooks-h () + "Run MODE-local-vars-hook after local variables are initialized." + (run-hook-wrapped (intern-soft (format "%s-local-vars-hook" major-mode)) + #'doom-try-run-hook)) +(add-hook 'hack-local-variables-hook #'doom-run-local-var-hooks-h) ;; If `enable-local-variables' is disabled, then `hack-local-variables-hook' is ;; never triggered. +(defun doom-run-local-var-hooks-if-necessary-h () + "Run `doom-run-local-var-hooks-h' if `enable-local-variables' is disabled." + (unless enable-local-variables + (doom-run-local-var-hooks-h))) (add-hook 'after-change-major-mode-hook - (defun doom-run-local-var-hooks-if-necessary-h () - "Run `doom-run-local-var-hooks-h' if `enable-local-variables' is disabled." - (unless enable-local-variables - (doom-run-local-var-hooks-h))) - 'append) + #'doom-run-local-var-hooks-if-necessary-h + 'append) ;; @@ -377,17 +351,18 @@ intervals." reqs t) (doom-log "Finished incremental loading"))))))) -(add-hook 'window-setup-hook - (defun doom-load-packages-incrementally-h () - "Begin incrementally loading packages in `doom-incremental-packages'. +(defun doom-load-packages-incrementally-h () + "Begin incrementally loading packages in `doom-incremental-packages'. If this is a daemon session, load them all immediately instead." - (if (daemonp) - (mapc #'require (cdr doom-incremental-packages)) - (when (integerp doom-incremental-first-idle-timer) - (run-with-idle-timer doom-incremental-first-idle-timer - nil #'doom-load-packages-incrementally - (cdr doom-incremental-packages) t))))) + (if (daemonp) + (mapc #'require (cdr doom-incremental-packages)) + (when (integerp doom-incremental-first-idle-timer) + (run-with-idle-timer doom-incremental-first-idle-timer + nil #'doom-load-packages-incrementally + (cdr doom-incremental-packages) t)))) + +(add-hook 'emacs-startup-hook #'doom-load-packages-incrementally-h) ;;