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.

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