📄 emacs.sgml
字号:
<!DOCTYPE CHAPTER PUBLIC "-//Stork//DTD chapter//EN"><!-- ``The contents of this file are subject to the Erlang Public License, Version 1.1, (the "License"); you may not use this file except in compliance with the License. You should have received a copy of the Erlang Public License along with this software. If not, it can be retrieved via the world wide web at http://www.erlang.org/. Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License. The Initial Developer of the Original Code is Ericsson Utvecklings AB. Portions created by Ericsson are Copyright 1999, Ericsson Utvecklings AB. All Rights Reserved.'' $Id$--><CHAPTER><HEADER><TITLE> The Erlang editing mode for Emacs </TITLE><PREPARED>Anders Lindgren <RESPONSIBLE> <DOCNO> <APPROVED> <CHECKED> <DATE>1998-04-20 <REV>C <FILE>emacs-user.sgml</HEADER><SECTION><TITLE> Introduction </TITLE><p>If you want to get started immediately, the chapters"<SEEALSO MARKER="#unix_dotemacs">An Example for UNIX</SEEALSO>"and"<SEEALSO MARKER="#win_dotemacs">An Example for Windows</SEEALSO>" gives you examples of the configurations you need to make to use theErlang Editing mode for Emacs.</P><P>Emacs has been the text editor of choice for programmers in the UNIXcommunity for many years. Thanks to a continuing development process,Emacs is the most powerful editor available. Today, Emacs runs undermost operating systems including MS-Windows, OS/2, Macintosh, andseveral dialects of UNIX.</P><P>Emacs has editing support for all major programming languages andquite a lot of minor and unknown languages are supported as well.</P><P>Emacs is designed to be extendible. In the unlikely event that youwould miss a feature in Emacs you can add it yourself, or you mightfind it in the large number of add-on packages that people all overthe world have written.</P><P>This book is the documentation to the Emacs package <C> erlang.el</C>.It provides support for the programming language Erlang. The packageprovides an editing mode with lots of bells and whistles, compilationsupport, and it makes it possible for the user to start Erlang shellsthat run inside Emacs.</P><P>Emacs is written by the Free Software Foundation and is part of theGNU project. Emacs, including the source code and documentation, isreleased under the GNU General Public License.</P><SECTION><TITLE>Overview of this Book</TITLE><P>This book can be divided into the following sections:<LIST><ITEM><EM> Introduction. </EM> This part introduces Emacs, the Erlangediting mode, and this book. In fact, this is the section youcurrently are reading.<ITEM><EM> The editing mode. </EM> Here the editing mode is described.The editing mode contains a whole series of features includingindentation, syntax highlighting, electric commands, module nameverification, comment support including paragraph filling, skeletons,tags support, and much more.<ITEM><EM> Erlang shells. </EM> How to start and use an Erlang shellthat runs inside Emacs is described in this section.<ITEM><EM> Compilation support. </EM> This package is capable ofstarting compilations of Erlang module. Should compilation errorsoccur Emacs is capable of placing the cursor on the erroneous lines.<ITEM><EM> Customization. </EM> The Erlang editing mode, like mostEmacs packages, supports extensive customization. In this chapter wedemonstrate how you can bind your favorite functions to the hotkeyson the keyboard. It also introduces the concept of "hooks", a generalmethod for the user to add code that will be executed when a specificsituation occur, for example when an Erlang file is loaded into Emacs.</LIST><P>The terminology used in this book is the terminology used in thedocumentation to Emacs. The chapter "<SEEALSOMARKER="#notation">Notation</SEEALSO>" contains a list of commonlyused words and their meaning in the Emacs world.</P><P>The intended readers of this book are Emacs users. The book containssome examples on how to customize this package using the Emacsextension language Emacs Lisp. You can safely skip those sections.</P></SECTION></SECTION><SECTION><TITLE>Emacs</TITLE><P>The first component needed to get this package up and running is, ofcourse, an Emacs editor. You can use either the standard Emacsdistribution from FSF or XEmacs, an alternative distribution. Bothbrands have their advantages and disadvantages.</P><P>Regardless of the brand, it is recommended to use a modern version.If an old version is used it is possible that some of the featuresprovided by the editing mode cannot be used.</P><P>The chapter "<SEEALSO MARKER="#distributions">EmacsDistributions</SEEALSO>" below contains a short summary on thedifferences between the Emacs brands, as well as instructions where toget the distributions and how to install them.</P></SECTION><SECTION><TITLE>Installing the Erlang Support Packages</TITLE><P>Once Emacs has been installed, it must be informed about the presenceof the Erlang support packages.</P><P>If you do not know if the packages have been installed open, an Erlangsource file. The mode line should contain the word "Erlang". You cancheck the version of the installed package by selecting the "version"entry in the Erlang menu in Emacs. Should no Erlang menu be present,or if the menu does not contain a "Version" item, you are using an oldversion.</P><P>The packages can either be installed for all users by the systemadministrator, or each individual user can install it in their ownEmacs setup. The chapter "<SEEALSOMARKER="#installation">Installation of the Erlang Editing Mode</SEEALSO>" contains a descriptionon how to install the packages.</P></SECTION><SECTION><TITLE> The Editing Mode </TITLE><P>The Erlang editing for Emacs provides a number of features describedin this and the following chapters. The editing mode can work witheither Erlang source mode or Mnesia database rules. The Erlangediting mode for Emacs is in Emacs terminology a <EM> Major mode </EM>.</P><P>When Erlang mode is correctly installed, it is automatically activatedwhen a file ending in <C>.erl</C> or <C>.hrl</C> is opened in Emacs.It is possible to activate Erlang mode for other buffers as well.</P><P>The editing mode provides a menu containing a selection of commandsstructured into logical subgroups. The menu is designed to help newusers get an overview of the features provided by the Erlang packageswhile still giving full power to more advanced users.</P><P>Erlang mode has got a local key map that contains keyboard bindingsfor a number of commands. In the chapter "<SEEALSO MARKER="#key_bindings">Custom Key Bindings</SEEALSO>" below,we will demonstrate how the users can bind their favorite commands tothe local Erlang key map.</P><P>It is possible for the users to perform advanced customizations byadding their own functions to the "hook" variables provided by thispackage. This will be described in the "<SEEALSOMARKER="#customization">Customization</SEEALSO>" chapter below.</P><SECTION><TITLE>The Mode</TITLE><LIST><ITEM><C>M-x erlang-mode RET</C><BR><P>This command activates the Erlang major mode for the current buffer.When this mode is active the mode line contain the word "Erlang".</P></LIST></SECTION><SECTION><TITLE>The Version</TITLE><LIST><ITEM><C>M-x erlang-version RET</C><BR><P>This command displays the version number of the Erlang editing mode.Remember to always supply the version number when asking questionsabout Erlang mode.</P><P>Should this command not be present in your setup (after Erlang modehas been activated) you probably have a very old version of the Erlangediting mode.</P></LIST></SECTION><SECTION><TITLE>Module Name Check</TITLE><P>When a file is saved the name in the <C>-module().</C> line is checkedagainst the file name. Should they mismatch Emacs can change themodule specifier so that it matches the file name. By default, the useris asked before the change is performed.</P><LIST><ITEM> <EM> Variable: </EM> <C>erlang-check-module-name</C> (default <C>ask</C>)<BR><P>This variable controls the behavior of the module name check system.When it is <C>t</C> Emacs changes the module specifier without askingthe user, when it is bound to the atom <C>ask</C> the user is asked.Should it be <C>nil</C> the module name check mechanism isdeactivated.</P></LIST></SECTION><SECTION><TITLE>Variables</TITLE><P>There are several variables that control the behavior of theErlang Editing mode.</P><LIST> <ITEM><EM> Variable: </EM> <C>erlang-mode-hook</C><BR><P>Functions to run when the Erlang mode is activated. See chapter"<SEEALSO MARKER="#customization">Customization</SEEALSO>" below forexamples.</P> <ITEM><EM> Variable: </EM> <C>erlang-new-file-hook</C><BR><P>Functions to run when a new file is created. See chapter "<SEEALSOMARKER="#customization">Customization</SEEALSO>" below for examples.</P> <ITEM><EM> Variable: </EM> <C>erlang-mode-load-hook</C><BR><P>Functions to run when the <C>erlang</C> package is loaded into Emacs.See chapter "<SEEALSO MARKER="#customization">Customization</SEEALSO>"below for examples.</P></LIST></SECTION></SECTION><!-- Chapter --><SECTION><TITLE>Indentation</TITLE><P>The "Oxford Advanced Learners Dictionary of Current English" says thefollowing about the word "indent":</P><QUOTE><P> "start (a line of print or writing) farther from the margin than the others".</P></QUOTE><P>Possibly the most important feature of an editor designed forprogrammers is the ability to indent a line of code in accordancewith the structure of the programming language.</P><P>The Erlang mode does, of course, provide this feature. The layoutused is based on the common use of the language.</P><P>It is strongly recommend to use this feature and avoid to indent linesin a nonstandard way. Some motivations are:</P><LIST> <ITEM> Code using the same layout is easy to read and maintain. <ITEM> The indentation features can be used to reindent large sections of afile. If some lines use nonstandard indentation they will bereindented. <ITEM> Since several features of Erlang mode is based on thestandard layout they might not work correctly if a nonstandard layoutis used. For example, the movement commands (described in chapter"<SEEALSO MARKER="#func_cmds">Function and clause commands</SEEALSO>"below) will not work unless the function headers start in the firstcolumn.</LIST><SECTION><TITLE>The Layout</TITLE><P>The basic layout is that the clause headers start in the first column,and the bodies of clauses and complex expressions (e.g. "case" and"if") are indented more that the surrounding code. For example:</P><CODE>remove_bugs([]) -> [];remove_bugs([X | Xs]) case X of bug -> test(Xs); _ -> [X | test(Xs)] end.</CODE><LIST><ITEM> <EM> Variable: </EM> <C>erlang-indent-level</C><BR><P>The depth of the indentation is controlled by the variable"erlang-indent-level", see section "<SEEALSOMARKER="#customization">Customization</SEEALSO>" below.</P></LIST></SECTION><SECTION><TITLE>Indentation of comments</TITLE><P>Lines containing comment are indented differently depending on thenumber of %-characters used:</P><LIST><ITEM> Lines with one %-character is indented to the right of thecode. The column is specified by the variable <C>comment-column</C>,by default column 48 is used.<ITEM> Lines with two %-characters will be indented to the same depthas code would have been in the same situation.<ITEM> Lines with three of more %-characters are indented to the leftmargin.</LIST><P><EM> Example: </EM></P><CODE>%%%%%% Function: remove_bugs%%%remove_bugs([]) -> [];remove_bugs([X | Xs]) case X of bug -> % Oh no, a bug! % Remove it. test(Xs); %% This element is not a bug, let's keep it. _ -> [X | test(Xs)] end.</CODE></SECTION><SECTION><TITLE>Indentation commands</TITLE><P>The following command are directly available for indentation.</P><LIST><ITEM><C>TAB</C> (<C>erlang-indent-command</C>)<BR><P>Indent the current line of code.</P><ITEM><C>M-C-\</C> (<C>indent-region</C>)<BR><P>Indent all lines in the region.</P><ITEM><C>M-l</C> (<C>indent-for-comment</C>)<BR><P>Insert a comment character to the right of the code on the line (ifany). The comment character is placed in the column specified by thevariable "comment-column", by default column 48 is used.</P><ITEM><C>C-c C-q</C> (<C>erlang-indent-function</C>)<BR><P>Indent the current Erlang function.</P><ITEM><C> M-x erlang-indent-clause RET</C><BR><P>Indent the current Erlang clause.</P><ITEM><C>M-x erlang-indent-current-buffer RET</C><BR><P>Indent the entire buffer.</P></LIST></SECTION><SECTION><MARKER ID="customization"><TITLE>Customization</TITLE><P>The most common customization of the indentation system is to bind thereturn key to <C>newline-and-indent</C>. Please see the chapter"<SEEALSO MARKER="#key_bindings">Custom Key Bindings</SEEALSO>"below for an example.</P><P>There are several Emacs variables that control the indentation system.</P>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -