天天看點

emacs為檔案設定本地變量

原文位址:http://www.gnu.org/software/emacs/manual/html_node/emacs/Specifying-File-Variables.html

有空再翻譯一下,一個簡單的應用的例子,編譯一個簡單的opencv程式:

#include <opencv2/opencv.hpp>
#include <stdio.h>
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char **argv)
{
  Mat image;
  image = imread(argv[1],1);
 
  if(argc != 2 || !image.data)
    {
      printf("No image data\n");
      return -1;
    }

  String winName = "Image";
  namedWindow(winName, CV_WINDOW_AUTOSIZE);
  imshow(winName,image);

  waitKey(0);
  return 0;
}

//下面這幾行為emacs設定本地變量compile-command,用emacs打開此檔案,并用M-x compile指令時,編譯指令就會成為外我們設定的
//需要注意的是必須是相應程式設計語言的注釋形式來包含這幾句,如c語言需要/* */來包含每一行,本例是c++是以是//
//否則會出現emacs local-variables error: (error "Local variables entry is missing the suffix")錯誤
//另外需要放到檔案第一行或檔案末,放到檔案末格式如下;放到檔案頭格式用-*-  -*-包含。
//還有就是輸入後,必須重新打開一次檔案或者用revert-buffer指令重新載入,才能使用,這些是在打開檔案時被emacs解析的。
// Local Variables:
// compile-command: "g++ -o di DisplayImage.cpp `pkg-config --libs --cflags opencv`"
// End:
           
48.2.4.1 Specifying File Variables

There are two ways to specify file local variable values: in the first line, or with a local variables list. Here's how to specify them in the first line:

-*- mode: modename; var: value; ... -*-
      

You can specify any number of variable/value pairs in this way, each pair with a colon and semicolon. The special variable/value pair 

mode: 

modename

;

, if present, specifies a major mode. The values are used literally, and not evaluated.

You can use M-x add-file-local-variable-prop-line instead of adding entries by hand. This command prompts for a variable and value, and adds them to the first line in the appropriate way. M-x delete-file-local-variable-prop-line prompts for a variable, and deletes its entry from the line. The command M-x copy-dir-locals-to-file-locals-prop-line copies the current directory-local variables to the first line (see Directory Variables).

Here is an example first line that specifies Lisp mode and sets two variables with numeric values:

;; -*- mode: Lisp; fill-column: 75; comment-column: 50; -*-
      

Aside from 

mode

, other keywords that have special meanings as file variables are 

coding

unibyte

, and 

eval

. These are described below.

In shell scripts, the first line is used to identify the script interpreter, so you cannot put any local variables there. To accommodate this, Emacs looks for local variable specifications in the second line if the first line specifies an interpreter. The same is true for man pages which start with the magic string ‘'\"’ to specify a list of troff preprocessors (not all do, however).

Apart from using a ‘-*-’ line, you can define file local variables using a local variables list near the end of the file. The start of the local variables list should be no more than 3000 characters from the end of the file, and must be on the last page if the file is divided into pages.

If a file has both a local variables list and a ‘-*-’ line, Emacs processes everything in the ‘-*-’ line first, and everything in the local variables list afterward. The exception to this is a major mode specification. Emacs applies this first, wherever it appears, since most major modes kill all local variables as part of their initialization.

A local variables list starts with a line containing the string ‘Local Variables:’, and ends with a line containing the string ‘End:’. In between come the variable names and values, one set per line, like this:

/* Local Variables:  */
     /* mode: c           */
     /* comment-column: 0 */
     /* End:              */
      

In this example, each line starts with the prefix ‘’. Emacs recognizes the prefix and suffix by finding them surrounding the magic string ‘Local Variables:’, on the first line of the list; it then automatically discards them from the other lines of the list. The usual reason for using a prefix and/or suffix is to embed the local variables list in a comment, so it won't confuse other programs that the file is intended for. The example above is for the C programming language, where comments start with ‘’.

Instead of typing in the local variables list directly, you can use the command M-x add-file-local-variable. This prompts for a variable and value, and adds them to the list, adding the ‘Local Variables:’ string and start and end markers as necessary. The command M-x delete-file-local-variable deletes a variable from the list. M-x copy-dir-locals-to-file-locals copies directory-local variables to the list (see Directory Variables).

As with the ‘-*-’ line, the variables in a local variables list are used literally, and are not evaluated first. If you want to split a long string value across multiple lines of the file, you can use backslash-newline, which is ignored in Lisp string constants; you should put the prefix and suffix on each line, even lines that start or end within the string, as they will be stripped off when processing the list. Here is an example:

# Local Variables:
     # compile-command: "cc foo.c -Dfoo=bar -Dhack=whatever \
     #   -Dmumble=blaah"
     # End:
      

Some “variable names” have special meanings in a local variables list:

  • mode

     enables the specified major mode.
  • eval

     evaluates the specified Lisp expression (the value returned by that expression is ignored).
  • coding

     specifies the coding system for character code conversion of this file. See Coding Systems.
  • unibyte

     says to load or compile a file of Emacs Lisp in unibyte mode, if the value is 

    t

    . See Disabling Multibyte.

These four keywords are not really variables; setting them in any other context has no special meaning.

Do not use the 

mode

 keyword for minor modes. To enable or disable a minor mode in a local variables list, use the 

eval

 keyword with a Lisp expression that runs the mode command (see Minor Modes). For example, the following local variables list enables Eldoc mode (see Lisp Doc) by calling 

eldoc-mode

with no argument (calling it with an argument of 1 would do the same), and disables Font Lock mode (see Font Lock) by calling 

font-lock-mode

 with an argument of -1.

;; Local Variables:
     ;; eval: (eldoc-mode)
     ;; eval: (font-lock-mode -1)
     ;; End:
      

Note, however, that it is often a mistake to specify minor modes this way. Minor modes represent individual user preferences, and it may be inappropriate to impose your preferences on another user who might edit the file. If you wish to automatically enable or disable a minor mode in a situation-dependent way, it is often better to do it in a major mode hook (see Hooks).

Use the command M-x normal-mode to reset the local variables and major mode of a buffer according to the file name and contents, including the local variables list if any. See Choosing Modes.