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)
(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)
"Call PROGRAM with ARGS and return output."
(with-output-to-string
@ -285,15 +291,21 @@ If DIR-LEFT is t, then move left, otherwise move right."
(setq count (1+ count)))
count))))
(defun page-number-mode (activate)
"Display of page number in mode line.
If ACTIVATE is non-nil, enable, disable otherwise. Interactively,
activate unless called with \\[universal-argument].\n\nThis adds
page-number/page-count to mode line. It will only display if
there is more than one page. A page is delimited by
page-delimiter.\n
(define-minor-mode page-number-mode
"Toggle page number display in the mode line (Page Number mode).
With a prefix argument ARG, enable Page Number mode if ARG is
positive, and disable it otherwise.
If called from Lisp, enable the mode if ARG is omitted or nil.
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."
(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
`("%e"
mode-line-front-space
@ -305,7 +317,7 @@ WARNING: this may slow down editing on big files."
mode-line-buffer-identification
" "
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)
" "
mode-line-modes
@ -331,6 +343,9 @@ WARNING: this may slow down editing on big files."
(set-buffer-modified-p nil)
(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 ()
"(Un)tabify, indent and delete trailing whitespace.
Tabify if `indent-tabs-mode' is true, otherwise use spaces.
@ -368,11 +383,12 @@ Hook function for skeletons."
(defun skeleton-next-position (&optional reverse)
"Move to next skeleton placeholder.
If REVERSE it t, move to previes placeholder."
If REVERSE it t, move to previous placeholder."
(interactive "P")
(let ((positions (mapcar 'marker-position skeleton-markers))
(comp (if reverse '< '<=))
pos prev)
pos
prev)
(when positions
(setq pos (pop positions))
(while (and pos (funcall comp pos (point)))
@ -490,6 +506,22 @@ This does not interfere with `subword-mode'."
(modify-syntax-entry ?_ "_")
(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 ()
"Paragraph at point is unwrapped on one single line."
(interactive)

View File

@ -97,10 +97,7 @@
;; There is no prog-mode-hook on Emacs<24.
(require 'functions) ; for `page-number-mode'
(add-hook
'prog-mode-hook
(lambda ()
(page-number-mode t)))
(add-hook 'prog-mode-hook 'page-number-mode)
(define-key mickey-minor-mode-map (kbd "<f5>") 'whitespace-mode)
(setq
@ -175,6 +172,7 @@
;; Compilation bindings and conveniences.
(setq compilation-ask-about-save nil)
(setq compilation-scroll-output 'first-error)
(with-eval-after-load 'compile
;; Making `compilation-directory' local only works with `recompile'
;; and if `compile' is never used. In such a scenario,
@ -231,7 +229,7 @@
(require 'functions)
;; Do not expand abbrevs in skeletons.
(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-<") (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 "M-TAB") 'semantic-complete-analyze-inline)
(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)
(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
;; variables.
(dolist (hook '(c-mode-hook c++-mode-hook))
(add-hook-and-eval
hook
(lambda ()
(c-set-style "ambrevar") ;; We override existing values.
(when (require 'company nil t)
(company-mode))
(cc-set-compiler))))
(add-hook-and-eval
hook
(lambda ()
(c-set-style "ambrevar"))) ;; We override existing values.
(when (require 'company nil t)
(add-hook-and-eval hook 'company-mode))
(add-hook-and-eval hook 'cc-set-compiler))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Skeletons

View File

@ -27,7 +27,7 @@
(require 'dired-x)
(setq dired-omit-files "^\\.")
(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'
(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
(concat (replace-regexp-in-string "\n$" "" (call-process-to-string "go" "env" "GOTOOLDIR")) "/guru"))))
(add-hook
'godoc-mode-hook
(lambda ()
(setq tab-width 8)))
(defun go-turn-on-gofmt-before-save ()
(add-hook 'before-save-hook #'gofmt-before-save nil t))
(add-hook-and-eval
'go-mode-hook
(lambda ()
(add-hook 'before-save-hook #'gofmt-before-save nil t)
(when (require 'go-eldoc nil t)
(go-eldoc-setup))
(go-set-compile-command)))
(add-hook-and-eval 'go-mode-hook 'go-turn-on-gofmt-before-save)
(when (require 'go-eldoc nil t)
(add-hook-and-eval 'go-mode-hook 'go-eldoc-setup))
(add-hook-and-eval 'go-mode-hook 'go-set-compile-command)
(defun godoc-setup ()
(setq tab-width 8))
(add-hook 'godoc-mode-hook 'godoc-setup)
(define-skeleton go-main
"Insert main function with basic includes."

View File

@ -68,17 +68,17 @@ by an {itemize} environment."
(dolist (block '("listing" "verbatim" "verbatim*"))
(add-to-list 'latex-block-body-alist `(,block nil '(delete-horizontal-space t) _)))
(add-hook-and-eval
'latex-mode-hook
(lambda ()
(set (make-local-variable 'tex-extension-list)
'("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-command) "pdflatex")
;; Need to reset the compiler because we changed tex-command.
(tex-set-compiler)
;; For some unknown reasons, `skeleton-end-hook' is set to nil in tex-mode.
(add-hook 'skeleton-end-hook 'skeleton-make-markers)
(turn-on-orgtbl)))
(defun latex-set-compiler ()
(set (make-local-variable 'tex-extension-list)
'("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-command) "pdflatex")
;; Need to reset the compiler because we changed tex-command, order matters.
(tex-set-compiler))
(add-hook-and-eval 'latex-mode-hook 'latex-set-compiler)
(add-hook-and-eval 'latex-mode-hook 'turn-on-orgtbl)
;; For some unknown reasons, `skeleton-end-hook' is set to nil in tex-mode.
(add-hook-and-eval 'latex-mode-hook 'turn-on-skeleton-markers)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Skeletons

View File

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

View File

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

View File

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

View File

@ -11,11 +11,7 @@
(setq compile-command
(concat interpreter " " (shell-quote-argument buffer-file-name)))))
(add-hook-and-eval
'python-mode-hook
(lambda ()
(set (make-local-variable 'compilation-scroll-output) t)
(python-set-compiler)))
(add-hook-and-eval 'python-mode-hook 'python-set-compiler)
;; Doc lookup. Requires the python.info file to be installed. See
;; 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)))
;; Convenient version.
(setq sh-shell-file (concat "/bin/" (symbol-name sh-shell)))
;; `buffer-file-name` seems to have a non-string type sometimes With `git
;; merge` and cause ediff to fail. Let's protect it.
;; Sometimes with `git merge` it seems that the `buffer-file-name' is not a
;; string. We safe-guard that case.
(when (stringp buffer-file-name)
(setq compile-command (concat sh-shell-file " " (shell-quote-argument buffer-file-name)))))
(add-hook-and-eval
'sh-mode-hook
(lambda ()
(setq sh-indent-for-case-label 0)
(setq sh-indent-for-case-alt '+)
(set (make-local-variable 'defun-prompt-regexp)
(concat "^\\(function[ \t]\\|[[:alnum:]_]+[ \t]+()[ \t]+\\)"))
(sh-set-compiler)))
(defun sh-set-indent-rules ()
(setq sh-indent-for-case-label 0)
(setq sh-indent-for-case-alt '+))
(defun sh-set-prompt ()
(set (make-local-variable 'defun-prompt-regexp)
(concat "^\\(function[ \t]\\|[[:alnum:]_]+[ \t]+()[ \t]+\\)")))
(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"))
(local-unset-key key))
(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
"The file that should be compiled. Useful for modular documents.")
@ -109,6 +109,13 @@ This does not interfere with `subword-mode'."
(modify-syntax-entry ?\\ "\\")
(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
@ -119,18 +126,17 @@ This does not interfere with `subword-mode'."
;; default options.
(setq-default tex-start-commands nil)
(add-hook-and-eval
'tex-mode-hook
(lambda ()
;; `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.
(setq indent-tabs-mode t)
(set (make-local-variable 'compilation-scroll-output) t)
(set (make-local-variable 'paragraph-start) "
")
;; (set (make-local-variable 'use-hard-newlines) t)
(tex-set-compiler)))
(defun tex-make-newline-paragraph ()
;; (set (make-local-variable 'use-hard-newlines) t)
(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.
(add-hook-and-eval 'tex-mode-hook 'turn-on-indent-tabs)
(add-hook-and-eval 'tex-mode-hook 'tex-make-newline-paragraph)
(add-hook-and-eval 'tex-mode-hook 'tex-set-compiler)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Skeletons

View File

@ -12,14 +12,15 @@
(hack-local-variables)
(texinfo-multiple-files-update (or tex-master-file buffer-file-name) t 8))
(add-hook-and-eval
'texinfo-mode-hook
(lambda ()
(setq fill-column 80) ;; Is it needed?
(set (make-local-variable 'tex-extension-list)
'("aux" "cp" "cps" "fn" "ky" "log" "pg" "toc" "tp" "vr" "vrs"))
(set (make-local-variable 'tex-start-options) nil)
(set (make-local-variable 'tex-command) "texi2pdf -b")
(tex-set-compiler)))
(defun texinfo-set-compiler ()
(set (make-local-variable 'tex-extension-list)
'("aux" "cp" "cps" "fn" "ky" "log" "pg" "toc" "tp" "vr" "vrs"))
(set (make-local-variable 'tex-start-options) nil)
(set (make-local-variable 'tex-command) "texi2pdf -b")
(tex-set-compiler))
(add-hook-and-eval 'texinfo-mode-hook 'texinfo-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)