Skip to content

Instantly share code, notes, and snippets.

@dmitry-saritasa
Created September 27, 2016 01:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dmitry-saritasa/104a6006eab6662cb2d6489dec8b1c0f to your computer and use it in GitHub Desktop.
Save dmitry-saritasa/104a6006eab6662cb2d6489dec8b1c0f to your computer and use it in GitHub Desktop.
python-settings.el
;;; package --- Python Programming ;;;
;;; Commentary:
;;; Code:
; don't show the startup screen
(setq inhibit-startup-screen t)
; don't show the menu bar
(menu-bar-mode nil)
; ignore case when searching
;(setq case-fold-search t)
; require final newlines in files when they are saved
;(setq require-final-newline t)
;; number of characters until the fill column
;(setq fill-column 80)
; display line numbers to the right of the window
;(global-linum-mode t)
; show the current line and column numbers in the stats bar as well
;(line-number-mode t)
;(column-number-mode t)
(require 'uniquify)
(setq uniquify-buffer-name-style 'forward)
(setq python-fill-docstring-style 'django)
(setq tab-width 4) ; or any other preferred value
;; -----------------------
;; python.el configuration
;; -----------------------
; from python.el
(require 'python)
;; 80 char line
; ( doesn't work well with elpy with auto-complete
;(require 'fill-column-indicator)
;(setq fci-rule-width 1)
;(setq fci-rule-color "darkgreen")
;(setq fci-rule-use-dashes t)
;(add-hook 'python-mode-hook 'fci-mode)
;; ------------------
;; misc django config
;; ------------------
; (require 'pony-mode)
;(add-to-list 'load-path "~/.emacs.d/el-get/python-django")
;(require 'python-django)
;; ------------------
;; misc python config
;; ------------------
(company-mode -1)
(elpy-enable)
(elpy-use-ipython "ipython")
(setq python-shell-interpreter "ipython" python-shell-interpreter-args "--simple-prompt --pprint")
(setq python-check-command "flake8")
(setq elpy-rpc-backend "jedi")
(setq elpy-rpc-python-command "python")
; https://github.com/gregsexton/ob-ipython/issues/28
(setq python-shell-completion-native-enable nil)
; pyenv
(pyenv-mode)
(defun projectile-pyenv-mode-set ()
"Set pyenv version matching project name."
(let ((project (projectile-project-name)))
(when (member project (pyenv-mode-versions))
(pyenv-mode-set project))))
(defun python2 ()
"Set pyenv version matching project name."
(interactive)
(elpy-use-ipython "ipython2")
(setq python-shell-interpreter "ipython2" python-shell-interpreter-args "--pprint")
(setq elpy-rpc-python-command "python2"))
(add-hook 'projectile-switch-project-hook 'projectile-pyenv-mode-set)
;;;; Syntax checking (Flycheck)
(add-hook 'after-init-hook #'global-flycheck-mode)
(setq flycheck-check-syntax-automatically '(mode-enabled save new-line idle-change))
;; Python autocompletion and doc browsing (Jedi, requires jedi and ipc
;; installed in the Python environment emacs is working in)
; pydoc info
(include-plugin "pydoc-info-0.2")
(require 'pydoc-info)
(info-lookup-add-help
:mode 'python-mode
:parse-rule 'pydoc-info-python-symbol-at-point
:doc-spec
'(("(python)Index" pydoc-info-lookup-transform-entry)
("(TARGETNAME)Index" pydoc-info-lookup-transform-entry)))
; jedi python completion
(include-elget-plugin "ctable") ; required for epc
(include-elget-plugin "deferred") ; required for epc
(include-elget-plugin "epc") ; required for jedi
(include-elget-plugin "jedi")
(require 'jedi)
(setq jedi:setup-keys t)
(autoload 'jedi:setup "jedi" nil t)
(add-hook 'python-mode-hook 'jedi:setup)
(add-hook 'python-mode-hook 'hs-minor-mode t)
; isort
(require 'py-isort)
(add-hook 'before-save-hook 'py-isort-before-save)
; Set PYTHONPATH, because we don't load .bashrc
(defun set-python-path-from-shell-PYTHONPATH ()
(let ((path-from-shell (shell-command-to-string "$SHELL -i -c 'echo $PYTHONPATH'")))
(setenv "PYTHONPATH" path-from-shell)))
(if window-system (set-python-path-from-shell-PYTHONPATH))
; flycheck
; autopep8
(require 'py-autopep8)
(add-hook 'elpy-mode-hook 'py-autopep8-enable-on-save)
; restart python processes
(setq elpy-rpc--buffer nil)
; keybindings
(eval-after-load 'python
'(define-key python-mode-map (kbd "C-c b") 'elpy-format-code))
(eval-after-load 'python
'(define-key python-mode-map (kbd "C-c f") 'hs-toggle-hiding))
;(global-set-key (kbd "C-c b") 'elpy-format-code)
; Django functions
; __init__.py
(defun create-empty-init-py()
(interactive)
(shell-command "touch __init__.py")
)
(global-set-key (kbd "C-c C-p i") 'create-empty-init-py)
(defun my-restart-python-console ()
"Restart python console before evaluate buffer or region to avoid various uncanny conflicts, like not reloding modules even when they are changed"
(interactive)
(kill-process "Python")
(sleep-for 0.05)
(kill-buffer "*Python*")
(elpy-shell-send-region-or-buffer))
;(require 'highlight-current-line)
;(highlight-current-line-minor-mode t)
;(add-hook 'elpy-mode-hook 'highlight-current-line-minor-mode)
;(require 'mmm-mode)
;(setq mmm-global-mode 'maybe)
;(mmm-add-classes
; '((python-rst
; :submode rst-mode
; :front "^ *[ru]?\"\"\"[^\"]*$"
; :back "^ *\"\"\""
; :include-front t
; :include-back t
; :end-not-begin t)))
;(mmm-add-mode-ext-class 'python-mode nil 'python-rst)
(eval-after-load 'python
'(define-key python-mode-map (kbd "C-c <right>") 'elpy-nav-indent-shift-right))
(eval-after-load 'python
'(define-key python-mode-map (kbd "C-c <left>") 'elpy-nav-indent-shift-left))
(defalias 'activate 'pyvenv-activate)
(provide 'python-settings)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment