From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Stefan Monnier Newsgroups: gmane.emacs.devel Subject: Re: proposal: edit-rectangle Date: Thu, 06 Oct 2016 17:07:07 -0400 Message-ID: References: NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: blaine.gmane.org 1475788094 20779 195.159.176.226 (6 Oct 2016 21:08:14 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Thu, 6 Oct 2016 21:08:14 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.2.50 (gnu/linux) To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Thu Oct 06 23:08:10 2016 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bsFtY-0003mQ-4z for ged-emacs-devel@m.gmane.org; Thu, 06 Oct 2016 23:08:00 +0200 Original-Received: from localhost ([::1]:59408 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bsFtZ-0006zp-HP for ged-emacs-devel@m.gmane.org; Thu, 06 Oct 2016 17:08:01 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:47262) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bsFtM-0006yY-51 for emacs-devel@gnu.org; Thu, 06 Oct 2016 17:07:49 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bsFtI-0000Mm-0u for emacs-devel@gnu.org; Thu, 06 Oct 2016 17:07:48 -0400 Original-Received: from [195.159.176.226] (port=38738 helo=blaine.gmane.org) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bsFtH-0000Ka-Qq for emacs-devel@gnu.org; Thu, 06 Oct 2016 17:07:43 -0400 Original-Received: from list by blaine.gmane.org with local (Exim 4.84_2) (envelope-from ) id 1bsFsy-0007rW-Gv for emacs-devel@gnu.org; Thu, 06 Oct 2016 23:07:24 +0200 X-Injected-Via-Gmane: http://gmane.org/ Original-Lines: 64 Original-X-Complaints-To: usenet@blaine.gmane.org Cancel-Lock: sha1:0LoNd3xfvymL1PlLm04frzDe08E= X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 195.159.176.226 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.org gmane.emacs.devel:208046 Archived-At: > (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