all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* left brackets insert both
@ 2005-03-25 15:37 Shug Boabby
  2005-03-25 15:54 ` David Hansen
                   ` (2 more replies)
  0 siblings, 3 replies; 12+ messages in thread
From: Shug Boabby @ 2005-03-25 15:37 UTC (permalink / raw)


hi there,

a friend of mine was demonstrating a (pay for) text editor he has for
Mac OS X, which had a very nice feature, i am sure it is implementable
in Emacs, but my lisp skills are not good enough to code it. could
somebody please help me implement it?

when the start of a brace pair was typed, one of [<[{(], the editor
would insert the corresponding closing brace and place the cursor
in-between. this worked for all text editing modes, but could be turned
off when needed.

anyone have a clue?

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: left brackets insert both
  2005-03-25 15:37 left brackets insert both Shug Boabby
@ 2005-03-25 15:54 ` David Hansen
  2005-03-25 16:45   ` Shug Boabby
  2005-03-25 15:54 ` rgb
  2005-03-25 16:39 ` Peter Lee
  2 siblings, 1 reply; 12+ messages in thread
From: David Hansen @ 2005-03-25 15:54 UTC (permalink / raw)


On 25 Mar 2005 07:37:20 -0800 Shug Boabby wrote:

> when the start of a brace pair was typed, one of [<[{(], the editor
> would insert the corresponding closing brace and place the cursor
> in-between. this worked for all text editing modes, but could be turned
> off when needed.

;;;; skeleton mode
(global-set-key "\"" 'skeleton-pair-insert-maybe)
;;(global-set-key "'" 'skeleton-pair-insert-maybe)
(global-set-key "`" 'skeleton-pair-insert-maybe)
(global-set-key "[" 'skeleton-pair-insert-maybe)
(global-set-key "(" 'skeleton-pair-insert-maybe)
(global-set-key "{" 'skeleton-pair-insert-maybe)
(setq skeleton-pair t)

See (info "(autotype)Top")

David

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: left brackets insert both
  2005-03-25 15:37 left brackets insert both Shug Boabby
  2005-03-25 15:54 ` David Hansen
@ 2005-03-25 15:54 ` rgb
  2005-03-25 16:39 ` Peter Lee
  2 siblings, 0 replies; 12+ messages in thread
From: rgb @ 2005-03-25 15:54 UTC (permalink / raw)



Shug Boabby wrote:
> hi there,
>
> a friend of mine was demonstrating a (pay for) text editor he has for
> Mac OS X, which had a very nice feature, i am sure it is
implementable
> in Emacs, but my lisp skills are not good enough to code it. could
> somebody please help me implement it?
>
> when the start of a brace pair was typed, one of [<[{(], the editor
> would insert the corresponding closing brace and place the cursor
> in-between. this worked for all text editing modes, but could be
turned
> off when needed.
> 
> anyone have a clue?

skeleton-pair-insert-maybe

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: left brackets insert both
  2005-03-25 15:37 left brackets insert both Shug Boabby
  2005-03-25 15:54 ` David Hansen
  2005-03-25 15:54 ` rgb
@ 2005-03-25 16:39 ` Peter Lee
  2 siblings, 0 replies; 12+ messages in thread
From: Peter Lee @ 2005-03-25 16:39 UTC (permalink / raw)


>>>> Shug Boabby writes:

    > when the start of a brace pair was typed, one of [<[{(], the editor
    > would insert the corresponding closing brace and place the cursor
    > in-between. this worked for all text editing modes, but could be turned
    > off when needed.

There is paren-pair-mode with cvs emacs... not sure what version it
was introduced.  It's buffer local, so you'll have to enable it for
any modes you want it.  The list of paired characters is controled by
paren-pair-list.

,----[ C-h f paren-pair-mode RET ]
| paren-pair-mode is an interactive compiled Lisp function in `paren-pair'.
| (paren-pair-mode arg)
| 
| Toggle paren pair mode.
| With a prefix arg, enable paren pair mode iff arg is nonzero.
| 
| [back]
`----

,----[ C-h v paren-pair-list RET ]
| paren-pair-list's value is shown below.
| Automatically becomes buffer-local when set in any fashion.
| 
| Not documented as a variable.
| 
| Defined in `paren-pair'.
| 
| Value:
| (("["
|   #("]" 0 1
|     (paren-paired t)))
|  ("("
|   #(")" 0 1
|     (paren-paired t)))
|  (#("\"" 0 1
|     (paren-paired t))
|   "\""))
| 
| Local in buffer *scratch*; global value is nil
| 
| [back]
`----

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: left brackets insert both
  2005-03-25 15:54 ` David Hansen
@ 2005-03-25 16:45   ` Shug Boabby
  2005-03-25 17:49     ` rgb
  2005-03-26 12:48     ` David Hansen
  0 siblings, 2 replies; 12+ messages in thread
From: Shug Boabby @ 2005-03-25 16:45 UTC (permalink / raw)


excellent! i had found the exact setup once rgb had pointed me to the
function call, but cheers david!

however, it is sometimes annoying that a single type of a bracket does
this... any chance i could set it only to work on double taps? i get
nothing when i try something like:
  (global-set-key [?\< ?\<] 'skeleton-pair-insert-maybe)
i guess its trying to find the skeleton pair to << instead of <

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: left brackets insert both
  2005-03-25 16:45   ` Shug Boabby
@ 2005-03-25 17:49     ` rgb
  2005-03-25 18:23       ` Shug Boabby
  2005-03-26 12:48     ` David Hansen
  1 sibling, 1 reply; 12+ messages in thread
From: rgb @ 2005-03-25 17:49 UTC (permalink / raw)



Shug Boabby wrote:
> excellent! i had found the exact setup once rgb had pointed me to the
> function call, but cheers david!
>
> however, it is sometimes annoying that a single type of a bracket
does
> this... any chance i could set it only to work on double taps? i get
> nothing when i try something like:
>   (global-set-key [?\< ?\<] 'skeleton-pair-insert-maybe)
> i guess its trying to find the skeleton pair to << instead of <

You can probably adapt this to your needs.

(defmacro my-insert-if-double (new-txt)
   "If last key same as this key, replace both with new-txt."
   `(lambda (cnt)
      (interactive "p")
      (if (equal (preceding-char) last-command-char)
          (progn
              (backward-delete-char 1)
              (insert ,new-txt))
          (self-insert-command cnt))))
 (add-hook 'html-mode-hook
   (lambda ()
     (local-set-key [(<)]  (my-insert-if-double "&lt;"))
     (local-set-key [(>)]  (my-insert-if-double "&gt;"))
     (local-set-key [(&)]  (my-insert-if-double "&amp;"))
     (local-set-key [(\")] (my-insert-if-double "&quot;"))))

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: left brackets insert both
  2005-03-25 17:49     ` rgb
@ 2005-03-25 18:23       ` Shug Boabby
  2005-03-25 19:53         ` rgb
  0 siblings, 1 reply; 12+ messages in thread
From: Shug Boabby @ 2005-03-25 18:23 UTC (permalink / raw)


excellent rgb! i have kept your original function as it is nice anyway,
but modified it to this:

(defmacro my-bothbraces-if-double (new-txt)
  "If the last key is the same as this key, insert new-txt and go back
a char"
  `(lambda (cnt)
     (interactive "p")
     (if (equal (preceding-char) last-command-char)
         (progn
           (insert ,new-txt)
           (backward-char))
       (self-insert-command cnt))))
(global-set-key "\"" (my-bothbraces-if-double "\""))
(global-set-key "\'" (my-bothbraces-if-double "\'"))
(global-set-key "\{" (my-bothbraces-if-double "\}"))
(global-set-key "\[" (my-bothbraces-if-double "\]"))
(global-set-key "\(" (my-bothbraces-if-double "\)"))
(global-set-key "\<" (my-bothbraces-if-double "\>"))

i hope it helps others!

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: left brackets insert both
  2005-03-25 18:23       ` Shug Boabby
@ 2005-03-25 19:53         ` rgb
  2005-03-25 20:05           ` rgb
  0 siblings, 1 reply; 12+ messages in thread
From: rgb @ 2005-03-25 19:53 UTC (permalink / raw)


> (defmacro my-bothbraces-if-double (new-txt)
>   "If the last key is the same as this key, insert new-txt and go
back
> a char"
>   `(lambda (cnt)
>      (interactive "p")
>      (if (equal (preceding-char) last-command-char)
>          (progn
>            (insert ,new-txt)
>            (backward-char))
>        (self-insert-command cnt))))

I'm reminded that I've long thought I should make this change:

(if (and (equal (preceding-char) last-command-char)
         (= cnt 1))

I just never got around to it.
Vary rarely does it make any difference.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: left brackets insert both
  2005-03-25 19:53         ` rgb
@ 2005-03-25 20:05           ` rgb
  2005-03-25 20:19             ` Shug Boabby
  0 siblings, 1 reply; 12+ messages in thread
From: rgb @ 2005-03-25 20:05 UTC (permalink / raw)


> I'm reminded that I've long thought I should make this change:
>
> (if (and (equal (preceding-char) last-command-char)
>          (= cnt 1))
>
> I just never got around to it.
> Vary rarely does it make any difference.

And now I remember why I didn't make it earlier.  It's
really a bit more compilcated than that.   But not much.

    (defmacro my-insert-if-double (new-txt)
   "If last key same as this key, replace both with new-txt."
   `(lambda (cnt raw)
      (interactive "p\nP")
      (if (and (equal (preceding-char) last-command-char)
               (not raw))
          (progn
              (backward-delete-char 1)
              (insert ,new-txt))
          (self-insert-command cnt))))

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: left brackets insert both
  2005-03-25 20:05           ` rgb
@ 2005-03-25 20:19             ` Shug Boabby
  2005-03-25 23:00               ` rgb
  0 siblings, 1 reply; 12+ messages in thread
From: Shug Boabby @ 2005-03-25 20:19 UTC (permalink / raw)


cool. i am a little confused by this macro... i have only ever written
defun's before. why is this a defmac and not a defun?

a few more questions if you don't mind...

what does the "p\nP" argument to interactive do?

what is "cnt raw"?

why do you call self-insert-command?

sorry for these trivial questions, i have only picked up elisp by
reading .emacs files, editing existing functions and creating very
simple new ones.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: left brackets insert both
  2005-03-25 20:19             ` Shug Boabby
@ 2005-03-25 23:00               ` rgb
  0 siblings, 0 replies; 12+ messages in thread
From: rgb @ 2005-03-25 23:00 UTC (permalink / raw)


Shug Boabby wrote:
> cool. i am a little confused by this macro... i have only ever
written
> defun's before. why is this a defmac and not a defun?

Look at it this way.
A defmacro does nothing by itself.
When you use it, it writes a custom function on the spot based on
the arguments you supplied.  It's a fairly deep subject for the
unfamiliar.
>
> a few more questions if you don't mind...
>
> what does the "p\nP" argument to interactive do?

I wanted both the numeric argument and the argument in raw form.
In raw form nil means that the user did not specify a numeric arg.
In numeric format a nil is converted to 1 and so you can't tell
by just looking at the numeric version if the user specified a
numeric arg or wants to use the default of 1.

>
> what is "cnt raw"?

These are the names by which I refer to the values provided by
the p and P interactive specification.  See C-h f interactive RET
>
> why do you call self-insert-command?

In the case where the user types [ for example and the prior
character is not [ you want the [ character inserted dont you?
That's what handles all keystrokes that don't meet the (if ...
criteria.  It's what all the keys are normally mapped to until
you re-assigned them to your new function.

>
> sorry for these trivial questions, i have only picked up elisp by
> reading .emacs files, editing existing functions and creating very
> simple new ones.

There is a pretty good intro... someplace.  I dont' have a link
handy but if you've got a 22.0.x version of Emacs it's now included
in the help along with the Emacs Lisp Reference manual.  Neither
should be hard to find once you know they exist.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: left brackets insert both
  2005-03-25 16:45   ` Shug Boabby
  2005-03-25 17:49     ` rgb
@ 2005-03-26 12:48     ` David Hansen
  1 sibling, 0 replies; 12+ messages in thread
From: David Hansen @ 2005-03-26 12:48 UTC (permalink / raw)


On 25 Mar 2005 08:45:27 -0800 Shug Boabby wrote:

> excellent! i had found the exact setup once rgb had pointed me to the
> function call, but cheers david!
>
> however, it is sometimes annoying that a single type of a bracket does
> this... any chance i could set it only to work on double taps? 

Well, seems you already found a solution.  What i do when i want
a single paren is C-q (.

> i get nothing when i try something like: (global-set-key [?\<
> ?\<] 'skeleton-pair-insert-maybe)

You can define new skeletons via

(add-to-list 'skeleton-pair-alist '(?< _ ?>))
(global-set-key "<" 'skeleton-pair-insert-maybe)


David

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2005-03-26 12:48 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-03-25 15:37 left brackets insert both Shug Boabby
2005-03-25 15:54 ` David Hansen
2005-03-25 16:45   ` Shug Boabby
2005-03-25 17:49     ` rgb
2005-03-25 18:23       ` Shug Boabby
2005-03-25 19:53         ` rgb
2005-03-25 20:05           ` rgb
2005-03-25 20:19             ` Shug Boabby
2005-03-25 23:00               ` rgb
2005-03-26 12:48     ` David Hansen
2005-03-25 15:54 ` rgb
2005-03-25 16:39 ` Peter Lee

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.