Skip to content

Commit

Permalink
fix(defaults): setup default hooks early
Browse files Browse the repository at this point in the history
  • Loading branch information
abougouffa committed Mar 28, 2023
1 parent 2a68535 commit 720da8c
Showing 1 changed file with 77 additions and 81 deletions.
158 changes: 77 additions & 81 deletions core/me-defaults.el
Original file line number Diff line number Diff line change
Expand Up @@ -311,99 +311,95 @@
;; Save buffer status
desktop-save-buffer t)

;; ====== Misc hooks and advices ======
;; Advice `emacs-session-filename' to ensure creating "session.ID" files in
;; a sub-directory
(with-eval-after-load 'x-win
(advice-add
#'emacs-session-filename :filter-return
(defun +emacs-session-filename--in-subdir-a (session-filename)
"Put the SESSION-FILENAME in the \"x-win/\" sub-directory."
(concat (+directory-ensure minemacs-local-dir "x-win/")
(file-name-nondirectory session-filename)))))

;; Kill `term' buffer on exit (reproduce a similar behavior to `shell's
;; `shell-kill-buffer-on-exit').
(advice-add
'term-sentinel :around
(defun +term--kill-after-exit-a (orig-fn proc msg)
(if (memq (process-status proc) '(signal exit))
(let ((buffer (process-buffer proc)))
(apply orig-fn (list proc msg))
(kill-buffer buffer))
(apply orig-fn (list proc msg)))))

;; Kill the minibuffer when switching by mouse to another window.
;; Adapted from: trey-jackson.blogspot.com/2010/04/emacs-tip-36-abort-minibuffer-when.html
(add-hook
'mouse-leave-buffer-hook
(defun +minibuffer--kill-on-mouse-h ()
"Kill the minibuffer when switching to window with mouse."
(when (and (>= (recursion-depth) 1) (active-minibuffer-window))
(abort-recursive-edit))))

;; ====== Tweaks on file save ======
;; Update time stamp (if available) before saving a file.
(add-hook 'before-save-hook 'time-stamp)

(defcustom +whitespace-auto-cleanup-modes
'(prog-mode conf-mode org-mode markdown-mode
latex-mode tex-mode bibtex-mode)
"Enable auto white space cleanup before saving for these derived modes."
:group 'minemacs-edit
:type '(repeat symbol))

;; Auto-remove trailing white spaces before saving for modes defined in
;; `+whitespace-auto-cleanup-modes'.
(add-hook
'before-save-hook
(defun +save--whitespace-cleanup-h ()
(when (cl-some #'derived-mode-p +whitespace-auto-cleanup-modes)
(whitespace-cleanup))))

;; Guess the major mode after saving a file in `fundamental-mode' (adapted
;; from Doom Emacs).
(add-hook
'after-save-hook
(defun +save--guess-file-mode-h ()
"Guess major mode when saving a file in `fundamental-mode'.
Likely, something has changed since the buffer was opened. e.g. A shebang line
or file path may exist now."
(when (eq major-mode 'fundamental-mode)
(let ((buffer (or (buffer-base-buffer) (current-buffer))))
(and (buffer-file-name buffer)
(eq buffer (window-buffer (selected-window))) ;; Only visible buffers
(set-auto-mode))))))

;; ====== Modes enabled locally, mainly for `prog-mode', `conf-mode' and `text-mode' ======
;; Show line numbers
(add-hook 'prog-mode-hook #'display-line-numbers-mode)
(add-hook 'conf-mode-hook #'display-line-numbers-mode)
(add-hook 'text-mode-hook #'display-line-numbers-mode)

;; Highlight the current line
(add-hook 'prog-mode-hook #'hl-line-mode)
(add-hook 'conf-mode-hook #'hl-line-mode)
(add-hook 'text-mode-hook #'hl-line-mode)

;; Hide/show code blocks, a.k.a. code folding
(add-hook 'prog-mode-hook #'hs-minor-mode)
(add-hook 'conf-mode-hook #'hs-minor-mode)

;; Wrap long lines
(add-hook 'prog-mode-hook #'visual-line-mode)
(add-hook 'conf-mode-hook #'visual-line-mode)
(add-hook 'text-mode-hook #'visual-line-mode)

;; When MinEmacs is running in an asynchronous Org export context, there is no
;; need to enable these modes. So we load them only if we haven't been launched
;; through the `me-org-export-async-init' file.
;; All modes and tweaks are enabled after MinEmacs is gets loaded
(+deferred-unless! (featurep 'me-org-export-async-init)
;; ====== Misc hooks and advices ======
;; Advice `emacs-session-filename' to ensure creating "session.ID" files in
;; a sub-directory
(with-eval-after-load 'x-win
(advice-add
#'emacs-session-filename :filter-return
(defun +emacs-session-filename--in-subdir-a (session-filename)
"Put the SESSION-FILENAME in the \"x-win/\" sub-directory."
(concat (+directory-ensure minemacs-local-dir "x-win/")
(file-name-nondirectory session-filename)))))

;; Integration of `compile' with `savehist'
(+after-load! '(:and savehist compile)
(add-to-list 'savehist-additional-variables 'compile-history))

;; Kill `term' buffer on exit (reproduce a similar behavior to `shell's
;; `shell-kill-buffer-on-exit').
(advice-add
'term-sentinel :around
(defun +term--kill-after-exit-a (orig-fn proc msg)
(if (memq (process-status proc) '(signal exit))
(let ((buffer (process-buffer proc)))
(apply orig-fn (list proc msg))
(kill-buffer buffer))
(apply orig-fn (list proc msg)))))

;; Kill the minibuffer when switching by mouse to another window.
;; Adapted from: trey-jackson.blogspot.com/2010/04/emacs-tip-36-abort-minibuffer-when.html
(add-hook
'mouse-leave-buffer-hook
(defun +minibuffer--kill-on-mouse-h ()
"Kill the minibuffer when switching to window with mouse."
(when (and (>= (recursion-depth) 1) (active-minibuffer-window))
(abort-recursive-edit))))

;; ====== Tweaks on file save ======
;; Update time stamp (if available) before saving a file.
(add-hook 'before-save-hook 'time-stamp)

;; Auto-remove trailing white spaces before saving for modes defined in
;; `+whitespace-auto-cleanup-modes'.
(add-hook
'before-save-hook
(defun +save--whitespace-cleanup-h ()
(when (cl-some #'derived-mode-p +whitespace-auto-cleanup-modes)
(whitespace-cleanup))))

;; Guess the major mode after saving a file in `fundamental-mode' (adapted
;; from Doom Emacs).
(add-hook
'after-save-hook
(defun +save--guess-file-mode-h ()
"Guess major mode when saving a file in `fundamental-mode'.
Likely, something has changed since the buffer was opened. e.g. A shebang line
or file path may exist now."
(when (eq major-mode 'fundamental-mode)
(let ((buffer (or (buffer-base-buffer) (current-buffer))))
(and (buffer-file-name buffer)
(eq buffer (window-buffer (selected-window))) ;; Only visible buffers
(set-auto-mode))))))

;; ====== Modes enabled locally, mainly for `prog-mode', `conf-mode' and `text-mode' ======
;; Show line numbers
(add-hook 'prog-mode-hook #'display-line-numbers-mode)
(add-hook 'conf-mode-hook #'display-line-numbers-mode)
(add-hook 'text-mode-hook #'display-line-numbers-mode)

;; Highlight the current line
(add-hook 'prog-mode-hook #'hl-line-mode)
(add-hook 'conf-mode-hook #'hl-line-mode)
(add-hook 'text-mode-hook #'hl-line-mode)

;; Hide/show code blocks, a.k.a. code folding
(add-hook 'prog-mode-hook #'hs-minor-mode)
(add-hook 'conf-mode-hook #'hs-minor-mode)

;; Wrap long lines
(add-hook 'prog-mode-hook #'visual-line-mode)
(add-hook 'conf-mode-hook #'visual-line-mode)
(add-hook 'text-mode-hook #'visual-line-mode)

;; Navigate windows using Shift+Direction
(windmove-default-keybindings)

Expand Down

0 comments on commit 720da8c

Please sign in to comment.