* [PATCH] Allow code edit buffer to inherit active region
@ 2010-09-04 17:56 Dan Davison
2010-09-14 13:33 ` Dan Davison
0 siblings, 1 reply; 7+ messages in thread
From: Dan Davison @ 2010-09-04 17:56 UTC (permalink / raw)
To: emacs org-mode mailing list
If we allow the current region to be inherited by the code edit buffer
(patch below), then language major mode commands that operate on the
region can be called remotely from the org buffer. For example
C-c C-v C-x M-; comment region according to language
C-c C-v C-x C-M-\ indent region according to language
Users can make these more convenient, e.g.
(defun my/org-comment-dwim (&optional arg)
(interactive "P")
(or (org-babel-do-key-sequence-in-edit-buffer "\M-;")
(comment-dwim arg)))
(define-key org-mode-map "\M-;" 'my/org-comment-dwim)
Dan
Proposed patch:
~~~~~~~~~~~~~~~
commit 6e14f016cdfe92357092461058def5d4073541e2
Author: Dan Davison <davison@stats.ox.ac.uk>
Date: Sat Sep 4 13:43:56 2010 -0400
Transmit active region from Org buffer to code edit buffer
* org-src.el (org-edit-src-code): If mark was inside code
block then code edit buffer inherits mark with active region.
diff --git a/lisp/org-src.el b/lisp/org-src.el
index d1948cc..d0a9729 100644
--- a/lisp/org-src.el
+++ b/lisp/org-src.el
@@ -209,6 +209,7 @@ buffer."
(setq org-edit-src-saved-temp-window-config (current-window-configuration)))
(let ((line (org-current-line))
(col (current-column))
+ (mark (and (use-region-p) (mark)))
(case-fold-search t)
(info (org-edit-src-find-region-and-lang))
(babel-info (org-babel-get-src-block-info))
@@ -217,7 +218,8 @@ buffer."
(end (make-marker))
(preserve-indentation org-src-preserve-indentation)
(allow-write-back-p (null code))
- block-nindent total-nindent ovl lang lang-f single lfmt begline buffer msg)
+ block-nindent total-nindent ovl lang lang-f single lfmt buffer msg
+ begline markline markcol)
(if (not info)
nil
(setq beg (move-marker beg (nth 0 info))
@@ -235,6 +237,10 @@ buffer."
block-nindent (nth 5 info)
lang-f (intern (concat lang "-mode"))
begline (save-excursion (goto-char beg) (org-current-line)))
+ (if (and mark (>= mark beg) (<= mark end))
+ (save-excursion (goto-char mark)
+ (setq markline (org-current-line)
+ markcol (current-column))))
(if (equal lang-f 'table.el-mode)
(setq lang-f (lambda ()
(text-mode)
@@ -290,6 +296,11 @@ buffer."
(while (re-search-forward "^," nil t)
(if (eq (org-current-line) line) (setq total-nindent (1+ total-nindent)))
(replace-match "")))
+ (when markline
+ (org-goto-line (1+ (- markline begline)))
+ (org-move-to-column
+ (if preserve-indentation markcol (max 0 (- markcol total-nindent))))
+ (push-mark (point) 'no-message t))
(org-goto-line (1+ (- line begline)))
(org-move-to-column
(if preserve-indentation col (max 0 (- col total-nindent))))
^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH] Allow code edit buffer to inherit active region
2010-09-04 17:56 [PATCH] Allow code edit buffer to inherit active region Dan Davison
@ 2010-09-14 13:33 ` Dan Davison
2010-09-21 12:34 ` Carsten Dominik
0 siblings, 1 reply; 7+ messages in thread
From: Dan Davison @ 2010-09-14 13:33 UTC (permalink / raw)
To: emacs org-mode mailing list
Dan Davison <davison@stats.ox.ac.uk> writes:
> If we allow the current region to be inherited by the code edit buffer
> (patch below), then language major mode commands that operate on the
> region can be called remotely from the org buffer. For example
>
> C-c C-v C-x M-; comment region according to language
> C-c C-v C-x C-M-\ indent region according to language
>
> Users can make these more convenient, e.g.
>
> (defun my/org-comment-dwim (&optional arg)
> (interactive "P")
> (or (org-babel-do-key-sequence-in-edit-buffer "\M-;")
> (comment-dwim arg)))
>
> (define-key org-mode-map "\M-;" 'my/org-comment-dwim)
>
> Dan
>
> Proposed patch:
> ~~~~~~~~~~~~~~~
I would like to make one addition to this patch: adding
(setq deactivate-mark nil)
towards the end means that if the region is contained within the src
block, then it will be active (and highlighted) in the edit buffer.
Dan
>
> commit 6e14f016cdfe92357092461058def5d4073541e2
> Author: Dan Davison <davison@stats.ox.ac.uk>
> Date: Sat Sep 4 13:43:56 2010 -0400
>
> Transmit active region from Org buffer to code edit buffer
>
> * org-src.el (org-edit-src-code): If mark was inside code
> block then code edit buffer inherits mark with active region.
>
> diff --git a/lisp/org-src.el b/lisp/org-src.el
> index d1948cc..d0a9729 100644
> --- a/lisp/org-src.el
> +++ b/lisp/org-src.el
> @@ -209,6 +209,7 @@ buffer."
> (setq org-edit-src-saved-temp-window-config (current-window-configuration)))
> (let ((line (org-current-line))
> (col (current-column))
> + (mark (and (use-region-p) (mark)))
> (case-fold-search t)
> (info (org-edit-src-find-region-and-lang))
> (babel-info (org-babel-get-src-block-info))
> @@ -217,7 +218,8 @@ buffer."
> (end (make-marker))
> (preserve-indentation org-src-preserve-indentation)
> (allow-write-back-p (null code))
> - block-nindent total-nindent ovl lang lang-f single lfmt begline buffer msg)
> + block-nindent total-nindent ovl lang lang-f single lfmt buffer msg
> + begline markline markcol)
> (if (not info)
> nil
> (setq beg (move-marker beg (nth 0 info))
> @@ -235,6 +237,10 @@ buffer."
> block-nindent (nth 5 info)
> lang-f (intern (concat lang "-mode"))
> begline (save-excursion (goto-char beg) (org-current-line)))
> + (if (and mark (>= mark beg) (<= mark end))
> + (save-excursion (goto-char mark)
> + (setq markline (org-current-line)
> + markcol (current-column))))
> (if (equal lang-f 'table.el-mode)
> (setq lang-f (lambda ()
> (text-mode)
> @@ -290,6 +296,11 @@ buffer."
> (while (re-search-forward "^," nil t)
> (if (eq (org-current-line) line) (setq total-nindent (1+ total-nindent)))
> (replace-match "")))
> + (when markline
> + (org-goto-line (1+ (- markline begline)))
> + (org-move-to-column
> + (if preserve-indentation markcol (max 0 (- markcol total-nindent))))
> + (push-mark (point) 'no-message t))
> (org-goto-line (1+ (- line begline)))
> (org-move-to-column
> (if preserve-indentation col (max 0 (- col total-nindent))))
>
> _______________________________________________
> Emacs-orgmode mailing list
> Please use `Reply All' to send replies to the list.
> Emacs-orgmode@gnu.org
> http://lists.gnu.org/mailman/listinfo/emacs-orgmode
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Re: [PATCH] Allow code edit buffer to inherit active region
2010-09-14 13:33 ` Dan Davison
@ 2010-09-21 12:34 ` Carsten Dominik
2010-09-21 14:25 ` Dan Davison
0 siblings, 1 reply; 7+ messages in thread
From: Carsten Dominik @ 2010-09-21 12:34 UTC (permalink / raw)
To: Dan Davison; +Cc: emacs org-mode mailing list
Hi Dan,
both changes look good to me.
- Carsten
On Sep 14, 2010, at 3:33 PM, Dan Davison wrote:
> Dan Davison <davison@stats.ox.ac.uk> writes:
>
>> If we allow the current region to be inherited by the code edit
>> buffer
>> (patch below), then language major mode commands that operate on the
>> region can be called remotely from the org buffer. For example
>>
>> C-c C-v C-x M-; comment region according to language
>> C-c C-v C-x C-M-\ indent region according to language
>>
>> Users can make these more convenient, e.g.
>>
>> (defun my/org-comment-dwim (&optional arg)
>> (interactive "P")
>> (or (org-babel-do-key-sequence-in-edit-buffer "\M-;")
>> (comment-dwim arg)))
>>
>> (define-key org-mode-map "\M-;" 'my/org-comment-dwim)
>>
>> Dan
>>
>> Proposed patch:
>> ~~~~~~~~~~~~~~~
>
> I would like to make one addition to this patch: adding
>
> (setq deactivate-mark nil)
>
> towards the end means that if the region is contained within the src
> block, then it will be active (and highlighted) in the edit buffer.
>
> Dan
>
>>
>> commit 6e14f016cdfe92357092461058def5d4073541e2
>> Author: Dan Davison <davison@stats.ox.ac.uk>
>> Date: Sat Sep 4 13:43:56 2010 -0400
>>
>> Transmit active region from Org buffer to code edit buffer
>>
>> * org-src.el (org-edit-src-code): If mark was inside code
>> block then code edit buffer inherits mark with active region.
>>
>> diff --git a/lisp/org-src.el b/lisp/org-src.el
>> index d1948cc..d0a9729 100644
>> --- a/lisp/org-src.el
>> +++ b/lisp/org-src.el
>> @@ -209,6 +209,7 @@ buffer."
>> (setq org-edit-src-saved-temp-window-config (current-window-
>> configuration)))
>> (let ((line (org-current-line))
>> (col (current-column))
>> + (mark (and (use-region-p) (mark)))
>> (case-fold-search t)
>> (info (org-edit-src-find-region-and-lang))
>> (babel-info (org-babel-get-src-block-info))
>> @@ -217,7 +218,8 @@ buffer."
>> (end (make-marker))
>> (preserve-indentation org-src-preserve-indentation)
>> (allow-write-back-p (null code))
>> - block-nindent total-nindent ovl lang lang-f single lfmt begline
>> buffer msg)
>> + block-nindent total-nindent ovl lang lang-f single lfmt buffer msg
>> + begline markline markcol)
>> (if (not info)
>> nil
>> (setq beg (move-marker beg (nth 0 info))
>> @@ -235,6 +237,10 @@ buffer."
>> block-nindent (nth 5 info)
>> lang-f (intern (concat lang "-mode"))
>> begline (save-excursion (goto-char beg) (org-current-line)))
>> + (if (and mark (>= mark beg) (<= mark end))
>> + (save-excursion (goto-char mark)
>> + (setq markline (org-current-line)
>> + markcol (current-column))))
>> (if (equal lang-f 'table.el-mode)
>> (setq lang-f (lambda ()
>> (text-mode)
>> @@ -290,6 +296,11 @@ buffer."
>> (while (re-search-forward "^," nil t)
>> (if (eq (org-current-line) line) (setq total-nindent (1+ total-
>> nindent)))
>> (replace-match "")))
>> + (when markline
>> + (org-goto-line (1+ (- markline begline)))
>> + (org-move-to-column
>> + (if preserve-indentation markcol (max 0 (- markcol total-
>> nindent))))
>> + (push-mark (point) 'no-message t))
>> (org-goto-line (1+ (- line begline)))
>> (org-move-to-column
>> (if preserve-indentation col (max 0 (- col total-nindent))))
>>
>> _______________________________________________
>> Emacs-orgmode mailing list
>> Please use `Reply All' to send replies to the list.
>> Emacs-orgmode@gnu.org
>> http://lists.gnu.org/mailman/listinfo/emacs-orgmode
>
> _______________________________________________
> Emacs-orgmode mailing list
> Please use `Reply All' to send replies to the list.
> Emacs-orgmode@gnu.org
> http://lists.gnu.org/mailman/listinfo/emacs-orgmode
- Carsten
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Re: [PATCH] Allow code edit buffer to inherit active region
2010-09-21 12:34 ` Carsten Dominik
@ 2010-09-21 14:25 ` Dan Davison
2010-09-30 8:02 ` Sébastien Vauban
0 siblings, 1 reply; 7+ messages in thread
From: Dan Davison @ 2010-09-21 14:25 UTC (permalink / raw)
To: Carsten Dominik; +Cc: emacs org-mode mailing list
Carsten Dominik <carsten.dominik@gmail.com> writes:
> Hi Dan,
>
> both changes look good to me.
OK, that is applied. So now, if the region is contained within a src
block, then it will be inherited by the language major mode edit
buffer. One consequence is that commands like M-; (comment-region) and
C-M-\ (indent-region) can be called with their native effects from a src
block in the Org buffer; see example below.
Dan
>
> - Carsten
>
> On Sep 14, 2010, at 3:33 PM, Dan Davison wrote:
>
>> Dan Davison <davison@stats.ox.ac.uk> writes:
>>
>>> If we allow the current region to be inherited by the code edit
>>> buffer
>>> (patch below), then language major mode commands that operate on the
>>> region can be called remotely from the org buffer. For example
>>>
>>> C-c C-v C-x M-; comment region according to language
>>> C-c C-v C-x C-M-\ indent region according to language
>>>
>>> Users can make these more convenient, e.g.
>>>
>>> (defun my/org-comment-dwim (&optional arg)
>>> (interactive "P")
>>> (or (org-babel-do-key-sequence-in-edit-buffer "\M-;")
>>> (comment-dwim arg)))
>>>
>>> (define-key org-mode-map "\M-;" 'my/org-comment-dwim)
>>>
>>> Dan
>>>
>>> Proposed patch:
>>> ~~~~~~~~~~~~~~~
>>
>> I would like to make one addition to this patch: adding
>>
>> (setq deactivate-mark nil)
>>
>> towards the end means that if the region is contained within the src
>> block, then it will be active (and highlighted) in the edit buffer.
>>
>> Dan
>>
>>>
>>> commit 6e14f016cdfe92357092461058def5d4073541e2
>>> Author: Dan Davison <davison@stats.ox.ac.uk>
>>> Date: Sat Sep 4 13:43:56 2010 -0400
>>>
>>> Transmit active region from Org buffer to code edit buffer
>>>
>>> * org-src.el (org-edit-src-code): If mark was inside code
>>> block then code edit buffer inherits mark with active region.
>>>
>>> diff --git a/lisp/org-src.el b/lisp/org-src.el
>>> index d1948cc..d0a9729 100644
>>> --- a/lisp/org-src.el
>>> +++ b/lisp/org-src.el
>>> @@ -209,6 +209,7 @@ buffer."
>>> (setq org-edit-src-saved-temp-window-config (current-window-
>>> configuration)))
>>> (let ((line (org-current-line))
>>> (col (current-column))
>>> + (mark (and (use-region-p) (mark)))
>>> (case-fold-search t)
>>> (info (org-edit-src-find-region-and-lang))
>>> (babel-info (org-babel-get-src-block-info))
>>> @@ -217,7 +218,8 @@ buffer."
>>> (end (make-marker))
>>> (preserve-indentation org-src-preserve-indentation)
>>> (allow-write-back-p (null code))
>>> - block-nindent total-nindent ovl lang lang-f single lfmt
>>> begline buffer msg)
>>> + block-nindent total-nindent ovl lang lang-f single lfmt buffer msg
>>> + begline markline markcol)
>>> (if (not info)
>>> nil
>>> (setq beg (move-marker beg (nth 0 info))
>>> @@ -235,6 +237,10 @@ buffer."
>>> block-nindent (nth 5 info)
>>> lang-f (intern (concat lang "-mode"))
>>> begline (save-excursion (goto-char beg) (org-current-line)))
>>> + (if (and mark (>= mark beg) (<= mark end))
>>> + (save-excursion (goto-char mark)
>>> + (setq markline (org-current-line)
>>> + markcol (current-column))))
>>> (if (equal lang-f 'table.el-mode)
>>> (setq lang-f (lambda ()
>>> (text-mode)
>>> @@ -290,6 +296,11 @@ buffer."
>>> (while (re-search-forward "^," nil t)
>>> (if (eq (org-current-line) line) (setq total-nindent (1+
>>> total-
>>> nindent)))
>>> (replace-match "")))
>>> + (when markline
>>> + (org-goto-line (1+ (- markline begline)))
>>> + (org-move-to-column
>>> + (if preserve-indentation markcol (max 0 (- markcol total-
>>> nindent))))
>>> + (push-mark (point) 'no-message t))
>>> (org-goto-line (1+ (- line begline)))
>>> (org-move-to-column
>>> (if preserve-indentation col (max 0 (- col total-nindent))))
>>>
>>> _______________________________________________
>>> Emacs-orgmode mailing list
>>> Please use `Reply All' to send replies to the list.
>>> Emacs-orgmode@gnu.org
>>> http://lists.gnu.org/mailman/listinfo/emacs-orgmode
>>
>> _______________________________________________
>> Emacs-orgmode mailing list
>> Please use `Reply All' to send replies to the list.
>> Emacs-orgmode@gnu.org
>> http://lists.gnu.org/mailman/listinfo/emacs-orgmode
>
> - Carsten
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] Allow code edit buffer to inherit active region
2010-09-21 14:25 ` Dan Davison
@ 2010-09-30 8:02 ` Sébastien Vauban
2010-09-30 9:53 ` Dan Davison
0 siblings, 1 reply; 7+ messages in thread
From: Sébastien Vauban @ 2010-09-30 8:02 UTC (permalink / raw)
To: emacs-orgmode-mXXj517/zsQ
Hi Dan,
Dan Davison wrote:
> Now, if the region is contained within a src block, then it will be
> inherited by the language major mode edit buffer. One consequence is that
> commands like M-; (comment-region) and C-M-\ (indent-region) can be called
> with their native effects from a src block in the Org buffer.
Am I right assuming we still need such a code in order to get the wanted
effect with the native keys (I mean, for example, =M-;= instead of =C-c C-v
C-x M-;=)?
--8<---------------cut here---------------start------------->8---
;; allow comment region in the code edit buffer (according to language)
(defun my/org-comment-dwim (&optional arg)
(interactive "P")
(or (org-babel-do-key-sequence-in-edit-buffer (kbd "M-;"))
(comment-dwim arg)))
(define-key org-mode-map (kbd "M-;") 'my/org-comment-dwim)
;; allow indent region in the code edit buffer (according to language)
(defun my/org-indent-region (&optional arg)
(interactive "P")
(or (org-babel-do-key-sequence-in-edit-buffer (kbd "C-M-\\"))
(indent-region arg)))
(define-key org-mode-map (kbd "C-M-\\") 'my/org-indent-region)
--8<---------------cut here---------------end--------------->8---
BTW, note that I make use of =kbd= to have a more intuitive writing of the key
bindings (just copy what =C-h k= shows, except for the =\= that has to be
escaped). Isn't this more portable (other versions of Emacs, or XEmacs)?
Best regards,
Seb
--
Sébastien Vauban
_______________________________________________
Emacs-orgmode mailing list
Please use `Reply All' to send replies to the list.
Emacs-orgmode-mXXj517/zsQ@public.gmane.org
http://lists.gnu.org/mailman/listinfo/emacs-orgmode
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] Allow code edit buffer to inherit active region
2010-09-30 8:02 ` Sébastien Vauban
@ 2010-09-30 9:53 ` Dan Davison
2010-09-30 15:19 ` Sébastien Vauban
0 siblings, 1 reply; 7+ messages in thread
From: Dan Davison @ 2010-09-30 9:53 UTC (permalink / raw)
To: Sébastien Vauban; +Cc: public-emacs-orgmode-mXXj517/zsQ
Sébastien Vauban <wxhgmqzgwmuf-geNee64TY+gS+FvcfC7Uqw@public.gmane.org>
writes:
> Hi Dan,
>
> Dan Davison wrote:
>> Now, if the region is contained within a src block, then it will be
>> inherited by the language major mode edit buffer. One consequence is that
>> commands like M-; (comment-region) and C-M-\ (indent-region) can be called
>> with their native effects from a src block in the Org buffer.
>
> Am I right assuming we still need such a code in order to get the wanted
> effect with the native keys (I mean, for example, =M-;= instead of =C-c C-v
> C-x M-;=)?
Hi Séb,
Yes. If people think it is worthwhile, we could make it so that these
are controlled by variables `org-src-comment-natively' and
`org-src-indent-natively'. However, currently that is not the case. The
only such variable currently is `org-src-tab-acts-natively'.
By the way, please note that there is a minor wrinkle: if you place
point in the #+end_src line in the Org buffer, then it goes to the
beginning of the last line of code in the edit buffer, and the region
will not contain what you were intending (all the code). I'm going to
submit a patch so that point goes to the end of the code in this case.
Dan
>
> ;; allow comment region in the code edit buffer (according to language)
> (defun my/org-comment-dwim (&optional arg)
> (interactive "P")
> (or (org-babel-do-key-sequence-in-edit-buffer (kbd "M-;"))
> (comment-dwim arg)))
>
> (define-key org-mode-map (kbd "M-;") 'my/org-comment-dwim)
>
> ;; allow indent region in the code edit buffer (according to language)
> (defun my/org-indent-region (&optional arg)
> (interactive "P")
> (or (org-babel-do-key-sequence-in-edit-buffer (kbd "C-M-\\"))
> (indent-region arg)))
>
> (define-key org-mode-map (kbd "C-M-\\") 'my/org-indent-region)
>
> BTW, note that I make use of =kbd= to have a more intuitive writing of the key
> bindings (just copy what =C-h k= shows, except for the =\= that has to be
> escaped). Isn't this more portable (other versions of Emacs, or XEmacs)?
>
> Best regards,
> Seb
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] Allow code edit buffer to inherit active region
2010-09-30 9:53 ` Dan Davison
@ 2010-09-30 15:19 ` Sébastien Vauban
0 siblings, 0 replies; 7+ messages in thread
From: Sébastien Vauban @ 2010-09-30 15:19 UTC (permalink / raw)
To: emacs-orgmode-mXXj517/zsQ
Hi Dan,
Dan Davison wrote:
> Sébastien Vauban writes:
>> Dan Davison wrote:
>>> Now, if the region is contained within a src block, then it will be
>>> inherited by the language major mode edit buffer. One consequence is that
>>> commands like M-; (comment-region) and C-M-\ (indent-region) can be called
>>> with their native effects from a src block in the Org buffer.
>>
>> Am I right assuming we still need such a code in order to get the wanted
>> effect with the native keys (I mean, for example, =M-;= instead of =C-c C-v
>> C-x M-;=)?
>
> Yes. If people think it is worthwhile, we could make it so that these are
> controlled by variables `org-src-comment-natively' and
> `org-src-indent-natively'. However, currently that is not the case. The only
> such variable currently is `org-src-tab-acts-natively'.
I find such a code worthwhile, for every Org-Babel user, yes. I would advocate
for "don't hesitate" one more minute... unless there's a "nope!" popping up
very soon...
> By the way, please note that there is a minor wrinkle: if you place point in
> the #+end_src line in the Org buffer, then it goes to the beginning of the
> last line of code in the edit buffer, and the region will not contain what
> you were intending (all the code). I'm going to submit a patch so that point
> goes to the end of the code in this case.
I've indeed got glitches when playing a bit with it. Thanks for the coming
updates...
Best regards,
Seb
--
Sébastien Vauban
_______________________________________________
Emacs-orgmode mailing list
Please use `Reply All' to send replies to the list.
Emacs-orgmode-mXXj517/zsQ@public.gmane.org
http://lists.gnu.org/mailman/listinfo/emacs-orgmode
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2010-09-30 15:19 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-09-04 17:56 [PATCH] Allow code edit buffer to inherit active region Dan Davison
2010-09-14 13:33 ` Dan Davison
2010-09-21 12:34 ` Carsten Dominik
2010-09-21 14:25 ` Dan Davison
2010-09-30 8:02 ` Sébastien Vauban
2010-09-30 9:53 ` Dan Davison
2010-09-30 15:19 ` Sébastien Vauban
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs/org-mode.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).