我根本不是一个 lisp 人,但我的主要脚本环境位于 emacs 上,当文件上没有 .py 扩展名时,我需要一些帮助来运行我的 Flymake/pyflakes。因为我工作中的一些脚本没有 .py 扩展名。
当我读取/编码扩展名为 .py 的文件时,这与 pylint、pep8、pychecker 等配合得很好。
;; flymake for python
(add-to-list 'load-path "~/.emacs.d/plugins/flymake")
(when (load "flymake" t)
(defun flymake-pylint-init (&optional trigger-type)
(let* ((temp-file (flymake-init-create-temp-buffer-copy
'flymake-create-temp-with-folder-structure))
(local-file (file-relative-name
temp-file
(file-name-directory buffer-file-name)))
(options (when trigger-type (list "--trigger-type" trigger-type))))
(list "~/.emacs.d/plugins/flymake/pyflymake.py" (append options (list local-file)))))
(add-to-list 'flymake-allowed-file-name-masks
'("\\.py\\'" flymake-pylint-init)))
(add-hook 'find-file-hook 'flymake-find-file-hook)
;; flymake help on minibuffer
(defun my-flymake-show-help ()
(when (get-char-property (point) 'flymake-overlay)
(let ((help (get-char-property (point) 'help-echo)))
(if help (message "%s" help)))))
(add-hook 'post-command-hook 'my-flymake-show-help)
当没有 .py 扩展名时,我尝试让这个工作初始化片段。我用 python-mode-hook 包装了上面的代码,并将 \.py\ 部分更改为类似 \.*\ 的内容。
然而,这不仅针对 python 文件调用 Flymake-pylint-init 函数。它称其为在 emacs 中打开的任何内容。
顺便说一句,我无法在无扩展名文件上使用 m-x Flymake-mode,它不会打开该次要模式。
我很想知道让它发挥作用的任何想法。谢谢!
首先我要说的是,下面的代码通常不是解决 Emacs 问题的方法。我所做的就是加载 Flymake,然后踩踏核心功能之一。由于 Flymake 的编写方式,我找不到挂钩函数甚至使用建议的方法。如果 Flymake 改变了这个函数或者它的调用方式,它就不再起作用了。也就是说,它已经为我工作了很多年了:)
这是基本代码:
(require 'flymake)
(defun flymake-get-file-name-mode-and-masks (file-name)
"Return the corresponding entry from `flymake-allowed-file-name-masks'."
(unless (stringp file-name)
(error "Invalid file-name"))
(let ((fnm flymake-allowed-file-name-masks)
(mode-and-masks nil)
(matcher nil))
(while (and (not mode-and-masks) fnm)
(setq matcher (car (car fnm)))
(if (or (and (stringp matcher) (string-match matcher file-name))
(and (symbolp matcher) (equal matcher major-mode)))
(setq mode-and-masks (cdr (car fnm))))
(setq fnm (cdr fnm)))
(flymake-log 3 "file %s, init=%s" file-name (car mode-and-masks))
mode-and-masks))
然后从上面的代码中,而不是这样:
(add-to-list 'flymake-allowed-file-name-masks '("\\.py\\'" flymake-pylint-init))
do this:
(add-to-list 'flymake-allowed-file-name-masks '(python-mode flymake-pylint-init))
你可以对 Perl 等做同样的事情。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)