Skip to content

Instantly share code, notes, and snippets.

@kracekumar
Last active June 17, 2022 11:16

Revisions

  1. kracekumar revised this gist Aug 17, 2014. 1 changed file with 27 additions and 19 deletions.
    46 changes: 27 additions & 19 deletions auto-remove.el
    Original file line number Diff line number Diff line change
    @@ -1,27 +1,35 @@
    ;; Auto remove unused functions in python
    ;;; auto-remove.el --- Auto remove unused functions in python

    ;;; Commentary:
    ;; Uses external tool autoflake to remove unused imports from a Python file.

    ;;; Code:

    (defcustom python-autoflake-path (executable-find "autoflake")
    "Autoflake executable path.
    Allows working with a virtualenv without actually adding support
    for it."
    :group 'python
    :type 'string)

    (defun python-remove-unused-imports()
    "Use Autoflake to remove unused function"
    "autoflake --remove-all-unused-imports -i unused_imports.py"

    (defun python-remove-unused-imports ()
    "Use Autoflake to remove unused function.
    $ autoflake --remove-all-unused-imports -i unused_imports.py"
    (interactive)
    (shell-command
    (format "%s --remove-all-unused-imports -i %s"
    python-autoflake-path
    (shell-quote-argument (buffer-file-name))))
    (revert-buffer t t t))

    (unless python-autoflake-path
    (error "Unable to find autoflake. Configure `python-autoflake-path`"))

    ;; Run python-remove-unused-imports before save
    ; As jaseemabid pointed out this gets called on all buffers
    ;(add-hook 'python-mode-hook
    ; (lambda ()
    ; (add-hook 'before-save-hook 'python-remove-unused-imports)))
    (when (eq major-mode 'python-mode)
    (shell-command (format "%s --remove-all-unused-imports -i %s"
    python-autoflake-path
    (shell-quote-argument (buffer-file-name))))
    (revert-buffer t t t))
    nil)

    (eval-after-load 'python
    '(if python-autoflake-path
    (add-hook 'before-save-hook 'python-remove-unused-imports)
    (message "Unable to find autoflake. Configure `python-autoflake-path`")))

    (provide 'auto-remove)

    ;;; auto-remove.el ends here
  2. kracekumar revised this gist Aug 17, 2014. 1 changed file with 4 additions and 3 deletions.
    7 changes: 4 additions & 3 deletions auto-remove.el
    Original file line number Diff line number Diff line change
    @@ -21,6 +21,7 @@ for it."
    (error "Unable to find autoflake. Configure `python-autoflake-path`"))

    ;; Run python-remove-unused-imports before save
    (add-hook 'python-mode-hook
    (lambda ()
    (add-hook 'before-save-hook 'python-remove-unused-imports)))
    ; As jaseemabid pointed out this gets called on all buffers
    ;(add-hook 'python-mode-hook
    ; (lambda ()
    ; (add-hook 'before-save-hook 'python-remove-unused-imports)))
  3. kracekumar revised this gist Aug 17, 2014. 1 changed file with 20 additions and 5 deletions.
    25 changes: 20 additions & 5 deletions auto-remove.el
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,26 @@
    ;; Auto remove unused functions in python
    (defcustom python-autoflake-path (executable-find "autoflake")
    "Autoflake executable path.
    Allows working with a virtualenv without actually adding support
    for it."
    :group 'python
    :type 'string)

    (defun python-remove-unused-imports()
    "Use Autoflake to remove unused function"
    "autoflake --remove-all-unused-imports -i unused_imports.py"
    (interactive)
    (shell-command
    (format "autoflake --remove-all-unused-imports -i %s"
    (shell-quote-argument (buffer-file-name)))
    )
    (revert-buffer t t t)
    )
    (format "%s --remove-all-unused-imports -i %s"
    python-autoflake-path
    (shell-quote-argument (buffer-file-name))))
    (revert-buffer t t t))

    (unless python-autoflake-path
    (error "Unable to find autoflake. Configure `python-autoflake-path`"))

    ;; Run python-remove-unused-imports before save
    (add-hook 'python-mode-hook
    (lambda ()
    (add-hook 'before-save-hook 'python-remove-unused-imports)))
  4. kracekumar created this gist Aug 16, 2014.
    11 changes: 11 additions & 0 deletions auto-remove.el
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    ;; Auto remove unused functions in python
    (defun python-remove-unused-imports()
    "Use Autoflake to remove unused function"
    "autoflake --remove-all-unused-imports -i unused_imports.py"
    (interactive)
    (shell-command
    (format "autoflake --remove-all-unused-imports -i %s"
    (shell-quote-argument (buffer-file-name)))
    )
    (revert-buffer t t t)
    )