What if I want to select text from columns COL to COL+4 over different rows, i.e., to add or remove characters or spaces at the beginning of multiple lines?
Yes, what if you do? How do you want to tell Emacs which rows to use?
Emacs does let you create such a noncontiguous region. (It lets you create noncontiguous regions of any kind, not just those that fit nicely into a rectangle.)
But it's up to you to define a function (e.g., a command) that somehow gets from the user the info about which rows in a rectangle to keep (or alternatively, which rows not to keep). You assign or bind variable
region-extract-function to your function.
Here's one way you can play with this - but if you're defining a feature for other users then you'll want to think about how you would have them tell you where the pieces of a noncontiguous region are, i.e., how to interactively split up a rectangular region into pieces that are not contiguous (which rows to leave out).
To get a feel for this, and as food for thought: you can leverage library Zones (
zones.el). For example:
In a buffer where you haven't done any narrowing (or if you have, do
(setq zz-izones nil)):
For each piece of text you want to include in your noncontiguous region, in order (e.g. from top to bottom of your rectangle: select it, then hit
C-x n a.
That adds that zone of text to the value of the default zones variable,
Evaluate this code:
(setq orig-r-e-f region-extract-function
(defun foo (method)
(if (eq method 'bounds)
(funcall orig-r-e-f method)))
Use some Emacs command, such as
query-replace, that can act on a noncontiguous region.
(You probably need to activate the region first, e.g. with
C-SPC, even though the region is ignored by the new value of
region-extract-function. This is because commands such as
query-replace only act on a noncontiguous region when they think the region is active.)
When done playing around, do
M-: (setq region-extract-function orig-r-e-f) to restore the original value of that variable.
In a real setting you would do a bit more, and more cleanly, but that gives you an idea. In a nutshell:
C-x n a adds the current active region to the sequence of zones recorded as the value of variable
zz-izones. (A sequence of zones is similar to a vanilla-Emacs noncontiguous region, but it's more general.)
zz-noncontiguous-region-from-izones does just what it says: it takes the zones from variable
zz-izones and returns a corresponding vanilla-Emacs noncontiguous region.
If you set or bind variable
region-extract-function to a function which, when passed the symbol
bounds, returns a noncontiguous region, then you can pass that variable to various vanilla Emacs functions -- such as
perform-replace -- that act on a noncontiguous region.
The reason I introduce you to
zones.el here is that
C-x n a gives you an easy interactive way to set up a sequence of zones, which give you a noncontiguous region.
Obviously, if the noncontiguous region that you want always has rows of a rectangle, then you could come up with another, simpler, way for a user to specify the region pieces. But at least
C-x n a gives you an interactive way to do so directly.