当文件没有.py扩展名时,为python运行flymake [英] Running flymake for python when files don't have .py extension

查看:181
本文介绍了当文件没有.py扩展名时,为python运行flymake的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是一个lisp的人,但我的主要脚本环境生活在emacs上,当文件上没有.py扩展名时,我需要一些帮助来让我的flymake / pyflakes运行。因为这里的一些脚本在我的工作没有.py扩展他们。



当我正在读取/编码具有.py扩展名的文件时,这很适用于pylint,pep8,pychecker等。

  ;;对于python 
(add-to-list'load-path〜/ .emacs.d / plugins / flymake)的flymake

(加载flymaket)
(defun flymake-pylint-init(& optional trigger-type)
(let *((temp-file(flymake-init-create-temp-buffer-copy
'flymake-create-temp -with-folder-structure))
(本地文件(文件相对名称
临时文件
(文件名目录缓冲区文件名)))
(options(当trigger-type(列表--trigger-typetrigger-type))))
(list〜/ .emacs.d / plugins / flymake / pyflymake.py(append options列表local-file)))))
(add-to-list'flymake-allowed-file-name-masks
'(\\.py\\flymake- pylint-init)))

(add-hook'find-file-hook'flymake-find-file-hook)

;;对于minibuffer的flymake帮助
(defun my-flymake-show-help()
(when(get-char-property(point)'flymake-overlay)
(let((help -char-property(point)'help-echo)))
(如果帮助(消息%s帮助)))))

(add-hook'post-command- hook'my-flymake-show-help)

我有尝试获取这个工作的初始化代码段,当那里不是.py扩展名。我用python-mode-hook包装了上面的代码,并将\.py\部分更改为\。* \。



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



BTW,我无法在无扩展文件上使用m-x flymake模式,它不会打开该小模式。



我很想得到任何想法,使其工作。谢谢!

解决方案

让我开始说下面的代码通常不是修复Emacs问题的方法。我做的是加载flymake然后踩在其中一个核心功能。由于写入flymake的方式,我无法找到一种方法来挂接一个函数,甚至使用建议。而如果flymake改变了这个功能,或者如何调用它将不再工作了。也就是说,它已经为我工作多年了:)



这是基础代码:

 (require'flymake)

(defun flymake-get-file-name-mode-and-masks(file-name)
返回相应的条目
(除非(stringp文件名)
(错误无效的文件名)
(let((fnm flymake -allowed-file-name-masks)
(mode-and-masks nil)
(matcher nil))
(while(和(不是模式和掩码)fnm)
(setq matcher(car(car fnm)))
(if(或(和(和(stringp matcher)(string-match matcher file-name))
(和(symbolp matcher)主要模式))
(setq mode-and-masks(cdr(car fnm))))
(setq fnm(cdr fnm)))
(flymake-log 3文件%s,init =%sfile-name(car mode-and-masks))
mode-and-masks))

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

 (add-to-list'flymake-allowed-file-name-masks'(\\.py\\flymake-pylint-init))

执行此操作:

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

你可以对Perl等做同样的事情。


I'm not a lisp guy at all, but my primary scripting environment lives on emacs and I need some help to get my flymake/pyflakes running when there is no .py extension on files. Because some of the scripts here at my work doesn't have .py extension on them.

This is pretty working with pylint, pep8, pychecker etc, when I'm reading/coding a file that has the .py extension.

;; 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)

I have tried to get this working init snippet when there is no .py extension. I wrapped the code above with python-mode-hook and changed the \.py\ section to something like \.*\.

However this is calling flymake-pylint-init function not only for python files. It calls it anything opened within emacs.

BTW, I'm not able to use m-x flymake-mode on no-extension files, it's not opening that minor mode.

I'd love to get any idea to get it working. thanks!

解决方案

Let me start by saying the code below is not generally the way to fix an Emacs problem. What I do is load flymake then stomp on one of the core functions. Because of the way flymake is written, I couldn't find a way to hook into a function or even use advice. And if flymake changes this function or how it's called it won't work anymore. That said, it has been working for me for years :)

This is the base code:

(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))

Then from your code above, instead of this:

(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))

You could do the same for Perl, etc.

这篇关于当文件没有.py扩展名时,为python运行flymake的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆