~gpanders/dotfiles

ref: 2da1dde5ea566f82ebe6532bc6d3b8670fd77ce8 dotfiles/emacs/.emacs.d/lisp/init-python.el -rw-r--r-- 3.9 KiB
2da1dde5Greg Anders Note 'open' subcommand opens editor in NOTES_DIR when no argument given 1 year, 3 months ago
                                                                                
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
;;; init-python.el

(defun pyenv-executable-find (command)
  "Find executable taking pyenv shims into account."
  (if (executable-find "pyenv")
      (progn
        (let ((pyenv-string (shell-command-to-string (concat "pyenv which " command))))
          (unless (string-match "not found" pyenv-string)
            pyenv-string)))
    (executable-find command)))

(defun python-setup-shell (&rest args)
  "Setup interactive Python shell."
  (when (pyenv-executable-find "ipython")
      (setq python-shell-interpreter "ipython"
            python-shell-interpreter-args "--simple-prompt --no-color-info -i"
            python-shell-prompt-regexp "In \\[[0-9]+\\]: "
            python-shell-prompt-block-regexp "\\.\\.\\.\\.: "
            python-shell-prompt-output-regexp "Out\\[[0-9]+\\]: "
            python-shell-completion-setup-code
            "from IPython.core.completerlib import module_completion"
            python-shell-completion-string-code
            "';'.join(get_ipython().Completer.all_completions('''%s'''))\n")))

(defun inferior-python-setup ()
    "Configure inferior Python mode."
  (setq-local indent-tabs-mode t)
  (when (featurep 'evil)
    (evil-define-key 'insert inferior-python-mode-map (kbd "C-p") 'comint-previous-input)
    (evil-define-key 'insert inferior-python-mode-map (kbd "C-n") 'comint-next-input)))

(defun run-ipython ()
  "Start an interactive IPython shell."
  (interactive)
  (if (pyenv-executable-find "ipython")
      (progn
        (require 'term)
        (let* ((cmd "ipython")
               (args "-i")
               (switches (split-string-and-unquote args))
               (termbuf (apply 'make-term "IPython" cmd nil switches)))
          (set-buffer termbuf)
          (term-mode)
          (term-char-mode)
          (setq term-prompt-regexp "In \\[[0-9]+\\]: ")
          (switch-to-buffer termbuf)))
    (message "IPython not found")))


(use-package pyenv-mode
  :ensure t
  :if (executable-find "pyenv")
  :commands (pyenv-mode-versions pyenv-mode-version)
  :init
  (setq pyenv-mode-mode-line-format "")
  ;; Cribbed from Spacemacs
  (defun pyenv-mode-set-local-version ()
    "Set pyenv version from \".python-version\" by looking in parent directories."
    (interactive)
    (let ((root-path (locate-dominating-file default-directory
                                             ".python-version")))
      (when root-path
        (let* ((file-path (expand-file-name ".python-version" root-path))
               (version
                (with-temp-buffer
                  (insert-file-contents-literally file-path)
                  (buffer-substring-no-properties (line-beginning-position)
                                                  (line-end-position)))))
          (if (member version (pyenv-mode-versions))
              (pyenv-mode-set version)
            (message "pyenv: version `%s' is not installed (set by %s)"
                     version file-path))))))
  (add-hook 'python-mode-hook #'pyenv-mode-set-local-version)
  (add-hook 'projectile-after-switch-project-hook #'pyenv-mode-set-local-version)

  (dolist (func '(pyenv-mode-set pyenv-mode-unset))
    (advice-add func :after 'python-setup-shell)))

(use-package anaconda-mode
  :ensure t
  :after python
  :hook python-mode
  :init
  (setq anaconda-mode-eldoc-as-single-line t)
  :config
  (add-hook 'anaconda-mode-hook #'anaconda-eldoc-mode)
  (with-eval-after-load 'evil
    (evil-define-key 'normal anaconda-mode-map (kbd "g r") 'anaconda-mode-find-references))
  (use-package company-anaconda
    :ensure t
    :after company
    :init
    (add-to-list 'company-backends 'company-anaconda)))

(use-package pip-requirements
  :ensure t
  :mode ("/requirements.txt$" . pip-requirements-mode))

(use-package ein
  :commands (ein:run ein:login)
  :ensure t)

(add-hook 'python-mode #'python-setup-shell)
(add-hook 'inferior-python-mode-hook #'inferior-python-setup)

(provide 'init-python)

;;; init-python.el ends here