From f92d7305f6a1623e6feefb77e970e995ce0cafd8 Mon Sep 17 00:00:00 2001
From: Christian Shtarkov <ops+git@shtarkov.net>
Date: Mon, 21 Oct 2024 00:09:50 +0100
Subject: [PATCH] Do not zap up to char

---
 shtarkov.net.org | 36 ++++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)

diff --git a/shtarkov.net.org b/shtarkov.net.org
index ddc018b..e495e20 100644
--- a/shtarkov.net.org
+++ b/shtarkov.net.org
@@ -85,3 +85,39 @@ handy here, because it saves the mark where the search starts. I've
 modified it to [[https://endlessparentheses.com/leave-the-cursor-at-start-of-match-after-isearch.html][leave point at the start of the match]], which makes it
 even more convenient. If you find Isearch too cumbersome you can use
 something like [[https://www.emacswiki.org/emacs/iy-go-to-char.el][iy-go-to-char.el]] or [[#/emacs/do-not-zap-up-to-char][do-not-zap-up-to-char]].
+
+* Do not zap up to char
+:properties:
+:one: shtarkov.net-article
+:custom_id: /emacs/do-not-zap-up-to-char/
+:date: <2024-10-20 Sun>
+:end:
+
+It struck me as odd that Emacs has ~zap-to-char~ and ~zap-up-to-char~
+that advance point to a character and kill, but there is no command to
+just advance point. Hence [[https://www.emacswiki.org/emacs/iy-go-to-char.el][iy-go-to-char.el]] and similar. I find myself
+using only to "up to" variant, so I put together this:
+
+#+begin_src elisp
+(defun cs-do-not-zap-up-to-char (arg char &optional interactive)
+  "'zap-up-to-char', but push mark first (unless Transient Mark mode
+is enabled and the mark is active) and do not kill. You can still kill
+with 'C-w' afterwards."
+  (interactive (list (prefix-numeric-value current-prefix-arg)
+		     (read-char-from-minibuffer "Do not zap up to char: "
+						nil 'read-char-history)
+                     t))
+  (let ((direction (if (>= arg 0) 1 -1))
+        (case-fold-search (if (and interactive (char-uppercase-p char))
+                              nil
+                            case-fold-search)))
+    (unless (region-active-p) (push-mark))
+    (forward-char direction)
+    (unwind-protect
+	(search-forward (char-to-string char) nil nil arg)
+      (backward-char direction))))
+#+end_src
+
+This is basically Isearch, but for one character only. You still have
+the option to kill afterwards with =C-w=, because the mark is saved
+where the search began (just like Isearch).
-- 
2.43.5