unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#55828: Add command to edit a rectangle arbitrarily in a separate buffer
@ 2022-06-07  9:24 Phil Sainty
  2022-06-08  6:58 ` Juri Linkov
  0 siblings, 1 reply; 4+ messages in thread
From: Phil Sainty @ 2022-06-07  9:24 UTC (permalink / raw)
  To: 55828; +Cc: Michael Heerdegen

[-- Attachment #1: Type: text/plain, Size: 1517 bytes --]

At https://debbugs.gnu.org/cgi/bugreport.cgi?bug=55234#11 Michael 
Heerdegen wrote:
> I implemented a command that lets me edit a rectangular region
> in a separate buffer

I've had one of those for years as well, and always intended to
work on it some more and propose it as a standard feature.

I bind it to "C-x r e", with "C-c C-c" to write the rectangle back
to the original buffer after editing.

There are a lot of special-purpose "do X to a rectangle" commands,
but I think a command which lets you do *absolutely anything* to
a rectangle is extremely useful, and a buffer with only that text
gives you that ability pretty trivially.  I think Emacs should have
this in its standard rectangle toolkit; so here's a new feature
request for that.

For reference I've attached my version.  It's old code which knows
nothing of `rectangle-mark-mode' or other newer things, and it has
the notable flaw of introducing trailing whitespace if the rectangular
region had intersected any line endings (something I'd intended to
address before suggesting this); but it's always done the job when
I've needed it, so I'm attaching it as-is (aside from renaming it to
`rectangle-edit' which seems to be an available name-space for such
a feature).

It sounds to me as if Michael's code does more than mine -- at minimum
mine does not ask what to do with modified dimensions as described at
https://debbugs.gnu.org/cgi/bugreport.cgi?bug=55234#17 (I just use the
original dimensions), so I'd be interested to see that.


-Phil

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: rectangle-edit.el --]
[-- Type: text/x-lisp; name=rectangle-edit.el, Size: 3536 bytes --]

(require 'rect)

(define-derived-mode rectangle-edit-mode nil "Rectangle-Edit"
  ;; The rectangle buffer contents will likely be invalid for the
  ;; major mode of the source buffer, so we derive from fundamental-mode
  ;; to avoid problems.
  "Major mode for *edit rectangle* buffers.

\\{rectangle-edit-mode-map}")

(define-key rectangle-edit-mode-map (kbd "C-c C-c") 'rectangle-edit-submit)

(defvar rectangle-edit-data)

(defun rectangle-edit (start end)
  "Edit the rectangle in a temporary buffer. C-c C-c applies the changes."
  (interactive "*r")
  (let* ((content (extract-rectangle start end))
         (width (length (car content)))
         (height (length content))
         (source-buffer (current-buffer))
         (source-syntax (syntax-table))
         (coords-point (list (line-number-at-pos) (current-column)))
         (coords-mark (save-excursion
                        (goto-char (mark))
                        (list (line-number-at-pos) (current-column)))))
    (switch-to-buffer (generate-new-buffer " *edit rectangle*"))
    (insert-rectangle content)
    (rectangle-edit-mode) ;; mode change kills local variables.
    (set-syntax-table source-syntax)
    (setq fill-column width)
    (buffer-enable-undo)
    ;; Store the rectangle details in a buffer-local structure.
    (set (make-local-variable 'rectangle-edit-data)
         (list start end width height source-buffer coords-point coords-mark))
    (message (substitute-command-keys
              "Editing rectangle. Type \\[rectangle-edit-submit] to confirm \
the changes, or \\[kill-buffer] RET to cancel."))))

(defun rectangle-edit-submit ()
  "Confirm changes to the rectangle, writing them back to the original buffer."
  (interactive)
  (cl-destructuring-bind
      (start end width height source-buffer coords-point coords-mark)
      rectangle-edit-data
    (let ((rectangle-edit-buffer (current-buffer)))
      ;; Account for possible changes in the dimensions of the
      ;; edit-buffer's contents by explicitly using the original
      ;; rectangle's height and width to establish the replacement
      ;; rectangle.
      (goto-char (point-min))
      (let ((remaining (forward-line (1- height))))
        (insert-char ?\n (if (looking-back "^" (point))
                             remaining
                           (1+ remaining))))
      (move-to-column width t)
      ;; Replace the original rectangle with the edited version.
      (let ((content (extract-rectangle (point-min) (point))))
        (switch-to-buffer source-buffer)
        (goto-char start)
        (delete-rectangle start end)
        (insert-rectangle content)
        (kill-buffer rectangle-edit-buffer)
        ;; Set point and mark in accordance with their values before
        ;; editing began. `insert-rectangle' sets point and mark to
        ;; the lower-right and upper-left corners of the rectangle
        ;; respectively, but these may not be the same corners we
        ;; started with. We cannot use the original character
        ;; positions, as inserting the rectangle may have introduced
        ;; additional characters in the form of trailing whitespace.
        (forward-line (- (car coords-mark) (line-number-at-pos)))
        (move-to-column (cadr coords-mark) t)
        (pop-mark) ;; the value pushed by insert-rectangle
        (pop-mark) ;; the original value
        (push-mark) ;; replacement for the original value
        (forward-line (- (car coords-point) (line-number-at-pos)))
        (move-to-column (cadr coords-point) t)))))

^ permalink raw reply	[flat|nested] 4+ messages in thread

* bug#55828: Add command to edit a rectangle arbitrarily in a separate buffer
  2022-06-07  9:24 bug#55828: Add command to edit a rectangle arbitrarily in a separate buffer Phil Sainty
@ 2022-06-08  6:58 ` Juri Linkov
  2022-06-08  9:57   ` Phil Sainty
  0 siblings, 1 reply; 4+ messages in thread
From: Juri Linkov @ 2022-06-08  6:58 UTC (permalink / raw)
  To: Phil Sainty; +Cc: Michael Heerdegen, 55828

> It sounds to me as if Michael's code does more than mine -- at minimum
> mine does not ask what to do with modified dimensions as described at
> https://debbugs.gnu.org/cgi/bugreport.cgi?bug=55234#17 (I just use the
> original dimensions), so I'd be interested to see that.

As Michael pointed out, it could be based on string-edit,
so rectangle-edit could just call string-edit with the rectangle
as a string, and success-callback to replace the original rectangle
with the edited string.





^ permalink raw reply	[flat|nested] 4+ messages in thread

* bug#55828: Add command to edit a rectangle arbitrarily in a separate buffer
  2022-06-08  6:58 ` Juri Linkov
@ 2022-06-08  9:57   ` Phil Sainty
  2022-06-08 16:18     ` Juri Linkov
  0 siblings, 1 reply; 4+ messages in thread
From: Phil Sainty @ 2022-06-08  9:57 UTC (permalink / raw)
  To: Juri Linkov; +Cc: Michael Heerdegen, 55828

On 2022-06-08 18:58, Juri Linkov wrote:
> As Michael pointed out, it could be based on string-edit,

Perhaps.  I just compiled from master to have a look at this,
and I don't know whether that's an obvious way to go.  I'm not
seeing a particularly nice way to pass the information about the
source of the rectangle through to the success callback, unless
that function was a closure generated at call time, or the data
was passed as text properties, both of which seem a bit ugly to
me in this scenario (the latter more so than the former, but
neither feels ideal).

Perhaps `string-edit' is missing a `data' argument to be passed
along to the callback functions?

There's also no obvious facility for preparing the edit buffer.
In my command I'm setting the syntax-table to match the original
buffer, and setting fill-column to the rectangle width, both of
which seem like sensible things for the purpose at hand; but
there's no way of doing such things with `string-edit' other
than `string-edit-mode-hook' which I think is going to be
awkward to deal with for such a purpose.  I guess you can
let-bind the hook variable around the `string-edit' call to add
a set-up function, but it again feels like there should be a
nicer option.

It does provide some boilerplate, but I'm not sure it makes
sense to shoe-horn the rectangle command into that framework
in its current form.  Maybe with some enhancements it makes
sense, though.


-Phil






^ permalink raw reply	[flat|nested] 4+ messages in thread

* bug#55828: Add command to edit a rectangle arbitrarily in a separate buffer
  2022-06-08  9:57   ` Phil Sainty
@ 2022-06-08 16:18     ` Juri Linkov
  0 siblings, 0 replies; 4+ messages in thread
From: Juri Linkov @ 2022-06-08 16:18 UTC (permalink / raw)
  To: Phil Sainty; +Cc: Michael Heerdegen, 55828

> It does provide some boilerplate, but I'm not sure it makes
> sense to shoe-horn the rectangle command into that framework
> in its current form.  Maybe with some enhancements it makes
> sense, though.

The initial version of string-edit is only 1 month old.
So you are welcome to propose any improvements that
will make it more useful for different use cases.





^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-06-08 16:18 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-06-07  9:24 bug#55828: Add command to edit a rectangle arbitrarily in a separate buffer Phil Sainty
2022-06-08  6:58 ` Juri Linkov
2022-06-08  9:57   ` Phil Sainty
2022-06-08 16:18     ` Juri Linkov

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).