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.

112 lines
4.0 KiB

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