Merge pull request #584 from toctan/cleanup-buffer

Cleanup code of `prelude-cleanup-buffer`
This commit is contained in:
Bozhidar Batsov 2014-06-30 14:14:20 +02:00
commit e3217b65e0
4 changed files with 39 additions and 86 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]
'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 ()
"Indent the current defun."
(interactive)
@ -286,43 +269,12 @@ there's a region, all lines that region covers will be duplicated."
(cond ((search-forward "<?xml" nil t) (nxml-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 ()
"Cleanup a region if selected, otherwise the whole buffer."
(interactive)
(save-excursion
(if (region-active-p)
(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."))))
(call-interactively 'untabify)
(unless (member major-mode prelude-indent-sensitive-modes)
(call-interactively 'indent-region))
(whitespace-cleanup))
(defun prelude-eval-and-replace ()

View file

@ -76,6 +76,23 @@ in the desired elisp file."
:type 'string
:group 'prelude)
(defcustom prelude-indent-sensitive-modes
'(coffee-mode python-mode slim-mode haml-mode)
"Modes for which auto-indenting is suppressed."
:type 'list
:group 'prelude)
(defcustom prelude-yank-indent-modes '(LaTeX-mode TeX-mode)
"Modes in which to indent regions that are yanked (or yank-popped).
Only modes that don't derive from `prog-mode' should be listed here."
:type 'list
:group 'prelude)
(defcustom prelude-yank-indent-threshold 1000
"Threshold (# chars) over which indentation does not automatically occur."
:type 'number
:group 'prelude)
(provide 'prelude-custom)
;;; prelude-custom.el ends here

View file

@ -155,19 +155,20 @@
(file-writable-p buffer-file-name))
(save-buffer)))
(defmacro advise-commands (advice-name commands &rest body)
(defmacro advise-commands (advice-name commands class &rest body)
"Apply advice named ADVICE-NAME to multiple COMMANDS.
The body of the advice is in BODY."
`(progn
,@(mapcar (lambda (command)
`(defadvice ,command (before ,(intern (concat (symbol-name command) "-" advice-name)) activate)
`(defadvice ,command (,class ,(intern (concat (symbol-name command) "-" advice-name)) activate)
,@body))
commands)))
;; advise all window switching functions
(advise-commands "auto-save"
(switch-to-buffer other-window windmove-up windmove-down windmove-left windmove-right)
before
(prelude-auto-save-command))
(add-hook 'mouse-leave-buffer-hook 'prelude-auto-save-command)
@ -285,41 +286,30 @@ The body of the advice is in BODY."
(interactive
(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
(defvar yank-indent-modes
'(LaTeX-mode TeX-mode)
"Modes in which to indent regions that are yanked (or yank-popped).
Only modes that don't derive from `prog-mode' should be listed here.")
(defvar yank-indent-blacklisted-modes
'(python-mode slim-mode haml-mode)
"Modes for which auto-indenting is suppressed.")
(defvar yank-advised-indent-threshold 1000
"Threshold (# chars) over which indentation does not automatically occur.")
(defun yank-advised-indent-function (beg end)
"Do indentation, as long as the region isn't too large."
(if (<= (- end beg) yank-advised-indent-threshold)
(if (<= (- end beg) prelude-yank-indent-threshold)
(indent-region beg end nil)))
(defadvice yank (after yank-indent activate)
"If current mode is one of 'yank-indent-modes,
(advise-commands "indent" (yank yank-pop) after
"If current mode is one of `prelude-yank-indent-modes',
indent yanked text (with prefix arg don't indent)."
(if (and (not (ad-get-arg 0))
(not (member major-mode yank-indent-blacklisted-modes))
(not (member major-mode prelude-indent-sensitive-modes))
(or (derived-mode-p 'prog-mode)
(member major-mode yank-indent-modes)))
(let ((transient-mark-mode nil))
(yank-advised-indent-function (region-beginning) (region-end)))))
(defadvice yank-pop (after yank-pop-indent activate)
"If current mode is one of `yank-indent-modes',
indent yanked text (with prefix arg don't indent)."
(when (and (not (ad-get-arg 0))
(not (member major-mode yank-indent-blacklisted-modes))
(or (derived-mode-p 'prog-mode)
(member major-mode yank-indent-modes)))
(member major-mode prelude-yank-indent-modes)))
(let ((transient-mark-mode nil))
(yank-advised-indent-function (region-beginning) (region-end)))))

View file

@ -50,7 +50,6 @@
(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 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-c u") 'prelude-view-url)
(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])
("Buffers"
["Clean up buffer" prelude-cleanup-buffer]
["Clean up buffer or region" prelude-cleanup-buffer-or-region]
["Kill other buffers" prelude-kill-other-buffers])
@ -101,10 +99,6 @@
["Insert empty line" prelude-insert-empty-line]
["Move line up" prelude-move-line-up]
["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]
["Indent rigidly and copy to clipboard" prelude-indent-rigidly-and-copy-to-clipboard]
["Insert date" prelude-insert-date]