comparison lispref/keymaps.texi @ 15766:0146c9f9f720

Explain new keymap-parent functions. Reorder the info about extra stuff in a key binding and document the menu-alias property.
author Richard M. Stallman <rms@gnu.org>
date Tue, 23 Jul 1996 15:48:58 +0000
parents 7097c189c25d
children 66d807bdc5b4
comparison
equal deleted inserted replaced
15765:8cc15b664c4c 15766:0146c9f9f720
274 @node Inheritance and Keymaps 274 @node Inheritance and Keymaps
275 @section Inheritance and Keymaps 275 @section Inheritance and Keymaps
276 @cindex keymap inheritance 276 @cindex keymap inheritance
277 @cindex inheriting a keymap's bindings 277 @cindex inheriting a keymap's bindings
278 278
279 A keymap can inherit the bindings of another keymap. Do do this, make 279 A keymap can inherit the bindings of another keymap, which we call the
280 a keymap whose ``tail'' is another existing keymap to inherit from. 280 @dfn{parent keymap}. Such a keymap looks like this:
281 Such a keymap looks like this: 281
282 282 @example
283 @example 283 (keymap @var{bindings}@dots{} . @var{parent-keymap})
284 (keymap @var{bindings}@dots{} . @var{other-keymap})
285 @end example 284 @end example
286 285
287 @noindent 286 @noindent
288 The effect is that this keymap inherits all the bindings of 287 The effect is that this keymap inherits all the bindings of
289 @var{other-keymap}, whatever they may be at the time a key is looked up, 288 @var{parent-keymap}, whatever they may be at the time a key is looked up,
290 but can add to them or override them with @var{bindings}. 289 but can add to them or override them with @var{bindings}.
291 290
292 If you change the bindings in @var{other-keymap} using @code{define-key} 291 If you change the bindings in @var{parent-keymap} using @code{define-key}
293 or other key-binding functions, these changes are visible in the 292 or other key-binding functions, these changes are visible in the
294 inheriting keymap unless shadowed by @var{bindings}. The converse is 293 inheriting keymap unless shadowed by @var{bindings}. The converse is
295 not true: if you use @code{define-key} to change the inheriting keymap, 294 not true: if you use @code{define-key} to change the inheriting keymap,
296 that affects @var{bindings}, but has no effect on @var{other-keymap}. 295 that affects @var{bindings}, but has no effect on @var{parent-keymap}.
296
297 The proper way to construct a keymap with a parent is to use
298 @code{set-keymap-parent}; if you have code that directly constructs a
299 keymap with a parent, please convert the program to use
300 @code{set-keymap-parent} instead.
301
302 @defun keymap-parent keymap
303 This returns the parent keymap of @var{keymap}. If @var{keymap}
304 has no parent, @code{keymap-parent} returns @code{nil}.
305 @end defun
306
307 @defun set-keymap-parent keymap parent
308 This sets the parent keymap of @var{keymap} to @var{parent}, and returns
309 @var{parent}. If @var{parent} is @code{nil}, this function gives
310 @var{keymap} no parent at all.
311
312 If @var{keymap} has submaps (bindings for prefix keys), they too receive
313 new parent keymaps that reflect what @var{parent} specifies for those
314 prefix keys.
315 @end defun
297 316
298 Here is an example showing how to make a keymap that inherits 317 Here is an example showing how to make a keymap that inherits
299 from @code{text-mode-map}: 318 from @code{text-mode-map}:
300 319
301 @example 320 @example
302 (setq my-mode-map (cons 'keymap text-mode-map)) 321 (let ((map (make-sparse-keymap)))
322 (set-keymap-parent map text-mode-map)
323 map)
303 @end example 324 @end example
304 325
305 @node Prefix Keys 326 @node Prefix Keys
306 @section Prefix Keys 327 @section Prefix Keys
307 @cindex prefix key 328 @cindex prefix key
1436 @end example 1457 @end example
1437 1458
1438 The item string for a binding should be short---one or two words. It 1459 The item string for a binding should be short---one or two words. It
1439 should describe the action of the command it corresponds to. 1460 should describe the action of the command it corresponds to.
1440 1461
1441 As far as @code{define-key} is concerned, @var{string} is part of the
1442 event's binding. However, @code{lookup-key} returns just
1443 @var{real-binding}, and only @var{real-binding} is used for executing
1444 the key.
1445
1446 You can also supply a second string, called the help string, as follows: 1462 You can also supply a second string, called the help string, as follows:
1447 1463
1448 @example 1464 @example
1449 (@var{string} @var{help-string} . @var{real-binding}) 1465 (@var{string} @var{help-string} . @var{real-binding})
1450 @end example 1466 @end example
1451 1467
1452 Currently Emacs does not actually use @var{help-string}; it knows only 1468 Currently Emacs does not actually use @var{help-string}; it knows only
1453 how to ignore @var{help-string} in order to extract @var{real-binding}. 1469 how to ignore @var{help-string} in order to extract @var{real-binding}.
1454 In the future we may use @var{help-string} as extended documentation for 1470 In the future we may use @var{help-string} as extended documentation for
1455 the menu item, available on request. 1471 the menu item, available on request.
1472
1473 As far as @code{define-key} is concerned, @var{string} and
1474 @var{help-string} are part of the event's binding. However,
1475 @code{lookup-key} returns just @var{real-binding}, and only
1476 @var{real-binding} is used for executing the key.
1456 1477
1457 If @var{real-binding} is @code{nil}, then @var{string} appears in the 1478 If @var{real-binding} is @code{nil}, then @var{string} appears in the
1458 menu but cannot be selected. 1479 menu but cannot be selected.
1459 1480
1460 If @var{real-binding} is a symbol and has a non-@code{nil} 1481 If @var{real-binding} is a symbol and has a non-@code{nil}
1468 The menu bar does not recalculate which items are enabled every time you 1489 The menu bar does not recalculate which items are enabled every time you
1469 look at a menu. This is because the X toolkit requires the whole tree 1490 look at a menu. This is because the X toolkit requires the whole tree
1470 of menus in advance. To force recalculation of the menu bar, call 1491 of menus in advance. To force recalculation of the menu bar, call
1471 @code{force-mode-line-update} (@pxref{Mode Line Format}). 1492 @code{force-mode-line-update} (@pxref{Mode Line Format}).
1472 1493
1494 You've probably noticed that menu items show the equivalent keyboard key
1495 sequence (if any) to invoke the same command. To save time on
1496 recalculation, menu display caches this information in a sublist in the
1497 binding, like this:
1498
1499 @c This line is not too long--rms.
1500 @example
1501 (@var{string} @r{[}@var{help-string}@r{]} (@var{key-binding-data}) . @var{real-binding})
1502 @end example
1503
1504 Don't put these sublists in the menu item yourself; menu display
1505 calculates them automatically. Don't add keyboard equivalents to the
1506 item strings in a mouse menu, since that is redundant.
1507
1473 Sometimes it is useful to make menu items that use the ``same'' command 1508 Sometimes it is useful to make menu items that use the ``same'' command
1474 but with different enable conditions. You can do this by defining alias 1509 but with different enable conditions. You can do this by defining alias
1475 commands. Here's an example that makes two aliases for 1510 commands. Here's an example that makes two aliases for
1476 @code{toggle-read-only} and gives them different enable conditions: 1511 @code{toggle-read-only} and gives them different enable conditions:
1477 1512
1480 (put 'make-read-only 'menu-enable '(not buffer-read-only)) 1515 (put 'make-read-only 'menu-enable '(not buffer-read-only))
1481 (defalias 'make-writable 'toggle-read-only) 1516 (defalias 'make-writable 'toggle-read-only)
1482 (put 'make-writable 'menu-enable 'buffer-read-only) 1517 (put 'make-writable 'menu-enable 'buffer-read-only)
1483 @end example 1518 @end example
1484 1519
1485 You've probably noticed that menu items show the equivalent keyboard key 1520 When using aliases in menus, often it is useful to display the
1486 sequence (if any) to invoke the same command. To save time on 1521 equivalent key bindings for the ``real'' command name, not the aliases
1487 recalculation, menu display caches this information in a sublist in the 1522 (which typically don't have any key bindings except for the menu
1488 binding, like this: 1523 itself). To request this, give the alias symbol a non-@code{nil}
1489 1524 @code{menu-alias} property. Thus,
1490 @c This line is not too long--rms. 1525
1491 @example 1526 @example
1492 (@var{string} @r{[}@var{help-string}@r{]} (@var{key-binding-data}) . @var{real-binding}) 1527 (put 'make-read-only 'menu-alias t)
1493 @end example 1528 (put 'make-writable 'menu-alias t)
1494 1529 @end example
1495 Don't put these sublists in the menu item yourself; menu display 1530
1496 calculates them automatically. Don't add keyboard equivalents to the 1531 @noindent
1497 item strings in a mouse menu, since that is redundant. 1532 causes menu items for @code{make-read-only} and @code{make-writable} to
1498 1533 show the keyboard bindings for @code{toggle-read-only}.
1499 If an alias command has no keyboard equivalent itself, menus show the
1500 keyboard equivalent of its underlying command. In the example above,
1501 menu items defined to run @code{make-read-only} or @code{make-writable}
1502 would show the keyboard equivalents of @code{toggle-read-only}.
1503 1534
1504 @node Mouse Menus 1535 @node Mouse Menus
1505 @subsection Menus and the Mouse 1536 @subsection Menus and the Mouse
1506 1537
1507 The way to make a menu keymap produce a menu is to make it the 1538 The way to make a menu keymap produce a menu is to make it the