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

Using Emacs 22 with CPerl mode and using cperl-hairy, with electric parentheses, after typing "if ", rather than this:

if () { }

I want to get this:

if ( ) { }

That is, I want extra space around whatever I put in the parens (with the point placed between the two spaces). Would be nice to get the same sort of behaviour for brackets and curlies (braces) too. Does anyone know how to get cperl mode to do that? I checked the CPerlMode page on the Emacswiki but didn't see anything that specific.

Seems like not having the extra space in there is too cramped, but might have to just get used to it because typing "Space Space C-b" all the time is beginning to wear on me. :)

Replies are listed 'Best First'.
Re: Emacs CPerl mode electric parentheses: get extra space in there?
by samtregar (Abbot) on Jul 01, 2008 at 17:53 UTC
    Use the source! A brief spelunk through cperl-mode.el leads me to think you could override cperl-electric-paren and add in some spaces. If you're an Emacs Lisp noob, may I suggest you pick up a copy of the Writing GNU Extensions for Emacs book (ignore the pirates at the top of that search result!). It's well worth the time to learn how to extend your favorite editor.

    -sam

      Thanks for the running start, Sam. Guess I'm gonna have to finally bite the bullet and dive into learning elisp (it's been on the TODO list for a while anyway). As an aside though, interesting that there doesn't seem to be an easy "(setq cperl-gimme-more-space t)" way of setting it. I'd have guessed that many programmers would prefer the extra space.

        Good luck. I personally don't use any of the electric stuff in cperl-mode. I don't bother formatting my code as I type anymore, I just run it all through Perl::Tidy at the end.

        -sam

Re: Emacs CPerl mode electric parentheses: get extra space in there?
by educated_foo (Vicar) on Jul 01, 2008 at 18:52 UTC
    That is, I want extra space around whatever I put in the parens
    Let me be the first to say "ick." I've never understood this coding style, since it goes against spacing conventions for normal text.
      Let me be the first to say "ick."

      You're the first! ;)

      By the way, I don't like the extra spaces everywhere. For example, "$foo{bar}" and "$baz[1]" need no extra spaces. However, things like "if ( -e $file ) ..." look better to me with that extra space. More visual separatation I guess.

        To me, it's uber-yuck on the level of putting spaces *before* commas. That said, I should also say something useful: download snippet.el. It's much better than cperl's electric mode, with a simple syntax to defined your own abbreviations, e.g. here.
      it goes against spacing conventions for normal text

      I completely agree which is why I capitalize "My" and end all my statements with periods instead of semi-colons and all my sub and method calls with question marks and I use proper left and right double quotes with all my arguments to file handles and such. :)

        *sigh* This is yet another example of why I hate talking to (fellow) programmers: pointless pedantry. Some syntax is required by a programming language; some is optional. I prefer that the optional stuff follow normal language conventions (i.e. those of written English; yes, I know it may not be your first language, but it's the language of the keywords in your programming language).