Clean up prelude-cleanup-buffer code

- Create a macro `with-region-or-buffer` to replace the prelude wrapper
  around `untabify` and `indent-region`
- Remove the function operated on buffers, cause we only need the dwim
  one
This commit is contained in:
toctan 2014-06-25 09:36:23 +08:00
parent 2e8859b936
commit d226fd63f7
3 changed files with 13 additions and 57 deletions

View file

@ -158,23 +158,6 @@ point reaches the beginning or end of the buffer, stop there."
(global-set-key [remap move-beginning-of-line] (global-set-key [remap move-beginning-of-line]
'prelude-move-beginning-of-line) 'prelude-move-beginning-of-line)
(defun prelude-indent-buffer ()
"Indent the currently visited buffer."
(interactive)
(indent-region (point-min) (point-max)))
(defun prelude-indent-buffer-or-region ()
"Indent a region if selected, otherwise the whole buffer."
(interactive)
(save-excursion
(if (region-active-p)
(progn
(indent-region (region-beginning) (region-end))
(message "Indented selected region."))
(progn
(prelude-indent-buffer)
(message "Indented buffer.")))))
(defun prelude-indent-defun () (defun prelude-indent-defun ()
"Indent the current defun." "Indent the current defun."
(interactive) (interactive)
@ -286,43 +269,11 @@ there's a region, all lines that region covers will be duplicated."
(cond ((search-forward "<?xml" nil t) (nxml-mode)) (cond ((search-forward "<?xml" nil t) (nxml-mode))
((search-forward "<html" nil t) (html-mode))))) ((search-forward "<html" nil t) (html-mode)))))
(defun prelude-untabify-buffer ()
"Remove all tabs from the current buffer."
(interactive)
(untabify (point-min) (point-max)))
(defun prelude-untabify-buffer-or-region ()
"Untabify a region if selected, otherwise the whole buffer."
(interactive)
(save-excursion
(if (region-active-p)
(progn
(untabify (region-beginning) (region-end))
(message "Untabify selected region."))
(progn
(prelude-untabify-buffer)
(message "Untabify buffer.")))))
(defun prelude-cleanup-buffer ()
"Perform a bunch of operations on the whitespace content of a buffer."
(interactive)
(prelude-untabify-buffer)
(prelude-indent-buffer)
(whitespace-cleanup))
(defun prelude-cleanup-buffer-or-region () (defun prelude-cleanup-buffer-or-region ()
"Cleanup a region if selected, otherwise the whole buffer." "Cleanup a region if selected, otherwise the whole buffer."
(interactive) (interactive)
(save-excursion (call-interactively 'untabify)
(if (region-active-p) (call-interactively 'indent-region)
(progn
(untabify (region-beginning) (region-end))
(indent-region (region-beginning) (region-end))
(message "Cleanup selected region."))
(progn
(prelude-untabify-buffer)
(prelude-indent-buffer)
(message "Cleanup buffer."))))
(whitespace-cleanup)) (whitespace-cleanup))
(defun prelude-eval-and-replace () (defun prelude-eval-and-replace ()

View file

@ -285,6 +285,17 @@ The body of the advice is in BODY."
(interactive (interactive
(list (not (region-active-p))))) (list (not (region-active-p)))))
(defmacro with-region-or-buffer (func)
"When called with no active region, call FUNC on current buffer."
`(defadvice ,func (before with-region-or-buffer activate compile)
(interactive
(if mark-active
(list (region-beginning) (region-end))
(list (point-min) (point-max))))))
(with-region-or-buffer indent-region)
(with-region-or-buffer untabify)
;; automatically indenting yanked text if in programming-modes ;; automatically indenting yanked text if in programming-modes
(defvar yank-indent-modes (defvar yank-indent-modes
'(LaTeX-mode TeX-mode) '(LaTeX-mode TeX-mode)

View file

@ -50,7 +50,6 @@
(define-key map [(meta shift down)] 'move-text-down) (define-key map [(meta shift down)] 'move-text-down)
(define-key map (kbd "C-c n") 'prelude-cleanup-buffer-or-region) (define-key map (kbd "C-c n") 'prelude-cleanup-buffer-or-region)
(define-key map (kbd "C-c f") 'prelude-recentf-ido-find-file) (define-key map (kbd "C-c f") 'prelude-recentf-ido-find-file)
(define-key map (kbd "C-M-\\") 'prelude-indent-buffer-or-region)
(define-key map (kbd "C-M-z") 'prelude-indent-defun) (define-key map (kbd "C-M-z") 'prelude-indent-defun)
(define-key map (kbd "C-c u") 'prelude-view-url) (define-key map (kbd "C-c u") 'prelude-view-url)
(define-key map (kbd "C-c e") 'prelude-eval-and-replace) (define-key map (kbd "C-c e") 'prelude-eval-and-replace)
@ -93,7 +92,6 @@
["Copy file name to clipboard" prelude-copy-file-name-to-clipboard]) ["Copy file name to clipboard" prelude-copy-file-name-to-clipboard])
("Buffers" ("Buffers"
["Clean up buffer" prelude-cleanup-buffer]
["Clean up buffer or region" prelude-cleanup-buffer-or-region] ["Clean up buffer or region" prelude-cleanup-buffer-or-region]
["Kill other buffers" prelude-kill-other-buffers]) ["Kill other buffers" prelude-kill-other-buffers])
@ -101,10 +99,6 @@
["Insert empty line" prelude-insert-empty-line] ["Insert empty line" prelude-insert-empty-line]
["Move line up" prelude-move-line-up] ["Move line up" prelude-move-line-up]
["Move line down" prelude-move-line-down] ["Move line down" prelude-move-line-down]
["Indent buffer" prelude-indent-buffer]
["Indent buffer or region" prelude-indent-buffer-or-region]
["Untabify buffer" prelude-untabify-buffer]
["Untabify buffer or region" prelude-untabify-buffer-or-region]
["Duplicate line or region" prelude-duplicate-current-line-or-region] ["Duplicate line or region" prelude-duplicate-current-line-or-region]
["Indent rigidly and copy to clipboard" prelude-indent-rigidly-and-copy-to-clipboard] ["Indent rigidly and copy to clipboard" prelude-indent-rigidly-and-copy-to-clipboard]
["Insert date" prelude-insert-date] ["Insert date" prelude-insert-date]