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.

109 lines
3.9 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. (prelude-require-package 'anaconda-mode)
  28. (when (boundp 'company-backends)
  29. (prelude-require-package 'company-anaconda)
  30. (add-to-list 'company-backends 'company-anaconda))
  31. (require 'electric)
  32. (require 'prelude-programming)
  33. ;; Copy pasted from ruby-mode.el
  34. (defun prelude-python--encoding-comment-required-p ()
  35. (re-search-forward "[^\0-\177]" nil t))
  36. (defun prelude-python--detect-encoding ()
  37. (let ((coding-system
  38. (or save-buffer-coding-system
  39. buffer-file-coding-system)))
  40. (if coding-system
  41. (symbol-name
  42. (or (coding-system-get coding-system 'mime-charset)
  43. (coding-system-change-eol-conversion coding-system nil)))
  44. "ascii-8bit")))
  45. (defun prelude-python--insert-coding-comment (encoding)
  46. (let ((newlines (if (looking-at "^\\s *$") "\n" "\n\n")))
  47. (insert (format "# coding: %s" encoding) newlines)))
  48. (defun prelude-python-mode-set-encoding ()
  49. "Insert a magic comment header with the proper encoding if necessary."
  50. (save-excursion
  51. (widen)
  52. (goto-char (point-min))
  53. (when (prelude-python--encoding-comment-required-p)
  54. (goto-char (point-min))
  55. (let ((coding-system (prelude-python--detect-encoding)))
  56. (when coding-system
  57. (if (looking-at "^#!") (beginning-of-line 2))
  58. (cond ((looking-at "\\s *#\\s *.*\\(en\\)?coding\\s *:\\s *\\([-a-z0-9_]*\\)")
  59. ;; update existing encoding comment if necessary
  60. (unless (string= (match-string 2) coding-system)
  61. (goto-char (match-beginning 2))
  62. (delete-region (point) (match-end 2))
  63. (insert coding-system)))
  64. ((looking-at "\\s *#.*coding\\s *[:=]"))
  65. (t (prelude-python--insert-coding-comment coding-system)))
  66. (when (buffer-modified-p)
  67. (basic-save-buffer-1)))))))
  68. (when (fboundp 'exec-path-from-shell-copy-env)
  69. (exec-path-from-shell-copy-env "PYTHONPATH"))
  70. (defun prelude-python-mode-defaults ()
  71. "Defaults for Python programming."
  72. (subword-mode +1)
  73. (anaconda-mode 1)
  74. (eldoc-mode 1)
  75. (setq-local electric-layout-rules
  76. '((?: . (lambda ()
  77. (and (zerop (first (syntax-ppss)))
  78. (python-info-statement-starts-block-p)
  79. 'after)))))
  80. (when (fboundp #'python-imenu-create-flat-index)
  81. (setq-local imenu-create-index-function
  82. #'python-imenu-create-flat-index))
  83. (add-hook 'post-self-insert-hook
  84. #'electric-layout-post-self-insert-function nil 'local)
  85. (add-hook 'after-save-hook 'prelude-python-mode-set-encoding nil 'local))
  86. (setq prelude-python-mode-hook 'prelude-python-mode-defaults)
  87. (add-hook 'python-mode-hook (lambda ()
  88. (run-hooks 'prelude-python-mode-hook)))
  89. (provide 'prelude-python)
  90. ;;; prelude-python.el ends here