【问题标题】:Emacs: How to enable a mode globally?Emacs:如何全局启用模式?
【发布时间】:2014-08-13 00:05:31
【问题描述】:

我只有一个来自addon 的模式,我想全局启用它。为了手动打开它,我需要输入 M-x highlight-indentation-mode。所以,下面是我尝试过的列表:(highlight-indentation-mode t)(highlight-indentation-mode 1)(setq highlight-indentation-mode t)。没有任何效果。接下来我发现可能是我需要全局启用一个模式,于是我开始用谷歌搜索它。我接下来尝试了什么:

(define-globalized-minor-mode global-highlight-indentation-mode highlight-indentation-mode
  (lambda () (setq highlight-indentation-mode t)))

不,这肯定不是我要找的机器人,它打开了变量,但模式仍然不起作用。

(define-globalized-minor-mode global-highlight-indentation-mode highlight-indentation-mode
  (lambda () highlight-indentation-mode t))

(define-globalized-minor-mode global-highlight-indentation-mode highlight-indentation-mode
  (highlight-indentation-mode t))

这两个只是破坏了我的 Emacs:当我尝试在 config 中使用这两个命令打开文件时,Emacs 写了一个错误,并拒绝打开文件。

UPD:基于 cmets 我也尝试过

(defun enable-highlight-indentation-mode ()
  (interactive)
    (highlight-indentation-mode t))

(define-globalized-minor-mode global-highlight-indentation-mode highlight-indentation-mode
  enable-highlight-indentation-mode)  

(global-highlight-indentation-mode t)

没有(interactive) 也一样。当我尝试用这个打开一个文件时,Emacs 拒绝打开,并写一个错误:

File mode specification error: (void-function nil)
c-font-lock-fontify-region: Symbol's function definition is void: nil

【问题讨论】:

  • 以下链接包含如何为半旧 Emacs 版本创建全局模式的示例:superuser.com/a/762495/206164 Emacs 的开发人员版本(尚未作为稳定的公开版本发布)有一个选项: :global t 本质上,对于半旧的 Emacs 版本,您只需要一个小函数来打开次要模式并在 globalized 中命名该函数。 . .陈述。您的第二个示例看起来非常接近,只是您没有将 highlight-indentation-mode t 括在括号中。
  • (defun turn-on-highlight-indentation-mode () (highlight-indentation-mode 1)) (define-globalized-minor-mode global-highlight-indentation-mode highlight-indentation-mode turn-on-highlight-indentation-mode)
  • @lawlist 我试过了。我在更新中写的结果。
  • 函数通常是无效的,因为缺少函数名后面的括号。 CORRECT(defun turn-on-highlight-indentation-mode () (interactive) (highlight-indentation-mode 1)) INCORRECT(defun turn-on-highlight-indentation-mode (interactive) (highlight-indentation-mode 1)) 不一定是这个新函数有错误——它可能是您的配置中缺少的另一个函数一些括号。
  • 是安东·约翰逊的那个吗?如果是这样,下面答案中的那个似乎可以正常工作。我添加了全局模式。

标签: emacs configuration mode minor-mode


【解决方案1】:
;;; highlight-indentation.el --- Minor modes for highlighting indentation
;; Author: Anton Johansson <anton.johansson@gmail.com> - http://antonj.se
;; Created: Dec 15 23:42:04 2010
;; Version: 0.6.0
;; URL: https://github.com/antonj/Highlight-Indentation-for-Emacs
;;
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation; either version 2 of
;; the License, or (at your option) any later version.
;;
;; This program is distributed in the hope that it will be
;; useful, but WITHOUT ANY WARRANTY; without even the implied
;; warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
;; PURPOSE. See the GNU General Public License for more details.
;;
;;; Commentary:
;; Customize `highlight-indentation-face', and
;; `highlight-indentation-current-column-face' to suit your theme.

;;; Code:

(defgroup highlight-indentation nil
  "Highlight Indentation"
  :prefix "highlight-indentation-"
  :group 'basic-faces)

(defface highlight-indentation-face
  ;; Fringe has non intrusive color in most color-themes
  '((t :inherit fringe))
  "Basic face for highlighting indentation guides."
  :group 'highlight-indentation)

(defcustom highlight-indentation-offset 4
  "Default indentation offset, used if no other can be found from
major mode. This value is always used by
`highlight-indentation-mode' if set buffer local. Set buffer
local with `highlight-indentation-set-offset'"
  :group 'highlight-indentation)

(defvar highlight-indentation-current-regex nil)

;;;###autoload
(define-minor-mode highlight-indentation-mode
  "Highlight indentation minor mode highlights indentation based
on spaces"
  :lighter " ||"
  (when highlight-indentation-current-regex ;; OFF
    (font-lock-remove-keywords nil `((,highlight-indentation-current-regex
                                      (1 'highlight-indentation-face)))))

  (set (make-local-variable 'highlight-indentation-current-regex) nil)

  (when highlight-indentation-mode ;; ON
    (when (not (local-variable-p 'highlight-indentation-offset))
      (set (make-local-variable 'highlight-indentation-offset)
           ;; Set indentation offset from highlight-indentation-offset if set, otherwise
           ;; according to major mode
           (cond ((and (eq major-mode 'python-mode) (boundp 'python-indent))
                  python-indent)
                 ((and (eq major-mode 'python-mode) (boundp 'py-indent-offset))
                  py-indent-offset)
                 ((and (eq major-mode 'python-mode) (boundp 'python-indent-offset))
                  python-indent-offset)
                 ((eq major-mode 'ruby-mode)
                  ruby-indent-level)
                 ((and (eq major-mode 'scala-mode) (boundp 'scala-indent:step))
                  scala-indent:step)
                 ((and (eq major-mode 'scala-mode) (boundp 'scala-mode-indent:step))
                  scala-mode-indent:step)
                 ((or (eq major-mode 'scss-mode) (eq major-mode 'css-mode))
                  css-indent-offset)
                 ((eq major-mode 'nxml-mode)
                  nxml-child-indent)
                 ((eq major-mode 'coffee-mode)
                  coffee-tab-width)
                 ((eq major-mode 'js-mode)
                  js-indent-level)
                 ((eq major-mode 'js2-mode)
                  js2-basic-offset)
                 ((local-variable-p 'c-basic-offset)
                  c-basic-offset)
                 (t
                  (default-value 'highlight-indentation-offset)))))
    (set (make-local-variable 'highlight-indentation-current-regex)
         (format "\\( \\) \\{%s\\}" (- highlight-indentation-offset 1)))
    (font-lock-add-keywords nil `((,highlight-indentation-current-regex
                                   (1 'highlight-indentation-face)))))
  (font-lock-fontify-buffer))

;;;###autoload
(defun highlight-indentation-set-offset (offset)
  "Set indentation offset localy in buffer, will prevent
highlight-indentation from trying to guess indentation offset
from major mode"
  (interactive
   (if (and current-prefix-arg (not (consp current-prefix-arg)))
       (list (prefix-numeric-value current-prefix-arg))
     (list (read-number "Indentation offset: "))))
  (set (make-local-variable 'highlight-indentation-offset) offset)
  (when highlight-indentation-mode
    (highlight-indentation-mode)))


;;;
;;; Copyright (C) Kresten Krab Thorup
;;; Available under Apache License, Version 2.
;;;
;;; This minor mode will highlight the indentation of the current line
;;; as a vertical bar (grey background color) aligned with the column of the
;;; first character of the current line.
;;;
(defface highlight-indentation-current-column-face
  ;; Fringe has non intrusive color in most color-themes
  '((t :inherit fringe))
  "Basic face for highlighting indentation guides."
  :group 'highlight-indentation)

;; used to hold the last regex we installed
(defvar highlight-indentation-current-column-regex nil)

;;;###autoload
(define-minor-mode
  highlight-indentation-current-column-mode
  "Hilight Indentation minor mode displays
a vertical bar corresponding to the indentation of the current line"
  :lighter " |"

  (when highlight-indentation-current-column-regex
    (font-lock-remove-keywords nil highlight-indentation-current-column-regex))

  (set (make-local-variable 'highlight-indentation-current-column-regex) nil)
  (cond (highlight-indentation-current-column-mode
          (add-hook 'post-command-hook 'highlight-indentation-current-column-post-command-hook nil t))
         (t
          (remove-hook 'post-command-hook 'highlight-indentation-current-column-post-command-hook t)

          (font-lock-fontify-buffer))))

(defun highlight-indentation-current-column-post-command-hook ()
  "This hook runs after every keystroke"
  (when highlight-indentation-current-column-regex
    (font-lock-remove-keywords nil highlight-indentation-current-column-regex))
  (let ((indent (save-excursion (back-to-indentation) (current-column))))
    (when (and highlight-indentation-current-column-mode
               (> indent 1))
      (let* ((re (format "^ \\{%d\\}\\( \\)" indent))
             (arg `((,re (1 'highlight-indentation-current-column-face prepend)))))
        (set (make-local-variable 'highlight-indentation-current-column-regex) arg)
        (font-lock-add-keywords nil arg))))
  (font-lock-fontify-buffer))

(defun turn-on-highlight-indentation-mode ()
(interactive)
  (highlight-indentation-mode 1))

(define-globalized-minor-mode global-highlight-indentation-mode
  highlight-indentation-mode turn-on-highlight-indentation-mode)

(global-highlight-indentation-mode 1)

(provide 'highlight-indentation)

【讨论】:

  • c-font-lock-fontify-region: Symbol's function definition is void: nil 再次。
  • 我没有看到使用此答案中的确切代码的错误,因此您可能需要将用户配置文件一分为二才能在其他地方找到错误。
  • 如果 Emacs 在完全精简的用户配置或零用户配置下无法正常工作,那么可能是时候备份您的整个安装(到安全位置)并尝试最最新版本的 Emacs —— 如果您能够应对挑战,甚至可以尝试开发人员构建快照。根据您的操作系统,甚至还有开发人员快照的预构建二进制文件。
  • Yagamy Light:运行 emacs -Q 以禁止所有用户和站点范围的 elisp,然后使用 M-x load-file 和提供的文件 lawlist 来验证默认行为。你运行的是哪个版本的 Emacs? (M-x emacs-version)
  • 啊哈。抱歉,我没有注意到你已经确定了这一点。 (我的错误;早期的 cmets 很清楚;只是其中有很多 :)。如果您有完整的调试堆栈跟踪,请务必向我们展示。
猜你喜欢
  • 1970-01-01
  • 2013-04-09
  • 1970-01-01
  • 2011-10-14
  • 1970-01-01
  • 1970-01-01
  • 2023-03-25
  • 1970-01-01
  • 2017-06-15
相关资源
最近更新 更多