Pandoc-mode Introduction
目录
简介
pandoc-mode
is an Emacs mode for interacting with Pandoc. Pandoc is a program (plus libraries) created by John MacFarlane that can convert a text written in one markup language into another markup language. pandoc-mode
is implemented as a minor mode that can be activated alongside the major mode for any of Pandoc's supported input formats. It provides facilities to set the various options that Pandoc accepts and to run Pandoc on the input file. It is possible to create different output profiles for a single input file, so that you can, for example, write your text in Markdown and then translate it to HTML for online reading, PDF for offline reading and Texinfo for reading in Emacs, all with just a few keystrokes.
= Pandoc -mode=是一个与Pandoc进行交互的Emacs模式。Pandoc是由John MacFarlane创建的一个程序(加上库),它可以将用一种标记语言编写的文本转换成另一种标记语言。= Pandoc -mode=被实现为一个小模式,可以与任何Pandoc支持的输入格式的主模式一起激活。它提供了设置Pandoc接受的各种选项并在输入文件上运行Pandoc的功能。可以为单个输入文件创建不同的输出配置文件,例如,您可以用Markdown编写文本,然后在Emacs中将其转换为HTML以供在线阅读、PDF以供离线阅读和Texinfo以供阅读,所有这些都只需几次按键操作。
The current version of pandoc-mode
is 2.28 and is compatible with Pandoc 2.7.
当前版本的= Pandoc -mode=是2.28,并且与Pandoc 2.7兼容。
安装
The easiest way to install pandoc-mode
is to use the Melpa package repository.
安装=pandoc-mode=的最简单方法是使用Melpa包存储库。
In order to activate pandoc-mode
in a buffer, you need to type M-x pandoc-mode
. To start pandoc-mode
automatically when you load e.g., a Markdown file, you can add a hook to your init file:
为了在缓冲区中激活=pandoc-mode=,您需要键入=M-x pandoc-mode=。当你加载一个标记文件,你可以添加一个钩子到你的init文件:
(add-hook 'markdown-mode-hook 'pandoc-mode)
However, if you do not want to start pandoc-mode
every time you work on a Markdown document, you can use a different function in markdown-mode-hook
: instead of using pandoc-mode
, you can use conditionally-turn-on-pandoc
. This function checks if a default settings file exists for the file you're opening and only turns on pandoc-mode
if it finds one. (For more info on the settings file, see the section ‘Settings Files'.)
但是,如果您不希望每次处理Markdown文档时都启动=pandoc-mode=,那么您可以在= Markdown -mode-hook=:中使用不同的函数,而不是使用=pandoc-mode=,您可以使用=condition -turn-on-pandoc=。该函数检查正在打开的文件是否存在默认设置文件,如果找到,则打开=pandoc-mode=。(有关设置文件的更多信息,请参见'设置文件'节。)
Additionally, if you want to automatically load the default pandoc-mode
settings file for the file you're opening, you can add the following to your init file:
另外,如果你想要为你打开的文件自动加载默认的=pandoc-mode=设置文件,你可以在你的init文件中添加以下内容:
(add-hook 'pandoc-mode-hook 'pandoc-load-default-settings)
The function pandoc-load-default-settings
checks if a default settings file exists for the file being loaded and reads its settings if it finds one.
函数=pandoc-load-default-settings=检查正在加载的文件是否存在默认设置文件,如果找到则读取其设置。
OS X
pandoc-mode
expects that the pandoc
binary can be found in your system's $PATH
and that the contents of $PATH
is available to Emacs. Especially on OS X, this may not always be the case. To remedy this, you can set the user option pandoc-binary
to the full path of pandoc
on your system. A more elegant solution is to install the exec-path-from-shell package. This package makes sure that the contents of your system's $PATH
variable is visible to Emacs, allowing Emacs to find pandoc
.
=pandoc-mode=期望可以在系统的=$PATH=中找到=pandoc=二进制文件,并且=$PATH=的内容对Emacs是可用的。特别是在OS X上,情况可能不总是这样。要解决这个问题,可以在系统上将user选项=pandoc-binary=设置为=pandoc=的完整路径。一个更好的解决方案是安装exec-path-from-shell包。这个包确保系统的=$PATH=变量的内容对Emacs是可见的,允许Emacs查找=pandoc=。
使用方法
pandoc-mode
uses the hydra package to create a keyboard-driven menu interface to all options and settings. The main menu is called by pressing C-c /
. After that, everything should be self-explanatory. From the main menu, you can run pandoc
on the buffer, view the output buffer and the current settings, set the input and output formats, and you can go to the options menu.
=pandoc-mode=使用hydra包创建一个键盘驱动的菜单界面,用于所有选项和设置。按=C-c /=调用主菜单。在那之后,一切都应该不言自明了。在主菜单中,您可以在缓冲区上运行=pandoc=,查看输出缓冲区和当前设置,设置输入和输出格式,然后转到选项菜单。
Note that if the menu bar is active, pandoc-mode
also provides a menu in the menu bar. It has the same structure as the hydra menu and it has the advantage that options that do not apply to the current file (due to its input or output format), are generally greyed out. On the other hand, the hydra menu shows the value of the options and allows you to change more than one option without having to keep reopening the menu. The menu bar menu disappears when you select an option, the hydra menu (generally) does not. Instead, it can be dismissed with q
. Below, I talk about the hydra menu specifically, but most of what is said applies to the menu bar menu as well.
注意,如果菜单栏是活动的,=pandoc-mode=也在菜单栏中提供一个菜单。它具有与hydra菜单相同的结构,并且它的优势是,不适用于当前文件的选项(由于其输入或输出格式)通常是灰色的。另一方面,hydra菜单显示选项的值,允许您更改多个选项,而不必一直重新打开菜单。当您选择一个选项时,菜单栏菜单将消失,hydra菜单(通常)不会消失。相反,可以使用=q=来取消它。下面,我将具体讨论hydra菜单,但是大部分内容也适用于菜单栏菜单。
In the options menu, you can set options for running pandoc
on your input file. All Pandoc options can be set from the menu (except for one or two that do not make sense, e.g., --print-default-template
). Note that when you set options, they only apply to the current file and the current output format. When you open another file, or when you change the output format, all settings are reset to their default values. (There are ways to make settings more permanent, of course, as discussed below.)
在选项菜单中,可以设置在输入文件上运行=pandoc=的选项。可以从菜单中设置所有Pandoc选项(除了一两个没有意义的选项,例如=——print-default-template=)。请注意,在设置选项时,它们只适用于当前文件和当前输出格式。当您打开另一个文件时,或者当您更改输出格式时,所有设置都将重置为默认值。(当然,有一些方法可以使设置更持久,如下所述。)
输入和输出格式
The most important settings are the input and output formats. The input format is set automatically by Emacs on the basis of the major mode of the input file, but you can change it if you need to. The output format defaults to “Native Haskell”, so most likely you will want to set it to something else before you run Pandoc. The input and output format menus also provide access to a submenu with the Markdown extensions that Pandoc supports. 最重要的设置是输入和输出格式。输入格式由Emacs根据输入文件的主要模式自动设置,但是如果需要,您可以更改它。输出格式默认为“本机Haskell”,因此您很可能希望在运行Pandoc之前将其设置为其他格式。输入和输出格式菜单还提供了对带有Pandoc支持的Markdown扩展的子菜单的访问。
As already stated, you may wish to use different output formats for a single input file. Most likely, the options that you want to pass to Pandoc will be different for each output format. To make this easier, pandoc-mode
has the ability to save the settings for a specific output format. The main menu has an option “Settings files” (C-c / s
), which takes you to a submenu where you can save the current settings. Emacs saves these settings to a hidden file in the same directory as the file you're editing, under a name composed of the input file, appended with the name of the output format and the string .pandoc
. So if your input file is called mytext.md
, the html
settings file will be called .mytext.md.html.pandoc
. (See the section ‘Settings Files' for details.)
如前所述,您可能希望对单个输入文件使用不同的输出格式。对于每种输出格式,您希望传递给Pandoc的选项很可能是不同的。为了简化这一点,=pandoc-mode=能够保存特定输出格式的设置。主菜单有一个选项“设置文件”(C-c / s
),它会带你到一个可以保存当前设置的子菜单。Emacs将这些设置保存到与正在编辑的文件相同目录中的一个隐藏文件中,该文件的名称由输入文件组成,并附加输出格式的名称和字符串=.pandoc=。如果你的输入文件被称为=mytext。md=, html=设置文件将被调用
.mytext.md.html.pandoc=。(详见' Settings Files'小节)
A single document can have a separate settings file for each output format that Pandoc supports. These can simply be created by setting all options the way you want them for the first output format, save them, then choose another output format, set the required options, save again, etc. Because the name of a settings file contains the output format for which it was created, the different settings files won't interfere with each other. When you switch the output format (with C-c / O
), Emacs checks if a corresponding settings file exists and loads it if one is found.
对于Pandoc支持的每种输出格式,单个文档可以有单独的设置文件。这些可以简单地创建通过设置所有选项的方式,你想要的第一个输出格式,保存它们,然后选择另一个输出格式,设置所需的选项,再次保存,等等。因为设置文件的名称包含它创建时的输出格式,所以不同的设置文件不会相互干扰。当您切换输出格式(with C-c / O
)时,Emacs检查是否存在相应的设置文件,如果找到,则加载它。
On systems that have symbolic links, it is also possible to specify a default output format (C-c / s d
). This is done by creating a symbolic link to the settings file of the current output format (a settings file is created if one doesn't exist yet) with the output format replaced by the string "default"
. The file it points to is read by the function pandoc-load-default-settings
, making it possible to automatically load a specific settings file when pandoc-mode
is invoked, as described above.
在具有符号链接的系统上,也可以指定默认的输出格式(C-c / s d
)。这是通过创建到当前输出格式的设置文件的符号链接来实现的(如果设置文件还不存在,则创建一个设置文件),输出格式将被string ="default"=替换。它所指向的文件由函数=pandoc-load-default-settings=读取,这样就可以在调用=pandoc-mode=时自动加载特定的设置文件,如上所述。
Note that the current output format is always visible in the mode line: the “lighter” for pandoc-mode
in the mode line has the form Pandoc/<format>
, where <format>
is the current output format.
注意,当前的输出格式在模式行中总是可见的:模式行中的“lighter”for =Pandoc -mode=有form =Pandoc/<format>=,其中=<format>=是当前的输出格式。
The major modes for which pandoc-mode
selects an input format automatically can be customised (user option pandoc-major-modes
). You can add major modes or remove those that you don't use. Similarly, you can customise the file extensions for each output format (pandoc-output-format-extensions
).
可以自定义=pandoc-mode=选择输入格式的主要模式(user option pandoc-major-modes
)。您可以添加主要模式或删除那些您不使用。类似地,您可以为每种输出格式定制文件扩展名(pandoc-output-format-extensions
)。
模板变量和元数据
pandoc-mode
allows you to set or change template variables through the menu. The variables are in the general writer options menu, the metadata in the reader options menu. Emacs will ask you for the name of a variable or metadata item and for a value for it. If you provide a name that already exists (TAB completion works), the new value replaces the old one.
=pandoc-mode=允许您通过菜单设置或更改模板变量。这些变量位于“常规写入器选项”菜单中,“读取器选项”菜单中的“元数据”菜单中。Emacs将询问变量或元数据项的名称及其值。如果您提供的名称已经存在(制表符完成工作),则新值将替换旧值。
Deleting a template variable or metadata item can be done by prefixing the menu key with -
. Emacs will ask you for the variable name (TAB completion works here, too) and removes it from the list.
删除模板变量或元数据项可以通过在菜单键前面加上=-=来完成。Emacs将询问您的变量名(TAB补全也适用于此)并将其从列表中删除。
运行 Pandoc
The first item in the menu is “Run Pandoc” (accessible with C-c / r
), which, as the name suggests, runs Pandoc on the document, passing all options you have set. By default, Pandoc sends the output to stdout (except when the output format is “odt”, “epub” or “docx”, in which case output is always sent to a file). Emacs captures this output and redirects it to the buffer *Pandoc output*
. The output buffer is not normally shown, but you can make it visible through the menu or by typing C-c / V
. Error messages from Pandoc are also displayed in this buffer.
第一项菜单中“Pandoc运行”(访问=碳碳/ r ),,顾名思义,运行Pandoc文档,通过所有选项设置。默认情况下,Pandoc将输出发送到stdout(除非输出格式是“odt”,“epub”或“多克斯”,在这种情况下,总是发送到输出文件)。Emacs捕获此输出并将其重定向到缓冲区
\*Pandoc输出\*=。输出缓冲区通常不显示,但您可以通过菜单或键入=C-c / V=使其可见。来自Pandoc的错误消息也显示在这个缓冲区中。
When you run Pandoc, pandoc-mode
also generates a few messages, which are logged in a buffer called *Pandoc log*
. You will rarely need to see this, since pandoc-mode
displays a message telling you whether Pandoc finished successfully or not. In the latter case, the output buffer is displayed, so you can see the error that Pandoc reported.
运行Pandoc时,= Pandoc -mode=也会生成一些消息,这些消息被记录在一个名为=\*Pandoc log\*=的缓冲区中。您很少需要看到这个,因为= Pandoc -mode=显示一条消息,告诉您Pandoc是否成功完成。在后一种情况下,将显示输出缓冲区,因此可以看到Pandoc报告的错误。
Note that when you run Pandoc, Pandoc doesn't read the file on disk. Rather, Emacs feeds it the contents of the buffer through stdin
. This means that you don't actually have to save your file before running Pandoc. Whatever is in your buffer, saved or not, is passed to Pandoc. Alternatively, if the region is active, only the region is sent to Pandoc.
注意,运行Pandoc时,Pandoc不会读取磁盘上的文件。相反,Emacs通过=stdin=将缓冲区的内容提供给它。这意味着您实际上不必在运行Pandoc之前保存文件。无论缓冲区中有什么,是否保存,都会传递给Pandoc。或者,如果该区域处于活动状态,则只将该区域发送到Pandoc。
If you call this command with a prefix argument C-u
(so the key sequence becomes C-/ C-u r
: C-/
to open the menu and C-u r
to run Pandoc), Emacs asks you for an output format to use. If there is a settings file for the format you specify, the settings in it will be passed to Pandoc instead of the settings in the current buffer. If there is no settings file, Pandoc will be called with just the output format and no other options.
如果您使用前缀参数=C-u=调用此命令(因此键序列变为=C-/ C-u r=: =C-/=打开菜单并=C-u r=运行Pandoc), Emacs将要求您使用输出格式。如果您指定的格式有设置文件,那么其中的设置将被传递给Pandoc,而不是当前缓冲区中的设置。如果没有设置文件,则只调用输出格式的Pandoc,没有其他选项。
Note that specifying an output format this way does not change the output format or any of the settings in the buffer, it just changes the output profile used for calling Pandoc. This can be useful if you use different output formats but don't want to keep switching between profiles when creating the different output files. 注意,以这种方式指定输出格式并不会更改输出格式或缓冲区中的任何设置,它只会更改用于调用Pandoc的输出配置文件。如果您使用不同的输出格式,但在创建不同的输出文件时不希望在配置文件之间不断切换,那么这将非常有用。
设置输出文件
If you want to save the output to a file rather than have it appear in the output buffer, you can set an explicit output file. Note that setting an output file is not the same thing as setting an output format (though normally the output file has a suffix that indicates the format of the file). 如果希望将输出保存到文件中,而不是显示在输出缓冲区中,可以设置显式输出文件。注意,设置输出文件与设置输出格式是不同的(尽管通常输出文件有一个表示文件格式的后缀)。
In pandoc-mode
, the output file setting has three options: the default is to send output to stdout, in which case it is redirected to the buffer *Pandoc output*
. This option can be selected by typing - o
in the file options menu. Alternatively, you can let Emacs create an output filename for you. In this case the output file will have the same base name as the input file but with the proper suffix for the output format. To select this option, prefix the output file key o
with C-u
in the file options menu. The third option is to specify an explicit output file. This can (obviously) be done by hitting just o
.
在= Pandoc -mode=中,输出文件设置有三个选项:默认是将输出发送到stdout,在这种情况下,输出被重定向到缓冲区=\*Pandoc output\*=。可以在“文件选项”菜单中键入=- o=来选择此选项。或者,您可以让Emacs为您创建一个输出文件名。在这种情况下,输出文件将具有与输入文件相同的基本名称,但是输出格式有适当的后缀。要选择此选项,请在“文件选项”菜单中的“输出文件”键=o=前面加上=C-u=。第三个选项是指定一个显式输出文件。这显然可以通过点击=o=来实现。
Note that Pandoc does not allow output to be sent to stdout if the output format is an OpenOffice.org Document (ODT), EPUB or MS Word (docx) file. Therefore, Emacs will always create an output filename in those cases, unless of course you've explicitly set an output file yourself. 注意,如果输出格式是OpenOffice.org文档(ODT)、EPUB或MS Word (docx)文件,Pandoc不允许将输出发送到stdout。因此,在这些情况下,Emacs将始终创建输出文件名,当然,除非您自己显式地设置了输出文件。
The output file you set is always just the base filename, it does not specify a directory. Which directory the output file is written to depends on the setting “Output Directory” (which is not actually a Pandoc option). Emacs creates an output destination out of the settings for the output directory and output file. If you don't specify any output directory, the output file will be written to the same directory that the input file is in. 您设置的输出文件总是基本文件名,它不指定目录。输出文件写入到哪个目录取决于设置“output directory”(实际上不是Pandoc选项)。Emacs从输出目录和输出文件的设置中创建输出目的地。如果不指定任何输出目录,则输出文件将被写入与输入文件所在的相同目录。
创建pdf
The second item in the main menu is “Create PDF” (invoked with C-c / p
). This option calls Pandoc with a PDF file as output file. Pandoc offers different ways of creating a PDF file: you can use LaTeX, an HTML-to-PDF converter, or groff. Which method is used depends on the output format you specify, because Pandoc creates a PDF file by first converting your input file to the specified output format and then calling the pdf converter on the output file.
主菜单中的第二项是“创建PDF”(用=C-c / p=调用)。此选项使用PDF文件作为输出文件调用Pandoc。Pandoc提供了创建PDF文件的不同方法:您可以使用LaTeX、HTML-to-PDF转换器或groff。使用哪种方法取决于指定的输出格式,因为Pandoc首先将输入文件转换为指定的输出格式,然后在输出文件上调用PDF转换器,从而创建PDF文件。
When creating a PDF using pandoc-mode
, Emacs first checks if the output format of the current buffer is set to latex
, context
, beamer
, html
, or ms
. If it is, C-c / p
creates the PDF using that format. If you want to bypass this automatic detection, use a prefix argument C-u
(i.e., type C-c / C-u p
). Emacs will then ask you for the output format to use.
在使用=pandoc-mode=创建PDF时,Emacs首先检查当前缓冲区的输出格式是否设置为=latex=, context=, =beamer=, =html=,或=ms=。如果是,则=C-c / p=使用该格式创建PDF。如果您想绕过这个自动检测,请使用前缀参数=C-u=(即, type =C-c / C-u p
)。然后Emacs将询问您要使用的输出格式。
If the buffer's current output format does not allow for PDF creation, Emacs will ask you which output format to use. If there is a settings file for the output format you specify, it is used to create the PDF. (The current buffer's settings aren't changed, however.) If there is no settings file, Pandoc is called with only the input and output formats and the output file. 如果缓冲区的当前输出格式不允许创建PDF,那么Emacs将询问您使用哪种输出格式。如果您指定的输出格式有一个设置文件,那么它将用于创建PDF。(不过,当前缓冲区的设置没有改变。)如果没有设置文件,则使用输入和输出格式以及输出文件调用Pandoc。
The format you choose is remembered (at least until you close the buffer or change the output format), so that the next time you convert the buffer to PDF, you are not asked for the format again. If you want to use a different format, use the prefix argument C-u
.
您选择的格式将被记住(至少在关闭缓冲区或更改输出格式之前),以便下次将缓冲区转换为PDF时,不会再次询问该格式。如果您想使用不同的格式,请使用前缀参数=C-u=。
This setup means that you do not need to switch the output format to latex
, context
or html5
every time you wish to create a PDF, which can be practical if you're also converting to another format. However, if you wish to change settings for PDF output, you do need to switch to the relevant output format.
这种设置意味着您不需要在每次希望创建PDF时都将输出格式切换为=latex=、=context=或=html5=,如果您也要转换为另一种格式,这是很实用的。但是,如果您希望更改PDF输出的设置,您\*确实\*需要切换到相关的输出格式。
Note that for latex
, beamer
and html
, you can use different PDF engines. For latex
and beamer
, these are pdflatex
(the default), xelatex
and lualatex
, for html
there are wkhtmltopdf
(the default), weasyprint
and prince
. If you wish to use a PDF engine other than the default, you need to set the option pdf-engine
.
注意,对于=latex=、=beamer=和=html=,可以使用不同的PDF引擎。For latex
and beamer=,其中=pdflatex=(默认值),=xelatex
and lualatex=, For =html
there wkhtmltopdf=(默认值),=weasyprint
and =prince=。如果希望使用默认之外的PDF引擎,则需要设置option = PDF -engine=。
查看输出
After running Pandoc, you can view the output file with the option View output file
in the menu (or C-c / v
). Emacs will try to display the file created during the most recent Pandoc run. Which viewer is used to display the output file depends on the output format (not on the output file's extension, so that you can use different viewers for different output formats, even if their file extensions are identical. For example, docbook
, jats=and =tei
all use xml
as the file extension, but you may not want to use the same viewer for all of them).
运行Pandoc后,可以使用菜单中的option = view output file=查看输出文件(或=C-c / v=)。Emacs将尝试显示在最近一次运行Pandoc期间创建的文件。使用哪个查看器来显示输出文件取决于输出格式(而不是输出文件的扩展名,因此您可以对不同的输出格式使用不同的查看器,即使它们的文件扩展名是相同的。例如,=docbook=、=jats=和=tei=都使用=xml=作为文件扩展名,但您可能不希望对所有扩展名使用相同的查看器)。
Viewers are defined in the customisation option pandoc-viewers
. There are three types of viewers: you can choose to use Emacs itself as the file viewer, in which case the output file is opened in Emacs and displayed using display-buffer
. It is also possible to define an external viewer, which should then be a program that takes a file argument on the command line. For example, word processor formats (odt
, docx
) and Powerpoint presentations (pptx
) are by default opened in LibreOffice, which can be called with the shell command libreoffice <filename>
.
查看器在自定义选项=pandoc-viewer 中定义。有三种类型的查看器:您可以选择使用Emacs本身作为文件查看器,在这种情况下,在Emacs中打开输出文件并使用=display-buffer=显示。还可以定义一个外部查看器,它应该是一个在命令行上接受文件参数的程序。例如,word处理器格式(=odt=, =docx
)和Powerpoint演示文稿(pptx
)在LibreOffice中默认打开,可以使用shell命令= LibreOffice <filename>=调用。
Lastly, it is also possible to specify a specific Emacs function to handle the file. This should be a function that takes a file name as argument. The function can pass on the file to an external program (HTML-based formats, for example, are by default handled by the function browse-url
, which sends the file to a suitable browser), or it can arrange to open the file in Emacs, if the standard find-file-noselect
is not suitable. Note, though, that if you choose this method, you should also make sure that your function not only opens the file but also displays its buffer (e.g., using display-buffer
).
最后,还可以指定一个特定的Emacs函数来处理该文件。这应该是一个以文件名作为参数的函数。该函数可以将文件传递给外部程序(例如,基于html的格式在默认情况下由函数=browse-url=处理,该函数将文件发送到合适的浏览器),或者如果标准的=find-file-noselect=不合适,它可以安排在Emacs中打开文件。但是请注意,如果选择此方法,还应该确保函数不仅打开文件,而且还显示其缓冲区(例如,使用=display-buffer=)。
If the most recent call to Pandoc created a pdf file (i.e, the option “Convert to pdf” was called), Emacs will display the pdf file instead of the output file defined by the output file/directory options. The viewer to use in this case is defined by the option pandoc-pdf-viewer
, which can be Emacs (which will then use =doc-view-mode, or=pdf-tools` if installed) or an external program.
如果最近调用Pandoc创建了一个pdf文件(i。,选项“转换为pdf”被调用),Emacs将显示pdf文件,而不是由输出文件/目录选项定义的输出文件。在本例中使用的查看器由option =pandoc-pdf-viewer=定义,它可以是Emacs(然后使用=doc-view-mode,如果安装了=pdf-tools ')或外部程序。
Note that this functionality is not as full-featured as with e.g., AUCTeX and SyncTeX. There is no forward or backward search and what happens when you view a file when an earlier version of that file is already open in some application is up to that application. Emacs will usually notice that the file has changed on disk and will ask you if you want to reload it. Pdf viewers will generally behave correctly (that includes pdf-tools
in Emacs) and simply reload the file without asking.
请注意,此功能不像AUCTeX和SyncTeX那样功能全面。不存在前向或后向搜索,当您查看一个文件时,当该文件的早期版本已经在某个应用程序中打开时,所发生的事情取决于该应用程序。Emacs通常会注意到磁盘上的文件已经更改,并询问是否要重新加载它。Pdf查看器的行为通常是正确的(包括= Pdf -tools= in Emacs),并且不需要询问就可以简单地重新加载文件。
If you want to open the output file automatically after conversion, you can add the function pandoc-view-output
to pandoc-async-success-hook
. As the name of the hook implies, this only works if you call Pandoc asynchronously ---cf. the option pandoc-use-async
--- but this is the default and there is usually little reason to change it.
如果希望在转换后自动打开输出文件,可以添加函数=pandoc-view-output= to =pandoc-async-success-hook=。正如钩子的名字所暗示的那样,这只有在异步调用Pandoc时才有效——cf。选项=pandoc-use-async=——但这是默认的,通常没有什么理由去改变它。
If you try to view an output file before calling Pandoc (e.g., after reopening the input file), Emacs will try to display the output file defined by the output directory
and output file
settings. If no output file is defined at all (which means that Pandoc does not create an output file but instead sends its output to standard out), Emacs will show the *Pandoc output*
buffer, which is where the output of the call to Pandoc is captured.
如果您尝试在调用Pandoc之前查看输出文件(例如,在重新打开输入文件之后),Emacs将尝试显示由=output目录=和=output file= settings定义的输出文件。如果根本没有定义输出文件(这意味着Pandoc不创建输出文件,而是将其输出发送到标准输出),则Emacs将显示=\*Pandoc输出\*= buffer,在这里捕获对Pandoc调用的输出。
If the most recent Pandoc run returned an error, trying to view the output file will result in an error as well, unless you provide a prefix argument (C-c / C-u v
), in which case Emacs will try to display the output file defined by the output directory
and output file
settings.
如果最近Pandoc运行返回一个错误,试图查看输出文件将导致一个错误,除非你提供一个前缀的论点(碳碳/ c u v =),在这种情况下,Emacs会显示定义的输出文件=输出文件输出目录=和
=设置。
连接类型
By default, Emacs starts pandoc
as an asynchronous process using a tty. If this causes problems for some reason, you can try using a pipe instead by customising pandoc-process-connection-type
. Alternatively, you can use a synchronous process by unsetting the user option pandoc-use-async
.
默认情况下,Emacs使用tty启动=pandoc=作为异步进程。如果由于某种原因导致问题,您可以通过自定义=pandoc-process-connection-type=来尝试使用管道。或者,您可以通过取消user选项=pandoc-use-async=的设置来使用同步进程。
引用 Jumping
pandoc-mode
provides the function pandoc-jump-to-reference
that locates a reference within external bibliography files indicated by the bibliography
user option. Note that entries to the bibliography
user option list must have an absolute path for this option to work properly (i.e. ="./Bibliography.bib"= rather than "Bibliography.bib"
). This feature is not bound to any key by default, but may of course be bound to a key combination as follows:
pandoc-mode=提供函数=pandoc-jump-to-reference=,该函数在由=bibliography
user选项指定的外部书目文件中定位一个引用。注意,=bibliography= user选项列表中的条目必须有一个绝对路径,以便该选项正常工作(例如="./ bibliography。而不是“bibliographi .bib”=)。这个特性默认情况下不绑定到任何键,但是当然可以绑定到一个键组合,如下所示:
(define-key markdown-mode-map (kbd "C-c j") 'pandoc-jump-to-reference)
The jump behaviour can be customised by changing the option pandoc-citation-jump-function
. Its default value is pandoc-goto-citation-reference
, which opens the relevant BibTeX file in a new window and moves point to the entry. Two alternative functions have been defined: pandoc-open-in-ebib
, which opens the relevant entry in Ebib, and pandoc-show-citation-as-help
, which shows the entry in a *Help*
buffer, but does not open the corresponding BibTeX file.
可以通过更改选项=pandoc-引用-跳转-函数=来定制跳转行为。它的默认值是=pandoc-goto- reference=,它将在一个新窗口中打开相关的BibTeX文件,并将指向该条目。已经定义了两个替代函数:=pandoc-open-in-ebib=,它打开Ebib中的相关条目;=pandoc-show-引用-as- Help ,它显示了
\*Help\*=缓冲区中的条目,但是没有打开相应的BibTeX文件。
Alternatively, you may also define your own function, which should take two arguments: the key of the entry to be displayed and a list of BibTeX files. 或者,您也可以定义自己的函数,该函数应该有两个参数:要显示的条目的键和BibTeX文件的列表。
Font lock
pandoc-mode
adds font lock keywords for citations and numbered example lists. The relevant faces can be customised in the customisation group pandoc
.
=pandoc-mode=添加引用和编号示例列表的字体锁定关键字。相关的面可以在自定义组=pandoc=中自定义。
配置文件
Apart from settings files for individual files (which are called local settings files), pandoc-mode
supports two other types of settings files: project files and global files. Project files are settings files that apply to all input files in a given directory (except those files for which a local settings file exists). Global settings files, as the name implies, apply globally, to files for which no local or project file is found. Both types of files are specific to a particular output format, just like local settings files. Project files live in the directory they apply to and are called Project.<format>.pandoc
. Global files live in the directory specified by the variable pandoc-data-dir
, which defaults to ~/.emacs.d/pandoc-mode/
, but this can of course be changed in the customisation group pandoc
.
除了个别文件的设置文件(称为本地设置文件)外,=pandoc-mode=还支持另外两种类型的设置文件:项目文件和全局文件。项目文件是应用于给定目录中的所有输入文件的设置文件(本地设置文件存在的文件除外)。全局设置文件,顾名思义,全局应用于没有找到本地或项目文件的文件。这两种类型的文件都特定于特定的输出格式,就像本地设置文件一样。项目文件位于它们所应用的目录中,被称为=Project.<format>.pandoc=。全局文件位于变量=pandoc-data-dir=指定的目录中,该变量默认为=~/.emacs。d/pandoc-mode/=,但这当然可以在自定义组=pandoc=中更改。
Whenever pandoc-mode
loads settings for an input file, it first checks if there is a local settings file. If none is found, it looks for a project file, and if that isn't found, it tries to load a global settings file. In this way, local settings override project settings and project settings override global settings. Note, however, that if a local settings file exists, all settings are read from this file. Any project file or global file for the relevant output format is ignored.
每当=pandoc-mode=加载输入文件的设置时,它首先检查是否有本地设置文件。如果没有找到,则查找项目文件,如果没有找到,则尝试加载全局设置文件。这样,本地设置将覆盖项目设置,而项目设置将覆盖全局设置。但是,请注意,如果存在本地设置文件,则从该文件读取所有设置。任何项目文件或相关输出格式的全局文件都将被忽略。
You can create a project or global settings file through the menu in the submenu “Settings Files”. This simply saves all settings for the current buffer to a project or global settings file. (Any local settings file for the file in the current buffer will be kept. You'll need to delete it manually if you no longer need it.) 您可以通过“设置文件”子菜单中的菜单创建项目或全局设置文件。这只是将当前缓冲区的所有设置保存到一个项目或全局设置文件中。(保存当前缓冲区中文件的任何本地设置文件。如果你不再需要它,你需要手动删除它。)
The name of a global settings file has the form <format>.pandoc
, where <format>
obviously specifies the output format. <format>
can also be the string "default“
, however, in which case it specifies a default settings file, which is loaded by pandoc-load-default-settings
when no default local or project settings file is found. In this way, you can override the default output format used for new files.
全局设置文件的格式为form =<format>。其中=<format>=显然指定了输出格式。=<format>=也可以是string ="default " =,但是,在这种情况下,它指定了一个默认的设置文件,当没有找到默认的本地或项目设置文件时,它由=pandoc-load-default-settings=加载。通过这种方式,您可以覆盖用于新文件的默认输出格式。
Note that starting with version 2.5, pandoc-mode
settings files are written in a Lisp format (as demonstrated above). Old-style settings files continue to be read, so there is no need to change anything, but if you change any settings and save them, the file is converted.
注意,从2.5版本开始,=pandoc-mode=设置文件是用Lisp格式编写的(如上所示)。老式的设置文件将继续被读取,因此不需要更改任何内容,但是如果您更改了任何设置并保存它们,文件将被转换。
File-local变量
pandoc-mode
also allows options to be set as file-local variables, which gives you the ability to keep the settings for a file in the file itself. To specify an option in this way, use the long form of the option as a variable name, prefixed with pandoc/
(note the slash; use pandoc/read
and pandoc/write
for the input and output formats, and pandoc/table-of-contents
for the TOC).
=pandoc-mode=还允许将选项设置为文件本地变量,这使您能够将文件的设置保存在文件本身中。要以这种方式指定一个选项,请使用该选项的长形式作为变量名,前缀为=pandoc/=(注意斜杠;对于输入和输出格式使用=pandoc/read=和=pandoc/write=,对于TOC使用=pandoc/table-of-content =)。
For example, in order to set a bibliography file, add the following line to the local variable block: 例如,为了设置一个书目文件,在局部变量块中添加以下行:
pandoc/bibliography: "~/path/to/mybib.bib"
The easiest way to add a file-local variable is to use the command M-x add-file-local-variable
. This will put the variable at the end of the file and add the correct comment syntax. Note that the values are Lisp expressions, which means that strings need to be surrounded with double quotes. Symbols do not need to be quoted, however.
添加文件本地变量的最简单方法是使用命令=M-x add-file-local-variable=。这将把变量放在文件的末尾,并添加正确的注释语法。注意,这些值是Lisp表达式,这意味着字符串需要用双引号括起来。但是,符号不需要引用。
Settings specified as file-local variables are kept separate from other settings: they cannot be set through the menu and they are never saved to a settings file. When you call pandoc-view-settings
(C-c / S
), they are shown in a separate section. A source file can both have a settings file and specify settings in file-local variables. If this happens, the latter override the former.
指定为文件本地变量的设置与其他设置是分开的:它们不能通过菜单进行设置,也不会保存到设置文件中。当您调用=pandoc-view-settings= (C-c / S
)时,它们将显示在单独的部分中。源文件既可以有设置文件,也可以在文件本地变量中指定设置。如果发生这种情况,后者将覆盖前者。
Note that it is also possible to specify the customisation option pandoc-binary
as a file-local variable. It does not require the pandoc/
prefix, but since its value is a string, it must be enclosed in quotes:
注意,还可以将定制选项=pandoc-binary=指定为文件本地变量。它不需要=pandoc/=前缀,但由于它的值是一个字符串,它必须用引号括起来:
pandoc-binary: "/path/to/alternate/pandoc“
管理编号的示例
Pandoc provides a method for creating examples that are numbered sequentially throughout the document (see Numbered example lists in the Pandoc documentation). pandoc-mode
makes it easier to manage such lists. First, by going to “Example Lists | Insert New Example” (C-c / e i
), you can insert a new example list item with a numeric label: the first example you insert will be numbered (@1)
, the second (@2)
, and so on. Before inserting the first example item, Emacs will search the document for any existing definitions and number the new items sequentially, so that the numeric label will always be unique.
Pandoc提供了一种创建示例的方法,这些示例在整个文档中按顺序编号(参见Pandoc文档中的编号示例列表)。=pandoc-mode=更容易管理这样的列表。首先,通过转到“示例列表|插入新示例”(C-c / e i
),您可以插入一个带有数字标签的新示例列表项:您插入的第一个示例编号为=(@1)=,第二个示例编号为=(@2)=,依此类推。在插入第一个示例项之前,Emacs将在文档中搜索任何现有定义并按顺序对新项编号,以便数字标签始终是惟一的。
Pandoc allows you to refer to such labeled example items in the text by writing (@1)
and pandoc-mode
provides a facility to make this easier. If you select the menu item “Example Lists | Select And Insert Example Label” (C-c / e s
) Emacs displays a list of all the (@)
-definitions in your document. You can select one with the up or down keys (you can also use j
and k
or n
and p
) and then hit return
to insert the label into your document. If you change your mind, you can leave the selection buffer with q
without inserting anything into your document.
Pandoc允许您通过写入=(@1)=和= Pandoc -mode=来引用文本中此类标记的示例项,并提供了一个工具来简化这一操作。如果选择菜单项“Example Lists | select And Insert Example Label”(C-c / e s
), Emacs将显示文档中所有=(@)=-definition的列表。您可以选择一个具有上键或下键的标签(也可以使用=j=和=k=或=n=和=p=),然后点击=return=将标签插入到文档中。如果您改变了主意,可以使用=q=保留选择缓冲区,而无需向文档插入任何内容。
使用@@-指令
pandoc-mode
includes a facility to make specific, automatic changes to the text before sending it to Pandoc. This is done with so-called @@
-directives, which trigger an Elisp function and are then replaced with the output of that function. A @@
-directive takes the form @@directive
, where directive
can be any user-defined string (see How to define directive strings). Before Pandoc is called, Emacs searches the text for these directives and replaces them with the output of the functions they call.
= Pandoc -mode=包含一个工具,用于在将文本发送给Pandoc之前对其进行特定的、自动的更改。这是通过所谓的=@@=-指令完成的,该指令触发Elisp函数,然后由该函数的输出替换。一个=@@=-directive采用了=@@directive=的形式,其中=directive=可以是任何用户定义的字符串(参见如何定义指令字符串)。在调用Pandoc之前,Emacs在文本中搜索这些指令,并用它们调用的函数的输出替换它们。
So suppose you define (e.g., in ~/.emacs.d/init
) a function my-pandoc-current-date
:
因此,假设您定义了一个函数(例如,in ~/.emacs.d/init
) my-pandoc-current-date
:
(defun my-pandoc-current-date (_) (format-time-string "%d %b %Y"))
Now you can define a directive @@date
that calls this function. The effect is that every time you write @@date
in your document, it is replaced with the current date.
现在您可以定义一个指令=@@date=来调用这个函数。其结果是,每次在文档中写入=@@date=时,它都被当前日期所取代。
Note that the function that the directive calls must have one argument, which is used to pass the output format to the function (as a string). This way you can have your directives do different things depending on the output format. This argument can be called anything you like. In the above example, it is called _
(i.e., just an underscore), to indicate that the variable is not actually used in the function. If you do use it, you should probably choose a more meaningful name.
注意,指令调用的函数必须有一个参数,用于将输出格式传递给函数(作为字符串)。通过这种方式,可以根据输出格式让指令执行不同的操作。这个论证可以叫做任何你喜欢的东西。在上面的例子中,它被称为=_=(即,只是一个下划线),以指示该变量实际上没有在函数中使用。如果你使用它,你应该选择一个更有意义的名字。
@@
-directives can also take the form @@directive{...}
. Here, the text between curly braces is an argument, which is passed to the function called by the directive as the second argument. Note that there should be no space between the directive and the left brace. If there is, Emacs won't see the argument and will treat it as normal text.
@@
-directive也可以采用=@@directive{…}=的形式。在这里,花括号之间的文本是一个参数,它作为第二个参数传递给指令调用的函数。注意,指令和左括号之间不应该有空格。如果有,Emacs将不会看到该参数,并将其视为正常文本。
It is possible to define a directive that can take an optional argument. This is simply done by defining the argument that the directive's function takes as optional. Suppose you define my-pandoc-current-date
as follows:
可以定义一个带有可选参数的指令。这可以通过定义指令函数可选的参数来实现。假设定义=my-pandoc-current-date=
(defun my-pandoc-current-date (_ &optional text) (format "%s%s" (if text (concat text ", ") "") (format-time-string "%d %b %Y")))
This way, you could write @@date
to get just the date, and @@date{Cologne}
to get “Cologne, 03 Oct 2019”.
通过这种方式,您可以编写=@@date=获取日期,而=@@date{Cologne}=获取“科隆,2019年10月3日”。
Two directives have been predefined: @@lisp
and @@include
. Both of these take an argument. @@lisp
can be used to include Elisp code in the document which is then executed and replaced by the result (which should be a string). For example, another way to put the current date in your document, without defining a special function for it, is to write the following:
已经预定义了两个指令:=@@lisp=和=@@include=。这两个都需要一个参数。=@@lisp=可用于在文档中包含Elisp代码,然后执行该代码并由结果(应该是字符串)替换。例如,另一种将当前日期放入文档的方法是,不需要为其定义特殊的函数,而是编写以下代码:
@@lisp{(format-time-string "%d %b %Y")}
Emacs takes the Elisp code between the curly braces, executes it, and replaces the directive with the result of the code. Note that the code can be anything, and there is no check to see if it is “safe”. Emacs在花括号之间获取Elisp代码,执行它,并用代码的结果替换指令。注意,代码可以是任何东西,没有检查是否“安全”。
@@include
can be used to include another file into the current document (which must of course have the same input format):
=@@include=可用于将另一个文件包含到当前文档中(当然必须具有相同的输入格式):
@@include{copyright.text}
This directive reads the file copyright.text
and replaces the @@include
directive with its contents.
这个指令读取文件=版权。text=并用它的内容替换=@@include=指令。
Processing @@
-directives works everywhere in the document, including in code and code blocks, and also in the %-header block. So by putting the above @@lisp
directive in the third line of the %-header block, the meta data for your documents will always show the date on which the file was created by Pandoc.
Processing @@
-directive在文档中的任何地方都能工作,包括代码和代码块,以及%-header块。因此,通过将上面的=@@lisp=指令放在%-header块的第三行,文档的元数据将始终显示Pandoc创建文件的日期。
If it should ever happen that you need to write a literal "@@lisp"
in your document, you can simply put a backslash before the first @
: @@lisp
. Emacs removes the backslash (which is necessary in case the string @@lisp
is contained in a code block) and then continues searching for the next directive.
如果需要在文档中写入文字="@@lisp",可以在第一个=@
:=@@lisp=前加一个反斜杠。Emacs删除反斜杠(在字符串=@@lisp=包含在代码块中的情况下,这是必需的),然后继续搜索下一个指令。
After Emacs has processed a directive and inserted the text it produced in the buffer, processing of directives is resumed from the start of the inserted text. That means that if an @@include
directive produces another @@include
directive, the newly inserted @@include
directive gets processed as well.
当Emacs处理了一个指令并将它在缓冲区中产生的文本插入之后,指令的处理将从插入的文本开始重新开始。这意味着如果一个=@@include=指令生成另一个=@@include=指令,则新插入的=@@include=指令也将被处理。
Master file
If you have a master file with one or more @@include
directives and you're editing one of the included files, running Pandoc from that buffer will not produce the desired result, because it runs Pandoc on the included file. To make working with included files easier, you can specify a master file for them, with the command pandoc-set-master-file
(through the menu with C-c / o f m
). When this option is set, Pandoc is run on the master file rather than on the file in the current buffer.
如果您有一个带有一个或多个=@@include=指令的主文件,并且您正在编辑其中一个包含的文件,那么从该缓冲区运行Pandoc将不会产生所需的结果,因为它在包含的文件上运行Pandoc。为了使处理包含的文件更容易,可以使用命令=pandoc-set-master-file=(通过菜单=C-c / o f m=)为它们指定一个主文件。设置此选项后,Pandoc将在主文件上运行,而不是在当前缓冲区中的文件上运行。
The settings used in this case are always the settings for the master file, not the settings for the included file. The only exception is the output format, which is taken from the buffer from which you run Pandoc. This makes it possible to change the output format while in a buffer visiting an included file and have pandoc-mode
do the right thing.
本例中使用的设置始终是主文件的设置,而不是包含的文件的设置。唯一的例外是输出格式,它取自运行Pandoc的缓冲区。这使得在缓冲区中访问包含的文件时更改输出格式成为可能,并且have =pandoc-mode=做正确的事情。
One thing to keep in mind is that the master file setting is dependent on the output format. When you set a master file, it is only set for the output format that is active. This means that you need to set the output format before you set the master file. 需要记住的一点是,主文件设置依赖于输出格式。当您设置主文件时,它只设置为活动的输出格式。这意味着您需要在设置主文件之前设置输出格式。
Note that the master file menu also has an option “Use this file as master file” (C-c / o f M
). When you select this option, the current file is set as master file and a project settings file is created for the current output format. This is a quick way to set the master file for all files in a directory, since the project settings will apply to all files in the directory.
注意,主文件菜单也有一个选项“使用这个文件作为主文件”(C-c / o f M
)。选择此选项时,将当前文件设置为主文件,并为当前输出格式创建项目设置文件。这是为目录中的所有文件设置主文件的快速方法,因为项目设置将应用于目录中的所有文件。
定义@@-指令
Defining @@
-directives yourself is done in two steps. First, you need to define the function that the directive will call. This function must take at least one argument to pass the output format and may take at most one additional argument. It should return a string, which is inserted into the buffer. The second step is to go to the customisation buffer with M-x customize-group
RET
pandoc
RET
. One of the options there is pandoc-directives
. This variable contains a list of directives and the functions that they are linked with. You can add a directive by providing a name (without @@
) and the function to call. Note that directive names may only consists of letters (a-z
, A-Z
) or numbers (0-9
). Other characters are not allowed. Directive names are case sensitive, so @@Date
is not the same as @@date
.
您自己定义=@@=-directive有两个步骤。首先,您需要定义指令将调用的函数。此函数必须使用至少一个参数来传递输出格式,并且最多可以使用一个附加参数。它应该返回一个字符串,该字符串被插入到缓冲区中。第二步是使用=M-x customize-group= RET
pandoc
RET=进入定制缓冲区。其中一个选项是=pandoc-directive =。这个变量包含一列指令和它们所链接的函数。您可以通过提供一个名称(没有=@@
)和要调用的函数来添加一个指令。注意,指令名只能由字母(a-z=, =a-z
)或数字(0-9
)组成。其他字符是不允许的。指令名是大小写敏感的,所以=@@Date=和=@@Date=不一样。
Passing more than one argument to an @@
-directive is not supported. However, if you really want to, you could use split-string
to split the argument of the @@
-directive and “fake” multiple arguments that way.
不支持向=@@=-指令传递多个参数。但是,如果您真的想这样做,您可以使用=split-string=来分割=@@=-指令的参数,并以这种方式“伪造”多个参数。
A final note: the function that processes the @@
-directives is called pandoc-process-directives
and can be called interactively. This may be useful if a directive is not producing the output that you expect. By running pandoc-process-directives
interactively, you can see what exactly your directives produce before the resulting text is sent to pandoc. The changes can of course be undone with M-x undo
(usually bound to C-/
).
最后注意:处理=@@=-伪指令的函数被称为=pandoc-process-directive ,可以交互调用。如果一个指令没有产生您期望的输出,这可能会有用。通过交互式地运行=pandoc-process-directive =,您可以看到在生成的文本被发送到pandoc之前,指令究竟生成了什么。当然,可以使用=M-x undo=(通常绑定到=C-/
)来撤消更改。
指令钩子
There is another customisable variable related to @@
-directives: pandoc-directives-hook
. This is a list of functions that are executed before the directives are processed. These functions are not supposed to change anything in the buffer, they are intended for setting up things that the directive functions might need.
还有一个与=@@=-相关的可定制变量:=pandoc-directives-hook=。这是在处理指令之前执行的函数列表。这些函数不应该改变缓冲区中的任何东西,它们是用来设置指令函数可能需要的东西的。