Question

I am an emacs newbie. I am trying to expand the character "." to ". " (period with two spaces in order to be more effective in ending a sentence in emacs) with abbrevs. In other words, when I type a "." followed by a space, emacs put a ". ".

I have put the next code in my abbrevs file, but it doesn't work.

(text-mode-abbrev-table)                                    
"."            0    ".    " 

Anybody can help me?

Était-ce utile?

La solution

I'm not sure why you would want this, but here it is:

Put this in ~/.emacs:

(defun electric-dot ()
  (interactive)
  (if (and (looking-back "\\w") (not (looking-back "[0-9]")))
      (progn
        (self-insert-command 1)
        (insert "  "))
    (self-insert-command 1)))

(defvar electric-dot-on-p nil)

(defun toggle-electric-dot ()
  (interactive)
  (global-set-key
   "."
   (if (setq electric-dot-on-p
             (not electric-dot-on-p))
       'electric-dot
     'self-insert-command)))

Afterwards, use M-xtoggle-electric-dot to make each . insert . , if it's after a word. You can call it again to restore the default behavior.

As a side-note, there's a ton of much better ways to improve your text input speed e.g. auto-complete-mode. You can install it with package-install.

UPD electric-dot inserts just a dot after digits.

UPD Here's electric-space instead:

This one will insert an extra space if it's looking back at a word followed by a dot.

(defun electric-space ()
  (interactive)
  (if (looking-back "\\w\\.")
      (insert " "))
  (self-insert-command 1))

(defvar electric-space-on-p nil)

(defun toggle-electric-space ()
  (interactive)
  (global-set-key
   " "
   (if (setq electric-space-on-p
             (not electric-space-on-p))
       'electric-space
     'self-insert-command)))

Autres conseils

Install the key-chords package. Then define one like this.

(key-chord-define-global ". " ".  ")

Key chords accept a larger variety of keys then abbrev, so ". " might work. But the two keys have to be pressed in quick succession (which shouldn't be a problem here).

The reason your abbrev does not work is because by default, abbrev names can only be composed of word-element characters (basically, alphanumeric chars), and . is not such a character. You can circumvent this problem by changing the :regexp property of your text-mode-abbrev-table but it's really not worth the trouble. Instead you can do

(defun my-electric-space-post-insert ()
  (and (eq (char-before) ?\s)
       (eq (char-before (1- (point))) ?.)
       (insert " ")))
(add-hook 'post-self-insert-hook #'my-electric-space-post-insert)
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top