Skip to content

Commit

Permalink
feat(yank): add threshold option to not indent large yank texts
Browse files Browse the repository at this point in the history
Set to nil by default, there is no change in behavior until a user
customizes the snap-indent-yank-threshold variable.
  • Loading branch information
jimeh committed Apr 29, 2023
1 parent 3480de4 commit f730e1c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,12 @@ To configure via `use-package`, adapt the following example as desired:

### Customization

| Variable | Type | Default | Description |
| :--------------------------- | :--------------- | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :-------------------------------------------- |
| `snap-indent-excluded-modes` | symbol list | `(cmake-ts-mode coffee-mode conf-mode elm-mode haml-mode haskell-mode makefile-automake-mode makefile-bsdmake-mode makefile-gmake-mode makefile-imake-mode makefile-makepp-mode makefile-mode occam-mode python-mode python-ts-mode slim-mode yaml-mode yaml-ts-mode)` | Major modes in which to ignore activation |
| `snap-indent-format` | function or list | `nil` | Additional formatting to apply when indenting |
| `snap-indent-on-save` | boolean | `nil` | Whether to indent the entire buffer on save |
| Variable | Type | Default | Description |
| :--------------------------- | :--------------- | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :------------------------------------------------------------- |
| `snap-indent-excluded-modes` | symbol list | `(cmake-ts-mode coffee-mode conf-mode elm-mode haml-mode haskell-mode makefile-automake-mode makefile-bsdmake-mode makefile-gmake-mode makefile-imake-mode makefile-makepp-mode makefile-mode occam-mode python-mode python-ts-mode slim-mode yaml-mode yaml-ts-mode)` | Major modes in which to ignore activation |
| `snap-indent-format` | function or list | `nil` | Additional formatting to apply when indenting |
| `snap-indent-on-save` | boolean | `nil` | Whether to indent the entire buffer on save |
| `snap-indent-yank-threshold` | boolean | `nil` | Do not indent yanked text if its length exceeds the threshold. |

### Additional formatting

Expand Down
20 changes: 18 additions & 2 deletions snap-indent.el
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,15 @@ tab/space conversion and `delete-trailing-whitespace'."
;; returns the form itself.) Hence, to distinguish what should be wrapped, we
;; must test the value's function-ness not just its list-ness.

(defcustom snap-indent-yank-threshold nil
"Do not indent yanked text if its length exceeds the threshold.
This can help prevent performance issues when yanking large
blocks of text.
When nil, no threshold is applied."
:type 'number)

(defun snap-indent-as-list (function-or-list)
"Return FUNCTION-OR-LIST as a list, treating lambda forms as atoms."
(if (or (not (listp function-or-list)) (functionp function-or-list))
Expand All @@ -117,9 +126,16 @@ tab/space conversion and `delete-trailing-whitespace'."
(snap-indent-indent (point-min) (point-max))))

(defun snap-indent-command-handler ()
"Indent region text on yank."
"Indent region text on yank.
When `snap-indent-yank-threshold' is not nil, do not trigger
snap-indent if the region length exceeds the threshold."
(when (memq this-command '(yank yank-pop))
(snap-indent-indent (region-beginning) (region-end))))
(let ((beg (region-beginning))
(end (region-end)))
(when (or (not snap-indent-yank-threshold)
(<= (- end beg) snap-indent-yank-threshold))
(snap-indent-indent beg end)))))

;;;###Autoload
(define-minor-mode snap-indent-mode
Expand Down

0 comments on commit f730e1c

Please sign in to comment.