emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: proposal: edit-rectangle


From: Stefan Monnier
Subject: Re: proposal: edit-rectangle
Date: Thu, 06 Oct 2016 17:07:07 -0400
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/25.2.50 (gnu/linux)

> (defvar edit-rectangle-origin)
> (defvar edit-rectangle-saved-window-config)

Since they're internal, I'd name them with a "rectangle--" prefix.
I'd also give them a docstring.

> (defun edit-rectangle (&optional start end)
>   (interactive "r")
>   (let ((strs (delete-extract-rectangle start end))

I would leave the original rectangle untouched until the user hits C-c C-c.

>         (mode major-mode)
>         (here (copy-marker (min (mark) (point)) t))
>         (config (current-window-configuration)))
>     (with-current-buffer (generate-new-buffer "*Rectangle*")
>       (funcall mode)
>       (set (make-local-variable 'edit-rectangle-origin) here)
>       (set (make-local-variable 'edit-rectangle-saved-window-config) config)

You can use setq-local here.

>       (local-set-key (kbd "C-c C-c") #'restore-rectangle)

This modifies the local keymap, i.e. the major mode's keymap (shared
by all other buffers using this major mode).

You could use (use-local-map `(keymap . ,(current-local-map))), but
I think I'd vote for a minor-mode map and actually define a matching
minor mode.  And make it (along with the above buffer-local vars)
permanent-local, so the user can switch major-mode.

>       (mapc #'(lambda (x) (insert x ?\n)) strs)

    (dolist (x strs) (insert x ?\n))

Indents better, runs faster, and might even generate more compact code
(haven't bothered to check, tho).  It only works if `strs` is a list
(whereas the `mapc` version also works for arrays).

> (defun restore-rectangle ()

"restore" makes it sound like you're reverting to an older state.
How 'bout `rectangle-edit-done` ?

>   (interactive)
>   (let ((content (split-string (buffer-string) "\n"))
>         (origin edit-rectangle-origin)
>         (config edit-rectangle-saved-window-config))
>     (with-current-buffer (marker-buffer origin)
>       (goto-char origin)
>       (insert-rectangle content))
>     (kill-buffer (current-buffer))
>     (set-window-configuration config)))

I don't like window-configurations, so I'd rather just use something
like bury-buffer.  One of the reasons is that in my kind of setup the
above code will fail: edit-rectangle's pop-to-buffer will have a created
a new frame, so calling set-window-configuration in that new frame at
best makes little sense, and in practice signals an error because
window-configs can only be applied to their original frame.


        Stefan




reply via email to

[Prev in Thread] Current Thread [Next in Thread]