-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmy-python.el
More file actions
209 lines (179 loc) · 8.39 KB
/
Copy pathmy-python.el
File metadata and controls
209 lines (179 loc) · 8.39 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
;; Python settings ------------------------------------------------------------
;; Indentation: prefer 4 spaces, and disable noisy guessing
(setq-default python-indent-offset 4)
(setq python-indent-offset 4)
(setq python-indent-guess-indent-offset nil)
(setq python-indent-guess-indent-offset-verbose nil)
;; Default virtualenv configuration
(setq my/default-venv (expand-file-name "~/opt/plus/def-venv"))
(setq my/current-venv my/default-venv)
(setq my/last-buffer-dir nil)
;; Function to find project-local .venv directory
(defun my/find-project-venv (start-dir)
"Find .venv directory by walking up from START-DIR."
(when start-dir
(let ((venv-path (expand-file-name ".venv" start-dir)))
(cond
((file-directory-p venv-path) venv-path)
((string= start-dir "/") nil)
(t (my/find-project-venv (file-name-directory (directory-file-name start-dir))))))))
;; Function to update Python paths for current venv
(defun my/update-python-paths (venv-path)
"Update all Python-related paths for VENV-PATH."
(setq my/current-venv venv-path
my/python-bin (expand-file-name "bin/python" venv-path)
my/pytest-bin (expand-file-name "bin/pytest" venv-path)
my/mypy-bin (expand-file-name "bin/mypy" venv-path))
;; Python interpreter: prefer venv python if executable, else system python
(setq python-shell-interpreter
(if (file-executable-p my/python-bin)
my/python-bin
(or (executable-find "python") "python")))
;; Linting: use mypy if present; otherwise fall back to flake8
(if (file-executable-p my/mypy-bin)
(progn
(setq elpy-syntax-check-command my/mypy-bin)
(setq python-check-command my/mypy-bin))
(progn
(setq elpy-syntax-check-command "flake8")
(setq python-check-command (or (executable-find "flake8") "flake8"))))
;; Testing: prefer venv pytest, else plain "pytest"
(setq elpy-test-pytest-runner-command
(if (file-executable-p my/pytest-bin)
my/pytest-bin
"pytest")))
;; Auto-switch virtualenv based on buffer location
(defun my/auto-switch-venv ()
"Automatically switch virtualenv based on current buffer's directory."
(when (and buffer-file-name
(derived-mode-p 'python-mode))
(let* ((buffer-dir (file-name-directory buffer-file-name))
(project-venv (my/find-project-venv buffer-dir)))
(when (not (string= buffer-dir my/last-buffer-dir))
(setq my/last-buffer-dir buffer-dir)
(let ((target-venv (or project-venv my/default-venv)))
(when (not (string= target-venv my/current-venv))
(my/update-python-paths target-venv)
(pyvenv-activate target-venv)
;; Ensure Elpy's RPC picks up the new environment immediately.
(when (featurep 'elpy)
(ignore-errors (elpy-rpc-restart)))
(message "Switched to venv: %s"
(if project-venv
(file-name-nondirectory (directory-file-name project-venv))
"default"))))))))
;; Set initial paths
(my/update-python-paths my/default-venv)
(require 'flymake)
(remove-hook 'flymake-diagnostic-functions 'flymake-proc-legacy-flymake)
(with-eval-after-load 'elpy
(local-set-key (kbd "C-x C-e") 'elpy-shell-send-buffer)
(local-set-key (kbd "C-x C-d") 'elpy-pdb-break-at-point)
(when (boundp 'elpy-mode-map)
(define-key elpy-mode-map (kbd "s-<left>") #'elpy-nav-indent-shift-left)
(define-key elpy-mode-map (kbd "s-<right>") #'elpy-nav-indent-shift-right)))
(setq elpy-modules '(elpy-module-company elpy-module-eldoc elpy-module-flymake
elpy-module-pyvenv
;; highlight-indentation can be slow on large files
;; and adds overlays; keep it disabled for snappier edits.
elpy-module-yasnippet elpy-module-sane-defaults)
elpy-test-discover-runner-command '("python" "-m" "pytest")
elpy-test-runner 'elpy-test-pytest-runner)
(setq comint-process-echoes t)
;; Activate default venv on startup
(pyvenv-activate my/default-venv)
;; Switch venv when entering a Python buffer
(add-hook 'python-mode-hook #'my/auto-switch-venv)
;; Manual command to check current venv and force switch
(defun my/show-current-venv ()
"Show current virtualenv and project detection status."
(interactive)
(let* ((buffer-dir (when buffer-file-name (file-name-directory buffer-file-name)))
(project-venv (when buffer-dir (my/find-project-venv buffer-dir)))
(venv-name (if (string= my/current-venv my/default-venv)
"default"
(file-name-nondirectory (directory-file-name my/current-venv)))))
(message "Current venv: %s | Buffer dir: %s | Project venv: %s"
venv-name
(or buffer-dir "none")
(if project-venv
(file-name-nondirectory (directory-file-name project-venv))
"none"))))
(defun my/force-venv-switch ()
"Force virtualenv switch check for current buffer."
(interactive)
(setq my/last-buffer-dir nil)
(my/auto-switch-venv)
(my/show-current-venv))
(defun my-mypy ()
"Docstring for my-mypy."
(interactive)
(run-python)
(if (require 'flycheck nil 'noerror)
(progn
(flycheck-mode)
(flycheck-compile 'python-mypy))
(message "Flycheck not available; install flycheck and flycheck-mypy.")))
(load-library "persistent-overlays")
(add-hook 'python-mode-hook
'(lambda ()
(interactive)
;; Ensure 4-space indentation consistently in this buffer
(setq-local python-indent-offset 4)
(when (boundp 'py-indent-offset)
(setq-local py-indent-offset 4))
(setenv "MYPYPATH"
(concat
(expand-file-name "~/src/mine/skunkworks/python/stubs")
":"
(expand-file-name "~/src/ext/python/typeshed/")
":"
(expand-file-name "~/src/oss/kingston/")
":"
(expand-file-name "~/src/oss/ormsnack/")
":"
(expand-file-name "~/src/oss/plus/")
)
)
(setq outline-regexp "[^ \t\n]\\|[ \t]*\\(if[ \t]+\\|elif[ \t]+\\|else[ \t]+\\|for[ \t]+\\|while[ \t]+\\|with[ \t]+\\|def[ \t]+\\|class[ \t]+\\)")
(outline-minor-mode t)
(persistent-overlays-minor-mode 1)
(persistent-overlays-load-overlays)
(setq python-shell-interpreter
(if (file-executable-p my/python-bin)
my/python-bin
(or (executable-find "python") "python")))
(add-hook 'before-save-hook 'persistent-overlays-save-overlays nil 'local)
(require 'yapfify nil 'noerror)
(highlight-lines-matching-regexp ".set_trace" 'hi-red-b)
(define-key python-mode-map (kbd "C-x C-m") 'outline-toggle-children)
(define-key python-mode-map (kbd "C-x C-c") 'my-mypy)
(define-key python-mode-map (kbd "") 'outline-toggle-children)
(define-key python-mode-map (kbd "") 'outline-toggle-children)
(elpy-enable)
(setq company-idle-delay 0.2)
)
)
(setq auto-mode-alist
(append '(("\\.wsgi$" . python-mode)
("\\.pyx$" . python-mode)) auto-mode-alist))
(setq auto-mode-alist
(append '(("Pipfile*" . conf-mode)) auto-mode-alist))
;; EIN settings ------------------------------------------------------------
(unless (package-installed-p 'ein)
(package-install 'ein)
(package-install 'ein-subpackages))
(require 'ein)
;; (require 'ein-subpackages)
;; RST settings ------------------------------------------------------------
(with-eval-after-load 'rst
(defun my/rst-clear-heading-background ()
"Remove bright backgrounds from rst headings/adornments in this buffer."
(dolist (face (face-list))
(let ((n (symbol-name face)))
(when (and (string-prefix-p "rst-" n)
(string-match-p "\\(level\\|header\\|title\\|adorn\\)" n))
;; buffer-local remap (doesn't clobber the theme globally)
(face-remap-add-relative face '(:background unspecified))))))
(add-hook 'rst-mode-hook #'my/rst-clear-heading-background))
(provide 'my-python)