Skip to content

Commit

Permalink
Don't remove trailing whitespaces in diff as well as in data sections…
Browse files Browse the repository at this point in the history
… in perl and ruby
  • Loading branch information
pkryger committed Oct 26, 2024
1 parent 064cda8 commit f25e3fd
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 4 deletions.
70 changes: 66 additions & 4 deletions modules/init-look-and-feel.el
Original file line number Diff line number Diff line change
Expand Up @@ -267,18 +267,80 @@
(use-package vlf-setup
:ensure vlf)

(defun exordium-delete-trailing-whitespace-in-buffer (&optional start end)
"Delete trailing whitespace in current buffer.
Like `delete-trailing-whitespace', but any restrictions are
ignored.
Additionally, when called with a nil END and
`exordium-delete-trailing-whitespace-skip-data' is non-nil it
will not affect data sections in Perl and Ruby modes. When both
END and `exordium-delete-trailing-whitespace-skip-data' are nil
it will delete whitespace in the whole buffer. When called with
a non-nil END it will delete trailing whitespace up to END.
If called interactively, START and END are the start/end of the
region if the mark is active, or the whole buffer, up to data
secion (when `exordium-delete-trailing-whitespace-skip-data' is
non-nil), if the mark is inactive.
The function doesn't delete trailing whitespaces when buffer is
in `diff-mode', `exordium-delete-trailing-whitespace-skip-data'
is non-nil, and the function has been called without a prefix argument.
Data section starts after a line with a one of keywords __END__
or __DATA__ or a one of control characters (^D, ^Z) and ends at
the end of file. Such a preservation of whitespaces may be
important for some types of data, i.e., patches. See
https://perldoc.perl.org/perldata and
https://docs.ruby-lang.org/en/3.3/Object.html for more details."
(interactive (progn
(barf-if-buffer-read-only)
(if (use-region-p)
(list (region-beginning) (region-end))
(list nil nil))))
(if (and exordium-delete-trailing-whitespace-skip-data
(not current-prefix-arg)
(derived-mode-p 'diff-mode))
(when (called-interactively-p 'any)
(message "Not deleting trailing whitespaces in %s mode. Call with prefix to override."
major-mode))
(without-restriction
(let ((end (or end
(when-let ((exordium-delete-trailing-whitespace-skip-data)
(keywords (cond
((derived-mode-p 'ruby-mode
'ruby-ts-mode
'enh-ruby-mode)
'("__END__"))
((derived-mode-p 'perl-mode
'perl-ts-mode)
'"__END__" "__DATA__" "" ""))))
(save-excursion
(save-match-data
(goto-char (point-min))
(when (re-search-forward
(rx-to-string
`(seq bol (or ,@keywords) (zero-or-more whitespace) eol)
t)
nil t)
(point))))))))
(delete-trailing-whitespace start end)))))

;; Remove trailing blanks on save
(define-minor-mode delete-trailing-whitespace-mode
"Remove trailing whitespace upon saving a buffer"
"Remove trailing whitespace upon saving a buffer.
See `exordium-delete-trailing-whitespace-in-buffer' for more details."
:lighter nil
(if delete-trailing-whitespace-mode
(add-hook 'before-save-hook #'delete-trailing-whitespace nil t)
(remove-hook 'before-save-hook #'delete-trailing-whitespace t)))
(add-hook 'before-save-hook #'exordium-delete-trailing-whitespace-in-buffer nil t)
(remove-hook 'before-save-hook #'exordium-delete-trailing-whitespace-in-buffer t)))

(define-globalized-minor-mode global-delete-trailing-whitespace-mode
delete-trailing-whitespace-mode
(lambda ()
(delete-trailing-whitespace-mode t)))
(delete-trailing-whitespace-mode t))
:group 'exordium)

(when exordium-delete-trailing-whitespace
(global-delete-trailing-whitespace-mode t))
Expand Down
10 changes: 10 additions & 0 deletions modules/init-prefs.el
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,16 @@ projects that have a lot of trailing whitespaces"
:group 'exordium
:type 'boolean)

(defcustom exordium-delete-trailing-whitespace-skip-data t
"When t preserve whitespaces in Data section in Perl and Ruby modes.
This variable is used by
`exordium-delete-trailing-whitespace-in-buffer' (see docstring
for details) and in consequence in
`delete-trailing-whitespace-mode' and
`global-delete-trailing-whitespace-mode'."
:group 'exordium
:type 'boolean)

(defcustom exordium-enable-electric-pair-mode t
"Enables electric-pair-mode."
:group 'exordium
Expand Down

0 comments on commit f25e3fd

Please sign in to comment.