Emacs: Refactor text/common.el.

This commit is contained in:
tastytea 2020-03-27 17:52:59 +01:00
parent 6faf976d67
commit 9e210e08f9

View File

@ -1,6 +1,6 @@
;;; common.el --- Common settings for text files. -*- lexical-binding: t; -*- ;;; common.el --- Common settings for text files. -*- lexical-binding: t; -*-
;; Time-stamp: <2020-03-27T17:08:08+0100> ;; Time-stamp: <2020-03-27T17:52:34+0100>
;;; Commentary: ;;; Commentary:
@ -31,27 +31,18 @@
:bind (("C-<f5>" . register-quicknav-prev-register) :bind (("C-<f5>" . register-quicknav-prev-register)
("C-<f6>" . register-quicknav-next-register) ("C-<f6>" . register-quicknav-next-register)
("C-<f7>" . register-quicknav-point-to-unused-register) ("C-<f7>" . register-quicknav-point-to-unused-register)
("C-S-<f7>" . register-quicknav-clear-current-register)) ("C-S-<f7>" . register-quicknav-clear-current-register)))
)
;; Save cursor position. ;; Save cursor position.
(use-package saveplace (use-package saveplace
:config :config (save-place-mode t))
(save-place-mode t)
)
;; Ruler with fill-column marker. ;; Ruler with fill-column marker.
(use-package ruler-mode (use-package ruler-mode
:config :config (progn
(defun my/ruler-on () (defun my/ruler-on ()
"Turn `ruler-mode' on." "Turn `ruler-mode' on."
(ruler-mode 1) (ruler-mode 1)))
;; Show a fill-column indicator vertically across the buffer.
(when (>= emacs-major-version 27)
(display-fill-column-indicator-mode))
)
:custom-face :custom-face
(ruler-mode-default ((t (ruler-mode-default ((t
(:inherit default (:inherit default
@ -61,11 +52,14 @@
(ruler-mode-column-number ((t (ruler-mode-column-number ((t
(:inherit ruler-mode-default (:inherit ruler-mode-default
:foreground "dark gray")))) :foreground "dark gray"))))
:hook ((find-file . my/ruler-on)
(text-mode . my/ruler-on))) ; For the scratch buffer.
:hook ;; Show a fill-column indicator vertically across the buffer.
(find-file . my/ruler-on) (when (>= emacs-major-version 27)
(text-mode . my/ruler-on) ; For the scratch buffer. (use-package display-fill-column-indicator
) :hook ((find-file . display-fill-column-indicator-mode)
(text-mode . display-fill-column-indicator-mode))))
;; Spell checking. ;; Spell checking.
(unless slow-computer (unless slow-computer
@ -74,62 +68,43 @@
(executable-find "hunspell") (executable-find "hunspell")
(executable-find "ispell")) (executable-find "ispell"))
:diminish flyspell-mode :diminish flyspell-mode
:custom (flyspell-default-dictionary "english")
:config (progn
(defun my/toggle-flyspell ()
"Toggle flyspell-mode and run flyspell-buffer after activating."
(interactive)
(if (bound-and-true-p flyspell-mode)
(flyspell-mode 0)
(flyspell-mode)
(flyspell-buffer)))
:custom (defun my/flyspell-german ()
(flyspell-default-dictionary "english") "Set dictionary to german."
(interactive)
:config (ispell-change-dictionary "german")))
(defun my/toggle-flyspell () :bind (("<f9>" . my/toggle-flyspell)
"Toggle flyspell-mode and run flyspell-buffer after activating." (:map flyspell-mode-map
(interactive) ("C-;" . nil))) ; iedit needs C-;.
(if (bound-and-true-p flyspell-mode) :hook ((prog-mode . flyspell-prog-mode) ; Spellcheck comments.
(flyspell-mode 0) (text-mode . flyspell-mode) ; Spellcheck text documents ↓.
(flyspell-mode) (LaTeX-mode . my/flyspell-german)
(flyspell-buffer))) (LaTeX-mode . flyspell-mode)
(adoc-mode . flyspell-mode)
(defun my/flyspell-german () (markdown-mode . flyspell-mode)
"Set dictionary to german." (git-commit-mode . flyspell-mode))
(interactive) :mode ("COMMIT_EDITMSG\\'" . flyspell-mode))
(ispell-change-dictionary "german"))
;; Allow setting the language in file/dir local variables.
(put 'ispell-dictionary 'safe-local-variable #'stringp)
:bind
("<f9>" . my/toggle-flyspell)
(:map flyspell-mode-map
("C-;" . nil)) ; iedit needs C-;.
:hook
;; Spellcheck comments.
(prog-mode . flyspell-prog-mode)
;; Spellcheck text documents.
(text-mode . flyspell-mode)
(LaTeX-mode . my/flyspell-german)
(LaTeX-mode . flyspell-mode)
(adoc-mode . flyspell-mode)
(markdown-mode . flyspell-mode)
(git-commit-mode . flyspell-mode)
:mode
("COMMIT_EDITMSG\\'" . flyspell-mode)
)
) ; unless slow-computer. ) ; unless slow-computer.
;; The string Time-stamp: <> in the first 8 lines of the file will be updated ;; The string Time-stamp: <> in the first 8 lines of the file will be updated
;; with the current timestamp. ;; with the current timestamp.
(use-package time-stamp (use-package time-stamp
:config :config (progn
(if (>= emacs-major-version 27) (if (>= emacs-major-version 27)
(setq time-stamp-format "%Y-%02m-%02dT%02H:%02M:%02S%5z") (setq time-stamp-format "%Y-%02m-%02dT%02H:%02M:%02S%5z")
(progn ;; Set to UTC since ISO 8601 is not supported.
(setq time-stamp-format "%Y-%02m-%02dT%02H:%02M:%02S+00:00") (setq time-stamp-format "%Y-%02m-%02dT%02H:%02M:%02S+0000")
(setq time-stamp-time-zone t))) ; Set to UTC since ISO 8601 is not supported. (setq time-stamp-time-zone t)))
:hook (before-save . time-stamp))
:hook
(before-save . time-stamp)
)
;; A template system. ;; A template system.
(use-package yasnippet (use-package yasnippet
@ -137,171 +112,133 @@
:defines (company-candidates) :defines (company-candidates)
:functions (yas-reload-all yas-expand-snippet) :functions (yas-reload-all yas-expand-snippet)
:diminish yas-minor-mode :diminish yas-minor-mode
:config (progn
(defun my/tab-yas-or-company ()
"Complete with company if possible, jump to next field otherwise."
(interactive)
(if company-candidates
(company-complete-selection)
(yas-next-field)))
(yas-reload-all))
:bind (:map yas-keymap
("<tab>" . my/tab-yas-or-company)
("TAB" . my/tab-yas-or-company))
:config :hook (prog-mode . yas-minor-mode))
(defun my/tab-yas-or-company ()
"Complete with company if possible, jump to next field otherwise."
(interactive)
(if company-candidates
(company-complete-selection)
(yas-next-field)))
(yas-reload-all)
:bind
(:map yas-keymap
("<tab>" . my/tab-yas-or-company)
("TAB" . my/tab-yas-or-company)
)
:hook
(prog-mode . yas-minor-mode)
)
;; Install snippet-collection but don't use it. ;; Install snippet-collection but don't use it.
(use-package yasnippet-snippets (use-package yasnippet-snippets
:after (yasnippet) :after (yasnippet)
;; Don't add the snippets.
:config :config (delete 'yasnippet-snippets-dir yas-snippet-dirs))
(delete 'yasnippet-snippets-dir yas-snippet-dirs) ; Don't add snippets.
)
;; Automatically insert text in new files. ;; Automatically insert text in new files.
(use-package autoinsert (use-package autoinsert
:after (yasnippet) :after (yasnippet)
:init (progn
:init (defun my/autoinsert-yas-expand ()
(defun my/autoinsert-yas-expand () "Replace text in yasnippet template."
"Replace text in yasnippet template." (yas-minor-mode t)
(yas-minor-mode t) (yas-expand-snippet (buffer-string) (point-min) (point-max))))
(yas-expand-snippet (buffer-string) (point-min) (point-max))) :custom ((auto-insert-directory (concat user-emacs-directory "auto-insert"))
(auto-insert-query nil)) ; Don't ask before inserting.
:custom :config (progn
(auto-insert-directory (concat user-emacs-directory "auto-insert")) (add-to-list
(auto-insert-query nil) ; Don't ask before inserting. 'auto-insert-alist '(("\\.c\\(pp\\|c\\|xx\\|\\+\\+\\)$" .
"C++ program") .
:config ["cpp" my/autoinsert-yas-expand]))
(add-to-list 'auto-insert-alist (add-to-list
'(("\\.\\(cpp\\|cc\\|cxx\\|c\\+\\+\\)$" . "C++ program") . 'auto-insert-alist '(("\\.h\\(pp\\|h\\|xx\\|\\+\\+\\)$" .
["cpp" my/autoinsert-yas-expand])) "C++ header") .
(add-to-list 'auto-insert-alist ["hpp" my/autoinsert-yas-expand]))
'(("\\.\\(hpp\\|hh\\|hxx\\|h\\+\\+\\)$" . "C++ header") . (add-to-list
["hpp" my/autoinsert-yas-expand])) 'auto-insert-alist '(("\\.[1-9]\\.adoc$" . "AsciiDoc manpage") .
(add-to-list 'auto-insert-alist ["manpage.adoc" my/autoinsert-yas-expand]))
'(("\\.[1-9]\\.adoc$" . "AsciiDoc manpage") . (add-to-list
["manpage.adoc" my/autoinsert-yas-expand])) 'auto-insert-alist '(("\\.user.js$" . "Userscript") .
(add-to-list 'auto-insert-alist ["user.js" my/autoinsert-yas-expand])))
'(("\\.user.js$" . "Userscript") . :hook (find-file . auto-insert))
["user.js" my/autoinsert-yas-expand]))
:hook
(find-file . auto-insert)
)
;; Better search. ;; Better search.
(use-package swiper (use-package swiper
:after (ivy) :after (ivy)
:functions (swiper) :functions (swiper)
:bind ("C-s" . 'swiper))
:bind
("C-s" . 'swiper)
)
;; Visualize and transform whitespace. ;; Visualize and transform whitespace.
(use-package whitespace (use-package whitespace
:after (company) :after (company)
:functions (my/whitespace-mode-enabled-p my/on-off-whitespace-before-company) :functions (my/on-off-whitespace-before-company)
:diminish whitespace-mode :diminish whitespace-mode
:custom (whitespace-line-column nil) ; Set to fill-column.
:config (progn (delete 'newline-mark whitespace-style) ; Don't paint $ at eol.
(delete 'lines whitespace-style) ; Don't mark whole long lines.
(when (< emacs-major-version 27) ; Mark end of too long lines.
(add-to-list 'whitespace-style 'lines-tail))
:custom ;; Workaround to not show dots in popup menus.
(whitespace-line-column nil) ; Set to fill-column. (defvar-local my/ws-enabled nil)
(defun my/whitespace-mode-off ()
(setq-local my/ws-enabled whitespace-mode)
(when my/ws-enabled
(whitespace-mode -1)))
(defun my/whitespace-mode-on ()
(when my/ws-enabled
(whitespace-mode t)))
;; company:
(defun my/on-off-whitespace-before-company (command)
(when (string= "show" command)
(my/whitespace-mode-off))
(when (string= "hide" command)
(my/whitespace-mode-off)))
(advice-add 'company-call-frontends
:before #'my/on-off-whitespace-before-company)
;; popup:
(defadvice popup-create (before my/popup-suspend-ws activate)
"Suspend whitespace-mode while popups are visible."
(my/whitespace-mode-off))
(defadvice popup-delete (after my/popup-restore-ws activate)
"Restore whitespace-mode when all popups have closed."
(my/whitespace-mode-on))
:config (if (display-graphic-p)
(delete 'newline-mark whitespace-style) ; Don't paint $ at eol. (custom-set-faces
(delete 'lines whitespace-style) ; Don't mark whole overly long lines. '(whitespace-line
(when (< emacs-major-version 27) ; Mark end of overly long lines. ((t (:inherit whitespace-line
(add-to-list 'whitespace-style 'lines-tail)) :weight normal
:foreground nil
:background nil
:box
(:line-width 1 :color "dark red"))))))
(custom-set-faces ; else
'(whitespace-line ((t (:inherit whitespace-line
:background nil
:underline t))))))
;; Workaround to not show dots in popup menus. ;; Workaround for
(defun my/whitespace-mode-enabled-p () ;; <https://debbugs.gnu.org/cgi/bugreport.cgi?bug=36837>, fixed
(symbol-value 'whitespace-mode)) ;; in 28.1.
(defvar-local my/ws-enabled nil) (defun my/ws-load-local-vars-first ()
"Loads fill-column before enabling whitespace-mode."
;; We don't use 'lines-tail in Emacs >= 27.
(when (< emacs-major-version 27)
(hack-local-variables))
(whitespace-mode))
;; company: (defvar-local my/no-ws-cleanup nil
(defun my/on-off-whitespace-before-company(command) "Do not cleanup whitespace if t.")
(when (string= "show" command) (put 'my/no-ws-cleanup 'safe-local-variable #'booleanp)
(setq-local my/ws-enabled (my/whitespace-mode-enabled-p))
(if my/ws-enabled
(whitespace-mode -1)))
(when (string= "hide" command)
(if my/ws-enabled
(whitespace-mode t))))
(advice-add 'company-call-frontends
:before #'my/on-off-whitespace-before-company)
(defun my/whitespace-mode-off () (defun my/ws-maybe-cleanup ()
(setq-local my/ws-enabled (my/whitespace-mode-enabled-p)) "Run `whitespace-cleanup' if `my/no-ws-cleanup' is not t."
(if my/ws-enabled (unless my/no-ws-cleanup
(whitespace-mode -1))) (whitespace-cleanup))))
(defun my/whitespace-mode-on () :bind ("C-c w" . whitespace-mode)
(if my/ws-enabled :hook ((prog-mode . my/ws-load-local-vars-first)
(whitespace-mode t))) (conf-mode . my/ws-load-local-vars-first)
(text-mode . my/ws-load-local-vars-first)
;; popup: (before-save . my/ws-maybe-cleanup))
(defadvice popup-create (before my/popup-suppress-whitespace-mode activate) :custom-face (whitespace-space ((nil :foreground "gray18"))))
"Suspend whitespace-mode while popups are visible."
(my/whitespace-mode-off))
(defadvice popup-delete (after my/popup-restore-whitespace-mode activate)
"Restore whitespace-mode when all popups have closed."
(my/whitespace-mode-on))
(if (display-graphic-p)
(custom-set-faces
'(whitespace-line ((t (:inherit whitespace-line
:weight normal
:foreground nil
:background nil
:box (:line-width 1 :color "dark red")
;; :underline (:color "dark red")
)))))
(custom-set-faces ; else
'(whitespace-line ((t (:inherit whitespace-line
:background nil
:underline t
)))))
)
;; Workaround for <https://debbugs.gnu.org/cgi/bugreport.cgi?bug=36837>, fixed
;; in 28.1.
(defun my/ws-load-local-vars-first ()
"Loads local variables (fill-column) before enabling whitespace-mode."
(when (< emacs-major-version 27) ; We don't use 'lines-tail in Emacs >= 27.
(hack-local-variables))
(whitespace-mode)
)
(defvar-local my/no-ws-cleanup nil
"Do not cleanup whitespace if t.")
(put 'my/no-ws-cleanup 'safe-local-variable #'booleanp) ; Mark as safe.
(defun my/ws-maybe-cleanup ()
"Run `whitespace-cleanup' if `my/no-ws-cleanup' is not t."
(unless my/no-ws-cleanup
(whitespace-cleanup))
)
:bind
("C-c w" . whitespace-mode)
:hook
(prog-mode . my/ws-load-local-vars-first)
(conf-mode . my/ws-load-local-vars-first)
(text-mode . my/ws-load-local-vars-first)
(before-save . my/ws-maybe-cleanup)
:custom-face
(whitespace-space ((nil :foreground "gray18")))
)
(use-package ripgrep) (use-package ripgrep)