;;; ravi-init-python.el --- python support ;; Copyright (C) 2013 ;; Author: ;; Keywords: ;; This program is free software; you can redistribute it and/or modify ;; it under the terms of the GNU General Public License as published by ;; the Free Software Foundation, either version 3 of the License, or ;; (at your option) any later version. ;; This program is distributed in the hope that it will be useful, ;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;; GNU General Public License for more details. ;; You should have received a copy of the GNU General Public License ;; along with this program. If not, see . ;;; Commentary: ;; Python programming setup ;;; Code: (setq load-prefer-newer t) (use-package python :mode ("\\.py\\'" . python-mode) :commands python-shell-switch-to-shell :hook ((python-mode-hook . ravi/python-mode-hook)) :config (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"))) (when (functionp 'consult-dash) (setq-local consult-dash-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 "" '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)