From d226fd63f72aa0a2226d98fd0e39d122f99dd924 Mon Sep 17 00:00:00 2001 From: toctan Date: Wed, 25 Jun 2014 09:36:23 +0800 Subject: [PATCH 1/3] 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 --- core/prelude-core.el | 53 ++---------------------------------------- core/prelude-editor.el | 11 +++++++++ core/prelude-mode.el | 6 ----- 3 files changed, 13 insertions(+), 57 deletions(-) diff --git a/core/prelude-core.el b/core/prelude-core.el index 7a9a144..f1a0cec 100644 --- a/core/prelude-core.el +++ b/core/prelude-core.el @@ -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,11 @@ there's a region, all lines that region covers will be duplicated." (cond ((search-forward " Date: Wed, 25 Jun 2014 10:03:15 +0800 Subject: [PATCH 2/3] Do not indent region with indent sensitive modes - Rename and move yank indent vars into prelude-custom.el - Add coffee-mode to prelude-indent-sensitive-modes - Do not mess up indentation when cleanup with indentation sensitive modes, such as Python. --- core/prelude-core.el | 3 ++- core/prelude-custom.el | 17 +++++++++++++++++ core/prelude-editor.el | 26 +++++++------------------- 3 files changed, 26 insertions(+), 20 deletions(-) diff --git a/core/prelude-core.el b/core/prelude-core.el index f1a0cec..d828e3f 100644 --- a/core/prelude-core.el +++ b/core/prelude-core.el @@ -273,7 +273,8 @@ there's a region, all lines that region covers will be duplicated." "Cleanup a region if selected, otherwise the whole buffer." (interactive) (call-interactively 'untabify) - (call-interactively 'indent-region) + (unless (member major-mode prelude-indent-sensitive-modes) + (call-interactively 'indent-region)) (whitespace-cleanup)) (defun prelude-eval-and-replace () diff --git a/core/prelude-custom.el b/core/prelude-custom.el index 335a1ea..23b4f47 100644 --- a/core/prelude-custom.el +++ b/core/prelude-custom.el @@ -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 diff --git a/core/prelude-editor.el b/core/prelude-editor.el index 99c6dba..55970a5 100644 --- a/core/prelude-editor.el +++ b/core/prelude-editor.el @@ -297,40 +297,28 @@ The body of the advice is in BODY." (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, + "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))) + (member major-mode prelude-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', + "If current mode is one of `prelude-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)) + (not (member major-mode prelude-indent-sensitive-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))))) From 3ac1cc8f00789592a067b334621c25756f3d4541 Mon Sep 17 00:00:00 2001 From: toctan Date: Wed, 25 Jun 2014 10:17:37 +0800 Subject: [PATCH 3/3] Remove duplication advice of yank and yank-pop --- core/prelude-editor.el | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/core/prelude-editor.el b/core/prelude-editor.el index 55970a5..5cc8e70 100644 --- a/core/prelude-editor.el +++ b/core/prelude-editor.el @@ -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) @@ -302,8 +303,8 @@ The body of the advice is in BODY." (if (<= (- end beg) prelude-yank-indent-threshold) (indent-region beg end nil))) -(defadvice yank (after yank-indent activate) - "If current mode is one of 'prelude-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 prelude-indent-sensitive-modes)) @@ -312,16 +313,6 @@ indent yanked text (with prefix arg don't indent)." (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 `prelude-yank-indent-modes', -indent yanked text (with prefix arg don't indent)." - (when (and (not (ad-get-arg 0)) - (not (member major-mode prelude-indent-sensitive-modes)) - (or (derived-mode-p 'prog-mode) - (member major-mode prelude-yank-indent-modes))) - (let ((transient-mark-mode nil)) - (yank-advised-indent-function (region-beginning) (region-end))))) - ;; abbrev config (add-hook 'text-mode-hook 'abbrev-mode)