summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lisp/ravi-init-python.el218
1 files changed, 74 insertions, 144 deletions
diff --git a/lisp/ravi-init-python.el b/lisp/ravi-init-python.el
index 36bc489..154cfdc 100644
--- a/lisp/ravi-init-python.el
+++ b/lisp/ravi-init-python.el
@@ -29,150 +29,80 @@
:mode ("\\.py\\'" . python-mode)
:commands python-shell-switch-to-shell
:config
- (progn
-
- (add-hook 'python-mode-hook 'ravi/python-mode-hook)
-
- (defun ravi/old-ipython-setup ()
- "Set up ipython interpreter for version less than 4, which works with readline"
- (setq python-shell-interpreter "ipython"
- python-shell-interpreter-args "--pylab"
- python-shell-prompt-regexp "In \\[[0-9]+\\]: "
- python-shell-prompt-output-regexp "Out\\[[0-9]+\\]: "
- python-shell-completion-setup-code
- "from IPython.core.completerlib import module_completion"
- python-shell-completion-module-string-code
- "';'.join(module_completion('''%s'''))\n"
- python-shell-completion-string-code
- "';'.join(get_ipython().Completer.all_completions('''%s'''))\n"
- ))
- (defun ravi/ipython-5p4plus-setup ()
- "Set up ipython interpreter for version 5.4+ which requires rlipython to be installed"
- (setq python-shell-interpreter "ipython"
- python-shell-interpreter-args "--pylab --TerminalIPythonApp.interactive_shell_class=rlipython.TerminalInteractiveShell"
- python-shell-prompt-regexp "In \\[[0-9]+\\]: "
- python-shell-prompt-output-regexp "Out\\[[0-9]+\\]: "
- python-shell-completion-setup-code
- "from IPython.core.completerlib import module_completion"
- python-shell-completion-module-string-code
- "';'.join(module_completion('''%s'''))\n"
- python-shell-completion-string-code
- "';'.join(get_ipython().Completer.all_completions('''%s'''))\n"
- ))
- (defun ravi/python-mode-hook()
- ;; Set ipython as our interpreter
- (ravi/old-ipython-setup)
-
- (when (functionp 'helm-dash)
- (setq-local dash-docs-docsets '("Python 2" "NumPy")))
-
- ;; I'd really prefer indentation by 2 spaces, but have too much existing
- ;; python code with indentation at 4 spaces.
- (setq python-indent-offset 4)
- )
-
- (bind-key "<return>" 'newline-and-indent python-mode-map)
-
- ;; We no longer use smart-tab-mode
- ;(add-to-list 'smart-tab-disabled-major-modes 'inferior-python-mode)
-
- (define-auto-insert "\\.py\\'" 'ravi/auto-insert-py)
- (defun ravi/auto-insert-py ()
- (progn
- (insert "#!/usr/bin/env python\n\n")
- )
- )
-
- (use-package virtualenvwrapper
- :config
- (progn
- (venv-initialize-interactive-shells)
- (venv-initialize-eshell)
- (setq venv-location (expand-file-name "~/usr/local/venv/"))))
-
- ;; Use jedi.el for code completion plus documentation browsing.
- ;; The main issue is that this requires both python-side and emacs-side
- ;; support, but the python-side support cannot be installed as a package
- ;; from MELPA. Fedora does not provide python-epc as an RPM either. We
- ;; work around it by adding sexpdata, jedi and python-epc as git submodules.
- (use-package jedi-core
- :config
- (progn
- (defun ravi/add-to-path-list (paths new-path-list)
- (let* ((old-paths (and paths (split-string paths ":" t)))
- (all-paths (-concat (and paths (list paths)) (-difference new-path-list old-paths)))
- (new-path (and all-paths (-reduce (lambda (lhs rhs) (concat lhs ":" rhs)) all-paths))))
- new-path))
- (defun ravi/add-epc-sexpdata-to-pythonpath (orig-fun &rest args)
- "Add paths to python-epc and sexpdata to pythonpath"
- (let ((process-environment
- (cons
- (concat "PYTHONPATH="
- (ravi/add-to-path-list
- (getenv "PYTHONPATH")
- (mapcar (lambda (x) (ravi/emacs-file x)) '("site-lisp/python-epc" "site-lisp/sexpdata"))))
- process-environment)))
- (apply orig-fun args)))
- (advice-add 'jedi:start-server :around #'ravi/add-epc-sexpdata-to-pythonpath)
- (defun ravi/jedi-setup ()
- (let ((basic-server-args '()))
- (set (make-local-variable 'jedi:server-args)
- (if (and (boundp 'extra-jedi-args)
- (listp extra-jedi-args))
- (append basic-server-args extra-jedi-args)
- basic-server-args))
- (jedi:setup)))
-
- ;; Override pos-tip support to automatically delete tooltip
- (defun ravi/jedi:tooltip-show (string)
- (cond
- ((and (memq 'pos-tip jedi:tooltip-method) window-system
- (featurep 'pos-tip))
- (pos-tip-show (jedi:string-fill-paragraph string)
- 'popup-tip-face nil nil 5))
- ((and (memq 'popup jedi:tooltip-method)
- (featurep 'popup))
- (popup-tip string))
- (t (when (stringp string)
- (let ((message-log-max nil))
- (message string))))))
- (fset 'jedi:tooltip-show 'ravi/jedi:tooltip-show)
-
- (defun ravi/python-jedi-hook-installer ()
- ;; (setq jedi:complete-on-dot t) ; needs auto-complete
- (add-hook 'hack-local-variables-hook 'ravi/jedi-setup nil t))
- (add-hook 'python-mode-hook 'ravi/python-jedi-hook-installer)))
-
- (use-package company-jedi
- :config
- (progn
- (add-to-list 'company-backends 'company-jedi)))
-
- (use-package sphinx-doc
- :config
- (progn
- (defun ravi/sphinx-doc-setup ()
- (sphinx-doc-mode 1))
- (add-hook 'python-mode-hook 'ravi/sphinx-doc-setup)
- )
- :diminish sphinx-doc-mode)
-
- (use-package python-docstring
- :config
- (progn
- (defun ravi/python-docstring-mode-setup ()
- (python-docstring-mode 1))
- (add-hook 'python-mode-hook 'ravi/python-docstring-mode-setup))
- :diminish python-docstring-mode)
-
- (use-package electric-operator
- :config
- (progn
- (add-hook 'python-mode-hook #'electric-operator-mode))
- ;; :diminish electric-operator
- )
- )
- )
+ (defun ravi/ipython-setup ()
+ "Set up ipython interpreter for version less than 4, which works with readline"
+ (setq python-shell-interpreter "ipython"
+ python-shell-interpreter-args "--pylab"
+ python-shell-prompt-regexp "In \\[[0-9]+\\]: "
+ python-shell-prompt-output-regexp "Out\\[[0-9]+\\]: "
+ python-shell-completion-setup-code
+ "from IPython.core.completerlib import module_completion"
+ python-shell-completion-module-string-code
+ "';'.join(module_completion('''%s'''))\n"
+ python-shell-completion-string-code
+ "';'.join(get_ipython().Completer.all_completions('''%s'''))\n"
+ ))
+ ;; Set ipython as our interpreter
+ (ravi/ipython-setup)
+ (defvar ravi/support-old-ipython nil
+ "Old ipython does not have --simple-prompt")
+ (unless ravi/support-old-ipython
+ (setq python-shell-interpreter-args "--pylab --simple-prompt"))
+ (defun ravi/python-mode-hook()
+ (when (functionp 'helm-dash)
+ (setq-local dash-docs-docsets '("Python 2" "Python 3" "NumPy")))
+
+ ;; I'd really prefer indentation by 2 spaces, but have too much existing
+ ;; python code with indentation at 4 spaces.
+ (setq python-indent-offset 4))
+
+ (use-package lsp-pyright
+ :init
+ (defun ravi/start-lsp-pyright ()
+ (require 'lsp-pyright)
+ (let* ((in-site-lisp (ravi/emacs-file "site-lisp/node_modules/.bin/pyright-langserver"))
+ (pyright-executable (or (and (boundp 'ravi/pyright-executable)
+ ravi/pyright-executable)
+ in-site-lisp)))
+ (when (file-exists-p pyright-executable)
+ (lsp-dependency 'pyright `(:system ,pyright-executable))))
+ (lsp-deferred))
+ :hook (python-mode-hook . ravi/start-lsp-pyright))
+
+ (bind-key "<return>" 'newline-and-indent python-mode-map)
+
+ (define-auto-insert "\\.py\\'" 'ravi/auto-insert-py)
+ (defun ravi/auto-insert-py ()
+ (insert "#!/usr/bin/env python\n\n"))
+
+ (defvar ravi/python-venv-locations nil
+ "List of python virtualenv locations")
+ (use-package virtualenvwrapper
+ :config
+ (venv-initialize-interactive-shells)
+ (venv-initialize-eshell)
+ (setq venv-location (flatten-list (list ravi/python-venv-locations
+ (expand-file-name "~/usr/local/venv/")))))
+
+ (use-package sphinx-doc
+ :config
+ (defun ravi/sphinx-doc-setup ()
+ (sphinx-doc-mode 1))
+ (add-hook 'python-mode-hook 'ravi/sphinx-doc-setup)
+ :diminish sphinx-doc-mode)
+
+ (use-package python-docstring
+ :config
+ (defun ravi/python-docstring-mode-setup ()
+ (python-docstring-mode 1))
+ (add-hook 'python-mode-hook 'ravi/python-docstring-mode-setup)
+ :diminish python-docstring-mode)
+
+ (use-package electric-operator
+ :config
+ (add-hook 'python-mode-hook #'electric-operator-mode)
+ ;; :diminish electric-operator
+ ))
(provide 'ravi-init-python)