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

2

3

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!

Advocaat answered 29/12, 2012 at 15:10 Comment(1)
Why not give the files the .py extension if you don't mind me asking? It would even be possible to temporarily copy the content to a .py buffer.Profusion
B
2

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.

Bowe answered 30/12, 2012 at 1:43 Comment(6)
hello sir, thanks for taking time. however this is not working for me, (it's not working even .py files) my config file looks like this; paste.ubuntu.com/1481689 I'm running Emacs 24.2.1 and I installed a forked version of flymake to get it working with tramp from here; github.com/rpatterson/flymake-python am I missing something? thanks again.Advocaat
Try either getting rid of the (when (load "flymake" t) line (and it's corresponding closing paren), or move the code I gave you inside there.Bowe
I keep getting Wrong type argument: stringp, python-mode error on minibuffer either way.Advocaat
btw, I figured out that the base code you provided here was already in my flymake.el file.Advocaat
No it's not. As I said above, I am overwriting one of the flymake.el functions with this one. Your problem is most likely that my function is being loaded first then overwritten by the original. Make sure this code comes last.Bowe
yes, you were right. overwriting was the problem, thank you so much for taking time, now I achieved what I want :)Advocaat
W
0

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.

Whitaker answered 29/12, 2012 at 18:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.