nmode-customizing.txt 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. How to customize NMODE
  2. Alan Snyder
  3. 24 September 1982
  4. -------------------------------------------------------------------------------
  5. This memo explains how to customize NMODE by redefining the effect of input
  6. keystrokes. NMODE is customized by executing Lisp forms. These forms may be
  7. executed directly within NMODE (using Lisp-E), or may be stored in an INIT
  8. file, which is read by NMODE when it first starts up. The name of the INIT
  9. file read by NMODE is "NMODE.INIT" in the user's home directory.
  10. There are three concepts that must be understood to customize NMODE: Commands,
  11. Functions, and Modes.
  12. 1) Commands. The effect of given keystroke or sequence of keystrokes in
  13. NMODE is based on a mapping between "commands" and "functions".
  14. A "command" may be either a single "extended character" or a sequence
  15. of characters. An extended character is a 9-bit character with
  16. distinct "Control" and "Meta" bits. Thus "C-M-A" is a single "extended
  17. character", even though on many terminals you have to use two keystrokes
  18. to enter it. Extended characters are specified using the macro X-CHAR,
  19. for example:
  20. (x-char A) the letter "A" (upper case)
  21. (x-char C-F) Control-F
  22. (x-char C-M-Z) Control-Meta-Z
  23. (x-char CR) Carriage-Return
  24. (x-char TAB) Tab
  25. (x-char BACKSPACE) Backspace
  26. (x-char NEWLINE) Newline
  27. (x-char RUBOUT) Rubout
  28. (x-char C-M-RUBOUT) Control-Meta-Rubout
  29. (The macros described in this section are defined in the load module
  30. EXTENDED-CHAR.) It is important to note that on most terminals, some Ascii
  31. control characters are mapped to extended "Control" characters and some aren't.
  32. Those that aren't are: Backspace, CR, Newline, Tab, and Escape. Even if you
  33. type "CNTL-I" on the keyboard, you will get "Tab" and not "Control-I". The
  34. remaining Ascii control characters are mapped to extended "Control" characters,
  35. thus typing "CNTL-A" on the keyboard gives "Control-A".
  36. As mentioned above, a command can be a sequence of characters. There are two
  37. forms: Prefix commands and Extended commands.
  38. Prefix commands: A prefix command consists of two characters, the first of
  39. which is a defined "prefix character". In NMODE, there are 3 predefined prefix
  40. characters: C-X, ESC, and C-]. Prefix commands are specified using the X-CHARS
  41. macro, for example:
  42. (x-chars C-X C-F)
  43. (x-chars ESC A)
  44. (x-chars C-] E)
  45. Extended commands: An extended command consists of the character M-X and a
  46. string. Extended commands are defined using the M-X macro, for example:
  47. (M-X "Lisp Mode")
  48. (M-X "Revert File")
  49. The case of the letters in the string is irrelevant, except to specify how the
  50. command name will be displayed when "completion" is used by the user. By
  51. convention, the first letter of each word in an extended command name is
  52. capitalized.
  53. 2) Functions. NMODE commands are implemented by PSL functions. By convention,
  54. most (but not all) PSL functions that implement NMODE commands have names
  55. ending with "-COMMAND", for example, MOVE-FORWARD-CHARACTER-COMMAND.
  56. An NMODE command function should take no arguments. The function can perform
  57. its task using a large number of existing support functions; see PN:BUFFER.SL
  58. and PN:MOVE-COMMANDS.SL for examples. A command function can determine the
  59. command argument (given by C-U) by inspecting global variables:
  60. nmode-command-argument: the numeric value (default: 1)
  61. nmode-command-argument-given: T if the user specified an argument
  62. nmode-command-number-given: T if the user typed digits in the argument
  63. See the files PN:MOVE-COMMANDS.SL, PN:LISP-COMMANDS.SL, and PN:COMMANDS.SL for
  64. many examples of NMODE command functions.
  65. 3) Modes. The mapping between commands and functions is dependent on the
  66. current "mode". Examples of existing modes are "Text Mode", which is the basic
  67. mode for text editing, "Lisp Mode", which is an extension of "Text Mode" for
  68. editing and executing Lisp code, and "Dired Mode", which is a specialized mode
  69. for the Directory Editor Subsystem.
  70. A mode is defined by a list of Lisp forms which are evaluated to determine the
  71. state of a Dispatch Table. The Dispatch Table is what is actually used to map
  72. from commands to functions. Every time the user selects a new buffer, the
  73. Dispatch Table is cleared and the Lisp forms defining the mode for the new
  74. buffer are evaluated to fill the Dispatch Table. The forms are evaluated in
  75. reverse order, so that the first form is evaluated last. Thus, any command
  76. definitions made by one form supercede those made by forms appearing after it
  77. in the list.
  78. Two functions are commonly invoked by mode-defining forms: NMODE-ESTABLISH-MODE
  79. and NMODE-DEFINE-COMMANDS. NMODE-ESTABLISH-MODE takes one argument, a list of
  80. mode defining forms, and evaluates those forms. Thus, NMODE-ESTABLISH-MODE can
  81. be used to define one mode in terms of (as an extension of or a modification
  82. to) another mode.
  83. NMODE-DEFINE-COMMANDS takes one argument, a list of pairs, where each pair
  84. consists of a COMMAND and a FUNCTION. This form of list is called a "command
  85. list". Command lists are not used directly to map from commands to functions.
  86. Instead, NMODE-DEFINE-COMMANDS reads the command list it is given and for each
  87. COMMAND-FUNCTION pair in the command list (in order), it alters the Dispatch
  88. Table to map the specified COMMAND to the corresponding FUNCTION.
  89. Note that as a convenience, whenever you define an "upper case" command, the
  90. corresponding "lower case" command is also defined to map to the same function.
  91. Thus, if you define C-M-A, you automatically define C-M-a to map to the same
  92. function. If you want the lower case command to map to a different function,
  93. you must define the lower case command "after" defining the upper case command.
  94. The usual technique for modifying one or more existing modes is to modify one
  95. of the command lists given to NMODE-DEFINE-COMMANDS. The file PN:MODE-DEFS.SL
  96. contains the definition of most predefined NMODE command lists, as well as the
  97. definition of most predefined modes. To modify a mode or modes, you must alter
  98. one or more command lists by adding (or perhaps removing) entries. Command
  99. lists are manipulated using two functions:
  100. (add-to-command-list list-name command func)
  101. (remove-from-command-list list-name command)
  102. Here are some examples:
  103. (add-to-command-list
  104. 'text-command-list (x-char BACKSPACE) 'delete-backward-character-command)
  105. (add-to-command-list
  106. 'lisp-command-list (x-char BACKSPACE) 'delete-backward-hacking-tabs-command)
  107. (remove-from-command-list
  108. 'read-only-text-command-list (x-char BACKSPACE))
  109. [The above forms change BACKSPACE from being the same as C-B to being
  110. the same as RUBOUT.]
  111. (add-to-command-list
  112. 'read-only-text-command-list (x-char M-@) 'set-mark-command)
  113. [The above form makes M-@ set the mark.]
  114. (add-to-command-list
  115. 'read-only-terminal-command-list (x-chars ESC Y) 'print-buffer-names-command)
  116. [The above form makes Esc-Y print a list of all buffer names. Esc-Y is
  117. sent by HP264X terminals when the "Display Functions" key is hit.]
  118. Note that these functions change only the command lists, not the Dispatch Table
  119. which is actually used to map from commands to functions. To cause the
  120. Dispatch Table to be updated to reflect any changes in the command lists, you
  121. must invoke the function NMODE-ESTABLISH-CURRENT-MODE.