6

I would like the period at the end of a sentence to be added automatically when two spaces are typed (similar to how typing works in iPhones).

I use two spaces between sentences, so the transformation I would like to happen would look like this:

b l a h SPC SPC  -->  b l a h . SPC SPC

I plan to use this along with the auto-capitalize mode.

Many thanks!

scaramouche
  • 1,772
  • 10
  • 24

2 Answers2

10

Give this a go:

(defun freaky-space ()
  (interactive)
  (cond ((looking-back "\\(?:^\\|\\.\\)  +")
         (insert " "))
        ((eq this-command
             last-command)
         (backward-delete-char 1)
         (insert ".  "))
        (t
         (insert " "))))

(define-key text-mode-map " " 'freaky-space)
abo-abo
  • 13,943
  • 1
  • 29
  • 43
  • Great. One nice thing about this solution is that it takes into account the context (e.g., only insert a period if there's no period already there). – scaramouche Nov 28 '14 at 05:52
7

Keychords would let you do this. You would also want SPC SPC to be translated to . SPC only when you type them fast enough. That's exactly what keychords would do. After installing key-chord you can define something like this:

(key-chord-define-local (kbd "SPC SPC") (lambda () (insert ". ")))
waymondo
  • 1,384
  • 11
  • 16
Pradhan
  • 2,330
  • 14
  • 28
  • I tried exactly that with key-chord. It makes it so that typing a space between words is very slow. I guess it's waiting to see if you'll type a second space. I turned it off. – incandescentman Jun 26 '15 at 07:32
  • 1
    @incandescentman Yes, you are right. Using a one-key keychord on a common key does have this issue. You could try tweaking `key-chord-one-key-delay` to your liking. – Pradhan Jun 26 '15 at 16:05
  • The fastest I could get it to recognize discrete key presses was `(setq key-chord-one-key-delay 0.11)`, which still creates a long lag when typing a single space. – incandescentman Jul 09 '15 at 16:31