Running flymake for python when files don't have .py extension

点点圈 提交于 2019-12-12 15:15:50

问题


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!


回答1:


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.




回答2:


AFAIU the ending is only important to auto-detect the needed buffer-mode. You may call the mode explicitly, resp. interactive M-x python-mode for any file.



来源:https://stackoverflow.com/questions/14082975/running-flymake-for-python-when-files-dont-have-py-extension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!