Skip to content

Instantly share code, notes, and snippets.

@bravosierrasierra
Created March 24, 2018 09:26
Show Gist options
  • Save bravosierrasierra/2741072ebe2a2fdd40c050af1b67f643 to your computer and use it in GitHub Desktop.
Save bravosierrasierra/2741072ebe2a2fdd40c050af1b67f643 to your computer and use it in GitHub Desktop.
*** python-mode
#+begin_src emacs-lisp
(use-package python
:mode ("\\.py" . python-mode)
:config
(setq py-python-command "python3")
(setq python-shell-interpreter "python3")
(setq python-indent-offset 4)
(use-package jedi-core
:config
(setq jedi:get-in-function-call-timeout 0
jedi:get-in-function-call-delay 0
jedi:goto-definition-config '((nil definition nil)))
)
(use-package company-jedi
:config
;; (setq jedi:complete-on-dot t)
;; (add-hook 'python-mode-hook 'jedi:setup)
;; (jedi:install-server)
)
(use-package company-flx
:config
(with-eval-after-load 'company
(company-flx-mode +1)))
(use-package helm-company
:config
(define-key company-mode-map (kbd "C-:") 'helm-company)
(define-key company-active-map (kbd "C-:") 'helm-company)
)
(add-hook 'python-mode-hook
(lambda()
(interactive)
(setenv "TERM" "ansi-term")
(setq python-shell-completion-native nil
indent-tabs-mode nil
tab-width 4
python-indent 4
python-shell-interpreter "ipython"
python-shell-interpreter-args "--profile=emacs"
)
(defun python-startup-function (start end &optional send-main msg)
(unless (python-shell-get-process)
(run-python)))
(add-function :before (symbol-function 'python-shell-send-region) #'python-startup-function)
;; (add-to-list
;; 'imenu-generic-expression
;; '("Sections" "^#### \\[ \\(.*\\) \\]$" 1))
;; (setq imenu-create-index-function 'my-merge-imenu)
;; pythom mode keybindings
(define-key python-mode-map (kbd "M-.") 'jedi:goto-definition)
;; (define-key python-mode-map (kbd "C-c C-b") 'insert_ipdb)
(define-key python-mode-map (kbd "C-c C-b") 'python-add-breakpoint)
(define-key python-mode-map (kbd "RET") 'newline-and-indent)
(define-key python-mode-map (kbd "M-RET") 'newline)
(define-key python-mode-map (kbd "M-,") 'jedi:goto-definition-pop-marker)
(define-key python-mode-map (kbd "M-/") 'jedi:show-doc)
(define-key python-mode-map (kbd "M-?") 'helm-jedi-related-names)
;; (define-key python-mode-map (kbd "<f8>") 'next-error)
;; (define-key python-mode-map (kbd "<S-F8>") 'previous-error)
;; end python mode keybindings
(add-to-list 'write-file-functions 'delete-trailing-whitespace)
(modify-syntax-entry ?_ "w")
(modify-syntax-entry ?- "w")
;; (py-autopep8-enable-on-save)
(set (make-local-variable 'compile-command) (concat "python3 " buffer-file-name))
(eval-after-load "company"
'(progn
(unless (member 'company-jedi (car company-backends))
(setq comp-back (car company-backends))
(push 'company-jedi comp-back)
(setq company-backends (list comp-back)))))
))
;;
;; pystart: python header
(define-skeleton skel-python-pystart
"Python file with utf output abbrev"
"\n"
"#!/usr/bin/env python\n"
"# -*- coding: utf-8 -*-\n"
"\n"
"import sys\n"
"reload(sys)\n"
"sys.setdefaultencoding('utf-8')\n"
"\n"
"def main():\n"
" " _
"\n"
"if __name__ == \"__main__\":\n"
" main()\n"
"\n")
(define-abbrev python-mode-abbrev-table "pystart" "" 'skel-python-pystart)
(define-skeleton skel-python-utffile
"utf file coding string"
"\n"
"# -*- coding: utf-8 -*-\n"
"from __future__ import unicode_literals\n"
)
(define-abbrev python-mode-abbrev-table "utffile" "" 'skel-python-utffile)
;; ))
;; Подсвечивать строки, которые обычно используются при отладке
(defun annotate-pdb()
(interactive)
(highlight-lines-matching-regexp "import ipdb")
(highlight-lines-matching-regexp "import pdb")
(highlight-lines-matching-regexp "set_trace()")
;; (highlight-phrase "TODO:")
;; (highlight-regexp "FIXME:")
;; (highlight-regexp "BUG:")
)
(add-hook 'python-mode-hook 'annotate-pdb)
;; cat ~/.config/flake8
;; [flake8]
;; ignore = E221,E501,E203,E202,E272,E251,E211,E222,E701
;; max-line-length = 160
;; exclude = tests/*
;; max-complexity = 10
(use-package python-django)
(use-package py-autopep8
:disabled
:config
(add-hook 'python-mode-hook 'py-autopep8-enable-on-save)
)
(use-package pip-requirements
:config
(add-hook 'pip-requirements-mode-hook #'pip-requirements-auto-complete-setup)
(add-to-list 'auto-mode-alist '("\\REQUIREMENTS" . pip-requirements-mode))
(add-to-list 'auto-mode-alist '("\\REQUIREMENTS.txt" . pip-requirements-mode))
(add-to-list 'auto-mode-alist '("\\requirements" . pip-requirements-mode))
(add-to-list 'auto-mode-alist '("\\requirements.txt" . pip-requirements-mode))
)
(use-package virtualenvwrapper
;; :ensure t
:config
;; (use-package virtualenvwrapper)
(venv-initialize-interactive-shells)
(venv-initialize-eshell)
(setq venv-location "~/.virtualenvs")
)
(add-to-list 'Info-default-directory-list "~/.emacs.d/elpa/python-info-20151228.1052")
(use-package info-look)
(info-lookup-add-help
:mode 'python-mode
:regexp "[[:alnum:]_]+"
:doc-spec
'(("(python)Index" nil "")))
;; http://pswinkels.blogspot.ru/2010/04/debugging-python-code-from-within-emacs.html
(defvar gud-pdb-marker-regexp
"^> \\([-a-zA-Z0-9_ /.:\\]*\\|<string>\\)(\\([0-9]+\\))\\([a-zA-Z0-9_]*\\|\\?\\|<module>\\)()\\(->[^\n]*\\)?[\n]")
(defun python-add-breakpoint ()
"Add a break point"
(interactive)
(newline-and-indent)
(insert "import pdb; pdb.set_trace()")
(highlight-lines-matching-regexp "^[ ]*import ipdb; ipdb.set_trace()"))
)
#+end_src
;; (lambda ()
;; (setq-local indent-tabs-mode nil)
;; (setq-local tab-width 4)
;; (setq-local py-indent-tabs-mode t)
;; (local-set-key [f8] 'next-error)
;; (local-set-key [(shift f8)] 'previous-error)
;; (add-to-list 'write-file-functions 'delete-trailing-whitespace)
;; (define-key python-mode-map (kbd "RET") 'newline-and-indent)
;; ;;(define-key python-mode-map "\C-m" 'newline-and-indent)
;; (define-key python-mode-map (kbd "C-c C-b") 'python-add-breakpoint)
;; (modify-syntax-entry ?_ "w")
;; (modify-syntax-entry ?- "w")
;; ;; (py-autopep8-enable-on-save)
;; (set (make-local-variable 'compile-command) (concat "python3 " buffer-file-name))
;; (turn-on-eldoc-mode)
;; (add-to-list 'company-backends 'company-jedi))
;; (add-hook 'python-mode-hook '
(use-package elpy
:disabled
;; https://elpy.readthedocs.io/en/latest/index.html
;; first install One-line install: pip install jedi flake8 importmagic autopep8
;; and then run M-x elpy-config to configure elpy
:init
(add-to-list 'auto-mode-alist '("\\.py$" . python-mode))
:config
(setq elpy-rpc-backend "jedi"
;; elpy-rpc-large-buffer-size 10000
)
(when (executable-find "ipython")
(elpy-use-ipython))
(elpy-enable)
:bind (:map elpy-mode-map
("M-." . elpy-goto-definition)
("M-," . pop-tag-mark)))
(use-package company-anaconda
;; :disabled
:config
(eval-after-load "company"
'(add-to-list 'company-backends 'company-anaconda))
(add-hook 'python-mode-hook 'anaconda-mode)
(add-hook 'python-mode-hook 'anaconda-eldoc-mode)
;; Keybinding Description
;; C-M-i anaconda-mode-complete
;; M-. anaconda-mode-find-definitions
;; M-, anaconda-mode-find-assignments
;; M-r anaconda-mode-find-references
;; M-* anaconda-mode-go-back
;; M-? anaconda-mode-show-doc
;; Python info ;; Info-default-directory-list
)
;; (use-package pyenv-mode
;; :init
;; (add-to-list 'exec-path "~/.pyenv/shims")
;; (setenv "WORKON_HOME" "~/.pyenv/versions/")
;; :config
;; (pyenv-mode)
;; :bind
;; ("C-x p e" . pyenv-activate-current-project))
;; (defun pyenv-init()
;; (setq global-pyenv (replace-regexp-in-string "\n" "" (shell-command-to-string "pyenv global")))
;; (message (concat "Setting pyenv version to " global-pyenv))
;; (pyenv-mode-set global-pyenv)
;; (defvar pyenv-current-version nil global-pyenv))
;; (defun pyenv-activate-current-project ()
;; "Automatically activates pyenv version if .python-version file exists."
;; (interactive)
;; (f-traverse-upwards
;; (lambda (path)
;; (message path)
;; (let ((pyenv-version-path (f-expand ".python-version" path)))
;; (if (f-exists? pyenv-version-path)
;; (progn
;; (setq pyenv-current-version (s-trim (f-read-text pyenv-version-path 'utf-8)))
;; (pyenv-mode-set pyenv-current-version)
;; (pyvenv-workon pyenv-current-version)
;; (message (concat "Setting virtualenv to " pyenv-current-version))))))))
;; (add-hook 'after-init-hook 'pyenv-init)
;; (add-hook 'projectile-after-switch-project-hook 'pyenv-activate-current-project)
;; Tree style source code viewer for Python buffer
;; (use-package jedi-direx
;; :ensure t
;; :config
;; (eval-after-load "python"
;; '(define-key python-mode-map "\C-cx" 'jedi-direx:pop-to-buffer))
;; (add-hook 'jedi-mode-hook 'jedi-direx:setup)
;; )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment