* Python
Python gehort zu den popularsten Sprachen (Stand 2023). Viele Leute entwicklen mit Emacs.
Daher ist die Unterstutzung der Sprache sehr gut. Eine Tree-Sitter-Grammatik und
mehrere LSP-Server sind verfugbar.
** Tree-sitter
#+begin_src emacs-lisp
(push '(python-mode . python-ts-mode) major-mode-remap-alist)
#+end_src
- moving around: ts-movement
- selections:
- expreg
- folding
** Konfiguration
#+begin_src emacs-lisp
(use-package python
:pin gnu
:bind (
:map python-ts-mode-map
("C-+" . expreg-expand)
("C--" . expreg-contract)
("S-<return>" . python-shell-send-statement)
("C-<return>" . python-shell-send-region)
("M-<return>" . nil)
)
:hook
(python-ts-mode . yas-minor-mode)
:config
(when (executable-find "ipython3")
(setq python-shell-interpreter "ipython3"
python-shell-interpreter-args "--simple-prompt --classic")))
#+end_src
There is a Hydra defined for ts-movement, open with C-.
** Language Server
Verwende den jedi-Server, da er auf allen Plattformen zur Verfügung stand.
#+begin_src emacs-lisp
(defun my/eglot-python-setup ()
(eglot-ensure)
;; eglot wipes out the flymake functions; re-add
(setq-local python-flymake-command '("flake8" "--max-line-length=100" "-"))
(add-hook 'eglot-managed-mode-hook
(lambda ()
(add-hook 'flymake-diagnostic-functions #'python-flymake t t))
nil t))
(add-hook 'python-mode-hook 'my/eglot-python-setup)
(add-hook 'python-ts-mode-hook 'my/eglot-python-setup)
#+end_src
Prerequisites:
*** void-Linux
#+begin_src sh
doas xbps-install -Sy jedi
doas xbps-install -Sy flake8
doas xbps-install -Sy black
pip install -U jedi-language-server
#+end_src
*** Windows
Using MSYS2:
#+begin_src sh
pacman -Sy mingw-w64-python-jedi
pacman -Sy mingw-w64-x86_64-python-ipython
pacman -Sy mingw-w64-x86_64-python-black
pacman -Sy mingw-w64-x86_64-python-flake8
pacman -Sy mingw-w64-x86_64-python-pip
pip install -U jedi-language-server
#+end_src
** UI
(defun elpy-shell-switch-to-shell ()
"Switch to inferior Python process buffer."
(interactive)
(setq elpy--shell-last-py-buffer (buffer-name))
(pop-to-buffer (process-buffer (elpy-shell-get-or-create-process))))
#+begin_src emacs-lisp
#+end_src
"C-p" '(run-python :wk "start py-shell")
"p" '(python-shell-switch-to-shell :wk "py-shell")
"+" '(yas-insert-snippet :wk "code snippets")
"b" '(python-shell-send-buffer :wk "eval buffer")
"F" '(python-shell-send-function :wk "eval function")
"f" '(hydra-fold/body :wk "Folding")
"m" '(hydra-tree-sitter-move/body :wk "movement hydra")
"s" '(:ignore t :which-key "mark/select")
"sp" '(mark-paragraph :wk "paragraph")
"sf" '(mark-defun :wk "function")
"sF" '((evil-textobj-tree-sitter-get-textobj "function.outer") :wk "function")