[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Two Emacs challenges

From: Joe Fineman
Subject: Re: Two Emacs challenges
Date: Sat, 17 Jun 2006 00:26:33 GMT
User-agent: Gnus/5.1006 (Gnus v5.10.6) Emacs/21.3 (windows-nt)

Leon <> writes:

> How to bind keys such as (, [, {, " etc to perform such function
> When type once, it will insert a pair while twice insert itself for
> example: `[' will insert `[]' and leave the cursor in the middle
> while `[[' will insert `['?

There are, IIRC, special keyings in the programming-language modes for
doing the first part of this (I can't find them in the manual at the
moment).  For text mode, I have done the following:

(defun close-paren ()
  (insert "()")
  (forward-char -1))

(defun close-bracket ()
  (insert "[]")
  (forward-char -1))

(defun close-brace ()
  (insert "{}")
  (forward-char -1))

(defun close-quote ()
  (insert "\"\"")
  (forward-char -1))

(global-set-key "(" 'close-paren)
(global-set-key "[" 'close-bracket)
(global-set-key "{" 'close-brace)
(global-set-key "\"" 'close-quote)

To get past the right fence in the most usual contexts, I have done
the following:

(defun jump-fence (n)
  "If this char is doubled before a right fence, jump over & insert it once."
  (interactive "p")
  (if (> n 1)
      (self-insert-command n)
    (let ((origin (point)))
      (self-insert-command 1)
      (if (= (point) (1+ origin))
            (if (looking-at "[])\"]\\|=[0-9][A-Z]\\|\\(\^[.\\)+")
                (if (= (char-after (- (point) 2)) (preceding-char))
                    (progn (delete-char -2)
                           (goto-char (- (match-end 0) 2))
                           (self-insert-command 1))))))

(global-set-key "." 'jump-fence)
(global-set-key "," 'jump-fence)
(global-set-key ";" 'jump-fence)
(global-set-key ":" 'jump-fence)
(global-set-key "?" 'jump-fence)
(global-set-key "!" 'jump-fence)
(global-set-key " " 'jump-fence)

I had not thought of the second part of your request.  If I want only
the left fence, I just delete the right one.

N.B.  I am not a programmer, so I am sure all this is dreadfully
---  Joe Fineman

||:  The tragedy is not what we suffer, but what we miss.  :||

reply via email to

[Prev in Thread] Current Thread [Next in Thread]