From b4680bf33548fa09d4c6d2b103611cfa5ddd7bc2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Pinard?= Date: Tue, 5 Feb 2013 13:51:42 -0500 Subject: [PATCH] poporg.el: Recover the edited region when dwim called in a greyed area. REAMDE.org: Adjusted. --- README.org | 4 ++-- poporg.el | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/README.org b/README.org index 97ff5e7..dd97526 100644 --- a/README.org +++ b/README.org @@ -16,11 +16,11 @@ To use *poporg*, you need to pick some unused keybinding and add a few lines to (global-set-key "\C-ceo" 'poporg-dwim) #+END_SRC ** Usage -While editing a buffer containing a program, you may edit a comment block or a string (often a doc-string) in Org mode by placing the cursor within or nearby that comment or string, and calling =poporg-dwim= using you selected keybinding. This pops another buffer in Org Mode (hence the project name), containing the comment or string. Once your edition is done, right in the popped up editing buffer, call =poporg-dwim= again to complete the edition, or merely kill that buffer to abandon the edition. +While editing a buffer containing a program, you may edit a comment block or a string (often a doc-string) in Org mode by placing the cursor within or nearby that comment or string, and calling =poporg-dwim= using your selected keybinding. This pops another buffer in Org Mode (hence the project name), containing the comment or string. Once your edition is done, right in the popped up editing buffer, call =poporg-dwim= again to complete the edition, or merely kill that buffer to abandon the edition. More precisely, if the cursor is within a comment block or a string, this is what gets edited. If the cursor is not within a comment block or a string, a comment or string following the cursor gets selected instead. Otherwise, this is the comment or string which precedes the cursor which is selected for edition. Python mode receives a special treatment: if the cursor is within a string, it is assumed to be a sextuple-quoted string (that is, a triple double-quoted one), and this is what the tool selects. -While the comment or string is being copied in the editing buffer and until the edition is completed, the original comment or string in the original buffer is greyed out and protected against accidental modification. *poporg* asks for confirmation when the user attempts to kill an editing buffer which has modifications. *poporg* also prevents the original buffer from being killed while there are pending *poporg* edits, the user should either complete or abandon all those edits before killing the original buffer. +While the comment or string is being copied in the editing buffer and until the edition is completed, the original comment or string in the original buffer is greyed out and protected against accidental modification. Calling =poporg-dwim= again from within a greyed out region recovers the editing buffer, it does not create a new one. *poporg* asks for confirmation when the user attempts to kill an editing buffer which has modifications. *poporg* also prevents the original buffer from being killed while there are pending *poporg* edits, the user should either complete or abandon all those edits before killing the original buffer. Functions added to =poporg-edit-hook= are run once the *poporg* editing buffer has been set up with its contents, with the common prefix already removed, these functions may further modify the buffer contents. Functions added to =poporg-edit-exit-hook= are run when *poporg* is about to reinstate the common prefix and move back the editing buffer contents into the original programming buffer, these functions may alter the contents as needed. (I did not need these hooks, so let's talk if you need them to be defined differenty!) ** Known bugs diff --git a/poporg.el b/poporg.el index ee2e153..affac83 100644 --- a/poporg.el +++ b/poporg.el @@ -46,6 +46,21 @@ them the original buffer.") (:foreground "gray"))) "Face for a region while it is being edited.") +(defun poporg-check-already-edited () + "Check if point is within an already edited region. +If yes, pop the editing buffer and return t." + (let ((data poporg-data) + found) + (while (and data (not found)) + (let* ((triplet (pop data)) + (overlay (cadr triplet))) + (when (and (eq (overlay-buffer overlay) (current-buffer)) + (<= (overlay-start overlay) (point)) + (>= (overlay-end overlay) (point))) + (pop-to-buffer (car triplet)) + (setq found t)))) + found)) + (defun poporg-current-line () "Return the contents of the line where the point is." (buffer-substring-no-properties @@ -60,6 +75,7 @@ the cursor, after the cursor, else before the cursor. Within a *poporg* edit buffer however, rather complete and exit the edit." (cond ((assq (current-buffer) poporg-data) (poporg-edit-exit)) ((use-region-p) (poporg-edit-region (region-beginning) (region-end))) + ((poporg-check-already-edited)) ((poporg-dwim-1 (point))) ((poporg-dwim-1 (let ((location (next-single-property-change (point) 'face)))