unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Joe Fineman <joe_f@verizon.net>
Subject: Re: Two Emacs challenges
Date: Sat, 17 Jun 2006 00:26:33 GMT	[thread overview]
Message-ID: <uodwsfwzm.fsf@verizon.net> (raw)
In-Reply-To: mailman.2952.1150488218.9609.help-gnu-emacs@gnu.org

Leon <sdl.web@gmail.com> 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 ()
  (interactive)
  (insert "()")
  (forward-char -1))

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

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

(defun close-quote ()
  (interactive)
  (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))
	  (progn
	    (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
naive.
-- 
---  Joe Fineman    joe_f@verizon.net

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

       reply	other threads:[~2006-06-17  0:26 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <mailman.2952.1150488218.9609.help-gnu-emacs@gnu.org>
2006-06-17  0:26 ` Joe Fineman [this message]
2006-06-17 10:07   ` Two Emacs challenges Leon
2006-06-16 20:03 Leon
2006-06-16 20:15 ` Lennart Borgman
2006-06-16 20:21   ` Lennart Borgman
2006-06-16 20:21   ` Leon
2006-06-17  3:42 ` William Xu
2006-06-17  9:42   ` Leon

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=uodwsfwzm.fsf@verizon.net \
    --to=joe_f@verizon.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).