Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3630138
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T00:13:55+00:00 2026-05-19T00:13:55+00:00

I’m looking to write a yasnippet template that would allow me to add a

  • 0

I’m looking to write a yasnippet template that would allow me to add a license header to a script buffer in Emacs. Kinda like this, but a bit improved:

  1. The header needs to include per-user data, such as the date name and email of the copyright holder,
    which can be obtained with embedded elisp expansion from yasnippet.
  2. The header needs to be commented with a syntax depending on the programming mode the file is currently in. There’s already a gist of a snippet that does all that. Basically it amounts to embedding (comment-region (point-min) (point)) at the end of your snippet.
  3. Now, I want to change the comment-style to a box. See the emacs documentation for the comment-style variable, or, if you want to see what a box-style comment looks like, just call M-x comment-box on an active region: It calls comment-region with the right options.

A first approach to do that is to setup the style by modifying the end of the previous snippet to:

(let ((comment-style 'box))
            (comment-region (point-min) (point)))

Unfortunately, the indentation gets screwed up, and my box isn’t rectangular. If I start from snippet:

Copyright (c) ${1:`(nth 5 (decode-time))`}
All rights reserved.

Redistribution and use in source and binary forms, with or
without modification, are permitted`
      (let ((comment-style 'box))
            (comment-region (point-min) (point)))`

The expansion of that snippet “breaks the box” (I’m debugging this snippet with ocaml comment syntax, not that it should matter):

(**************************************************************)
(* Copyright (c) 2010                                    *)
(* All rights reserved.                                       *)
(*                                                            *)
(* Redistribution and use in source and binary forms, with or *)
(* without modification, are permitted     *)
(**************************************************************)
  • At first I thought the second line was indented based on the size of the pre-expansion embedded code, but in that case, it should make the final *) of that line come 25 spaces too soon, not 4.
  • If it indented based on no text being present at embedding point, the final *) should arrive 4 spaces too late, not too soon.
  • Finally, I don’t understand what’s going on with the last line, in which there is no embedded code expansion: Usually I don’t have any problem getting a square comment box from a paragraph with a short last line (either using comment-box, or the elisp function in the 1st comment-block of this question.

I tried making the comment happen after snippet expansion, to avoid any side-effect, by adding it to yas/after-exit-snippet-hook, replacing the last function of the snippet above with:

(add-hook 'yas/after-exit-snippet-hook
      (let ((comment-style 'box))
            (comment-region (point-min) (point))) nil t)

But that didn’t help. Even if it did, it would leave me with an expansion hook that would comment all the snippets I would want to use in that buffer, something I certainly do not want.

I should also add that I tried to set yas/indent-line to fixed, by adding

# expand-env: ((yas/indent-line 'fixed))

at the beginning of my snippet but that didn’t change anything. Any ideas on how to get a rectangular box ?


Edit : We have a very nice answer, along with a proposed fix, (kudos & thanks, Seiji !) but a question remains on how to adapt it to the case where one would want to reuse a field, say like the reuse of $1 in:

Copyright (c) ${1:`(nth 5 (decode-time))`}
All rights reserved.

Redistribution and use in $1, in source and binary forms
`(let ((comment-style 'box))
        (comment-region (point-min) (point-at-eol 0)))`

In that case, the template engine copies the (variable-length) value obtained for the field $1, namely 2011, to the last line, at template expansion (after indentation), giving a comment line 2 characters too wide. It becomes hard to predict when writing the template that one should remove 4 characters at this line. Perhaps field reuse and correct indentation are too much to ask for simultaneously. Does any one see a way to do this, though ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T00:13:56+00:00Added an answer on May 19, 2026 at 12:13 am

    Changing your snippet to this one fixes the last line.

    Copyright (c) ${1:`(nth 5 (decode-time))`}
    All rights reserved.
    
    Redistribution and use in source and binary forms, with or
    without modification, are permitted
    `(let ((comment-style 'box))(comment-region (point-min) (point-at-eol 0)))`
    

    Note that there is a line break between the license (“… are permitted”) and embedded emacs lisp code.

    As for the second line, I’ll first explain why this occurs and then offer an (ugly) fix. When your snippet is inserted into a file, embedded blocks of lisp are sequentially evaluated from the beginning of buffer to the end. What this means in your snippet is that (nth 5 (decode-time)) has been replaced by 2011 when the next block, (comment-region ...), is evaluated.

    As a result, comment-region sees the second line as

    Copyright (c) ${1:2011}
    

    So, comment-region puts enough white spaces for this string. Then, the template engine transforms ${1:2011} into 2011, and this transformation shortens the string by 5 characters. This explains the premature appearance of *) by 5 characters in the second line.

    One way to fix this situation is putting 5 white spaces back after comment-region is evaluated — Something along the line of:

    Copyright (c) ${1:`(nth 5 (decode-time))`} @
    All rights reserved.
    
    Redistribution and use in source and binary forms, with or
    without modification, are permitted.
    `(let ((comment-style 'box))(comment-region (point-min) (point-at-eol 0)))``
    (replace-string "@" "      " nil (point-min) (point))`$0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm looking for suggestions for debugging... If you view this site in Firefox or
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.