Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Personal Emacs configuration for detecting virtual environments, adding egg directories, and allowing extra paths to be added to the Jedi auto-completion server.
(add-hook 'hack-local-variables-hook 'run-local-vars-mode-hook)
(defun run-local-vars-mode-hook ()
"Run a hook for the major-mode after the local variables have been processed."
(run-hooks (intern (concat (symbol-name major-mode) "-local-vars-hook"))))
(defun detect_buffer_venv (buffer-name)
(let ((buffer-dir (file-name-directory buffer-name)))
(while (and (not (file-exists-p
(concat buffer-dir "bin/activate")))
buffer-dir)
(setq buffer-dir
(if (equal buffer-dir "/")
nil
(file-name-directory (directory-file-name buffer-dir))
))
)
;; return the buffer-dir (or nil)
buffer-dir
)
)
(defun detect_buffer_eggs_dirs (buffer-name)
(let (
(buffer-dir (file-name-directory buffer-name))
)
(while (and (not (file-exists-p
(concat buffer-dir "eggs")))
buffer-dir
)
(setq buffer-dir
(if (equal buffer-dir "/")
nil
(file-name-directory (directory-file-name buffer-dir))
)
)
)
(if buffer-dir
(directory-files (concat buffer-dir "eggs") t ".\.egg")
nil
)
)
)
(setq additional_paths nil)
(defun setup-jedi-extra-args ()
(let (
(venv (detect_buffer_venv buffer-file-name))
(egg-dirs (detect_buffer_eggs_dirs buffer-file-name))
)
(make-local-variable 'jedi:server-args)
(when venv (set 'jedi:server-args (list "--virtual-env" venv)))
(when egg-dirs
(dolist (egg egg-dirs)
(set 'jedi:server-args (append jedi:server-args (list "--sys-path" egg))))
)
)
(make-local-variable 'additional_paths)
(when additional_paths
(dolist (path additional_paths)
(set 'jedi:server-args (append jedi:server-args (list "--sys-path" path)))
))
)
(setq jedi:setup-keys t)
(add-hook 'python-mode-local-vars-hook 'setup-jedi-extra-args)
(add-hook 'python-mode-local-vars-hook 'jedi:setup)

thank you for this, i have just started using emacs as my main editor and this was bit which was missing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment