Skip to content

Commit

Permalink
Rename to autopep8-mode and make it a minor mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Benoit Coste committed Jul 21, 2018
1 parent 667962d commit bb7449f
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 48 deletions.
74 changes: 32 additions & 42 deletions py-autopep8.el → autopep8-mode.el
Original file line number Diff line number Diff line change
@@ -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 <f.paetzke@gmail.com>

;; Author: Friedrich Paetzke <f.paetzke@gmail.com>
;; URL: http://paetzke.me/project/py-autopep8.el
;; Version: 2016.1
;; Author: Benoit Coste <benoit.coste@protonmail.com>
;; 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 !!!
Expand All @@ -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))
Expand All @@ -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)))
Expand All @@ -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)))
Expand All @@ -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)))
Expand All @@ -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
8 changes: 4 additions & 4 deletions tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 \
Expand All @@ -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
}


Expand Down
2 changes: 1 addition & 1 deletion tests/02/init.el
Original file line number Diff line number Diff line change
@@ -1 +1 @@
(setq py-autopep8-options '("--max-line-length=130"))
(setq autopep8-options '("--max-line-length=130"))
2 changes: 1 addition & 1 deletion tests/03/init.el
Original file line number Diff line number Diff line change
@@ -1 +1 @@
(setq py-autopep8-options '("--max-line-length=120"))
(setq autopep8-options '("--max-line-length=120"))

0 comments on commit bb7449f

Please sign in to comment.