2012-12-15 22:07:25 +02:00
|
|
|
;;; prelude-python.el --- Emacs Prelude: python.el configuration.
|
|
|
|
;;
|
2020-01-20 16:35:36 +02:00
|
|
|
;; Copyright © 2011-2020 Bozhidar Batsov
|
2012-12-15 22:07:25 +02:00
|
|
|
;;
|
|
|
|
;; Author: Bozhidar Batsov <bozhidar@batsov.com>
|
2013-03-07 09:57:33 +02:00
|
|
|
;; URL: https://github.com/bbatsov/prelude
|
2012-12-15 22:07:25 +02:00
|
|
|
|
|
|
|
;; This file is not part of GNU Emacs.
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
;; Some basic configuration for python.el (the latest and greatest
|
|
|
|
;; Python mode Emacs has to offer).
|
|
|
|
|
|
|
|
;;; License:
|
|
|
|
|
|
|
|
;; This program is free software; you can redistribute it and/or
|
|
|
|
;; modify it under the terms of the GNU General Public License
|
|
|
|
;; as published by the Free Software Foundation; either version 3
|
|
|
|
;; of the License, or (at your option) any later version.
|
|
|
|
;;
|
|
|
|
;; This program is distributed in the hope that it will be useful,
|
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
;;
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
|
|
|
;; along with GNU Emacs; see the file COPYING. If not, write to the
|
|
|
|
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
;; Boston, MA 02110-1301, USA.
|
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
2014-09-04 04:49:07 +03:00
|
|
|
(prelude-require-package 'anaconda-mode)
|
|
|
|
|
|
|
|
(when (boundp 'company-backends)
|
|
|
|
(prelude-require-package 'company-anaconda)
|
|
|
|
(add-to-list 'company-backends 'company-anaconda))
|
|
|
|
|
2014-10-17 09:33:37 +03:00
|
|
|
(require 'electric)
|
2012-12-15 22:07:25 +02:00
|
|
|
(require 'prelude-programming)
|
|
|
|
|
2014-08-20 11:14:05 +03:00
|
|
|
;; Copy pasted from ruby-mode.el
|
|
|
|
(defun prelude-python--encoding-comment-required-p ()
|
|
|
|
(re-search-forward "[^\0-\177]" nil t))
|
|
|
|
|
|
|
|
(defun prelude-python--detect-encoding ()
|
|
|
|
(let ((coding-system
|
|
|
|
(or save-buffer-coding-system
|
|
|
|
buffer-file-coding-system)))
|
|
|
|
(if coding-system
|
|
|
|
(symbol-name
|
2014-08-30 18:30:16 +03:00
|
|
|
(or (coding-system-get coding-system 'mime-charset)
|
|
|
|
(coding-system-change-eol-conversion coding-system nil)))
|
2014-08-20 11:14:05 +03:00
|
|
|
"ascii-8bit")))
|
|
|
|
|
|
|
|
(defun prelude-python--insert-coding-comment (encoding)
|
|
|
|
(let ((newlines (if (looking-at "^\\s *$") "\n" "\n\n")))
|
|
|
|
(insert (format "# coding: %s" encoding) newlines)))
|
|
|
|
|
|
|
|
(defun prelude-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--encoding-comment-required-p)
|
|
|
|
(goto-char (point-min))
|
|
|
|
(let ((coding-system (prelude-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 (prelude-python--insert-coding-comment coding-system)))
|
|
|
|
(when (buffer-modified-p)
|
|
|
|
(basic-save-buffer-1)))))))
|
|
|
|
|
2014-10-15 13:30:17 +03:00
|
|
|
(when (fboundp 'exec-path-from-shell-copy-env)
|
|
|
|
(exec-path-from-shell-copy-env "PYTHONPATH"))
|
|
|
|
|
2012-12-15 22:07:25 +02:00
|
|
|
(defun prelude-python-mode-defaults ()
|
2013-04-21 22:28:32 +03:00
|
|
|
"Defaults for Python programming."
|
2014-08-20 11:12:29 +03:00
|
|
|
(subword-mode +1)
|
2015-04-16 20:14:34 +03:00
|
|
|
(anaconda-mode 1)
|
|
|
|
(eldoc-mode 1)
|
2014-08-20 11:12:29 +03:00
|
|
|
(setq-local electric-layout-rules
|
2014-08-30 18:30:16 +03:00
|
|
|
'((?: . (lambda ()
|
2014-09-18 17:11:28 +03:00
|
|
|
(and (zerop (first (syntax-ppss)))
|
|
|
|
(python-info-statement-starts-block-p)
|
|
|
|
'after)))))
|
2014-08-30 18:30:16 +03:00
|
|
|
(when (fboundp #'python-imenu-create-flat-index)
|
|
|
|
(setq-local imenu-create-index-function
|
|
|
|
#'python-imenu-create-flat-index))
|
2014-10-15 13:23:31 +03:00
|
|
|
(add-hook 'post-self-insert-hook
|
|
|
|
#'electric-layout-post-self-insert-function nil 'local)
|
2014-08-20 11:14:05 +03:00
|
|
|
(add-hook 'after-save-hook 'prelude-python-mode-set-encoding nil 'local))
|
2012-12-15 22:07:25 +02:00
|
|
|
|
|
|
|
(setq prelude-python-mode-hook 'prelude-python-mode-defaults)
|
|
|
|
|
|
|
|
(add-hook 'python-mode-hook (lambda ()
|
|
|
|
(run-hooks 'prelude-python-mode-hook)))
|
2014-09-04 04:49:07 +03:00
|
|
|
|
2012-12-15 22:07:25 +02:00
|
|
|
(provide 'prelude-python)
|
|
|
|
|
|
|
|
;;; prelude-python.el ends here
|