yasnippet with Sweave files

61 views
Skip to first unread message

awellis

unread,
Jan 21, 2009, 5:30:50 PM1/21/09
to smart-snippet and YASnippet
Hi,
Has anyone managed to get yasnippet working with Rnw files. These
files contain both LaTeX and R code, and emacs switches between ESS
and latex modes.
My snippets for ess-mode and latex-mode work as expected in either of
those modes, but not when editing a file with the .Rnw extension.

Anyone got any suggestions?

Thanks,
Andrew

awellis

unread,
Jan 21, 2009, 6:13:40 PM1/21/09
to smart-snippet and YASnippet
PS. expanding snippets with M-x yas/expand works in Rnw files for both
latex and ess major modes, but TAB expansions does not work...

Zhang Chiyuan

unread,
Jan 21, 2009, 11:27:48 PM1/21/09
to smart-...@googlegroups.com
What's the value of the variable major-mode in a Rnw file? In other
word, open a Rnw file and
type C-h v and type major-mode and hit Enter.
--
pluskid

awellis

unread,
Jan 22, 2009, 7:44:14 AM1/22/09
to smart-snippet and YASnippet
in latex segments, it's latex-mode, and in R chunks it's ess-mode.

yasnippets works fine in either mode if I'm editing a .tex or a .R
file.

Zhang Chiyuan

unread,
Jan 22, 2009, 8:33:47 AM1/22/09
to smart-...@googlegroups.com
Then it might be that tab key is overwrite by Rnw. What is the result
of "C-h k TAB"?
It should be yas/expand. Here's the paragraph copied from the FAQ for
this problem:

Why TAB key doesn't expand a snippet?
=====================================

First check the mode line to see if there's ``yas``. If no, then try
``M-x yas/minor-mode-on`` to manually turn on ``yas/minor-mode`` and
try to expand the snippet again. If it works, then, you can add the
following code to your ``.emacs`` *before* loading YASnippet:

.. sourcecode:: lisp

(setq yas/extra-mode-hooks '(the-major-mode))

where ``the-major-mode`` is the major mode in which ``yas/minor-mode``
isn't enabled by default.

If ``yas/minor-mode`` is on but the snippet still not expanded. Then
try to see what command is bound to the ``TAB`` key: press ``C-h k``
and then press ``TAB``. Emacs will show you the result.

You'll see a buffer prompted by Emacs saying that ``TAB runs the
command ...``. Alternatively, you might see ``<tab> runs the command
...``, note the difference between ``TAB`` and ``<tab>`` where the
latter has priority. If you see ``<tab>`` bound to a command other
than ``yas/expand``, (e.g. in ``org-mode``) you can try the following
code to work around:

.. sourcecode:: lisp

(add-hook 'org-mode-hook
'(lambda ()
(make-variable-buffer-local 'yas/trigger-key)
(setq yas/trigger-key [tab])))

replace ``org-mode-hook`` with the major mode hook you are dealing
with (``C-h m`` to see what major mode you are in).

If it says ``TAB`` but YASnippet still doesn't work, check your
configuration and you may also ask for help on the `discussion group
<http://groups.google.com/group/smart-snippet>`_. Don't forget to
attach the information on what command is bound to TAB as well as the
mode information (Can be obtained by ``C-h m``).
--
pluskid

Andrew Ellis

unread,
Jan 22, 2009, 9:03:07 AM1/22/09
to smart-...@googlegroups.com
thanks, <tab> was already bound by noweb-mode.

The following lisp code gets yasnippets working for latex in noweb mode:

(require 'yasnippet)
(yas/initialize)
(yas/load-directory "/path/to/yasnippet/snippets")

(add-hook 'LaTeX-mode-hook
'(lambda ()
(make-variable-buffer-local 'yas/trigger-key)
(setq yas/trigger-key [tab])))



Reply all
Reply to author
Forward
0 new messages