Enabling certain emacs modes or functions * almost * always

There are a couple emacs functions, such as flyspell-mode, highlight-beyond-fill-column, or auto-fill-mode, I think so useful that I want them to be involved almost all the time. However, there are always conditions in which they do not make much sense.

highlight-beyond-fill-columnFor example, I usually want pretty much everything that I edit, but for reading things that others wrote, for example, in Gnus or when reading the built-in documentation, this is actually quite annoying.

Likewise auto-fill-modeincredibly handy when writing text. However, this is completely useless when programming.

For these reasons, I cannot simply enable features such as globally. Always letting them in manually is also not very practical, but you need to write hooks for each mode or application that I use in emacs, obviously not being able to cover all of them, and still ending up allowing me to use these functions manually.

What I think I'm looking for is a way to globally use some features, but selectively disable them again based on various conditions, such as major or minor modes, if the buffer is read-only or write-accessible, or depending from a buffer containing text or source code. I really understand that emacs may not be the last to answer, but at least for that I believe that everything will be fine with a hard-coded list of "programming modes" that I use regularly.

+3
source share
5 answers

Interesting idea. I recommend using the espect extension of your github .

+2
source

, , ... OK :

;; The function where you could put all your customization
(defun my-func ()
  (turn-on-auto-fill))

;; This is an example, customize it like you need it.
(defvar functions-to-call
  `(((c-mode c++-mode) ".h$" (my-func))
    ((cperl-mode perl-mode) nil (my-func)))
  "A list of triples, used for storing functions.
A triplet is composed of a symbol for the major mode (or a list of symbols),
a regular expression to match against the buffer file name,
and the functions to call when both the major mode and regular expr match.")

(defun call-mode-functions ()
  "call functions, based on major mode and buffer name regexp matching"
  (interactive)
  (let ((l functions-to-call))
      (while l
        (let* ((elt (car l))
               (modes (if (listp (car elt)) (car elt) (list (car elt))))
               (re (cadr elt))
               (fcts (caddr elt)))
          (when (and (member major-mode modes)
                     (or (null re)
                         (string-match re (buffer-file-name))))
            (while fcts
              (funcall (car fcts))
              (setq fcts (cdr fcts)))
            (setq l nil)))
        (setq l (cdr l)))))

(add-hook 'after-change-major-mode-hook 'call-mode-functions)

. , .

+5

, " ". " " , . , /, , / .

/ , , - major-mode-hook. :

(add-hook 'text-mode-hook 'auto-fill-mode)

, , :

(defun my-text-mode-hook ()
  "Stuff to do when `text-mode' is invoked."
  (auto-fill-mode 1))

(add-hook 'text-mode-hook 'my-text-mode-hook)

:

(defun my-text-mode-hook ()
  "Stuff to do when `text-mode' is invoked."
  ;; skip modes based on text-mode
  (when (eq major-mode 'text-mode)
      (auto-fill-mode 1))
  )

(add-hook 'text-mode-hook 'my-text-mode-hook)

major-mode-load-hook, :

(defun my-tnt-load-hook ()
  (defun my-tnt-im-mode-hook ()
    "Hook for TNT im-mode hook."
    (flyspell-mode 1)
    (setq fill-column (- (frame-width) 5)))

  (add-hook 'tnt-im-mode-hook 'my-tnt-im-mode-hook)
  (add-hook 'tnt-chat-mode-hook 'my-tnt-im-mode-hook))

(add-hook 'tnt-load-hook 'my-tnt-load-hook)

load-hook ( , ). load-hook, eval-after-load:

(defun my-view-mode-after-load-hook ()
  "Stuff to do after view mode loads."
  (defun my-view-mode-hook ()
    "Stuff to run in `view-mode'."
    (flyspell-mode 0))
  (add-hook 'view-mode-hook 'my-view-mode-hook)

  (define-key view-mode-map "b" 'View-scroll-page-backward)
  (define-key view-mode-map [(delete)] 'View-scroll-page-backward)
  (define-key view-mode-map "q" 'View-kill-and-leave)
  (define-key view-mode-map "Q" 'View-quit))

(eval-after-load 'view '(my-view-mode-after-load-hook))

load-hook, , mode-hook , my-mode-hook customize; .emacs, .

- , major-mode-hook, , , define-derived-mode. , , .

(defun replace-alist-mode (alist oldmode newmode)
  (dolist (aitem alist)
    (if (eq (cdr aitem) oldmode)
        (setcdr aitem newmode))))

(define-derived-mode hooked-foobar-mode foobar-mode "Foobar")
(replace-alist-mode auto-mode-alist 'foobar-mode 'hooked-foobar-mode)
(defun my-hooked-foobar-mode-hook ()
  "Hook to run when `hooked-foobar-mode' is called."
  (flyspell-mode 0))
(add-hook 'hooked-foobar-mode-hook 'my-hooked-foobar-mode-hook)

. , , , .

(global-font-lock-mode 1)
;; example of how to do it without a defun
(add-hook 'text-mode-hook (function
                           (lambda () ""
                             (interactive)
                             (font-lock-mode 0))))

, , , , .

+1

, [Jérôme Radix] [1] . after-change-major-mode-hook .

, , :

  ;; no `highlight-beyond-fill-column' for w3m and gnus
'((((:not ((:mode "^gnus") (:mode w3m-mode))))
   (lambda () (highlight-beyond-fill-column)))
  ;; `flyspell-mode` and `auto-fill-mode` for text-ish buffers
  (((:mode message-mode)
    (:mode org-mode)
    (:mode pod-mode)
    (:mode markdown-mode)
    (:name "\\.\\(txt\\|mkn\\)$"))
   (lambda ()
     (flyspell-mode)
     (auto-fill-mode)))
  ;; indenting with tabs for certain projects
  (((:name t :fun (lambda () (and (not eproject-root)
                                  (eproject-maybe-turn-on)))))
   (lambda () (setq indent-tabs-mode t)))

, , , , :

(add-hook 'after-change-major-mode-hook
          (lambda () (rafl:apply-buffer-settings rafl:buffer-settings)))

(defun rafl:apply-buffer-settings (settings)
  (dolist (setting rafl:buffer-settings)
    (let ((condition (car setting))
          (action (cadr setting)))
      (when (rafl:evaluate-buffer-condition condition)
        (funcall action)))))

, .

(defun rafl:evaluate-buffer-condition (con)
  (cond
   ((functionp con)
    (funcall con))
   ((listp con)
    (cond
     ((listp (car con))
      (reduce
       (lambda (a b) (or a b))
       (cons nil (mapcar #'rafl:evaluate-buffer-condition con))))
     (t
      (reduce
       (lambda (a b) (and a b))
       (cons
        t
        (let (ret)
          (while con
            (let ((k (pop con))
                  (v (pop con)))
              (push (cond
                     ((eq k :fun)
                      (funcall v))
                     ((eq k :not)
                      (when (not (listp v))
                        (error ":not requires a list"))
                      (not (rafl:evaluate-buffer-condition v)))
                     ((eq k :mode)
                      (if (stringp v)
                          (string-match-p v (symbol-name major-mode))
                        (eq v major-mode)))
                     ((eq k :name)
                      (cond
                       ((and (buffer-file-name) (stringp v))
                        (string-match-p v (buffer-file-name)))
                       ((buffer-file-name)
                        v)
                       (t
                        (not v))))
                     (t
                      (error "unknown cond")))
                    ret)))
          ret))))))
   (t
    (error "invalid condition"))))

, , , . .

1: emacs * *

+1

(require 'linum)
;(global-linum-mode t)
(add-hook 'find-file-hook (lambda ()
                            (if (not(equal major-mode 'term-mode))
                                (linum-mode nil))))
0

Source: https://habr.com/ru/post/1763950/


All Articles