From 4d447e723afcb84e76121f4901e898b2fd398666 Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Thu, 7 May 2020 19:06:14 +0100 Subject: [PATCH] feat(text-editing): Safer case change keybindings (C-x C-u, C-x C-l) By default when region is not active, both `downcase-region` (C-x C-l) and `upcase-region` (C-x C-u) will operate on surrounding text in different ways depending on major-mode. This has caught me off guard a few times. So let's replace them with safer `downcase-region-only` and `upcase-region-only` functions which ONLY perform changes when the region is active. And obviously changes are only applied to the text within the region. --- core/siren-core-modules.el | 1 + .../text-editing/siren-safe-change-case.el | 32 +++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 modules/text-editing/siren-safe-change-case.el diff --git a/core/siren-core-modules.el b/core/siren-core-modules.el index 049dfee..aa1a30d 100644 --- a/core/siren-core-modules.el +++ b/core/siren-core-modules.el @@ -88,6 +88,7 @@ (require 'siren-move-dup) (require 'siren-multiple-cursors) (require 'siren-randomize-region) +(require 'siren-safe-change-case) (require 'siren-smart-shift) (require 'siren-smartparens) (require 'siren-sort-symbols) diff --git a/modules/text-editing/siren-safe-change-case.el b/modules/text-editing/siren-safe-change-case.el new file mode 100644 index 0000000..8e59d6a --- /dev/null +++ b/modules/text-editing/siren-safe-change-case.el @@ -0,0 +1,32 @@ +;;; siren-safe-change-case.el --- jimeh's Emacs Siren: change-case + +;;; Commentary: + +;; Set strict/safer case changing functions to C-x C-u and C-x C-l which ONLY +;; perform case modifications when region is active. + +;;; Code: + +(defun downcase-region-only (beg end &rest args) + "Only downcase if region (BEG END) is active. + +Avoids accidental downcase when region is not active. Passes all +additional ARGS passed along to `downcase-region'." + (interactive "r") + (when (region-active-p) + (apply #'downcase-region beg end args))) + +(defun upcase-region-only (beg end &rest args) + "Only upcase if region (BEG END) is active. + +Avoids accidental upcase when region is not active. Passes all +additional ARGS passed along to `upcase-region'." + (interactive "r") + (when (region-active-p) + (apply #'upcase-region beg end args))) + +(global-set-key (kbd "C-x C-l") 'downcase-region-only) +(global-set-key (kbd "C-x C-u") 'upcase-region-only) + +(provide 'siren-safe-change-case) +;;; siren-safe-change-case.el ends here