Emacs config utilizing prelude as a base
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
4.2 KiB

  1. ;;; prelude-python.el --- Emacs Prelude: python.el configuration.
  2. ;;
  3. ;; Copyright © 2011-2020 Bozhidar Batsov
  4. ;;
  5. ;; Author: Bozhidar Batsov <bozhidar@batsov.com>
  6. ;; URL: https://github.com/bbatsov/prelude
  7. ;; This file is not part of GNU Emacs.
  8. ;;; Commentary:
  9. ;; Some basic configuration for python.el (the latest and greatest
  10. ;; Python mode Emacs has to offer).
  11. ;;; License:
  12. ;; This program is free software; you can redistribute it and/or
  13. ;; modify it under the terms of the GNU General Public License
  14. ;; as published by the Free Software Foundation; either version 3
  15. ;; of the License, or (at your option) any later version.
  16. ;;
  17. ;; This program is distributed in the hope that it will be useful,
  18. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. ;; GNU General Public License for more details.
  21. ;;
  22. ;; You should have received a copy of the GNU General Public License
  23. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  24. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  25. ;; Boston, MA 02110-1301, USA.
  26. ;;; Code:
  27. (defcustom prelude-python-mode-set-encoding-automatically nil
  28. "Non-nil values enable auto insertion of '# coding: utf-8' on python buffers."
  29. :type 'boolean
  30. :group 'prelude)
  31. (prelude-require-package 'anaconda-mode)
  32. (when (boundp 'company-backends)
  33. (prelude-require-package 'company-anaconda)
  34. (add-to-list 'company-backends 'company-anaconda))
  35. (require 'electric)
  36. (require 'prelude-programming)
  37. ;; Copy pasted from ruby-mode.el
  38. (defun prelude-python--encoding-comment-required-p ()
  39. (re-search-forward "[^\0-\177]" nil t))
  40. (defun prelude-python--detect-encoding ()
  41. (let ((coding-system
  42. (or save-buffer-coding-system
  43. buffer-file-coding-system)))
  44. (if coding-system
  45. (symbol-name
  46. (or (coding-system-get coding-system 'mime-charset)
  47. (coding-system-change-eol-conversion coding-system nil)))
  48. "ascii-8bit")))
  49. (defun prelude-python--insert-coding-comment (encoding)
  50. (let ((newlines (if (looking-at "^\\s *$") "\n" "\n\n")))
  51. (insert (format "# coding: %s" encoding) newlines)))
  52. (defun prelude-python-mode-set-encoding ()
  53. "Insert a magic comment header with the proper encoding if necessary."
  54. (save-excursion
  55. (widen)
  56. (goto-char (point-min))
  57. (when (prelude-python--encoding-comment-required-p)
  58. (goto-char (point-min))
  59. (let ((coding-system (prelude-python--detect-encoding)))
  60. (when coding-system
  61. (if (looking-at "^#!") (beginning-of-line 2))
  62. (cond ((looking-at "\\s *#\\s *.*\\(en\\)?coding\\s *:\\s *\\([-a-z0-9_]*\\)")
  63. ;; update existing encoding comment if necessary
  64. (unless (string= (match-string 2) coding-system)
  65. (goto-char (match-beginning 2))
  66. (delete-region (point) (match-end 2))
  67. (insert coding-system)))
  68. ((looking-at "\\s *#.*coding\\s *[:=]"))
  69. (t (prelude-python--insert-coding-comment coding-system)))
  70. (when (buffer-modified-p)
  71. (basic-save-buffer-1)))))))
  72. (when (fboundp 'exec-path-from-shell-copy-env)
  73. (exec-path-from-shell-copy-env "PYTHONPATH"))
  74. (defun prelude-python-mode-defaults ()
  75. "Defaults for Python programming."
  76. (subword-mode +1)
  77. (anaconda-mode 1)
  78. (eldoc-mode 1)
  79. (setq-local electric-layout-rules
  80. '((?: . (lambda ()
  81. (and (zerop (first (syntax-ppss)))
  82. (python-info-statement-starts-block-p)
  83. 'after)))))
  84. (when (fboundp #'python-imenu-create-flat-index)
  85. (setq-local imenu-create-index-function
  86. #'python-imenu-create-flat-index))
  87. (add-hook 'post-self-insert-hook
  88. #'electric-layout-post-self-insert-function nil 'local)
  89. (when prelude-python-mode-set-encoding-automatically
  90. (add-hook 'after-save-hook 'prelude-python-mode-set-encoding nil 'local)))
  91. (setq prelude-python-mode-hook 'prelude-python-mode-defaults)
  92. (add-hook 'python-mode-hook (lambda ()
  93. (run-hooks 'prelude-python-mode-hook)))
  94. (provide 'prelude-python)
  95. ;;; prelude-python.el ends here