Posts Tagged ‘emacs’

flymake for emacs + Python

Posted in General on November 6th, 2009 by slacy – Be the first to comment

flymake is an emacs mode that lets you “compile” (or syntax check) your code on the fly. For Python, this means that you can run several syntax checkers, like pep8 or pylint, or pyflakes (or all of the above) while you’re editing your code in realtime. To get this set up in emacs, do the following:

Create ~/bin/pychecker, which will be a simple script calling all your checkers.

#!/bin/bash
pylint --output-format=parseable "$1"
pyflakes "$1"
pep8 --repeat "$1"
true

Second, edit your .emacs and set up flymake to call this script for python files, and add some useful keyboard shortcuts for jumping between errors & warnings:

(require 'flymake)
(defun flymake-pylint-init ()
 (let* ((temp-file (flymake-init-create-temp-buffer-copy
                    'flymake-create-temp-inplace))
        (local-file (file-relative-name
                     temp-file
                     (file-name-directory buffer-file-name))))
   (list "~/bin/pychecker" (list local-file))))

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

(defun my-python-mode-hook ()
 (interactive)
 (flymake-mode)
 (local-set-key [S-up]
                (lambda ()
                  (interactive)
                  (flymake-goto-prev-error)
                  (message "%s"
                    (flymake-ler-text (caar (flymake-find-err-info
                    flymake-err-info
                    (flymake-current-line-no)))))))
 (local-set-key [S-down]
                (lambda ()
                  (interactive)
                  (flymake-goto-next-error)
                  (message "%s"
                    (flymake-ler-text (caar (flymake-find-err-info
                    flymake-err-info
                    (flymake-current-line-no)))))))
)
(add-hook 'python-mode-hook 'my-python-mode-hook)

Useful keybindings for emacs python-mode

Posted in General on November 4th, 2009 by slacy – Be the first to comment

I wrote up this keybinding the other day at work, and found that I missed it when I went home, so I wrote it again from scratch.

It allows you easily indent or undent the current region using M-left and M-right:

(global-set-key [M-left] '(lambda () (interactive)
  (save-excursion
    (py-shift-region-left (region-beginning) (region-end)))))

(global-set-key [M-right] '(lambda () (interactive)
  (save-excursion
     (py-shift-region-right (region-beginning) (region-end)))))