loris has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

To get folding in Emacs 25 for Perl programs I have cargo-cult-copied the outline-based set-up https://github.com/villadora/emacs-config/blob/master/modes.el.

In particular I have:

;; CPerl mode hook (setq cperl-mode-hook 'my-cperl-customizations) (defun my-cperl-customizations () "cperl-mode customizations that must be done after cperl-mode load +s" (outline-minor-mode) (abbrev-mode) (defun cperl-outline-level () (looking-at outline-regexp) (let ((match (match-string 1))) (cond ((eq match "=head1" ) 1) ((eq match "package") 2) ((eq match "=head2" ) 3) ((eq match "=item" ) 4) ((eq match "sub" ) 5) (t 7) ))) (setq cperl-outline-regexp my-cperl-outline-regexp) (setq outline-regexp cperl-outline-regexp) (setq outline-level 'cperl-outline-level) )

My expectation was that if I have

=head2 STUFF =over =item foo Do foo =cut sub foo { return 'foo'; }

I should be able to fold to

=head2 STUFF...

but instead I just can only fold to, say

=head2 STUFF... =item foo Do foo =cut sub foo { return 'foo'; }

i.e. the hierarchy, which I thought gets defined by 'outline-level' doesn't seem to work.

I do have

(add-hook 'outline-minor-mode-hook 'outshine-hook-function)

to get tab-cycling, but maybe this is screwing things up.

Any thoughts or other approaches?

Thanks,

loris

Note: This is something I originally posted, somewhat spuriously, to the Orgmode mailing list several weeks ago. Not that surprisingly, I got no reply.

Replies are listed 'Best First'.
Re: [OT] Folding Perl code with Emacs 25
by LanX (Saint) on Sep 24, 2018 at 11:17 UTC
    Hi

    I'm not using any folding and skeptical about such approaches with regexes.

    But your example could have another problem:

    Perl defines that =POD commands are surrounded by empty lines. Many IDEs (and even Perl) are not enforcing this, but cperl-mode does!

    So do you still have your problem with...?

    =head2 STUFF =over =item foo Do foo =cut sub foo { return 'foo'; }

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

      Unfortunately, adding empty lines doesn't make any difference. It looks as if the outline level isn't being set correctly, so maybe you're right to be skeptical, because I can't get it to work.

      Thanks,

      loris

        I'll try to look into it this weekend.

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Re: [OT] Folding Perl code with Emacs 25
by fishy (Friar) on Sep 24, 2018 at 17:02 UTC
    Hi loris,

    many years ago I had to analyze a huge piece of code and was very pleased with fold-this.el
    Easy to use and install.

    Good luck!
      That looks like something which might be generally quite useful. However, I suppose I am just a bit surprised that there doesn't seem to be anything much which would allow Perl/Pod-aware folding.

      Thanks,

      loris

Re: [OT] Folding Perl code with Emacs 25
by LanX (Saint) on Sep 24, 2018 at 11:23 UTC
    On another note:

    my-cperl-outline-regexp

    is undefined in your post but available in the source you linked to.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

      I should have included it, but yes, I am using the regex for the outline defined in the link.

      Thanks,

      loris

Re: [OT] Folding Perl code with Emacs 25
by LanX (Saint) on Sep 26, 2018 at 01:21 UTC
    So I had a glimpse into it.

    Unfortunately the regex is not best quality, it allows preceding white-space to the POD commands.

    Please note that cperl-mode already supports outline-mode, I didn't install outshine yet, but as a first step you should try to see if works out of the box without your modifications.

    This should be your first step!

    If outshine works and you don't like the levels:

    see (defvar cperl-imenu--function-name-regexp-perl ... for the regex

    and

    (defun cperl-outline-level () for the prioritization of levels.

    and adjust them to your needs (they already match everything you wanted except =item).

    outline-level is just a function which maps any regex-group (number) to a level and the regex group numbers are very well documented in cperl.el

    HTH!

    Excerpt from cperl.el

    ;;; Details of groups in this are used in `cperl-imenu--create-perl-in +dex' ;;; and `cperl-outline-level'. ;;;; Was: 2=sub|package; now 2=package-group, 5=package-name 8=sub-nam +e (+3) (defvar cperl-imenu--function-name-regexp-perl (concat "^\\(" ; 1 = all "\\([ \t]*package" ; 2 = package-group "\\(" ; 3 = package-name-group cperl-white-and-comment-rex ; 4 = pre-package-name "\\([a-zA-Z_0-9:']+\\)\\)?\\)" ; 5 = package-name "\\|" "[ \t]*sub" (cperl-after-sub-regexp 'named nil) ; 8=name 11=proto 14=attr-st +art cperl-maybe-white-and-comment-rex ; 15=pre-block "\\|" "=head\\([1-4]\\)[ \t]+" ; 16=level "\\([^\n]+\\)$" ; 17=text "\\)")) (defvar cperl-outline-regexp (concat cperl-imenu--function-name-regexp-perl "\\|" "\\`")) ;; ... ;; Suggested by Mark A. Hershberger (defun cperl-outline-level () (looking-at outline-regexp) (cond ((not (match-beginning 1)) 0) ; beginning-of-file ;;;; 2=package-group, 5=package-name 8=sub-name 16=head-level ((match-beginning 2) 0) ; package ((match-beginning 8) 1) ; sub ((match-beginning 16) (- (char-after (match-beginning 16)) ?0)) ; headN ==> N (t 5))) ; should not happen

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice