help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: hideshow minor-mode


From: Michael Heerdegen
Subject: Re: hideshow minor-mode
Date: Tue, 16 Oct 2012 03:03:57 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.2.50 (gnu/linux)

Shiyuan <address@hidden> writes:

> Yes, that solves the problem. Now, I can turn on hs-minor-mode. 
>
> But actually the block in my major mode is defined by tags, not by
> braces/parentheses, like,
> [begin]
> This is the block.
> This is the block.
> [end]
>
> accord to the commentary in the hideshow.el file, we can define the
> BEGIN and the END of the block by regex, this is what I did,
>
> (add-to-list 'hs-special-modes-alist '(mymode "\\[begin]" "\\[end]"
> "#" nil nil))

Yes, that's not wrong.

> However, it doesn't work. Anything else I need to do?

I'm afraid you'll need to specify a FORWARD-SEXP-FUNC element in your
`hs-special-modes-alist' entry, since the default `forward-sexp'
function won't work for your mode.

This function must accept one argument ARG and implement moving over ARG
balanced blocks.

If you don't have something like that yet - I tried the following:

--8<---------------cut here---------------start------------->8---
(defun mymode-forward-sexp-func (arg)
  (dotimes (_ arg)
    (let ((counter 0))
      (catch 'done
        (while t
          (search-forward-regexp "\\[begin]\\|\\[end]")
          (setq counter (+ counter (if (looking-back "\\[begin]") 1 -1)))
          (when (= counter 0) (throw 'done t)))))))
--8<---------------cut here---------------end--------------->8---

It doesn't check for comments, dunno what else I forgot.  But,
if I then use

(add-to-list 'hs-special-modes-alist '(mymode "\\[begin]" "\\[end]"
"#" my-mode-forward-sexp-func))

folding worked for me in a test buffer using your syntax.

> or is there other packages allow me to fold the source where the
> blocks are defined in this way?

Fundamentally, hideshow is fine for that.  It's just the setup that
isn't trivial.  No doubt, there is room for improvement.


Regards,

Michael.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]