Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Specific Indentation request for perl in Emacs

by Yoyoson (Novice)
on Aug 06, 2004 at 21:11 UTC ( [id://380724]=perlquestion: print w/replies, xml ) Need Help??

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

I am trying to get emacs indentation to behave in such a way that this piece of code will get indented just like the way it's displayed here:
my $var = $new->function( arg1 => 1, );
The thing is that right now, if I ask it to auto indent, I get this:
my $var = $new->function( arg1 => 1, );
Any help is appreciated! I've tried switching between perl-mode and cperl-mode, and setting all kinds of indent-level variables and otherwise. The only thing I haven't tried is editing the emacs or the perl-mode source to get exactly what I want, partially because I am relatively new to emacs/linux and I wouldn't know where to start.

Replies are listed 'Best First'.
Re: Specific Indentation request for perl in Emacs
by rhythmicus (Sexton) on Aug 06, 2004 at 22:47 UTC

    Instead of worrying too much about indentation while I'm coding, I just get it down and then use Perltidy to clean up the details.

    You can use something like this to tidy up your Perl code from within Emacs:

    (global-set-key "\C-xt" 'perltidy-region) (setq shell-command-default-error-buffer "perltidy-errors") (defun perltidy-region () "Run the perltidy parser on the current region." (interactive) (let ((start (mark)) (end (point)) (command "perltidy")) (shell-command-on-region start end command t t shell-command-d +efault-error-buffer)))

    Put this in your ~/.emacs file, select the region you want to clean up and type C-xt or M-x perltidy-region.

      What, you use perltidy? Whenever I use it, I gain bugs! Is there some special magic to making it be nice?
        I use it constantly, on every single piece of code I write that's longer than a command-line. I've never had a single problem with it. Have you reported the bugs you've found?
        I don't use it often, but I've never had a problem with it personally. I mainly use it for downloaded code where the formatting was screwed up somehow. Sometimes, though, it has helped me to find a forgotten closing brace or some similar syntax mistake on my part.
        I read the warnings about Perltidy, but the only problem it gives me is formatting that I didn't ask for. I don't think it should be used on a large script that's not easy to test afterwards.

        All I use is:
        cd directory_of_PerlTidy
        perl perltidy -io -i=3 path_to_script_to _be_formatted

        I keep the above in a comment in the script it's used in. There should be a feature to automatically insert such a comment, with a time stamp of when it was titied.

Re: Specific Indentation request for perl in Emacs
by pdcawley (Hermit) on Aug 17, 2004 at 21:01 UTC
    If you really want to do that, and I strongly recommend that you don't, then add the following to your .emacs
    (setq cperl-indent-parens-as-block t)
    You can also customize the cperl-indent-parens-as-block variable so as not to confuse customize later. Why do I recommend you don't do that? Because indenting parenthesized expressions the same way as a block can create confusion in the reader. If you've got a problem with the right margin encroaching, you can always reindent as follows:
    my $var = $new->function( arg1 => 1, );
    And what do you know? You claw back some vertical space, which is never a bad thing.
      Darn i've been looking for this for a while.

      My Coworkers have been moaning about my emacs-indentation for months!

      I had to download the latest cperl-mode.el from the cpan module, and then it worked fine.

      The perltidy macro above is really nice too.

      I love emacs!

      pdcawley,

      I truly appreciate your reply, this is the closest I've been to solving this issue so far. Unfortunately, I'm having trouble getting it to work.
      By default, my emacs uses perl-mode when I open a perl file or module. So I know how to switch into cperl-mode after I've started the buffer (M-x cperl-mode) and I know how to put the (setq ... ) line in my .emacs, except I'm wondering if I also need to start up cperl-mode automatically from within my .emacs to get this to work.

      As for your advice against using this, I can definitely see the logic in your argument, the only thing is that this particular indentation nuance is a part of my workplace's official coding standard.

      I guess my question then, is, how can I get cperl-mode to start up automatically in my .emacs, or how can I set that toggle-setting after I've started the buffer?

      Thanks!!
        Okay, I managed to get cperl-mode starting up automatically on my own - but the dang thing still doesn't work. I also found the variable cperl-indent-parens-as-block inside of my cperl-mode.el (it's version 4.32) so I don't think that's the issue here. I'm not sure what it could be.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2024-03-28 20:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found