Why does (indent-region …) insert a 't' into my buffer?

2017-03-20 23:39:38

I'm using this yasnippet:

# -*- mode: snippet -*-

# name : case : {...}

# key: case

# --

`(indent-region (- (point) 20) (+ (point) 20) nil)`case ${2:constexpr}: ${3:\{}

$0

break;

${3:$(if (string-match "\{" yas-text) "\}" "")}

I think it's some default, but I'm not sure. Until recently, this snippet worked fine, but now I get a t inserted into my buffer, and I think it comes from (indent-region):

break;

t case constexpr: {

break;

}

case

How do I get rid of this t?

The meaning of backquotes in a snippet is to insert the value of the form within the backquotes into the snippet expansion. However, indent-region works by side-effect, and in recent Emacs versions happens to return t. As politza suggested in the comments, you can use (progn (indent-region ...) nil) to drop the return value.

However, this still performs a side effect inside backquotes which will stop being supported in some future version (it triggers

  • The meaning of backquotes in a snippet is to insert the value of the form within the backquotes into the snippet expansion. However, indent-region works by side-effect, and in recent Emacs versions happens to return t. As politza suggested in the comments, you can use (progn (indent-region ...) nil) to drop the return value.

    However, this still performs a side effect inside backquotes which will stop being supported in some future version (it triggers a warning in versions 0.11 and higher). In this case, the indentation effect can be written much clearer by binding yas-also-auto-indent-first-line:

    # -*- mode: snippet -*-

    # name : case : {...}

    # key: case

    # expand-env: ((yas-also-auto-indent-first-line t))

    # --

    case ${2:constexpr}:${3: \{}

    $0

    break;

    ${3:$(if (string-match "\{" yas-text) "\}" "")}

    This modification has just been accepted at https://github.com/AndreaCrotti/yasnippet-snippets/pull/176, and will come with the next yasnippet version (0.12), at least for MELP

    2017-03-21 00:40:34