emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [PATCH] Indent code with TAB in Org buffer
@ 2010-08-15  6:43 Dan Davison
  2010-08-15  7:42 ` Carsten Dominik
  0 siblings, 1 reply; 4+ messages in thread
From: Dan Davison @ 2010-08-15  6:43 UTC (permalink / raw)
  To: emacs org-mode mailing list

[-- Attachment #1: Type: text/plain, Size: 340 bytes --]

With this patch TAB in a code block aligns the code according to the
major mode. The macro could be used to do the same thing for other
commands (i.e. allow other commands to be invoked in the Org buffer, but
actually carried out in the code buffer.)

Patch attached and in branch src-block-tab at
git@github.com:dandavison/org-devel.git.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: src-block-tab.diff --]
[-- Type: text/x-diff, Size: 870 bytes --]

diff --git a/lisp/org-src.el b/lisp/org-src.el
index baa2b11..fc15a83 100644
--- a/lisp/org-src.el
+++ b/lisp/org-src.el
@@ -435,6 +435,19 @@ the fragment in the Org-mode buffer."
       (message "%s" msg)
       t)))
 
+(defmacro org-src-do-in-edit-buffer (&rest body)
+  "Evaluate BODY in edit buffer if there is a code block at point.
+Return t if a code block was found at point, nil otherwise."
+  `(when (org-edit-src-code)
+     ,@body
+     (org-edit-src-exit) t))
+
+(defun org-src-tab-command ()
+  "Do what TAB does in the edit buffer for code block at point."
+  (org-src-do-in-edit-buffer
+   (funcall (key-binding (kbd "TAB")))))
+
+(add-hook 'org-tab-first-hook 'org-src-tab-command)
 (defun org-edit-src-find-region-and-lang ()
   "Find the region and language for a local edit.
 Return a list with beginning and end of the region, a string representing

[-- Attachment #3: Type: text/plain, Size: 5 bytes --]


Dan

[-- Attachment #4: Type: text/plain, Size: 201 bytes --]

_______________________________________________
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 related	[flat|nested] 4+ messages in thread

* Re: [PATCH] Indent code with TAB in Org buffer
  2010-08-15  6:43 [PATCH] Indent code with TAB in Org buffer Dan Davison
@ 2010-08-15  7:42 ` Carsten Dominik
  2010-08-15 23:00   ` Dan Davison
  0 siblings, 1 reply; 4+ messages in thread
From: Carsten Dominik @ 2010-08-15  7:42 UTC (permalink / raw)
  To: Dan Davison; +Cc: emacs org-mode mailing list


On Aug 15, 2010, at 8:43 AM, Dan Davison wrote:

> With this patch TAB in a code block aligns the code according to the
> major mode. The macro could be used to do the same thing for other
> commands (i.e. allow other commands to be invoked in the Org buffer,  
> but
> actually carried out in the code buffer.)
>
> Patch attached and in branch src-block-tab at
> git@github.com:dandavison/org-devel.git.
>
> diff --git a/lisp/org-src.el b/lisp/org-src.el
> index baa2b11..fc15a83 100644
> --- a/lisp/org-src.el
> +++ b/lisp/org-src.el
> @@ -435,6 +435,19 @@ the fragment in the Org-mode buffer."
>       (message "%s" msg)
>       t)))
>
> +(defmacro org-src-do-in-edit-buffer (&rest body)
> +  "Evaluate BODY in edit buffer if there is a code block at point.
> +Return t if a code block was found at point, nil otherwise."
> +  `(when (org-edit-src-code)

Wow, an excursion to the temp buffer for every TAB call
in a source code block?

Sounds heavy.  But I see that it could be nice together with
the new fontification stuff.  My gut feeling would be to have
both of these feature off by default.  But I have not tried
them yet.

- Carsten

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] Indent code with TAB in Org buffer
  2010-08-15  7:42 ` Carsten Dominik
@ 2010-08-15 23:00   ` Dan Davison
  2010-08-16  6:56     ` Carsten Dominik
  0 siblings, 1 reply; 4+ messages in thread
From: Dan Davison @ 2010-08-15 23:00 UTC (permalink / raw)
  To: Carsten Dominik; +Cc: emacs org-mode mailing list

[-- Attachment #1: Type: text/plain, Size: 2680 bytes --]

Carsten Dominik <carsten.dominik@gmail.com> writes:

> On Aug 15, 2010, at 8:43 AM, Dan Davison wrote:
>
>> With this patch TAB in a code block aligns the code according to the
>> major mode. The macro could be used to do the same thing for other
>> commands (i.e. allow other commands to be invoked in the Org buffer,
>> but
>> actually carried out in the code buffer.)
>>
>> Patch attached and in branch src-block-tab at
>> git@github.com:dandavison/org-devel.git.
>>
>> diff --git a/lisp/org-src.el b/lisp/org-src.el
>> index baa2b11..fc15a83 100644
>> --- a/lisp/org-src.el
>> +++ b/lisp/org-src.el
>> @@ -435,6 +435,19 @@ the fragment in the Org-mode buffer."
>>       (message "%s" msg)
>>       t)))
>>
>> +(defmacro org-src-do-in-edit-buffer (&rest body)
>> +  "Evaluate BODY in edit buffer if there is a code block at point.
>> +Return t if a code block was found at point, nil otherwise."
>> +  `(when (org-edit-src-code)
>
> Wow, an excursion to the temp buffer for every TAB call
> in a source code block?
>
> Sounds heavy.  But I see that it could be nice together with
> the new fontification stuff.  My gut feeling would be to have
> both of these feature off by default.  But I have not tried
> them yet.

Hi Carsten,

In a small code block the TAB is actually not bad at all, but in a big
one there's a delay (that's worst case scenario: a netbook with an atom
processor).

I think it's clear that automatic fontification as you type by emacs
font-lock should be turned off by default. We could consider
fontification of code blocks e.g. on start up, and when
folding/unfolding, but let's discuss that in a separate thread.

We can go one further with the tab command, and supply a general
mechanism for calling major mode commands from the src buffer (patch
attached):

(defun org-src-do-key-sequence-in-edit-buffer (&optional key)
  "Read key sequence and execute the command in edit buffer."
  (interactive)
  (org-src-do-in-edit-buffer
   (call-interactively
    (key-binding (or key (read-key-sequence nil))))))

So I'm proposing to bind that within the org-babel keymap (currently 
C-c C-v x, and C-c C-v C-x). That way for example:

- C-c C-v C-x TAB :: indent code natively

and with an interactive R session:

- C-c C-v C-x C-c C-c :: `ess-eval-function-or-paragraph-and-step'

I've included a variable `org-src-indent-natively' to control the native
TAB by default (currently defaulting to off, seeing as the C-v C-v x
alternative is always available).

Eric and I are planning on providing the inverse also: exposing the
babel keymap when in the edit buffer.

Dan

patch attached and at git@github.com:dandavison/org-devel.git[1] branch src-block-tab


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: src-block-tab.diff --]
[-- Type: text/x-diff, Size: 2401 bytes --]

diff --git a/lisp/ob-keys.el b/lisp/ob-keys.el
index e9ba3d9..f953110 100644
--- a/lisp/ob-keys.el
+++ b/lisp/ob-keys.el
@@ -77,7 +77,9 @@ functions which are assigned key bindings, and see
     ("z" . org-babel-switch-to-session)
     ("\C-a" . org-babel-sha1-hash)
     ("a" . org-babel-sha1-hash)
-    ("h" . org-babel-describe-bindings))
+    ("h" . org-babel-describe-bindings)
+    ("\C-x" . org-src-do-key-sequence-in-edit-buffer)
+    ("x" . org-src-do-key-sequence-in-edit-buffer))
   "Alist of key bindings and interactive Babel functions.
 This list associates interactive Babel functions
 with keys.  Each element of this list will add an entry to the
diff --git a/lisp/org-src.el b/lisp/org-src.el
index baa2b11..c9e6f73 100644
--- a/lisp/org-src.el
+++ b/lisp/org-src.el
@@ -321,6 +321,8 @@ the edited version. Optional argument CONTEXT is used by
      (if (eq context 'edit) (delete-other-windows))
      (org-switch-to-buffer-other-window buffer)
      (if (eq context 'exit) (delete-other-windows)))
+    ('switch-invisibly
+     (set-buffer buffer))
     (t
      (message "Invalid value %s for org-src-window-setup"
 	      (symbol-name org-src-window-setup))
@@ -435,6 +437,31 @@ the fragment in the Org-mode buffer."
       (message "%s" msg)
       t)))
 
+(defmacro org-src-do-in-edit-buffer (&rest body)
+  "Evaluate BODY in edit buffer if there is a code block at point.
+Return t if a code block was found at point, nil otherwise."
+  `(let ((org-src-window-setup 'switch-invisibly))
+     (when (org-edit-src-code)
+       ,@body
+       (org-edit-src-exit) t)))
+
+(defun org-src-do-key-sequence-in-edit-buffer (&optional key)
+  "Read key sequence and execute the command in edit buffer."
+  (interactive)
+  (org-src-do-in-edit-buffer
+   (call-interactively
+    (key-binding (or key (read-key-sequence nil))))))
+
+(defvar org-src-indent-natively nil
+  "If non-nil, TAB in a code block indents the code as if it were
+called in the edit buffer.")
+
+(defun org-src-tab-command ()
+  "Do what TAB does in the edit buffer for code block at point."
+  (and org-src-indent-natively
+       (org-src-do-key-sequence-in-edit-buffer (kbd "TAB"))))
+
+(add-hook 'org-tab-first-hook 'org-src-tab-command)
 (defun org-edit-src-find-region-and-lang ()
   "Find the region and language for a local edit.
 Return a list with beginning and end of the region, a string representing

[-- Attachment #3: Type: text/plain, Size: 232 bytes --]




>
> - Carsten
>
>
> _______________________________________________
> 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

[-- Attachment #4: Type: text/plain, Size: 201 bytes --]

_______________________________________________
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 related	[flat|nested] 4+ messages in thread

* Re: [PATCH] Indent code with TAB in Org buffer
  2010-08-15 23:00   ` Dan Davison
@ 2010-08-16  6:56     ` Carsten Dominik
  0 siblings, 0 replies; 4+ messages in thread
From: Carsten Dominik @ 2010-08-16  6:56 UTC (permalink / raw)
  To: Dan Davison; +Cc: emacs org-mode mailing list

Hi Dan,

On Aug 16, 2010, at 1:00 AM, Dan Davison wrote:

> Carsten Dominik <carsten.dominik@gmail.com> writes:
>
>> On Aug 15, 2010, at 8:43 AM, Dan Davison wrote:
>>
>>> With this patch TAB in a code block aligns the code according to the
>>> major mode. The macro could be used to do the same thing for other
>>> commands (i.e. allow other commands to be invoked in the Org buffer,
>>> but
>>> actually carried out in the code buffer.)
>>>
>>> Patch attached and in branch src-block-tab at
>>> git@github.com:dandavison/org-devel.git.
>>>
>>> diff --git a/lisp/org-src.el b/lisp/org-src.el
>>> index baa2b11..fc15a83 100644
>>> --- a/lisp/org-src.el
>>> +++ b/lisp/org-src.el
>>> @@ -435,6 +435,19 @@ the fragment in the Org-mode buffer."
>>>      (message "%s" msg)
>>>      t)))
>>>
>>> +(defmacro org-src-do-in-edit-buffer (&rest body)
>>> +  "Evaluate BODY in edit buffer if there is a code block at point.
>>> +Return t if a code block was found at point, nil otherwise."
>>> +  `(when (org-edit-src-code)
>>
>> Wow, an excursion to the temp buffer for every TAB call
>> in a source code block?
>>
>> Sounds heavy.  But I see that it could be nice together with
>> the new fontification stuff.  My gut feeling would be to have
>> both of these feature off by default.  But I have not tried
>> them yet.
>
> Hi Carsten,
>
> In a small code block the TAB is actually not bad at all, but in a big
> one there's a delay (that's worst case scenario: a netbook with an  
> atom
> processor).
>
> I think it's clear that automatic fontification as you type by emacs
> font-lock should be turned off by default. We could consider
> fontification of code blocks e.g. on start up, and when
> folding/unfolding, but let's discuss that in a separate thread.

Yes, this does make sense.  Although I am worried about thing which
increase the org-mode startup time significantly, because Org often
pulls in a lot of files to scan them, and time spent when initializing
the mode would hurt all these processes.  I guess we can figure out
all of this - maybe with a minor mode called org-live-code-mode or
something which can turn on various features in this direction.

It might also be possible to implement this in a much more efficient
way.  For example, we could keep a indirect buffer for the
source snippet which was modifed most recently.  That buffer could
be narrowed to the snippet, and executing TAB and fontification
could be done in that indirect buffer which would have the correct mode,
etc.  Whenever we edit the code, we could have an overlay with  
modification hooks
which would just copy the fontification overlays from the indirect  
buffer.
That should be a lot faster that creating a buffer, initializing a  
mode etc.

- Carsten

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2010-08-16  7:16 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-08-15  6:43 [PATCH] Indent code with TAB in Org buffer Dan Davison
2010-08-15  7:42 ` Carsten Dominik
2010-08-15 23:00   ` Dan Davison
2010-08-16  6:56     ` Carsten Dominik

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