Thursday, September 9, 2010

Emacs and Pyflakes using a /tmp directory

One of the issues in how most instructions to use Pyflakes with Emacs is to create a temporary buffer xxx_flymake.py in the file you're editing. This issue can cause problems when trying to view other people's Python files in which you have no write permissions. The typical example code for editing your .emacs file is as follows:

(when (load "flymake" t)
  (defun flymake-pyflakes-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 "pyflakes" (list local-file))))
  (add-to-list 'flymake-allowed-file-name-masks
               '("\\.py\\'" flymake-pyflakes-init)))
(add-hook 'python-mode-hook 'flymake-mode)
We can change the code to use the Emacs Lisp make-temp-file:

(defun flymake-create-temp-in-system-tempdir (filename prefix)
  (make-temp-file (or prefix "flymake")))

(when (load "flymake" t)
  (defun flymake-pyflakes-init ()
    (let* ((temp-file (flymake-init-create-temp-buffer-copy
                       'flymake-create-temp-in-system-tempdir))
           (local-file (file-relative-name
                        temp-file
                        (file-name-directory buffer-file-name))))
      (list "pyflakes" (list local-file))))
  (add-to-list 'flymake-allowed-file-name-masks
               '("\\.py\\'" flymake-pyflakes-init)))
(add-hook 'python-mode-hook 'flymake-mode)

I give credit to http://github.com/purcell/emacs.d/blob/master/site-lisp/flymake-ruby/flymake-ruby.el for showing how it can be done for editing Ruby environments.

Another thing that can be done is to move all Emacs temporary files to the /tmp dir, or your own ~/.emacs.d/tmp directory:
(setq temporary-file-directory "~/.emacs/tmp")

(setq backup-directory-alist
      `((".*" . ,temporary-file-directory)))
(setq auto-save-file-name-transforms
      `((".*" ,temporary-file-directory t)))

1 comment:

  1. Thanks for this. I use Dropbox to back up my code and it's always needlessly sending these files back and forth. Now it isn't!

    ReplyDelete