From 9e210e08f98b9e27459f19f14dc2753baf6c456a Mon Sep 17 00:00:00 2001 From: tastytea Date: Fri, 27 Mar 2020 17:52:59 +0100 Subject: [PATCH] Emacs: Refactor text/common.el. --- init.d/text/common.el | 359 +++++++++++++++++------------------------- 1 file changed, 148 insertions(+), 211 deletions(-) diff --git a/init.d/text/common.el b/init.d/text/common.el index f8f2e03..16ccd74 100644 --- a/init.d/text/common.el +++ b/init.d/text/common.el @@ -1,6 +1,6 @@ ;;; 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: @@ -31,27 +31,18 @@ :bind (("C-" . register-quicknav-prev-register) ("C-" . register-quicknav-next-register) ("C-" . register-quicknav-point-to-unused-register) - ("C-S-" . register-quicknav-clear-current-register)) - ) + ("C-S-" . register-quicknav-clear-current-register))) ;; Save cursor position. (use-package saveplace - :config - (save-place-mode t) - ) + :config (save-place-mode t)) ;; Ruler with fill-column marker. (use-package ruler-mode - :config - (defun my/ruler-on () - "Turn `ruler-mode' on." - (ruler-mode 1) - - ;; Show a fill-column indicator vertically across the buffer. - (when (>= emacs-major-version 27) - (display-fill-column-indicator-mode)) - ) - + :config (progn + (defun my/ruler-on () + "Turn `ruler-mode' on." + (ruler-mode 1))) :custom-face (ruler-mode-default ((t (:inherit default @@ -61,11 +52,14 @@ (ruler-mode-column-number ((t (:inherit ruler-mode-default :foreground "dark gray")))) + :hook ((find-file . my/ruler-on) + (text-mode . my/ruler-on))) ; For the scratch buffer. - :hook - (find-file . my/ruler-on) - (text-mode . my/ruler-on) ; For the scratch buffer. - ) +;; Show a fill-column indicator vertically across the buffer. +(when (>= emacs-major-version 27) + (use-package display-fill-column-indicator + :hook ((find-file . display-fill-column-indicator-mode) + (text-mode . display-fill-column-indicator-mode)))) ;; Spell checking. (unless slow-computer @@ -74,62 +68,43 @@ (executable-find "hunspell") (executable-find "ispell")) :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 - (flyspell-default-dictionary "english") - - :config - (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))) - - (defun my/flyspell-german () - "Set dictionary to german." - (interactive) - (ispell-change-dictionary "german")) - - ;; Allow setting the language in file/dir local variables. - (put 'ispell-dictionary 'safe-local-variable #'stringp) - - :bind - ("" . 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) - ) + (defun my/flyspell-german () + "Set dictionary to german." + (interactive) + (ispell-change-dictionary "german"))) + :bind (("" . my/toggle-flyspell) + (:map flyspell-mode-map + ("C-;" . nil))) ; iedit needs C-;. + :hook ((prog-mode . flyspell-prog-mode) ; Spellcheck comments. + (text-mode . flyspell-mode) ; Spellcheck text documents ↓. + (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. ;; The string Time-stamp: <> in the first 8 lines of the file will be updated ;; with the current timestamp. (use-package time-stamp - :config - (if (>= emacs-major-version 27) - (setq time-stamp-format "%Y-%02m-%02dT%02H:%02M:%02S%5z") - (progn - (setq time-stamp-format "%Y-%02m-%02dT%02H:%02M:%02S+00:00") - (setq time-stamp-time-zone t))) ; Set to UTC since ISO 8601 is not supported. - - :hook - (before-save . time-stamp) - ) + :config (progn + (if (>= emacs-major-version 27) + (setq time-stamp-format "%Y-%02m-%02dT%02H:%02M:%02S%5z") + ;; Set to UTC since ISO 8601 is not supported. + (setq time-stamp-format "%Y-%02m-%02dT%02H:%02M:%02S+0000") + (setq time-stamp-time-zone t))) + :hook (before-save . time-stamp)) ;; A template system. (use-package yasnippet @@ -137,171 +112,133 @@ :defines (company-candidates) :functions (yas-reload-all yas-expand-snippet) :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 + ("" . my/tab-yas-or-company) + ("TAB" . my/tab-yas-or-company)) - :config - (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 - ("" . my/tab-yas-or-company) - ("TAB" . my/tab-yas-or-company) - ) - - :hook - (prog-mode . yas-minor-mode) - ) + :hook (prog-mode . yas-minor-mode)) ;; Install snippet-collection but don't use it. (use-package yasnippet-snippets :after (yasnippet) - - :config - (delete 'yasnippet-snippets-dir yas-snippet-dirs) ; Don't add snippets. - ) + ;; Don't add the snippets. + :config (delete 'yasnippet-snippets-dir yas-snippet-dirs)) ;; Automatically insert text in new files. (use-package autoinsert :after (yasnippet) - - :init - (defun my/autoinsert-yas-expand () - "Replace text in yasnippet template." - (yas-minor-mode t) - (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. - - :config - (add-to-list 'auto-insert-alist - '(("\\.\\(cpp\\|cc\\|cxx\\|c\\+\\+\\)$" . "C++ program") . - ["cpp" my/autoinsert-yas-expand])) - (add-to-list 'auto-insert-alist - '(("\\.\\(hpp\\|hh\\|hxx\\|h\\+\\+\\)$" . "C++ header") . - ["hpp" my/autoinsert-yas-expand])) - (add-to-list 'auto-insert-alist - '(("\\.[1-9]\\.adoc$" . "AsciiDoc manpage") . - ["manpage.adoc" my/autoinsert-yas-expand])) - (add-to-list 'auto-insert-alist - '(("\\.user.js$" . "Userscript") . - ["user.js" my/autoinsert-yas-expand])) - - :hook - (find-file . auto-insert) - ) + :init (progn + (defun my/autoinsert-yas-expand () + "Replace text in yasnippet template." + (yas-minor-mode t) + (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. + :config (progn + (add-to-list + 'auto-insert-alist '(("\\.c\\(pp\\|c\\|xx\\|\\+\\+\\)$" . + "C++ program") . + ["cpp" my/autoinsert-yas-expand])) + (add-to-list + 'auto-insert-alist '(("\\.h\\(pp\\|h\\|xx\\|\\+\\+\\)$" . + "C++ header") . + ["hpp" my/autoinsert-yas-expand])) + (add-to-list + 'auto-insert-alist '(("\\.[1-9]\\.adoc$" . "AsciiDoc manpage") . + ["manpage.adoc" my/autoinsert-yas-expand])) + (add-to-list + 'auto-insert-alist '(("\\.user.js$" . "Userscript") . + ["user.js" my/autoinsert-yas-expand]))) + :hook (find-file . auto-insert)) ;; Better search. (use-package swiper :after (ivy) :functions (swiper) - - :bind - ("C-s" . 'swiper) - ) + :bind ("C-s" . 'swiper)) ;; Visualize and transform whitespace. (use-package whitespace :after (company) - :functions (my/whitespace-mode-enabled-p my/on-off-whitespace-before-company) + :functions (my/on-off-whitespace-before-company) :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 - (whitespace-line-column nil) ; Set to fill-column. + ;; Workaround to not show dots in popup menus. + (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 - (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)) + (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")))))) + (custom-set-faces ; else + '(whitespace-line ((t (:inherit whitespace-line + :background nil + :underline t)))))) - ;; Workaround to not show dots in popup menus. - (defun my/whitespace-mode-enabled-p () - (symbol-value 'whitespace-mode)) - (defvar-local my/ws-enabled nil) + ;; Workaround for + ;; , fixed + ;; in 28.1. + (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: - (defun my/on-off-whitespace-before-company(command) - (when (string= "show" command) - (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) + (defvar-local my/no-ws-cleanup nil + "Do not cleanup whitespace if t.") + (put 'my/no-ws-cleanup 'safe-local-variable #'booleanp) - (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: - (defadvice popup-create (before my/popup-suppress-whitespace-mode activate) - "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 , 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"))) - ) + (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)