Emacs: Name functions instead of using lambdas in hooks

This is good practice as it allows for using `remove-hook'.
master
Pierre Neidhardt 2017-05-28 12:03:28 +02:00
parent a372221cd8
commit 9ebd726a4a
13 changed files with 130 additions and 99 deletions

View File

@ -29,6 +29,12 @@ while `run-mode-hooks' is running."
(add-hook hook function) (add-hook hook function)
(funcall function)) (funcall function))
(defun beginning-of-next-defun ()
"Move forward to the beginning of a defun.
Useful when bound to a key opposed to `beginning-of-defun'."
(beginning-of-defun -1))
(define-key mickey-minor-mode-map (kbd "C-M-e") 'beginning-of-next-defun)
(defun call-process-to-string (program &rest args) (defun call-process-to-string (program &rest args)
"Call PROGRAM with ARGS and return output." "Call PROGRAM with ARGS and return output."
(with-output-to-string (with-output-to-string
@ -285,15 +291,21 @@ If DIR-LEFT is t, then move left, otherwise move right."
(setq count (1+ count))) (setq count (1+ count)))
count)))) count))))
(defun page-number-mode (activate) (define-minor-mode page-number-mode
"Display of page number in mode line. "Toggle page number display in the mode line (Page Number mode).
If ACTIVATE is non-nil, enable, disable otherwise. Interactively, With a prefix argument ARG, enable Page Number mode if ARG is
activate unless called with \\[universal-argument].\n\nThis adds positive, and disable it otherwise.
page-number/page-count to mode line. It will only display if
there is more than one page. A page is delimited by If called from Lisp, enable the mode if ARG is omitted or nil.
page-delimiter.\n
It will only display if there is more than one page. A page is
delimited by page-delimiter.
WARNING: this may slow down editing on big files." WARNING: this may slow down editing on big files."
(interactive (list (not (equal current-prefix-arg '(4))))) :global t :group 'mode-line
;; TODO: Don't setq the mode-line, insert instead. See column-number-mode.
;; TODO: Move page-related functions to a separate file.
;; TODO: Should we use a lighter if it's running in the background? Maybe not, just make sure we always print like column-number-mode.
(setq mode-line-format (setq mode-line-format
`("%e" `("%e"
mode-line-front-space mode-line-front-space
@ -305,7 +317,7 @@ WARNING: this may slow down editing on big files."
mode-line-buffer-identification mode-line-buffer-identification
" " " "
mode-line-position mode-line-position
,(when activate '(:eval (when (> (page-count) 1) (format "%d/%d" (page-number) (page-count))))) ,(when page-number-mode '(:eval (when (> (page-count) 1) (format "%d/%d" (page-number) (page-count)))))
(vc-mode vc-mode) (vc-mode vc-mode)
" " " "
mode-line-modes mode-line-modes
@ -331,6 +343,9 @@ WARNING: this may slow down editing on big files."
(set-buffer-modified-p nil) (set-buffer-modified-p nil)
(message "File '%s' successfully renamed to '%s'" name (file-name-nondirectory new-name)))))))) (message "File '%s' successfully renamed to '%s'" name (file-name-nondirectory new-name))))))))
(defun reset-fill-column ()
"Reset `fill-column' to its default value."
(setq fill-column (default-value 'fill-column)))
(defun sanitize () (defun sanitize ()
"(Un)tabify, indent and delete trailing whitespace. "(Un)tabify, indent and delete trailing whitespace.
Tabify if `indent-tabs-mode' is true, otherwise use spaces. Tabify if `indent-tabs-mode' is true, otherwise use spaces.
@ -368,11 +383,12 @@ Hook function for skeletons."
(defun skeleton-next-position (&optional reverse) (defun skeleton-next-position (&optional reverse)
"Move to next skeleton placeholder. "Move to next skeleton placeholder.
If REVERSE it t, move to previes placeholder." If REVERSE it t, move to previous placeholder."
(interactive "P") (interactive "P")
(let ((positions (mapcar 'marker-position skeleton-markers)) (let ((positions (mapcar 'marker-position skeleton-markers))
(comp (if reverse '< '<=)) (comp (if reverse '< '<=))
pos prev) pos
prev)
(when positions (when positions
(setq pos (pop positions)) (setq pos (pop positions))
(while (and pos (funcall comp pos (point))) (while (and pos (funcall comp pos (point)))
@ -490,6 +506,22 @@ This does not interfere with `subword-mode'."
(modify-syntax-entry ?_ "_") (modify-syntax-entry ?_ "_")
(message "_ is a word delimiter"))) (message "_ is a word delimiter")))
(defun turn-off-indent-tabs ()
"Unconditionally turn off tab indentation."
(setq indent-tabs-mode nil))
(defun turn-on-indent-tabs ()
"Unconditionally turn on tab indentation."
(setq indent-tabs-mode t))
(defun turn-on-skeleton-markers ()
"Allow skeletons to make markers to ease field navigation."
(add-hook 'skeleton-end-hook 'skeleton-make-markers))
(defun turn-off-linum ()
"Unconditionally turn off Linum mode."
(linum-mode 0))
(defun unfill-paragraph () (defun unfill-paragraph ()
"Paragraph at point is unwrapped on one single line." "Paragraph at point is unwrapped on one single line."
(interactive) (interactive)

View File

@ -97,10 +97,7 @@
;; There is no prog-mode-hook on Emacs<24. ;; There is no prog-mode-hook on Emacs<24.
(require 'functions) ; for `page-number-mode' (require 'functions) ; for `page-number-mode'
(add-hook (add-hook 'prog-mode-hook 'page-number-mode)
'prog-mode-hook
(lambda ()
(page-number-mode t)))
(define-key mickey-minor-mode-map (kbd "<f5>") 'whitespace-mode) (define-key mickey-minor-mode-map (kbd "<f5>") 'whitespace-mode)
(setq (setq
@ -175,6 +172,7 @@
;; Compilation bindings and conveniences. ;; Compilation bindings and conveniences.
(setq compilation-ask-about-save nil) (setq compilation-ask-about-save nil)
(setq compilation-scroll-output 'first-error)
(with-eval-after-load 'compile (with-eval-after-load 'compile
;; Making `compilation-directory' local only works with `recompile' ;; Making `compilation-directory' local only works with `recompile'
;; and if `compile' is never used. In such a scenario, ;; and if `compile' is never used. In such a scenario,
@ -231,7 +229,7 @@
(require 'functions) (require 'functions)
;; Do not expand abbrevs in skeletons. ;; Do not expand abbrevs in skeletons.
(setq-default skeleton-further-elements '((abbrev-mode nil))) (setq-default skeleton-further-elements '((abbrev-mode nil)))
(add-hook 'skeleton-end-hook 'skeleton-make-markers) (turn-on-skeleton-markers)
(define-key mickey-minor-mode-map (kbd "C->") 'skeleton-next-position) (define-key mickey-minor-mode-map (kbd "C->") 'skeleton-next-position)
(define-key mickey-minor-mode-map (kbd "C-<") (lambda () (interactive) (skeleton-next-position t))) (define-key mickey-minor-mode-map (kbd "C-<") (lambda () (interactive) (skeleton-next-position t)))

View File

@ -8,7 +8,6 @@
(local-set-key (kbd "C-c C-d") 'semantic-ia-show-summary) (local-set-key (kbd "C-c C-d") 'semantic-ia-show-summary)
(local-set-key (kbd "M-TAB") 'semantic-complete-analyze-inline) (local-set-key (kbd "M-TAB") 'semantic-complete-analyze-inline)
(local-set-key (kbd "C-c o") 'ff-find-other-file) (local-set-key (kbd "C-c o") 'ff-find-other-file)
(local-set-key (kbd "C-M-e") (lambda () (interactive) (c-beginning-of-defun -1)))
(when (fboundp 'company-mode) (when (fboundp 'company-mode)
(local-set-key (kbd "M-TAB") (if (require 'company nil t) 'helm-company 'company-complete))) (local-set-key (kbd "M-TAB") (if (require 'company nil t) 'helm-company 'company-complete)))
@ -124,13 +123,13 @@ restored."
;; time at the end of the initialization. No big deal since we only set some ;; time at the end of the initialization. No big deal since we only set some
;; variables. ;; variables.
(dolist (hook '(c-mode-hook c++-mode-hook)) (dolist (hook '(c-mode-hook c++-mode-hook))
(add-hook-and-eval (add-hook-and-eval
hook hook
(lambda () (lambda ()
(c-set-style "ambrevar") ;; We override existing values. (c-set-style "ambrevar"))) ;; We override existing values.
(when (require 'company nil t) (when (require 'company nil t)
(company-mode)) (add-hook-and-eval hook 'company-mode))
(cc-set-compiler)))) (add-hook-and-eval hook 'cc-set-compiler))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Skeletons ;; Skeletons

View File

@ -27,7 +27,7 @@
(require 'dired-x) (require 'dired-x)
(setq dired-omit-files "^\\.") (setq dired-omit-files "^\\.")
(dired-omit-mode) (dired-omit-mode)
(add-hook 'dired-mode-hook (lambda () (dired-omit-mode 1))) (add-hook 'dired-mode-hook 'dired-omit-mode)
(require 'tool-pdf) ; for `pdf-viewer' (require 'tool-pdf) ; for `pdf-viewer'
(setq dired-guess-shell-alist-user (setq dired-guess-shell-alist-user

View File

@ -81,18 +81,20 @@ Note that the -cover test flag is left out since it shifts line numbers."
(setq go-guru-command (setq go-guru-command
(concat (replace-regexp-in-string "\n$" "" (call-process-to-string "go" "env" "GOTOOLDIR")) "/guru")))) (concat (replace-regexp-in-string "\n$" "" (call-process-to-string "go" "env" "GOTOOLDIR")) "/guru"))))
(add-hook (defun go-turn-on-gofmt-before-save ()
'godoc-mode-hook (add-hook 'before-save-hook #'gofmt-before-save nil t))
(lambda ()
(setq tab-width 8)))
(add-hook-and-eval (add-hook-and-eval 'go-mode-hook 'go-turn-on-gofmt-before-save)
'go-mode-hook
(lambda () (when (require 'go-eldoc nil t)
(add-hook 'before-save-hook #'gofmt-before-save nil t) (add-hook-and-eval 'go-mode-hook 'go-eldoc-setup))
(when (require 'go-eldoc nil t)
(go-eldoc-setup)) (add-hook-and-eval 'go-mode-hook 'go-set-compile-command)
(go-set-compile-command)))
(defun godoc-setup ()
(setq tab-width 8))
(add-hook 'godoc-mode-hook 'godoc-setup)
(define-skeleton go-main (define-skeleton go-main
"Insert main function with basic includes." "Insert main function with basic includes."

View File

@ -68,17 +68,17 @@ by an {itemize} environment."
(dolist (block '("listing" "verbatim" "verbatim*")) (dolist (block '("listing" "verbatim" "verbatim*"))
(add-to-list 'latex-block-body-alist `(,block nil '(delete-horizontal-space t) _))) (add-to-list 'latex-block-body-alist `(,block nil '(delete-horizontal-space t) _)))
(add-hook-and-eval (defun latex-set-compiler ()
'latex-mode-hook (set (make-local-variable 'tex-extension-list)
(lambda () '("aux" "bbl" "blg" "glg" "glo" "gls" "idx" "ilg" "ind" "lof" "log" "maf" "mt" "mtc" "nav" "out" "snm" "synctex" "synctex.gz" "tns" "toc" "xdy"))
(set (make-local-variable 'tex-extension-list) (set (make-local-variable 'tex-command) "pdflatex")
'("aux" "bbl" "blg" "glg" "glo" "gls" "idx" "ilg" "ind" "lof" "log" "maf" "mt" "mtc" "nav" "out" "snm" "synctex" "synctex.gz" "tns" "toc" "xdy")) ;; Need to reset the compiler because we changed tex-command, order matters.
(set (make-local-variable 'tex-command) "pdflatex") (tex-set-compiler))
;; Need to reset the compiler because we changed tex-command.
(tex-set-compiler) (add-hook-and-eval 'latex-mode-hook 'latex-set-compiler)
;; For some unknown reasons, `skeleton-end-hook' is set to nil in tex-mode. (add-hook-and-eval 'latex-mode-hook 'turn-on-orgtbl)
(add-hook 'skeleton-end-hook 'skeleton-make-markers) ;; For some unknown reasons, `skeleton-end-hook' is set to nil in tex-mode.
(turn-on-orgtbl))) (add-hook-and-eval 'latex-mode-hook 'turn-on-skeleton-markers)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Skeletons ;; Skeletons

View File

@ -2,9 +2,9 @@
(defvaralias 'lua-indent-level 'tab-width) (defvaralias 'lua-indent-level 'tab-width)
(add-hook-and-eval (defun lua-set-compiler ()
'lua-mode-hook (setq compile-command (concat lua-default-application " " (shell-quote-argument buffer-file-name))))
(lambda ()
(setq compile-command (concat lua-default-application " " (shell-quote-argument buffer-file-name))))) (add-hook-and-eval 'lua-mode-hook 'lua-set-compiler)
(provide 'mode-lua) (provide 'mode-lua)

View File

@ -17,11 +17,8 @@
("ArchLinux" "https://wiki.archlinux.org/" "Ambrevar" "" "Mutt") ("ArchLinux" "https://wiki.archlinux.org/" "Ambrevar" "" "Mutt")
("WikEmacs" "https://wikemacs.org/wiki/" "Ambrevar" "" "Main Page"))) ("WikEmacs" "https://wikemacs.org/wiki/" "Ambrevar" "" "Main Page")))
(add-hook-and-eval (add-hook-and-eval 'mediawiki-mode-hook 'visual-line-mode)
'mediawiki-mode-hook (add-hook-and-eval 'mediawiki-mode-hook 'turn-off-auto-fill)
(lambda ()
(visual-line-mode 1)
(turn-off-auto-fill)))
;; Skeletons ;; Skeletons

View File

@ -26,11 +26,8 @@
(setcdr (assoc "\\.pdf\\'" org-file-apps) (setcdr (assoc "\\.pdf\\'" org-file-apps)
(concat pdf-viewer " " (mapconcat 'identity pdf-viewer-args " ")))) (concat pdf-viewer " " (mapconcat 'identity pdf-viewer-args " "))))
(add-hook-and-eval (add-hook-and-eval 'org-mode-hook 'turn-off-linum)
'org-mode-hook (add-hook-and-eval 'org-mode-hook 'turn-off-indent-tabs)
(lambda () (add-hook-and-eval 'org-mode-hook 'turn-off-auto-fill)
(linum-mode 0)
(setq indent-tabs-mode nil)
(auto-fill-mode -1)))
(provide 'mode-org) (provide 'mode-org)

View File

@ -11,11 +11,7 @@
(setq compile-command (setq compile-command
(concat interpreter " " (shell-quote-argument buffer-file-name))))) (concat interpreter " " (shell-quote-argument buffer-file-name)))))
(add-hook-and-eval (add-hook-and-eval 'python-mode-hook 'python-set-compiler)
'python-mode-hook
(lambda ()
(set (make-local-variable 'compilation-scroll-output) t)
(python-set-compiler)))
;; Doc lookup. Requires the python.info file to be installed. See ;; Doc lookup. Requires the python.info file to be installed. See
;; https://bitbucket.org/jonwaltman/pydoc-info/. ;; https://bitbucket.org/jonwaltman/pydoc-info/.

View File

@ -44,19 +44,22 @@ The advantages of this function over the vanilla code are:
; (setq sh-shell-file (executable-find (symbol-name sh-shell))) ; (setq sh-shell-file (executable-find (symbol-name sh-shell)))
;; Convenient version. ;; Convenient version.
(setq sh-shell-file (concat "/bin/" (symbol-name sh-shell))) (setq sh-shell-file (concat "/bin/" (symbol-name sh-shell)))
;; `buffer-file-name` seems to have a non-string type sometimes With `git ;; Sometimes with `git merge` it seems that the `buffer-file-name' is not a
;; merge` and cause ediff to fail. Let's protect it. ;; string. We safe-guard that case.
(when (stringp buffer-file-name) (when (stringp buffer-file-name)
(setq compile-command (concat sh-shell-file " " (shell-quote-argument buffer-file-name))))) (setq compile-command (concat sh-shell-file " " (shell-quote-argument buffer-file-name)))))
(add-hook-and-eval (defun sh-set-indent-rules ()
'sh-mode-hook (setq sh-indent-for-case-label 0)
(lambda () (setq sh-indent-for-case-alt '+))
(setq sh-indent-for-case-label 0)
(setq sh-indent-for-case-alt '+) (defun sh-set-prompt ()
(set (make-local-variable 'defun-prompt-regexp) (set (make-local-variable 'defun-prompt-regexp)
(concat "^\\(function[ \t]\\|[[:alnum:]_]+[ \t]+()[ \t]+\\)")) (concat "^\\(function[ \t]\\|[[:alnum:]_]+[ \t]+()[ \t]+\\)")))
(sh-set-compiler)))
(add-hook-and-eval 'sh-mode-hook 'sh-set-indent-rules)
(add-hook-and-eval 'sh-mode-hook 'sh-set-prompt)
(add-hook-and-eval 'sh-mode-hook 'sh-set-compiler)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

View File

@ -21,7 +21,7 @@
(dolist (key '("\C-c\C-f" "\C-c\C-b")) (dolist (key '("\C-c\C-f" "\C-c\C-b"))
(local-unset-key key)) (local-unset-key key))
(local-set-key (kbd "<f9>") 'tex-pdf-view) (local-set-key (kbd "<f9>") 'tex-pdf-view)
(local-set-key (kbd "<f10>") (lambda () (interactive) (progn (compile compile-command) (sit-for tex-compilation-delay) (delete-windows-on "*compilation*")))) (local-set-key (kbd "<f10>") 'tex-compile)
(defvar-local tex-masterfile nil (defvar-local tex-masterfile nil
"The file that should be compiled. Useful for modular documents.") "The file that should be compiled. Useful for modular documents.")
@ -109,6 +109,13 @@ This does not interfere with `subword-mode'."
(modify-syntax-entry ?\\ "\\") (modify-syntax-entry ?\\ "\\")
(message "\\ is a an escape character"))) (message "\\ is a an escape character")))
(defun tex-compile ()
"Compile the TeX document."
(interactive)
(compile compile-command)
(sit-for tex-compilation-delay)
(delete-windows-on "*compilation*"))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; TeX setup ;; TeX setup
@ -119,18 +126,17 @@ This does not interfere with `subword-mode'."
;; default options. ;; default options.
(setq-default tex-start-commands nil) (setq-default tex-start-commands nil)
(add-hook-and-eval (defun tex-make-newline-paragraph ()
'tex-mode-hook ;; (set (make-local-variable 'use-hard-newlines) t)
(lambda () (set (make-local-variable 'paragraph-start) "
;; `tex-mode' sets `indent-tabs-mode' to nil, invoking the following "))
;; argument: "TABs in verbatim environments don't do what you think." Not
;; sure how relevant this bad comment is. We revert it. ;; `tex-mode' sets `indent-tabs-mode' to nil, invoking the following
(setq indent-tabs-mode t) ;; argument: "TABs in verbatim environments don't do what you think." Not
(set (make-local-variable 'compilation-scroll-output) t) ;; sure how relevant this bad comment is. We revert it.
(set (make-local-variable 'paragraph-start) " (add-hook-and-eval 'tex-mode-hook 'turn-on-indent-tabs)
") (add-hook-and-eval 'tex-mode-hook 'tex-make-newline-paragraph)
;; (set (make-local-variable 'use-hard-newlines) t) (add-hook-and-eval 'tex-mode-hook 'tex-set-compiler)
(tex-set-compiler)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Skeletons ;; Skeletons

View File

@ -12,14 +12,15 @@
(hack-local-variables) (hack-local-variables)
(texinfo-multiple-files-update (or tex-master-file buffer-file-name) t 8)) (texinfo-multiple-files-update (or tex-master-file buffer-file-name) t 8))
(add-hook-and-eval (defun texinfo-set-compiler ()
'texinfo-mode-hook (set (make-local-variable 'tex-extension-list)
(lambda () '("aux" "cp" "cps" "fn" "ky" "log" "pg" "toc" "tp" "vr" "vrs"))
(setq fill-column 80) ;; Is it needed? (set (make-local-variable 'tex-start-options) nil)
(set (make-local-variable 'tex-extension-list) (set (make-local-variable 'tex-command) "texi2pdf -b")
'("aux" "cp" "cps" "fn" "ky" "log" "pg" "toc" "tp" "vr" "vrs")) (tex-set-compiler))
(set (make-local-variable 'tex-start-options) nil)
(set (make-local-variable 'tex-command) "texi2pdf -b") (add-hook-and-eval 'texinfo-mode-hook 'texinfo-set-compiler)
(tex-set-compiler))) ;; For some reason, Texinfo-mode forces the fill-column to 70...
(add-hook-and-eval 'texinfo-mode-hook 'reset-fill-column)
(provide 'mode-texinfo) (provide 'mode-texinfo)