emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Carsten Dominik <dominik@science.uva.nl>
To: Tassilo Horn <tassilo@member.fsf.org>
Cc: emacs-orgmode@gnu.org
Subject: Re: Using orgstruct-mode to structure source code
Date: Fri, 5 Sep 2008 09:15:35 +0200	[thread overview]
Message-ID: <0E11839C-C0CE-41E8-A937-2CBCCB163BB1@uva.nl> (raw)
In-Reply-To: <87k5dtfs3z.fsf@thinkpad.tsdh.de>

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

  parent reply	other threads:[~2008-09-05  7:15 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2008-09-05  8:02       ` Tassilo Horn
2008-09-05  9:07         ` Carsten Dominik
2008-09-05  9:41           ` Tassilo Horn

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=0E11839C-C0CE-41E8-A937-2CBCCB163BB1@uva.nl \
    --to=dominik@science.uva.nl \
    --cc=emacs-orgmode@gnu.org \
    --cc=tassilo@member.fsf.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).