diff --git a/py-autopep8.el b/autopep8-mode.el similarity index 70% rename from py-autopep8.el rename to autopep8-mode.el index 13b1a61..fe727d4 100644 --- a/py-autopep8.el +++ b/autopep8-mode.el @@ -1,70 +1,59 @@ -;;; py-autopep8.el --- Use autopep8 to beautify a Python buffer +;;; autopep8-mode.el --- Use autopep8 to beautify a Python buffer ;; Copyright (C) 2013-2015, Friedrich Paetzke -;; Author: Friedrich Paetzke -;; URL: http://paetzke.me/project/py-autopep8.el -;; Version: 2016.1 +;; Author: Benoit Coste +;; URL: https://github.com/wizmer/autopep8-mode +;; Version: 2018.1 ;;; Commentary: -;; Provides the `py-autopep8' command, which uses the external "autopep8" +;; Provides the `autopep8' command, which uses the external "autopep8" ;; tool to tidy up the current buffer according to Python's PEP8. ;; To automatically apply when saving a python file, use the ;; following code: -;; (add-hook 'python-mode-hook 'py-autopep8-enable-on-save) +;; (add-hook 'python-mode-hook 'autopep8-enable-on-save) ;; To customize the behaviour of "autopep8" you can set the -;; py-autopep8-options e.g. +;; autopep8-options e.g. -;; (setq py-autopep8-options '("--max-line-length=100")) +;; (setq autopep8-options '("--max-line-length=100")) ;;; Code: -(defgroup py-autopep8 nil - "Use autopep8 to beautify a Python buffer." - :group 'convenience - :prefix "py-autopep8-") +(define-minor-mode autopep8-mode + "Automatic python code formating with autopep8" + :group 'autopep8 + (if autopep8-mode + (add-hook 'before-save-hook 'autopep8-buffer nil 'buffer-local) + (remove-hook 'before-save-hook 'autopep8-buffer 'buffer-local))) -(defcustom py-autopep8-options nil +(defcustom autopep8-options nil "Options used for autopep8. Note that `--in-place' is used by default." - :group 'py-autopep8 + :group 'autopep8 :type '(repeat (string :tag "option"))) -(defun py-autopep8--call-executable (errbuf file) +(defun autopep8--call-executable (errbuf file) (zerop (apply 'call-process "autopep8" nil errbuf nil - (append py-autopep8-options `("--in-place", file))))) + (append autopep8-options `("--in-place", file))))) ;;;###autoload -(defun py-autopep8 () - "Deprecated! Use py-autopep8-buffer instead." - (interactive) - (py-autopep8-buffer)) - - -;;;###autoload -(defun py-autopep8-buffer () +(defun autopep8-buffer () "Uses the \"autopep8\" tool to reformat the current buffer." (interactive) - (py-autopep8-bf--apply-executable-to-buffer "autopep8" - 'py-autopep8--call-executable + (autopep8-bf--apply-executable-to-buffer "autopep8" + 'autopep8--call-executable nil "py")) -;;;###autoload -(defun py-autopep8-enable-on-save () - "Pre-save hook to be used before running autopep8." - (interactive) - (add-hook 'before-save-hook 'py-autopep8-buffer nil t)) - ;; BEGIN GENERATED ----------------- ;; !!! This file is generated !!! @@ -78,7 +67,7 @@ Note that `--in-place' is used by default." ;; See LICENSE or https://raw.githubusercontent.com/dominikh/go-mode.el/master/LICENSE -(defun py-autopep8-bf--apply-rcs-patch (patch-buffer) +(defun autopep8-bf--apply-rcs-patch (patch-buffer) "Apply an RCS-formatted diff from PATCH-BUFFER to the current buffer." (let ((target-buffer (current-buffer)) (line-offset 0)) @@ -87,7 +76,7 @@ Note that `--in-place' is used by default." (goto-char (point-min)) (while (not (eobp)) (unless (looking-at "^\\([ad]\\)\\([0-9]+\\) \\([0-9]+\\)") - (error "invalid rcs patch or internal error in py-autopep8-bf--apply-rcs-patch")) + (error "invalid rcs patch or internal error in autopep8-bf--apply-rcs-patch")) (forward-line) (let ((action (match-string 1)) (from (string-to-number (match-string 2))) @@ -110,19 +99,20 @@ Note that `--in-place' is used by default." (kill-whole-line len) (pop kill-ring))) (t - (error "invalid rcs patch or internal error in py-autopep8-bf--apply-rcs-patch"))))))))) + (error "invalid rcs patch or internal error in autopep8-bf--apply-rcs-patch"))))))))) -(defun py-autopep8-bf--replace-region (filename) +(defun autopep8-bf--replace-region (filename) (delete-region (region-beginning) (region-end)) (insert-file-contents filename)) -(defun py-autopep8-bf--apply-executable-to-buffer (executable-name +(defun autopep8-bf--apply-executable-to-buffer (executable-name executable-call only-on-region file-extension) "Formats the current buffer according to the executable" + (message (format "Only on region: %s" file-extension)) (when (not (executable-find executable-name)) (error (format "%s command not found." executable-name))) (let ((tmpfile (make-temp-file executable-name nil (concat "." file-extension))) @@ -148,8 +138,8 @@ Note that `--in-place' is used by default." (message (format "Buffer is already %sed" executable-name))) (if only-on-region - (py-autopep8-bf--replace-region tmpfile) - (py-autopep8-bf--apply-rcs-patch patchbuf)) + (autopep8-bf--replace-region tmpfile) + (autopep8-bf--apply-rcs-patch patchbuf)) (kill-buffer errbuf) (message (format "Applied %s" executable-name))) @@ -159,11 +149,11 @@ Note that `--in-place' is used by default." (delete-file tmpfile))) -;; py-autopep8-bf.el ends here +;; autopep8-bf.el ends here ;; END GENERATED ------------------- -(provide 'py-autopep8) +(provide 'autopep8-mode) -;;; py-autopep8.el ends here +;;; autopep8-mode.el ends here diff --git a/tests.sh b/tests.sh index 003c48e..82f3b50 100755 --- a/tests.sh +++ b/tests.sh @@ -5,7 +5,7 @@ TEST_FILE=/tmp/py-test-file.py # Run the test: # - First argument is test folder # - Second argument defines whether or not -# py-autopep8-buffer should be called +# autopep8-buffer should be called test_autopep() { echo $FUNCNAME $1 rm -f $TEST_FILE @@ -15,11 +15,11 @@ test_autopep() { fi if [ "$2" = true ]; then - call_autopep8="-f py-autopep8-buffer" + call_autopep8="-f autopep8-buffer" fi emacs --batch \ - --load py-autopep8.el \ + --load autopep8-mode.el \ $load_init_file \ ./tests/$1/before.py \ $call_autopep8 \ @@ -33,7 +33,7 @@ test_autopep() { # The package won't actually be installed # in the user distribution because we are using --batch test_install_package() { - emacs --batch py-autopep8.el -f package-install-from-buffer + emacs --batch autopep8-mode.el -f package-install-from-buffer } diff --git a/tests/02/init.el b/tests/02/init.el index 3abedbe..3a3cec6 100644 --- a/tests/02/init.el +++ b/tests/02/init.el @@ -1 +1 @@ -(setq py-autopep8-options '("--max-line-length=130")) +(setq autopep8-options '("--max-line-length=130")) diff --git a/tests/03/init.el b/tests/03/init.el index 735d1a0..d2a0ba1 100644 --- a/tests/03/init.el +++ b/tests/03/init.el @@ -1 +1 @@ -(setq py-autopep8-options '("--max-line-length=120")) +(setq autopep8-options '("--max-line-length=120"))