major.texi 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. @node Major Modes, Indentation, Mule, Top
  2. @chapter Major Modes
  3. @cindex major modes
  4. @kindex TAB
  5. @kindex DEL
  6. @kindex LFD
  7. Emacs has many different @dfn{major modes}, each of which customizes
  8. Emacs for editing text of a particular sort. The major modes are mutually
  9. exclusive; at any time, each buffer has one major mode. The mode line
  10. normally contains the name of the current major mode in parentheses.
  11. @xref{Mode Line}.
  12. The least specialized major mode is called @dfn{Fundamental mode}. This
  13. mode has no mode-specific redefinitions or variable settings. Each
  14. Emacs command behaves in its most general manner, and each option is in its
  15. default state. For editing any specific type of text, such as Lisp code or
  16. English text, you should switch to the appropriate major mode, such as Lisp
  17. mode or Text mode.
  18. Selecting a major mode changes the meanings of a few keys to become
  19. more specifically adapted to the language being edited. @key{TAB},
  20. @key{DEL}, and @key{LFD} are changed frequently. In addition, commands
  21. which handle comments use the mode to determine how to delimit comments.
  22. Many major modes redefine the syntactical properties of characters
  23. appearing in the buffer. @xref{Syntax}.
  24. The major modes fall into three major groups. Lisp mode (which has
  25. several variants), C mode, and Muddle mode are for specific programming
  26. languages. Text mode, Nroff mode, @TeX{} mode, and Outline mode are for
  27. editing English text. The remaining major modes are not intended for use
  28. on users' files; they are used in buffers created by Emacs for specific
  29. purposes and include Dired mode for buffers made by Dired (@pxref{Dired}),
  30. Mail mode for buffers made by @kbd{C-x m} (@pxref{Sending Mail}), and Shell
  31. mode for buffers used for communicating with an inferior shell process
  32. (@pxref{Interactive Shell}).
  33. Most programming language major modes specify that only blank lines
  34. separate paragraphs. This is so that the paragraph commands remain useful.
  35. @xref{Paragraphs}. They also cause Auto Fill mode to use the definition of
  36. @key{TAB} to indent the new lines it creates. This is because most lines
  37. in a program are usually indented. @xref{Indentation}.
  38. @menu
  39. * Choosing Modes:: How major modes are specified or chosen.
  40. @end menu
  41. @node Choosing Modes,,Major Modes,Major Modes
  42. @section Choosing Major Modes
  43. You can select a major mode explicitly for the current buffer, but
  44. most of the time Emacs determines which mode to use based on the file
  45. name or some text in the file.
  46. Use a @kbd{M-x} command to explicitly select a new major mode. Add
  47. @code{-mode} to the name of a major mode to get the name of a command to
  48. select that mode. For example, to enter Lisp mode, execute @kbd{M-x
  49. lisp-mode}.
  50. @vindex auto-mode-alist
  51. When you visit a file, Emacs usually chooses the right major mode
  52. based on the file's name. For example, files whose names end in
  53. @code{.c} are edited in C mode. The variable @code{auto-mode-alist}
  54. controls the correspondence between file names and major mode. Its value
  55. is a list in which each element has the form:
  56. @example
  57. (@var{regexp} . @var{mode-function})
  58. @end example
  59. @noindent
  60. For example, one element normally found in the list has the form
  61. @code{(@t{"\\.c$"} . c-mode)}. It is responsible for selecting C mode
  62. for files whose names end in @file{.c}. (Note that @samp{\\} is needed in
  63. Lisp syntax to include a @samp{\} in the string, which is needed to
  64. suppress the special meaning of @samp{.} in regexps.) The only practical
  65. way to change this variable is with Lisp code.
  66. You can specify which major mode should be used for editing a certain
  67. file by a special sort of text in the first non-blank line of the file.
  68. The mode name should appear in this line both preceded and followed by
  69. @samp{-*-}. Other text may appear on the line as well. For example,
  70. @example
  71. ;-*-Lisp-*-
  72. @end example
  73. @noindent
  74. tells Emacs to use Lisp mode. Note how the semicolon is used to make Lisp
  75. treat this line as a comment. Such an explicit specification overrides any
  76. default mode based on the file name.
  77. Another format of mode specification is:
  78. @example
  79. -*-Mode: @var{modename};-*-
  80. @end example
  81. @noindent
  82. which allows other things besides the major mode name to be specified.
  83. However, Emacs does not look for anything except the mode name.
  84. The major mode can also be specified in a local variables list.
  85. @xref{File Variables}.
  86. @vindex default-major-mode
  87. When you visit a file that does not specify a major mode to use, or
  88. when you create a new buffer with @kbd{C-x b}, Emacs uses the major mode
  89. specified by the variable @code{default-major-mode}. Normally this
  90. value is the symbol @code{fundamental-mode}, which specifies Fundamental
  91. mode. If @code{default-major-mode} is @code{nil}, the major mode is
  92. taken from the previously selected buffer.