当文件没有.py扩展名时运行flymakeforpython

2024-06-13 08:55:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我根本不是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扩展名的情况下获得这个有效的init片段。我用python模式钩子包装了上面的代码,并将\.py\部分改为类似\.*\。在

然而,这不仅仅是为python文件调用flymake pylint init函数。它称之为emacs中打开的任何东西。在

顺便说一句,我不能在没有扩展文件的情况下使用m-x flymake模式,它不能打开次要模式。在

我很想知道能不能用。谢谢!在


Tags: 文件namepyaddinittypehelphook
2条回答

AFAIU结束只对自动检测所需的缓冲模式很重要。 您可以显式地调用该模式。任何文件的交互式M-x python模式。在

首先让我说下面的代码通常不是解决Emacs问题的方法。我要做的是加载flymake,然后踩踏其中一个核心功能。由于flymake的编写方式,我无法找到一种方法来钩住函数,甚至无法使用建议。如果flymake改变了这个函数或者它的调用方式,它就不能再工作了。也就是说,它已经为我工作多年了:)

这是基本代码:

^{1}$

然后从上面的代码中,而不是这样:

^{pr2}$

这样做:

(add-to-list 'flymake-allowed-file-name-masks '(python-mode flymake-pylint-init))

您可以对Perl等执行相同的操作

相关问题 更多 >