8

Usually after inserting punctuation marks like . , : I make an space before inserting the next character. Is it possible that Emacs automatically do this behavior? For example inserting something like .x results in typing . x where x is an arbitrary character (except for some particular cases excluded by the user for example the case where x is an space itself).

Sometimes this can help to speed up typing.

Name
  • 7,689
  • 4
  • 38
  • 84

2 Answers2

8

Smart operator looks promising, but I have not tried it so I can't speak for it. A prebuilt solution would be ideal, but if none are sufficient, it would be very easy to wrap write this functionality and wrap it in a minor mode though.

Here's my go:

(defvar auto-punc-punctuation-members
  (string-to-list ".,:;?!")
  "List of charactesr to insert spaces after")

(defvar auto-punc-ignore-members
  (string-to-list " \t")
  "List of characters to not auto insert spaces before")

(defun auto-punc-maybe-do ()
  "If the last entered character is not in `auto-punc-punctuation-members' or `auto-punc-ignore-members',
and the prior character is in `auto-punc-punctuation-members',
insert a space between the two characters. "
  (when (and auto-space-punctuation-mode
             (not (member (char-before) (append auto-punc-punctuation-members auto-punc-ignore-members)))
             (member (char-before (1- (point))) auto-punc-punctuation-members))
    (backward-char 1)
    (insert " ")
    (forward-char 1)))

(define-minor-mode auto-space-punctuation-mode
  "Automatically inserts spaces between some punctuation and other characters."
  :init-value nil
  :lighter "._a"
  :keymap nil
  (make-variable-buffer-local 'post-self-insert-hook)
  (if auto-space-punctuation-mode
      (add-hook 'post-self-insert-hook 'auto-punc-maybe-do)
    (remove-hook 'post-self-insert-hook 'auto-punc-maybe-do)))

You could simply add it to your init and auto enable it when you want like

(add-hook 'text-mode-hook 'auto-space-punctuation-mode)

Everytime you insert a character the function auto-punc-maybe-do runs, read the docstring to ensure this is the behavior you want. Basically if you type punctuation, then anything that is not punctuation or whitespace, a space will be inserted automatically.

Jordon Biondo
  • 12,332
  • 2
  • 41
  • 62
  • 2
    I confirm that your solution perfectly works in text-mode. – Name Feb 10 '15 at 14:37
  • 1
    Great, glad to hear it. I just updated the code to make the `post-self-insert-hook buffer local, if you grabbed the code earlier, you may find the mode working in buffers you don't expect as it may have been added globally, evalling the new code should fix it. – Jordon Biondo Feb 10 '15 at 15:44
2

You might be interested in electric-operator, it's a fairly general minor mode for adding spacing around operators (mostly for programming modes, but it works in text modes as well).

It doesn't look at what you typed after the operator though, so it can't currently handle the "add a space only if I didn't already type one" part of your question.

dshepherd
  • 1,281
  • 6
  • 18