6
\$\begingroup\$

Problem

I use Emacs for Python development along with several linters. When I activate a Python virtual environment (venv) from within Emacs, I would like to set the linter binaries according to the following rules:

  1. If the venv has a particular linter installed, use it
  2. If the venv does not have a particular linter, use the one in a pre-specified, default venv
  3. If the linter does not exist in either the active venv or the default venv, set the linter binary to nil

For example, let's say I activate a venv called my_venv that has pylint installed, but no flake8. flake8 is however installed in my default linters venv. After activating my_venv, the linters that will be used are

  • pylint -- my_venv
  • flake8 -- linters

Purpose

The reason for implementing this is that I develop Python on multiple machines that share a single, version-controlled init.el file. I do not want to guarantee that I have the same Python binaries and venvs across these machines; this implementation helps decouple my Emacs setup from the Python venvs that are present on a machine.

Additional info

  • The code will go inside my init.el file
  • I use flycheck as the interface between Emacs and the linters
  • I use pyvenv for Python virtual environments in Emacs

Code

(defvar linter-execs '((flycheck-python-flake8-executable "bin/flake8")
                       (flycheck-python-pylint-executable "bin/pylint")
                       (flycheck-python-pycompile-executable "bin/python")))
(defvar default-linter-venv-path (concat (getenv "WORKON_HOME") "/linters/"))

(defun switch-linters ()
  "Switch linter executables to those in the current venv.

If the venv does not have any linter packages, then they will be
set to those in the `default-linter-venv-path` venv.  If these do
not exist, then no linter will be set."
  (dolist (exec linter-execs)
    (let ((venv-linter-bin    (concat pyvenv-virtual-env (nth 1 exec)))
          (default-linter-bin (concat default-linter-venv-path (nth 1 exec)))
          (flycheck-var       (nth 0 exec)))
      (cond ((file-exists-p venv-linter-bin)
             (set flycheck-var venv-linter-bin))
            ((file-exists-p default-linter-bin)
             (set flycheck-var default-linter-bin))
            (t (set flycheck-var nil))))))

(add-hook 'pyvenv-post-activate-hooks 'switch-linters)

Explanation

  • linter-execs is a list of two-element lists. The first element of an entry is the flycheck variable that contains the path of a linter binary. The second element is the relative path of the binary from within the venv.
  • The default linter venv is $WORKON_HOME/linters
  • switch-linters is the call-back function attached to pyvenv-post-activate-hooks
  • The conditional checks for the presence of the linter binary files, first in the current venv and next in the default venv. Failing these, it sets the binary to nil in the line (t (set flycheck-var nil))

Specific questions

  • Is this idiomatic elisp, or is it too "Pythonic"?
  • Is linter-execs the proper way to implement a list of tuples in elisp?
\$\endgroup\$
7
\$\begingroup\$
  1. It would be a good idea to write a docstring for the defvar'd variables like linter-execs. The explanations in the post would make an excellent start, for example:

    (defvar linter-execs '((flycheck-python-flake8-executable "bin/flake8")
                           (flycheck-python-pylint-executable "bin/pylint")
                           (flycheck-python-pycompile-executable "bin/python"))
      "The linter executables, as list of two-element lists. The
    first element of an entry is the flycheck variable that contains
    the path of a linter executable. The second element is the
    relative path of the executable from within the venv.")
    
  2. Writing (nth 0 exec) and (nth 1 exec) makes it hard to understand the meaning of these items of data. In Python you'd use tuple unpacking to give meaningful names to each element of the exec data structure, like this:

    flycheck_var, path = exec
    

    In Emacs Lisp you can use cl-destructuring-bind in a similar way:

    (dolist (exec linter-execs)
      (cl-destructuring-bind (flycheck-var path) exec
        (let ((venv-linter-bin    (concat pyvenv-virtual-env path))
          ;; etc
    

    But exec comes from a list that you are looping over, so you could use the cl-loop macro instead of dolist, and cl-loop has destructuring built in:

    (cl-loop for (flycheck-var path) in linter-execs do
      (let ((venv-linter-bin    (concat pyvenv-virtual-env path))
        ;; etc
    

    You'll need (require 'cl-macs) to use cl-destructuring-bind or cl-loop.

  3. The two directories pyvenv-virtual-env and default-linter-venv-path are treated in exactly the same way: first we do (let ((Y (concat X path)) and then (file-exists-p Y) and finally (set flycheck-var Y). This repetition could be factored out into a loop:

    (defun switch-linters ()
      "Switch linter executables to those in the current venv.
    
    If the venv does not have any linter packages, then they will be
    set to those in the `default-linter-venv-path` venv.  If these do
    not exist, then the linter will be set to nil."
      (cl-loop with dirs = (list pyvenv-virtual-env default-linter-venv-path)
               for (flycheck-var path) in linter-execs
               do (set flycheck-var
                       (cl-loop for directory in dirs
                                for executable = (concat directory path)
                                if (file-exists-p executable) return executable)))
    
  4. Instead of file-exists-p, you probably want to use file-executable-p.

  5. Looking for a file in a list of directories is built into Emacs as the function locate-file. Using this, we get:

    (defun switch-linters ()
      "Switch linter executables to those in the current venv.
    
    If the venv does not have any linter packages, then they will be
    set to those in the `default-linter-venv-path` venv.  If these do
    not exist, then the linter will be set to nil."
      (cl-loop with dirs = (list pyvenv-virtual-env default-linter-venv-path)
               for (flycheck-var path) in linter-execs
               do (set flycheck-var (locate-file path dirs nil 'file-executable-p))))
    
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.