Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Formatting Perl code with Emacs

by isntvoid (Novice)
on Nov 06, 2013 at 13:34 UTC ( #1061428=perlquestion: print w/ replies, xml ) Need Help??
isntvoid has asked for the wisdom of the Perl Monks concerning the following question:

I can't format my code properly with CPerl GNU Emacs major mode.

I've got

sub _get_options { Getopt::Long::Configure('gnu_getopt'); return Getopt::Long::GetOptions(\%Options, qw( version|V debug ) ); }
instead of desirable
sub _get_options { Getopt::Long::Configure('gnu_getopt'); return Getopt::Long::GetOptions(\%Options, qw( version|V debug ) ); }

Playing with cperl-* variables didn't give any positive effect.

Seek for your wisdom, monks!

Comment on Formatting Perl code with Emacs
Select or Download Code
Re: Formatting Perl code with Emacs (updated)
by LanX (Canon) on Nov 06, 2013 at 13:53 UTC
    That's a long known issue which can only be solved by patching cperl-mode.

    I identified the necessary changes in a discussion in "gnu emacs help" some years ago and Ilya welcomed any patches...¹

    Don't know if recent versions already solved this...

    HTH ! =)

    Cheers Rolf

    ( addicted to the Perl Programming Language)

    update

    An easy workaround is to put the opening bracket into the next line...

    Sorry can't demonstrate while on mobile...

    update

    See also http://www.emacswiki.org/emacs/IndentingPerl

    footnotes
    ¹) see here CPerl-Mode Indentation and here

      Thank you!

      I've read Indenting Perl later and tried to customize Emacs with cperl-* variables, but it doesn't help.

      So, I have to continue using Vim for coding, because I need a tool, not the master...

Re: Formatting Perl code with Emacs
by karlgoethebier (Curate) on Nov 06, 2013 at 15:12 UTC

    Perhaps using perltidy is an alternative it does'n - results don't look bad:

    sub _get_options { Getopt::Long::Configure('gnu_getopt'); return Getopt::Long::GetOptions( \%Options, qw( version|V debug ) ); }

    And perhaps you can configure perltidy to get exactly the result you want :-)

    Nice command to invoke perltidy from within emacs:

    Type C-x h and then M-1 M-| to run the shell command (perltidy) on the marked region.

    Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

      Thank you, but this way is too complicated for me. If you have to run shell command from the text editor to format your code, why don't you run another one to save the file?..

      "If you have to run shell command from the text editor to format your code, why don't you run another one to save the file?.."

      Perhaps a misunderstanding? But it is quite simple:

      • Mark the region that you want to format
      • Apply the described command (Shell command on region: perltidy)
      • Save your stuff

      Alternatively you can run perltidy from the commandline. Then you must rename the resulting formatted file and reload the buffer (M-x revert-buffer).

      IMHO applying the shell command (perltidy) on a region respectively running perltidy from within emacs is convinient - and the results look good.

      Update:

      The macro and the binding to F11:

      (fset 'perltidy "\C-xh\C-[1\C-[|perltidy\C-m") (global-set-key [f11] 'perltidy)

      Run it with F11 or M-x perltidy

      Update2:

      OK, on a Mac F11 isn't a good idea :-(

      (global-set-key [f5] 'perltidy)

      Best regards, Karl

      P.S.: BTW, i think, many stuff posted on PM is formatted with perltidy, from within emacs or vim. See also

      «The Crux of the Biscuit is the Apostrophe»

        Thanks. Perltidy is quite good, but I don't use it in my coding practice, because I prefer to format my code manually with the editor commands at the same time as I type it. TMTOWTDI.

        It's not that complicated, you can easily bind the whole process to a key!

        Either define and save a macro or search for ready-to-use elisp snippets. There are plenty...

        Cheers Rolf

        ( addicted to the Perl Programming Language)

Re: Formatting Perl code with Emacs (solved?)
by LanX (Canon) on Nov 06, 2013 at 16:47 UTC
    setting cperl-indent-parens-as-block to 1 seems to solve this now! =)

    M-x customize-option :

    Cperl Indent Parens As Block: Hide Value Toggle on (non-nil) State: SAVED and set. Non-nil means that non-block ()-, {}- and []-groups are indented as + blocks, Hide Rest but for trailing "," inside the group, which won't increase indenta +tion. One should tune up `cperl-close-paren-offset' as well. Groups: Cperl Indentation Details

    please note that \%Options must be in the next line since all arguments are aligned.

    sub _get_options { Getopt::Long::Configure('gnu_getopt'); return Getopt::Long::GetOptions( \%Options, qw( version|V debug ) ); }

    HTH! =)

    Cheers Rolf

    ( addicted to the Perl Programming Language)

    update

    you may want to experiment with

    Cperl Close Paren Offset: Hide Value -2 State: SET for current session only. Extra indent for substatements that start with close-parenthesis.

    sub _get_options { Getopt::Long::Configure('gnu_getopt'); return Getopt::Long::GetOptions( \%Options, qw( version|V debug ), ); }

    all options available per GUI via M-x customize-group cperl-indentation-details

    or via mouse-menu

    Options Customize Emacs Specific Group

    update

    untabified my samples b/c of display problems in code -blocks

      Thanks, I've tried to redefine these variables, but my code didn't look like I want to.

      (setq-default cperl-indent-parens-as-block t) and (setq-default cperl-close-paren-offset -2) help to reindent parentheses, but why have we got 4 spaces inside the qw() block then cperl-indent-level was set to 2?

        Don't know ... can't see a problem...

        But well if you don't like it, better continue using VIM ... :)

        Cheers Rolf

        ( addicted to the Perl Programming Language)

Re: Formatting Perl code with Emacs
by bojinlund (Curate) on Nov 07, 2013 at 10:04 UTC

    I am using Emacs and perltidy by adding the lines

    # (compile (concat "perltidy " (buffer-name)) nil) # (ediff-files (concat (buffer-name) ".tdy") (buffer-name))
    to my Perl scripts.

    Putting the cursor at the end of the added lines and entering control-X, control-E will execute perltidy and activate the ediff-files function in Emacs. Then I can select which part of my script I want to be changed.

      Thats a pretty cool idea, thanks for sharing! =)

      Cheers Rolf

      ( addicted to the Perl Programming Language)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1061428]
Approved by hdb
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (7)
As of 2014-07-23 03:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (133 votes), past polls