summaryrefslogtreecommitdiffstats
path: root/lisp/ravi-init-python.el
blob: 4de804d4e07eeb748c0db0664d7fe45baa2e9789 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
;;; ravi-init-python.el --- python support

;; Copyright (C) 2013

;; Author:  <ravi@nero.lan>
;; 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 <http://www.gnu.org/licenses/>.

;;; 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 "<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)