m-x.texi 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. @node M-x, Help, Minibuffer, Top
  2. @chapter Running Commands by Name
  3. The Emacs commands that are used often or that must be quick to type are
  4. bound to keys---short sequences of characters---for convenient use. Other
  5. Emacs commands that are used more rarely are not bound to keys; to run
  6. them, you must refer to them by name.
  7. A command name consists, by convention, of one or more words,
  8. separated by hyphens: for example, @code{auto-fill-mode} or
  9. @code{manual-entry}. The use of English words makes the command name
  10. easier to remember than a key made up of obscure characters, even though
  11. it results in more characters to type. You can run any command by name,
  12. even if it can be run by keys as well.
  13. @kindex M-x
  14. @cindex minibuffer
  15. To run a command by name, start with @kbd{M-x}, then type the
  16. command name, and finish with @key{RET}. @kbd{M-x} uses the minibuffer
  17. to read the command name. @key{RET} exits the minibuffer and runs the
  18. command.
  19. Emacs uses the minibuffer for reading input for many different purposes;
  20. on this occasion, the string @samp{M-x} is displayed at the beginning of
  21. the minibuffer as a @dfn{prompt} to remind you that your input should be
  22. the name of a command to be run. @xref{Minibuffer}, for full information
  23. on the features of the minibuffer.
  24. You can use completion to enter a command name. For example, to
  25. invoke the command @code{forward-char}, type:
  26. @example
  27. M-x forward-char @key{RET}
  28. @end example
  29. or
  30. @example
  31. M-x fo @key{TAB} c @key{RET}
  32. @end example
  33. @noindent
  34. After you type in @code{M-x fo TAB} emacs will give you a possible list of
  35. completions from which you can choose. Note that @code{forward-char} is the
  36. same command that you invoke with the key @kbd{C-f}. You can call any
  37. command (interactively callable function) defined in Emacs by its name
  38. using @kbd{M-x} regardless of whether or not any keys are bound to it.
  39. If you type @kbd{C-g} while Emacs reads the command name, you cancel
  40. the @kbd{M-x} command and get out of the minibuffer, ending up at top level.
  41. To pass a numeric argument to a command you are invoking with
  42. @kbd{M-x}, specify the numeric argument before the @kbd{M-x}. @kbd{M-x}
  43. passes the argument along to the function that it calls. The argument
  44. value appears in the prompt while the command name is being read.
  45. @findex interactive
  46. You can use the command @code{M-x interactive} to specify a way of
  47. parsing arguments for interactive use of a function. For example, write:
  48. @example
  49. (defun foo (arg) "Doc string" (interactive "p") ...use arg...)
  50. @end example
  51. to make @code{arg} be the prefix argument when @code{foo} is called as a
  52. command. The call to @code{interactive} is actually a declaration
  53. rather than a function; it tells @code{call-interactively} how to read
  54. arguments to pass to the function. When actually called, @code{interactive}
  55. returns @code{nil}.
  56. The argument of @var{interactive} is usually a string containing a code
  57. letter followed by a prompt. Some code letters do not use I/O to get
  58. the argument and do not need prompts. To prompt for multiple arguments,
  59. you must provide a code letter, its prompt, a newline, and another code
  60. letter, and so forth. If the argument is not a string, it is evaluated
  61. to get a list of arguments to pass to the function. If you do not provide an
  62. argument to @code{interactive}, no arguments are passed when calling
  63. interactively.
  64. Available code letters are:
  65. @table @code
  66. @item a
  67. Function name: symbol with a function definition
  68. @item b
  69. Name of existing buffer
  70. @item B
  71. Name of buffer, possibly nonexistent
  72. @item c
  73. Character
  74. @item C
  75. Command name: symbol with interactive function definition
  76. @item d
  77. Value of point as number (does not do I/O)
  78. @item D
  79. Directory name
  80. @item e
  81. Last mouse event
  82. @item f
  83. Existing file name
  84. @item F
  85. Possibly nonexistent file name
  86. @item k
  87. Key sequence (string)
  88. @item m
  89. Value of mark as number (does not do I/O)
  90. @item n
  91. Number read using minibuffer
  92. @item N
  93. Prefix arg converted to number, or if none, do like code @code{n}
  94. @item p
  95. Prefix arg converted to number (does not do I/O)
  96. @item P
  97. Prefix arg in raw form (does not do I/O)
  98. @item r
  99. Region: point and mark as two numeric arguments, smallest first (does
  100. not do I/O)
  101. @item s
  102. Any string
  103. @item S
  104. Any symbol
  105. @item v
  106. Variable name: symbol that is @code{user-variable-p}
  107. @item x
  108. Lisp expression read but not evaluated
  109. @item X
  110. Lisp expression read and evaluated
  111. @end table
  112. In addition, if the string begins with @samp{*}, an error is
  113. signaled if the buffer is read-only. This happens before reading any
  114. arguments. If the string begins with @samp{@@}, the window the mouse is
  115. over is selected before anything else is done. You may use both
  116. @samp{@@} and @samp{*}; they are processed in the order that they appear.
  117. Normally, when describing a command that is run by name, we omit the
  118. @key{RET} that is needed to terminate the name. Thus we may refer to
  119. @kbd{M-x auto-fill-mode} rather than @kbd{M-x auto-fill-mode} @key{RET}.
  120. We mention the @key{RET} only when it is necessary to emphasize its
  121. presence, for example, when describing a sequence of input that contains
  122. a command name and arguments that follow it.
  123. @findex execute-extended-command
  124. @kbd{M-x} is defined to run the command @code{execute-extended-command},
  125. which is responsible for reading the name of another command and invoking
  126. it.