diff --git a/doc/define_snippet.rst b/doc/define_snippet.rst index edb9ee1..d81a58f 100644 --- a/doc/define_snippet.rst +++ b/doc/define_snippet.rst @@ -748,3 +748,29 @@ is not. Here's an snippet for rst title: .. [1] With some minor change, mainly for fixing some trivial bugs. .. [2] This is done when you call ``yas/initialize``. .. [3] Of course, this can be customized. + +Indenting +--------- + +Many people miss the indenting feature of smart-snippet: when you +place a ``$>`` in your snippet, an ``(indent-according-to-mode)`` will +be executed there to indent the line. So you'll not need to hard-code +the indenting in the snippet template, and it will be very convenient +when you need to work with several different project where coding +styles are different. + +The reason why this feature wasn't added to YASnippet until after +0.5.6 is that it doesn't work well for all modes. In some cases +(e.g. python-mode), calling ``indent-according-to-mode`` will break +the overlays created by YASnippet. + +However, since many people asked for this feature, I finally added +this to YASnippet. Here's an example of the usage: + +.. sourcecode:: text + + for (${int i = 0}; ${i < 10}; ${++i}) + {$> + $0$> + }$> + diff --git a/yasnippet.el b/yasnippet.el index c691f5b..e3d51e4 100644 --- a/yasnippet.el +++ b/yasnippet.el @@ -797,7 +797,14 @@ will be deleted before inserting template." (yas/group-primary-field (car groups))))) ;; no need to call exit-snippet, since no overlay created. - (yas/exit-snippet snippet))))))) + (yas/exit-snippet snippet))) + + ;; Step 16: Do necessary indenting + (save-excursion + (goto-char (overlay-start (yas/snippet-overlay snippet))) + (while (re-search-forward "$>" nil t) + (replace-match "") + (indent-according-to-mode))))))) (defun yas/current-snippet-overlay (&optional point) "Get the most proper overlay which is belongs to a snippet."