10

When using paredit in programming modes such as C, typing ( will insert a space before the paren when I'm trying to call a function, leaving me with:

foo ()

Is there a way to disable the insertion of the space without changing paredit's source?

viam0Zah
  • 25,949
  • 8
  • 77
  • 100
Nathaniel Flath
  • 15,477
  • 19
  • 69
  • 94

3 Answers3

12

Well, the way paredit appears to work is that it checks the syntax tables to see if you're inserting a pair right after a word/symbol/etc., in which case it forces a space to be inserted. You need to override that functionality - which can be done a number of different ways: advice, redefine the function determining space, changing the syntax table, etc.

I'd try the straight forward:

(defun paredit-space-for-delimiter-p (endp delimiter)
  (and (not (if endp (eobp) (bobp)))
       (memq (char-syntax (if endp (char-after) (char-before)))
             (list ?\"  ;; REMOVED ?w ?_
                   (let ((matching (matching-paren delimiter)))
                     (and matching (char-syntax matching)))))))

This will obviously apply to all places where you use paredit. If you want something more mode specific, you can add some conditions to that and statement (e.g. (and ... (memq major-mode '(c-mode lisp-mode)))).

So... I guess I did change the "source", but you can do the same thing with a piece of defadvice ... it's all elisp, so the difference is minimal. There doesn't appear to be a setting to control this type of behavior.

Trey Jackson
  • 73,529
  • 11
  • 197
  • 229
  • 2
    I've been wanting this for a long time, since I find autopair.el _very_ slow. Paredit now works like a charm in `c-mode`. Thank you! :) – Vicky Chijwani Jan 31 '12 at 13:31
  • For being mode specific, one can also write `(defvar-local my-paredit-no-insert-space nil)` and use `(and ... my-paredit-no-insert-space)` instead of `(and ... (memq major-mode '(c-mode lisp-mode))))` and then set `my-paredit-no-insert-space` to `t` from `c-mode-common-hook`. This way two people with difference preferences can use the same new definition of `paredit-space-for-delimiter-p` – Jisang Yoo Oct 03 '13 at 08:44
  • `If you want something more mode specific, you can add some conditions to that and statement` - I do want to not override this for some modes, but I don't understand which `if' statement i can add the condition to. Can you please clarify? – Max Oct 05 '16 at 01:17
6

See paredit-space-for-delimiter-predicates

5fec
  • 517
  • 6
  • 11
  • This might be of interest in this regard (might need adjustment to mode names, or even removal of 'dolist' and simple addition of that 'lambda()' to your mode's hook (if having only one mode)): https://gist.github.com/remvee/879305 – dimsuz Nov 16 '13 at 10:32
4

Well, Paredit is ideal for editing languages built of S-expressions. If you just like how it automatically inserts the closing paren, use feature skeleton-pair.

(setq skeleton-pair t)
(global-set-key "(" 'skeleton-pair-insert-maybe)
viam0Zah
  • 25,949
  • 8
  • 77
  • 100
  • Good point. I forgot about skeleton-pair. I'm so used to Paredit for mucking around with emacs lisp. I need to set that up for my non-lispy languages.. – Jeremy Grozavescu May 29 '09 at 00:26
  • 3
    Actually, paredit mode is extremely useful outside s-expression-based languages as well for enforcing validity of matched delimiters; it's not just about insertion. –  Jun 26 '09 at 19:01