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,11 +68,8 @@
(executable-find "hunspell") (executable-find "hunspell")
(executable-find "ispell")) (executable-find "ispell"))
:diminish flyspell-mode :diminish flyspell-mode
:custom (flyspell-default-dictionary "english")
:custom :config (progn
(flyspell-default-dictionary "english")
:config
(defun my/toggle-flyspell () (defun my/toggle-flyspell ()
"Toggle flyspell-mode and run flyspell-buffer after activating." "Toggle flyspell-mode and run flyspell-buffer after activating."
(interactive) (interactive)
@ -90,46 +81,30 @@
(defun my/flyspell-german () (defun my/flyspell-german ()
"Set dictionary to german." "Set dictionary to german."
(interactive) (interactive)
(ispell-change-dictionary "german")) (ispell-change-dictionary "german")))
:bind (("<f9>" . my/toggle-flyspell)
;; 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 (:map flyspell-mode-map
("C-;" . nil)) ; iedit needs C-;. ("C-;" . nil))) ; iedit needs C-;.
:hook ((prog-mode . flyspell-prog-mode) ; Spellcheck comments.
:hook (text-mode . flyspell-mode) ; Spellcheck text documents ↓.
;; Spellcheck comments.
(prog-mode . flyspell-prog-mode)
;; Spellcheck text documents.
(text-mode . flyspell-mode)
(LaTeX-mode . my/flyspell-german) (LaTeX-mode . my/flyspell-german)
(LaTeX-mode . flyspell-mode) (LaTeX-mode . flyspell-mode)
(adoc-mode . flyspell-mode) (adoc-mode . flyspell-mode)
(markdown-mode . flyspell-mode) (markdown-mode . flyspell-mode)
(git-commit-mode . flyspell-mode) (git-commit-mode . flyspell-mode))
:mode ("COMMIT_EDITMSG\\'" . 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
:config
(defun my/tab-yas-or-company () (defun my/tab-yas-or-company ()
"Complete with company if possible, jump to next field otherwise." "Complete with company if possible, jump to next field otherwise."
(interactive) (interactive)
(if company-candidates (if company-candidates
(company-complete-selection) (company-complete-selection)
(yas-next-field))) (yas-next-field)))
(yas-reload-all))
(yas-reload-all) :bind (:map yas-keymap
:bind
(:map yas-keymap
("<tab>" . my/tab-yas-or-company) ("<tab>" . my/tab-yas-or-company)
("TAB" . my/tab-yas-or-company) ("TAB" . my/tab-yas-or-company))
)
:hook :hook (prog-mode . yas-minor-mode))
(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"))
:custom (auto-insert-query nil)) ; Don't ask before inserting.
(auto-insert-directory (concat user-emacs-directory "auto-insert")) :config (progn
(auto-insert-query nil) ; Don't ask before inserting. (add-to-list
'auto-insert-alist '(("\\.c\\(pp\\|c\\|xx\\|\\+\\+\\)$" .
:config "C++ program") .
(add-to-list 'auto-insert-alist
'(("\\.\\(cpp\\|cc\\|cxx\\|c\\+\\+\\)$" . "C++ program") .
["cpp" my/autoinsert-yas-expand])) ["cpp" my/autoinsert-yas-expand]))
(add-to-list 'auto-insert-alist (add-to-list
'(("\\.\\(hpp\\|hh\\|hxx\\|h\\+\\+\\)$" . "C++ header") . 'auto-insert-alist '(("\\.h\\(pp\\|h\\|xx\\|\\+\\+\\)$" .
"C++ header") .
["hpp" my/autoinsert-yas-expand])) ["hpp" my/autoinsert-yas-expand]))
(add-to-list 'auto-insert-alist (add-to-list
'(("\\.[1-9]\\.adoc$" . "AsciiDoc manpage") . 'auto-insert-alist '(("\\.[1-9]\\.adoc$" . "AsciiDoc manpage") .
["manpage.adoc" my/autoinsert-yas-expand])) ["manpage.adoc" my/autoinsert-yas-expand]))
(add-to-list 'auto-insert-alist (add-to-list
'(("\\.user.js$" . "Userscript") . 'auto-insert-alist '(("\\.user.js$" . "Userscript") .
["user.js" my/autoinsert-yas-expand])) ["user.js" my/autoinsert-yas-expand])))
:hook (find-file . auto-insert))
: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.
:custom :config (progn (delete 'newline-mark whitespace-style) ; Don't paint $ at eol.
(whitespace-line-column nil) ; Set to fill-column. (delete 'lines whitespace-style) ; Don't mark whole long lines.
(when (< emacs-major-version 27) ; Mark end of too long lines.
:config
(delete 'newline-mark whitespace-style) ; Don't paint $ at eol.
(delete 'lines whitespace-style) ; Don't mark whole overly long lines.
(when (< emacs-major-version 27) ; Mark end of overly long lines.
(add-to-list 'whitespace-style 'lines-tail)) (add-to-list 'whitespace-style 'lines-tail))
;; Workaround to not show dots in popup menus. ;; Workaround to not show dots in popup menus.
(defun my/whitespace-mode-enabled-p ()
(symbol-value 'whitespace-mode))
(defvar-local my/ws-enabled nil) (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: ;; company:
(defun my/on-off-whitespace-before-company (command) (defun my/on-off-whitespace-before-company (command)
(when (string= "show" command) (when (string= "show" command)
(setq-local my/ws-enabled (my/whitespace-mode-enabled-p)) (my/whitespace-mode-off))
(if my/ws-enabled
(whitespace-mode -1)))
(when (string= "hide" command) (when (string= "hide" command)
(if my/ws-enabled (my/whitespace-mode-off)))
(whitespace-mode t))))
(advice-add 'company-call-frontends (advice-add 'company-call-frontends
:before #'my/on-off-whitespace-before-company) :before #'my/on-off-whitespace-before-company)
(defun my/whitespace-mode-off ()
(setq-local my/ws-enabled (my/whitespace-mode-enabled-p))
(if my/ws-enabled
(whitespace-mode -1)))
(defun my/whitespace-mode-on ()
(if my/ws-enabled
(whitespace-mode t)))
;; popup: ;; popup:
(defadvice popup-create (before my/popup-suppress-whitespace-mode activate) (defadvice popup-create (before my/popup-suspend-ws activate)
"Suspend whitespace-mode while popups are visible." "Suspend whitespace-mode while popups are visible."
(my/whitespace-mode-off)) (my/whitespace-mode-off))
(defadvice popup-delete (after my/popup-restore-whitespace-mode activate) (defadvice popup-delete (after my/popup-restore-ws activate)
"Restore whitespace-mode when all popups have closed." "Restore whitespace-mode when all popups have closed."
(my/whitespace-mode-on)) (my/whitespace-mode-on))
(if (display-graphic-p) (if (display-graphic-p)
(custom-set-faces (custom-set-faces
'(whitespace-line ((t (:inherit whitespace-line '(whitespace-line
((t (:inherit whitespace-line
:weight normal :weight normal
:foreground nil :foreground nil
:background nil :background nil
:box (:line-width 1 :color "dark red") :box
;; :underline (:color "dark red") (:line-width 1 :color "dark red"))))))
)))))
(custom-set-faces ; else (custom-set-faces ; else
'(whitespace-line ((t (:inherit whitespace-line '(whitespace-line ((t (:inherit whitespace-line
:background nil :background nil
:underline t :underline t))))))
)))))
)
;; Workaround for <https://debbugs.gnu.org/cgi/bugreport.cgi?bug=36837>, fixed ;; Workaround for
;; <https://debbugs.gnu.org/cgi/bugreport.cgi?bug=36837>, fixed
;; in 28.1. ;; in 28.1.
(defun my/ws-load-local-vars-first () (defun my/ws-load-local-vars-first ()
"Loads local variables (fill-column) before enabling whitespace-mode." "Loads fill-column before enabling whitespace-mode."
(when (< emacs-major-version 27) ; We don't use 'lines-tail in Emacs >= 27. ;; We don't use 'lines-tail in Emacs >= 27.
(when (< emacs-major-version 27)
(hack-local-variables)) (hack-local-variables))
(whitespace-mode) (whitespace-mode))
)
(defvar-local my/no-ws-cleanup nil (defvar-local my/no-ws-cleanup nil
"Do not cleanup whitespace if t.") "Do not cleanup whitespace if t.")
(put 'my/no-ws-cleanup 'safe-local-variable #'booleanp) ; Mark as safe. (put 'my/no-ws-cleanup 'safe-local-variable #'booleanp)
(defun my/ws-maybe-cleanup () (defun my/ws-maybe-cleanup ()
"Run `whitespace-cleanup' if `my/no-ws-cleanup' is not t." "Run `whitespace-cleanup' if `my/no-ws-cleanup' is not t."
(unless my/no-ws-cleanup (unless my/no-ws-cleanup
(whitespace-cleanup)) (whitespace-cleanup))))
) :bind ("C-c w" . whitespace-mode)
:hook ((prog-mode . my/ws-load-local-vars-first)
:bind
("C-c w" . whitespace-mode)
:hook
(prog-mode . my/ws-load-local-vars-first)
(conf-mode . my/ws-load-local-vars-first) (conf-mode . my/ws-load-local-vars-first)
(text-mode . my/ws-load-local-vars-first) (text-mode . my/ws-load-local-vars-first)
(before-save . my/ws-maybe-cleanup) (before-save . my/ws-maybe-cleanup))
:custom-face (whitespace-space ((nil :foreground "gray18"))))
:custom-face
(whitespace-space ((nil :foreground "gray18")))
)
(use-package ripgrep) (use-package ripgrep)