2015-07-21 12:09:21 +01:00
|
|
|
;; Functions -------------------------------------------------
|
2015-07-21 13:12:38 +01:00
|
|
|
(defun setup-keybindings ()
|
|
|
|
"Setup default key bindings for python mode"
|
|
|
|
|
|
|
|
;; FIXME: Replace the global key map with python-mode-map
|
|
|
|
(global-set-key (kbd "C-c C-b") 'python-add-breakpoint)
|
|
|
|
(global-set-key (kbd "C-c C-n") 'python-interactive))
|
|
|
|
|
|
|
|
|
2015-07-21 12:09:21 +01:00
|
|
|
(defun python--encoding-comment-required-p ()
|
|
|
|
(re-search-forward "[^\0-\177]" nil t))
|
|
|
|
|
2015-07-21 13:12:38 +01:00
|
|
|
;;;###autoload
|
2015-07-21 12:09:21 +01:00
|
|
|
(defun python--detect-encoding ()
|
|
|
|
(let ((coding-system
|
|
|
|
(or save-buffer-coding-system
|
|
|
|
buffer-file-coding-system)))
|
|
|
|
(if coding-system
|
|
|
|
(symbol-name
|
|
|
|
(or (coding-system-get coding-system 'mime-charset)
|
|
|
|
(coding-system-change-eol-conversion coding-system nil)))
|
|
|
|
"ascii-8bit")))
|
|
|
|
|
2015-07-21 13:12:38 +01:00
|
|
|
;;;###autoload
|
2015-07-21 12:09:21 +01:00
|
|
|
(defun python--insert-coding-comment (encoding)
|
|
|
|
(let ((newlines (if (looking-at "^\\s *$") "\n" "\n\n")))
|
|
|
|
(insert (format "# coding: %s" encoding) newlines)))
|
|
|
|
|
2015-07-21 13:12:38 +01:00
|
|
|
;;;###autoload
|
2015-07-21 12:09:21 +01:00
|
|
|
(defun python-mode-set-encoding ()
|
|
|
|
"Insert a magic comment header with the proper encoding if necessary."
|
|
|
|
(save-excursion
|
|
|
|
(widen)
|
|
|
|
(goto-char (point-min))
|
|
|
|
(when (prelude-python--encodings-comment-required-p)
|
|
|
|
(goto-char (point-min))
|
|
|
|
(let ((coding-system (python--detect-encoding)))
|
|
|
|
(when coding-system
|
|
|
|
(if (looking-at "^#!") (beginning-of-line 2))
|
|
|
|
(cond ((looking-at "\\s *#\\s *.*\\(en\\)?coding\\s *:\\s *\\([-a-z0-9_]*\\)")
|
|
|
|
;; update existing encoding comment if necessary
|
|
|
|
(unless (string= (match-string 2) coding-system)
|
|
|
|
(goto-char (match-beginning 2))
|
|
|
|
(delete-region (point) (match-end 2))
|
|
|
|
(insert coding-system)))
|
|
|
|
((looking-at "\\s *#.*coding\\s *[:=]"))
|
|
|
|
(t (python--insert-coding-comment coding-system)))
|
|
|
|
(when (buffer-modified-p)
|
|
|
|
(basic-save-buffer-1)))))))
|
|
|
|
|
2015-07-21 13:12:38 +01:00
|
|
|
;;;###autoload
|
2015-07-21 12:09:21 +01:00
|
|
|
(defun python-add-breakpoint ()
|
|
|
|
"Add a break point"
|
|
|
|
(interactive)
|
|
|
|
(newline-and-indent)
|
|
|
|
(insert "import ipdb; ipdb.set_trace()")
|
|
|
|
(highlight-lines-matching-regexp "^[ ]*import ipdb; ipdb.set_trace()"))
|
|
|
|
|
2015-07-21 13:12:38 +01:00
|
|
|
;;;###autoload
|
2015-07-21 12:09:21 +01:00
|
|
|
(defun python-interactive ()
|
|
|
|
"Enter the interactive Python environment"
|
|
|
|
(interactive)
|
|
|
|
(progn
|
|
|
|
(newline-and-indent)
|
|
|
|
(insert "from IPython import embed; embed()")
|
|
|
|
(move-end-of-line 1)))
|
|
|
|
|
2018-08-30 12:20:22 +01:00
|
|
|
(defun jedi-python-mode-hook ()
|
|
|
|
(add-to-list 'company-backends 'company-jedi))
|
2015-07-21 13:12:38 +01:00
|
|
|
|
|
|
|
;;;###autoload
|
2015-07-21 12:09:21 +01:00
|
|
|
(defun python-mode-defaults ()
|
|
|
|
"Defaults for Python programming."
|
2015-07-21 13:12:38 +01:00
|
|
|
|
|
|
|
;(require 'anaconda-mode)
|
|
|
|
;(require 'eldoc-mode)
|
|
|
|
|
2015-07-21 12:09:21 +01:00
|
|
|
(subword-mode +1)
|
2018-07-04 11:38:42 +01:00
|
|
|
;(anaconda-mode 1)
|
2015-07-21 12:09:21 +01:00
|
|
|
(eldoc-mode 1)
|
2015-07-21 13:12:38 +01:00
|
|
|
|
|
|
|
(setup-keybindings)
|
|
|
|
(with-ability auto-pair
|
|
|
|
(setq-local electric-layout-rules
|
|
|
|
'((?: . (lambda ()
|
|
|
|
(and (zerop (first (syntax-ppss)))
|
|
|
|
(python-info-statement-starts-block-p)
|
|
|
|
'after))))))
|
|
|
|
|
|
|
|
;; FIXME: we don't use imenu either remove this or
|
|
|
|
;; setup imenu too
|
2015-07-21 12:09:21 +01:00
|
|
|
(when (fboundp #'python-imenu-create-flat-index)
|
|
|
|
(setq-local imenu-create-index-function
|
|
|
|
#'python-imenu-create-flat-index))
|
|
|
|
(add-hook 'post-self-insert-hook
|
2019-07-05 09:03:41 +01:00
|
|
|
#'electric-layout-post-self-insert-function nil 'local))
|
2018-07-04 11:38:42 +01:00
|
|
|
;(add-hook 'after-save-hook 'python-mode-set-encoding nil 'local))
|
2019-07-05 09:03:41 +01:00
|
|
|
|
2015-07-21 12:09:21 +01:00
|
|
|
;;;###autoload
|
|
|
|
(defun extensions/python-initialize ()
|
2018-08-30 12:20:22 +01:00
|
|
|
(ability jedi ()
|
|
|
|
"Python autocompletion based on Jedi"
|
|
|
|
(add-hook 'python-mode-hook 'jedi-python-mode-hook))
|
|
|
|
|
2018-08-30 11:51:34 +01:00
|
|
|
(ability elpy ()
|
|
|
|
"Full feature python IDE. (A little bit heavy)"
|
|
|
|
|
|
|
|
(require 'py-autopep8)
|
2019-07-05 09:03:41 +01:00
|
|
|
(advice-add 'python-mode :before 'elpy-enable)
|
2018-08-30 11:51:34 +01:00
|
|
|
|
|
|
|
(setq python-shell-interpreter "ipython"
|
|
|
|
python-shell-interpreter-args "-i --simple-prompt")
|
|
|
|
|
|
|
|
;; enable autopep8 formatting on save
|
2019-07-05 09:03:41 +01:00
|
|
|
(add-hook 'elpy-mode-hook 'py-autopep8-enable-on-save))
|
|
|
|
|
2019-12-20 14:27:31 +00:00
|
|
|
(ability python-black '()
|
|
|
|
"Add support for black syntax linter"
|
|
|
|
(require 'python-black)
|
|
|
|
(add-hook 'python-mode-hook 'python-black-on-save-mode))
|
2018-08-30 11:51:34 +01:00
|
|
|
|
2018-07-04 11:38:42 +01:00
|
|
|
(ability venv ()
|
|
|
|
"Virtualenv support"
|
|
|
|
(require 'virtualenvwrapper)
|
|
|
|
(venv-initialize-interactive-shells)
|
2019-07-05 09:03:41 +01:00
|
|
|
(venv-initialize-eshell))
|
|
|
|
|
2015-07-21 13:12:38 +01:00
|
|
|
(ability python-editor ()
|
|
|
|
"Gives FG42 the ability to edit pytho codes."
|
|
|
|
|
|
|
|
(add-hook 'python-mode-hook 'python-mode-defaults)
|
2018-07-04 11:38:42 +01:00
|
|
|
(add-hook 'after-init-hook #'global-flycheck-mode)
|
2015-07-21 13:12:38 +01:00
|
|
|
(when (fboundp 'exec-path-from-shell-copy-env)
|
|
|
|
(exec-path-from-shell-copy-env "PYTHONPATH"))
|
2015-07-21 12:09:21 +01:00
|
|
|
|
2015-07-21 13:12:38 +01:00
|
|
|
(with-ability kivy-editor
|
|
|
|
(add-to-list 'auto-mode-alist
|
|
|
|
'("\\.kv\\'" . kivy-mode)))
|
2015-07-21 12:09:21 +01:00
|
|
|
|
2015-07-21 13:12:38 +01:00
|
|
|
(with-ability cython-editor
|
|
|
|
(add-to-list 'auto-mode-alist
|
|
|
|
'("\\.pyd\\'" . cython-mode))
|
|
|
|
(add-to-list 'auto-mode-alist
|
|
|
|
'("\\.pyi\\'" . cython-mode))
|
|
|
|
(add-to-list 'auto-mode-alist
|
2019-07-05 09:03:41 +01:00
|
|
|
'("\\.pyx\\'" . cython-mode)))
|
2019-12-09 11:26:09 +00:00
|
|
|
(ability lsp-python ()
|
|
|
|
;; Instruct LSP to use pyls
|
|
|
|
(require 'lsp-python-ms)
|
|
|
|
;; (lsp-register-client
|
|
|
|
;; (make-lsp-client :new-connection (lsp-stdio-connection "pyls")
|
|
|
|
;; :major-modes '(python-mode)
|
|
|
|
;; :server-id 'pyls))
|
|
|
|
;; Setup LSP for python mode
|
|
|
|
(add-hook 'python-mode-hook
|
|
|
|
(lambda ()
|
|
|
|
(push 'company-lsp company-backends)
|
|
|
|
(lsp)
|
|
|
|
(setq lsp-ui-sideline-show-code-actions nil)))))
|
|
|
|
|
2015-07-21 12:09:21 +01:00
|
|
|
|
|
|
|
|
2015-07-21 13:12:38 +01:00
|
|
|
(ability python-code-completion ('code-completion)
|
2019-07-05 09:03:41 +01:00
|
|
|
"Gives FG42 the ability to complete python codes.")
|
2015-07-21 12:09:21 +01:00
|
|
|
|
2018-07-04 11:38:42 +01:00
|
|
|
;(when (boundp 'company-backends)
|
2016-11-20 09:17:45 +00:00
|
|
|
; (add-to-list 'company-backends 'company-anaconda))))
|
2019-07-05 09:03:41 +01:00
|
|
|
|
2019-01-23 14:10:15 +00:00
|
|
|
(message "'python' extension has been initialized."))
|
2015-07-21 12:09:21 +01:00
|
|
|
|
|
|
|
(provide 'extensions/python/init)
|