emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Using orgstruct-mode to structure source code
@ 2008-09-03  8:32 Tassilo Horn
  2008-09-03  9:36 ` Carsten Dominik
  0 siblings, 1 reply; 9+ messages in thread
From: Tassilo Horn @ 2008-09-03  8:32 UTC (permalink / raw)
  To: emacs-orgmode

Hi,

this is a resend of my message from <2008-08-20 Wed>.  So far nobody
answered and since Carsten is back now, I thought I may try again.

I'd like to switch from outline-minor-mode to orgstruct-mode for various
source code files, because the outline keybindings are not really quick
nor easy to remember.

For example in shell scripts (sh-mode) a headline looks like

#* Foobar

and in an elisp file it would be

;;* Foobar

Now I've found out that `org-context-p' returns nil for these lines, and
thus no org-mode command will be executed.  So I've redefined it to this
version:

--8<---------------cut here---------------start------------->8---
(defun org-context-p (&rest contexts)
  "Check if local context is and of CONTEXTS.
Possible values in the list of contexts are `table', `headline', and `item'."
  (let ((pos (point))
        (comment-starter (replace-regexp-in-string "[ ]+$" "" (or comment-start ""))))
    (goto-char (point-at-bol))
    (prog1 (or (and (memq 'table contexts)
		    (looking-at (concat comment-starter "?[ \t]*|")))
	       (and (memq 'headline contexts)
		    (looking-at (concat comment-starter "?\\*+")))
	       (and (memq 'item contexts)
		    (looking-at (concat comment-starter "?[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))))
      (goto-char pos))))
--8<---------------cut here---------------end--------------->8---

Now it does return t for headlines like above, but still no org-mode
command is issued.

What do I have to do to get that working correctly?

Bye,
Tassilo

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

* Re: Using orgstruct-mode to structure source code
  2008-09-03  8:32 Using orgstruct-mode to structure source code Tassilo Horn
@ 2008-09-03  9:36 ` Carsten Dominik
  2008-09-03 10:20   ` Tassilo Horn
  0 siblings, 1 reply; 9+ messages in thread
From: Carsten Dominik @ 2008-09-03  9:36 UTC (permalink / raw)
  To: Tassilo Horn; +Cc: emacs-orgmode

Hi Tassilo,


"?..." is not a correct regular expression in Emacs.  You are using  
wildcard synax, it seems.


If you want to allow extra characters # and ; at the begin of the  
line, try something like

"[;#]*..."


I am not sure if this will work, in particular if it will make  
structure editing work.
Give it a try and send us a report, ok?

- Carsten


On Sep 3, 2008, at 10:32 AM, Tassilo Horn wrote:

> Hi,
>
> this is a resend of my message from <2008-08-20 Wed>.  So far nobody
> answered and since Carsten is back now, I thought I may try again.
>
> I'd like to switch from outline-minor-mode to orgstruct-mode for  
> various
> source code files, because the outline keybindings are not really  
> quick
> nor easy to remember.
>
> For example in shell scripts (sh-mode) a headline looks like
>
> #* Foobar
>
> and in an elisp file it would be
>
> ;;* Foobar
>
> Now I've found out that `org-context-p' returns nil for these lines,  
> and
> thus no org-mode command will be executed.  So I've redefined it to  
> this
> version:
>
> --8<---------------cut here---------------start------------->8---
> (defun org-context-p (&rest contexts)
>  "Check if local context is and of CONTEXTS.
> Possible values in the list of contexts are `table', `headline', and  
> `item'."
>  (let ((pos (point))
>        (comment-starter (replace-regexp-in-string "[ ]+$" "" (or  
> comment-start ""))))
>    (goto-char (point-at-bol))
>    (prog1 (or (and (memq 'table contexts)
> 		    (looking-at (concat comment-starter "?[ \t]*|")))
> 	       (and (memq 'headline contexts)
> 		    (looking-at (concat comment-starter "?\\*+")))
> 	       (and (memq 'item contexts)
> 		    (looking-at (concat comment-starter "?[ \t]*\\([-+*] \\|[0-9]+ 
> [.)] \\)"))))
>      (goto-char pos))))
> --8<---------------cut here---------------end--------------->8---
>
> Now it does return t for headlines like above, but still no org-mode
> command is issued.
>
> What do I have to do to get that working correctly?
>
> Bye,
> Tassilo
>
>
>
> _______________________________________________
> Emacs-orgmode mailing list
> Remember: 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] 9+ messages in thread

* Re: Using orgstruct-mode to structure source code
  2008-09-03  9:36 ` Carsten Dominik
@ 2008-09-03 10:20   ` Tassilo Horn
  2008-09-03 14:39     ` Eric Schulte
  2008-09-05  7:15     ` Carsten Dominik
  0 siblings, 2 replies; 9+ messages in thread
From: Tassilo Horn @ 2008-09-03 10:20 UTC (permalink / raw)
  To: Carsten Dominik; +Cc: emacs-orgmode

Carsten Dominik <dominik@science.uva.nl> writes:

Hi Carsten,

> "?..." is not a correct regular expression in Emacs.  You are using
> wildcard synax, it seems.

No, I used (concat comment-starter "?...") to make the comment starter
optional.  But that's not fully correct.  In elisp the comment starter
may be there multiple times.  So now I go with this:

--8<---------------cut here---------------start------------->8---
(defun org-context-p (&rest contexts)
  (let* ((pos (point))
         (comment-starter (replace-regexp-in-string "[ ]+$" "" (or comment-start "")))
         (regexp-start (if (string= "" comment-start)
                           ""
                         (concat comment-start "*"))))
    (goto-char (point-at-bol))
    (let ((ret (prog1
                   (or (and (memq 'table contexts)
                            (looking-at (concat regexp-start "[ \t]*|")))
                       (and (memq 'headline contexts)
                            (looking-at (concat regexp-start "\\*+")))
                       (and (memq 'item contexts)
                            (looking-at (concat regexp-start
                                                "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))))
                 (goto-char pos))))
      (message "org-context-p with regexp-start = %s ==> %s" regexp-start ret))))
--8<---------------cut here---------------end--------------->8---

Here's a short elisp test file:

--8<---------------cut here---------------start------------->8---
;;* First Headline    ; org-context-p ==> t, no visible action
                      ; org-context-p ==> nil, no visible action
(defun foo ()
  nil)                ; org-context-p ==> nil, TAB deletes indentation!
                      ; org-context-p ==> nil, TAB indents to nil's column
;;** Level2           ; org-context-p ==> t, TAB indents heading to nil's column

(+ 1 2 3)

;;** Another Level2

(- 1 1)

;;*** Three

;;* Second part

(progn
  (list 1 2 3)        ; org-context-p ==> nil, TAB deletes indentation!
  (* 1 2 3))          ; org-context-p ==> nil, TAB deletes indentation!

;;* Third part        ; org-context-p ==> t, TAB indents heading to
                      ; column of (* 1 2 3)
--8<---------------cut here---------------end--------------->8---

Behind the lines I've written what my redefinition of `org-context-p'
returns and what TAB does (*without* these comments).  As you can see
`org-context-p' seems to work correctly, but TAB doesn't cycle through
the visibility states but instead breakes indentation.

And if I invoke `M-x org-cycle' on a headline nothing happens.  As it
seems, `org-context-p' is not the only function which has to be adapted
to do the right thing if headlines are inside comments.

> If you want to allow extra characters # and ; at the begin of the line, try
> something like
>
> "[;#]*..."

Yes, see the code above.

> I am not sure if this will work, in particular if it will make
> structure editing work.

Sadly, it's not that easy.

> Give it a try and send us a report, ok?

Done!

Bye,
Tassilo

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

* Re: Using orgstruct-mode to structure source code
  2008-09-03 10:20   ` Tassilo Horn
@ 2008-09-03 14:39     ` Eric Schulte
  2008-09-03 15:39       ` Tassilo Horn
  2008-09-05  7:15     ` Carsten Dominik
  1 sibling, 1 reply; 9+ messages in thread
From: Eric Schulte @ 2008-09-03 14:39 UTC (permalink / raw)
  To: Tassilo Horn; +Cc: emacs-orgmode

Instead of putting org into the source file, would it make sense to put
the source code into an org file.  Using MuMaMo-Mode
http://www.emacswiki.org/cgi-bin/wiki/MuMaMo and delimiters like

#+BEGIN_LISP

#+END_LISP

it would be possible to activate the appropriate source mode inside of
the blocks, and it shouldn't be too hard to define an
`org-export-to-source-code' function. -- Eric

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

* Re: Using orgstruct-mode to structure source code
  2008-09-03 14:39     ` Eric Schulte
@ 2008-09-03 15:39       ` Tassilo Horn
  0 siblings, 0 replies; 9+ messages in thread
From: Tassilo Horn @ 2008-09-03 15:39 UTC (permalink / raw)
  To: Eric Schulte; +Cc: emacs-orgmode

"Eric Schulte" <schulte.eric@gmail.com> writes:

Hi Eric,

> Instead of putting org into the source file, would it make sense to
> put the source code into an org file.  Using MuMaMo-Mode
> http://www.emacswiki.org/cgi-bin/wiki/MuMaMo and delimiters like
>
> #+BEGIN_LISP
>
> #+END_LISP
>
> it would be possible to activate the appropriate source mode inside of
> the blocks, and it shouldn't be too hard to define an
> `org-export-to-source-code' function.

I wouldn't like that for at least two reasons.  

1) Not all files I where I use headlines for structuring are private to
   me.  Such a headline doesn't disturb anybody, but your suggestion
   works out only if everybody who needs to edit those files uses emacs
   and org.

2) All the links to files you get with `describe-function' and friends
   would point to generated files.  If I want to change something, I'd
   have to find the corresponding org file first.

Bye,
Tassilo

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

* Re: Using orgstruct-mode to structure source code
  2008-09-03 10:20   ` Tassilo Horn
  2008-09-03 14:39     ` Eric Schulte
@ 2008-09-05  7:15     ` Carsten Dominik
  2008-09-05  8:02       ` Tassilo Horn
  1 sibling, 1 reply; 9+ messages in thread
From: Carsten Dominik @ 2008-09-05  7:15 UTC (permalink / raw)
  To: Tassilo Horn; +Cc: emacs-orgmode

Hi Tassilo,

I though a little bit about this, and using org-struct-mode in a  
commented block of text is going to be bumpy.  No easy path.

What I do in such cases:

I put tow markers into the buffer hat delimit the text I want to edit  
in orgstruct-mode.  And then I have custom function that toggles the  
comment starters on an off, like so:

;; BEGIN ORGSTRUCT ";; "
;; * heading 1
;;   with text
;; * heading 2
;; ** subheading
;; END ORGSTRUCT

(defun my-toggle-comment-or-orgstruct ()
  "Toggle comment for orgstruc editing."
  (interactive)
  (let* ((re-beg "BEGIN ORGSTRUCT \"\\(.*?\\)\"")
	 (re-end "END ORGSTRUCT")
	 re rpl line1 line2)
    (save-excursion
      (save-excursion
	(unless (re-search-backward re-beg nil t)
	  (error "Not in an orgstruct bock"))
	(setq line1 (org-current-line)
	      leader (match-string 1)))
      (save-excursion
	(unless (re-search-forward re-end nil t)
	  (error "Not in an orgstruct bock"))
	(setq line2 (org-current-line)))
      (goto-line (1+ line1))
      (if (equal (buffer-substring
		  (point) (+ (point) (length leader)))
		 leader)
	  (setq re (concat "^" (regexp-quote leader))
		rpl "")
	(setq re "^" rpl leader))
      (while (and (re-search-forward re nil t)
		  (< (org-current-line) line2))
	(replace-match rpl t t)))))

Hope this helps

- Carsten


On Sep 3, 2008, at 12:20 PM, Tassilo Horn wrote:

> Carsten Dominik <dominik@science.uva.nl> writes:
>
> Hi Carsten,
>
>> "?..." is not a correct regular expression in Emacs.  You are using
>> wildcard synax, it seems.
>
> No, I used (concat comment-starter "?...") to make the comment starter
> optional.  But that's not fully correct.  In elisp the comment starter
> may be there multiple times.  So now I go with this:
>
> --8<---------------cut here---------------start------------->8---
> (defun org-context-p (&rest contexts)
> (let* ((pos (point))
>        (comment-starter (replace-regexp-in-string "[ ]+$" "" (or  
> comment-start "")))
>        (regexp-start (if (string= "" comment-start)
>                          ""
>                        (concat comment-start "*"))))
>   (goto-char (point-at-bol))
>   (let ((ret (prog1
>                  (or (and (memq 'table contexts)
>                           (looking-at (concat regexp-start  
> "[ \t]*|")))
>                      (and (memq 'headline contexts)
>                           (looking-at (concat regexp-start "\\*+")))
>                      (and (memq 'item contexts)
>                           (looking-at (concat regexp-start
>                                               "[ \t]*\\([-+*] \\| 
> [0-9]+[.)] \\)"))))
>                (goto-char pos))))
>     (message "org-context-p with regexp-start = %s ==> %s" regexp- 
> start ret))))
> --8<---------------cut here---------------end--------------->8---
>
> Here's a short elisp test file:
>
> --8<---------------cut here---------------start------------->8---
> ;;* First Headline    ; org-context-p ==> t, no visible action
>                     ; org-context-p ==> nil, no visible action
> (defun foo ()
> nil)                ; org-context-p ==> nil, TAB deletes indentation!
>                     ; org-context-p ==> nil, TAB indents to nil's  
> column
> ;;** Level2           ; org-context-p ==> t, TAB indents heading to  
> nil's column
>
> (+ 1 2 3)
>
> ;;** Another Level2
>
> (- 1 1)
>
> ;;*** Three
>
> ;;* Second part
>
> (progn
> (list 1 2 3)        ; org-context-p ==> nil, TAB deletes indentation!
> (* 1 2 3))          ; org-context-p ==> nil, TAB deletes indentation!
>
> ;;* Third part        ; org-context-p ==> t, TAB indents heading to
>                     ; column of (* 1 2 3)
> --8<---------------cut here---------------end--------------->8---
>
> Behind the lines I've written what my redefinition of `org-context-p'
> returns and what TAB does (*without* these comments).  As you can see
> `org-context-p' seems to work correctly, but TAB doesn't cycle through
> the visibility states but instead breakes indentation.
>
> And if I invoke `M-x org-cycle' on a headline nothing happens.  As it
> seems, `org-context-p' is not the only function which has to be  
> adapted
> to do the right thing if headlines are inside comments.
>
>> If you want to allow extra characters # and ; at the begin of the  
>> line, try
>> something like
>>
>> "[;#]*..."
>
> Yes, see the code above.
>
>> I am not sure if this will work, in particular if it will make
>> structure editing work.
>
> Sadly, it's not that easy.
>
>> Give it a try and send us a report, ok?
>
> Done!
>
> Bye,
> Tassilo

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

* Re: Using orgstruct-mode to structure source code
  2008-09-05  7:15     ` Carsten Dominik
@ 2008-09-05  8:02       ` Tassilo Horn
  2008-09-05  9:07         ` Carsten Dominik
  0 siblings, 1 reply; 9+ messages in thread
From: Tassilo Horn @ 2008-09-05  8:02 UTC (permalink / raw)
  To: Carsten Dominik; +Cc: emacs-orgmode

Carsten Dominik <dominik@science.uva.nl> writes:

Hi Carsten,

> I though a little bit about this, and using org-struct-mode in a
> commented block of text is going to be bumpy.  No easy path.

Too bad.  I thought that it would be easy (at least for headlines) by
simply allowing `comment-start' before the headline.

> What I do in such cases:
>
> I put tow markers into the buffer hat delimit the text I want to edit
> in orgstruct-mode.  And then I have custom function that toggles the
> comment starters on an off, like so:
>
> ;; BEGIN ORGSTRUCT ";; "
> ;; * heading 1
> ;;   with text
> ;; * heading 2
> ;; ** subheading
> ;; END ORGSTRUCT

The problem with this is, that "with text" will be commented, too.  But
in my case this would be source code, which should not be commented.

So now I think I'll stay with outline-minor-mode.  The thing which is
much better with orgstruct-mode is that TAB has a special meaning
depending on the context point is on.  So I'll use that feature to make
outline-minor-mode do what I want.  IMO this facility could be of use in
may other places, too.  I think it would be nice to rip that out of
org-mode into something more generic, like

  (define-context-key some-keymap (kbd "TAB")
                      'foobar-context-p
                      'do-foo).

I played a bit with the code and tried to elicit how you get the command
that normally would have been invoked if the stealing function wasn't
there, but I failed.  Could you please enlighten me?

All the best,
Tassilo

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

* Re: Using orgstruct-mode to structure source code
  2008-09-05  8:02       ` Tassilo Horn
@ 2008-09-05  9:07         ` Carsten Dominik
  2008-09-05  9:41           ` Tassilo Horn
  0 siblings, 1 reply; 9+ messages in thread
From: Carsten Dominik @ 2008-09-05  9:07 UTC (permalink / raw)
  To: Tassilo Horn; +Cc: emacs-orgmode


On Sep 5, 2008, at 10:02 AM, Tassilo Horn wrote:

> Carsten Dominik <dominik@science.uva.nl> writes:
>
> Hi Carsten,
>
>> I though a little bit about this, and using org-struct-mode in a
>> commented block of text is going to be bumpy.  No easy path.
>
> Too bad.  I thought that it would be easy (at least for headlines) by
> simply allowing `comment-start' before the headline.
>
>> What I do in such cases:
>>
>> I put tow markers into the buffer hat delimit the text I want to edit
>> in orgstruct-mode.  And then I have custom function that toggles the
>> comment starters on an off, like so:
>>
>> ;; BEGIN ORGSTRUCT ";; "
>> ;; * heading 1
>> ;;   with text
>> ;; * heading 2
>> ;; ** subheading
>> ;; END ORGSTRUCT
>
> The problem with this is, that "with text" will be commented, too.   
> But
> in my case this would be source code, which should not be commented.
>
> So now I think I'll stay with outline-minor-mode.  The thing which is
> much better with orgstruct-mode is that TAB has a special meaning
> depending on the context point is on.  So I'll use that feature to  
> make
> outline-minor-mode do what I want.  IMO this facility could be of  
> use in
> may other places, too.  I think it would be nice to rip that out of
> org-mode into something more generic, like
>
>  (define-context-key some-keymap (kbd "TAB")
>                      'foobar-context-p
>                      'do-foo).
>
> I played a bit with the code and tried to elicit how you get the  
> command
> that normally would have been invoked if the stealing function wasn't
> there, but I failed.  Could you please enlighten me?

I am explaining this in detail near the end of the google tech talk,  
with a code example.
Basically, it works by temporarily turning of the minor mode that  
defines the command and doing a key lookup.

- Carsten

>
>
> All the best,
> Tassilo

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

* Re: Using orgstruct-mode to structure source code
  2008-09-05  9:07         ` Carsten Dominik
@ 2008-09-05  9:41           ` Tassilo Horn
  0 siblings, 0 replies; 9+ messages in thread
From: Tassilo Horn @ 2008-09-05  9:41 UTC (permalink / raw)
  To: Carsten Dominik; +Cc: emacs-orgmode

Carsten Dominik <dominik@science.uva.nl> writes:

Hi Carsten,

>> I played a bit with the code and tried to elicit how you get the
>> command that normally would have been invoked if the stealing
>> function wasn't there, but I failed.  Could you please enlighten me?
>
> I am explaining this in detail near the end of the google tech talk,
> with a code example.  Basically, it works by temporarily turning of
> the minor mode that defines the command and doing a key lookup.

Watching that video is on my TODO list anyway. :-)

Bye,
Tassilo

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

end of thread, other threads:[~2008-09-05  9:43 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-09-03  8:32 Using orgstruct-mode to structure source code Tassilo Horn
2008-09-03  9:36 ` Carsten Dominik
2008-09-03 10:20   ` Tassilo Horn
2008-09-03 14:39     ` Eric Schulte
2008-09-03 15:39       ` Tassilo Horn
2008-09-05  7:15     ` Carsten Dominik
2008-09-05  8:02       ` Tassilo Horn
2008-09-05  9:07         ` Carsten Dominik
2008-09-05  9:41           ` Tassilo Horn

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