unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Phil Sainty <psainty@orcon.net.nz>
To: 55828@debbugs.gnu.org
Cc: Michael Heerdegen <michael_heerdegen@web.de>
Subject: bug#55828: Add command to edit a rectangle arbitrarily in a separate buffer
Date: Tue, 07 Jun 2022 21:24:30 +1200	[thread overview]
Message-ID: <42925cc4927c79c1bf80ed4e3acf5581@webmail.orcon.net.nz> (raw)

[-- 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)))))

             reply	other threads:[~2022-06-07  9:24 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-07  9:24 Phil Sainty [this message]
2022-06-08  6:58 ` bug#55828: Add command to edit a rectangle arbitrarily in a separate buffer Juri Linkov
2022-06-08  9:57   ` Phil Sainty
2022-06-08 16:18     ` Juri Linkov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=42925cc4927c79c1bf80ed4e3acf5581@webmail.orcon.net.nz \
    --to=psainty@orcon.net.nz \
    --cc=55828@debbugs.gnu.org \
    --cc=michael_heerdegen@web.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).