Giter Site home page Giter Site logo

mineo / yatemplate Goto Github PK

View Code? Open in Web Editor NEW
68.0 3.0 11.0 69 KB

File templates for Emacs with YASnippet

Home Page: https://github.com/mineo/yatemplate

License: GNU General Public License v3.0

Emacs Lisp 99.12% Makefile 0.88%
emacs-lisp yasnippet melpa template

yatemplate's Introduction

MELPA MELPA Stable

YATemplate

Installation

Install this package from MELPA with M-x package-install RET yatemplate RET.

Description

This package bridges the gap between YASnippet and auto-insert-mode. By populating auto-insert-alist with filenames and automatically expanding their content after insertion by auto-insert-mode, it's an easy way to create dynamic file templates. Simply call yatemplate-fill-alist somewhere in your Emacs initialization file to populate auto-insert-alist with filenames from yatemplate-dir.

Each filename will be turned into a new element to push onto auto-insert-alist. To guarantee a particular order, filenames must contain one colon (":"). After collecting all the filenames in yatemplate-dir, their names will be sorted with string<, then split on the colon. The first substring will be discarded, which means it can be used to establish an ordering. The second substring will be used as a regexp as the CONDITION of the element to push onto auto-insert-alist. The ACTION will be a vector of actions that first insert the content of the template file and then expand the content of the buffer with yatemplate-expand-yas-buffer, which simply calls yas-expand-snippet, so you can use everything YASnippet offers in the template.

Note that a dollar sign $ will be added to the end of the regular expression automatically because most of the template filenames will very likely be of the form filename.extension. If you want to specify a template filename where the last letters are not the extension, add .* at the end.

This means that if yatemplate-dir looks like this:

.emacs.d/templates
├── 00:test_.*.py
└── 01:.*.py

yatemplate-fill-alist will first push (".*.py$" . ACTION) onto auto-insert-alist and then ("test_.*.py$" . ACTION).

Of course, you will need to enable auto-insert-mode to have the snippet inserted and expanded into new files.

yatemplate's People

Contributors

conao3 avatar emayej avatar mineo avatar mjgpy3 avatar rubikitch avatar syohex avatar terlar avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

yatemplate's Issues

How to keep tab for Makefile

When evaluating a template file for Makefile, the tab before each command is gone.

The template:

help:
	@echo "help"

The result:

help:
@echo "help"

Tried to add below code to the head of the template but that doesn't work.
template:

# expand-env: ((yas-indent-line 'fixed))
# --
help:
	@echo "help"

result:

# expand-env: ((yas-indent-line 'fixed))
# --
help:
@echo "help"

Is there any way we could keep the tab?

autoload cookies for safe local variables

This block of code could benefit from an autoload cookie so that the variables are considered safe before yatemplate is loaded.

  (put 'yatemplate-owner 'safe-local-variable #'stringp)
  (put 'yatemplate-license 'safe-local-variable #'stringp)

Multiple templates not distinguishable by regex

Hi,

Sorry it's more of a question than an issue, but I could not find any info in the docs & the web.

I have 2 templates for .tex files: one for scientific articles, one for beamer presentation.

The thing is I do not have a pattern to name my .tex files according to the content (article/beamer), so I cannot differentiate the template insertion based on the file name.

So my question : is there a mean to ask the user which template to insert, when a new .tex is created. Or any other solution?

not possible to create templates in windows

It is not possible AFAICT to create files with names such as 50:*.el on Windows 7.

Can you please support characters that are supported? Or allow the regex patterns to be defined by variables that refer to simple named files.

support simpler filename convention?

Supporting full regexes is great and all, but having config files with special characters is really stressing out my config-sync scripts 😄

Can you think of a simpler way of defining the templates? Maybe the filename only gives a suffix (string, not regex) match, unless the name contains the character * (in which case its interpreted as a regex)?

Support templates by major-mode

auto-insert-mode can define insertions by major mode (in addition to regex matching the filename). It would be great if yatemplate also supported this. Perhaps something-like:

00:major-mode:c++-mode

To define a snippet for c++-mode.

yatemplate searches under non-template directory

(sort (file-expand-wildcards (concat yatemplate-dir "**/*")) 'string<))

This searches ~/.emacs.d/templatesabc/*, ~/.emacs.d/templatesxyz/* along with ~/.emacs.d/templates/*, because yatemplate-dir is ~/.emacs.d/templates

We could use

(sort (file-expand-wildcards (concat (file-name-as-directory yatemplate-dir) "*")) 'string<)

to avoid that.

We can add another search for "**/*" if we want to support files under sub-directory of yatemplate-dir

Find new way to store regular expressions

Currently regular expressions are stored in filename. this seems to be not working well on some os (yes, windows.). May there be a new way considered to store regular expressions? Maybe in the snippet file? I currently have changed "yatemplate-regex-from-filename" to this:

;;;###autoload
(defun yatemplate-fill-alist ()
  "Fill `auto-insert-alist'."
  (yatemplate-remove-old-yatemplates-from-alist)
  (dolist (filename (reverse (yatemplate-sorted-files-in-dir)) nil)
    (let ((file-regex (yatemplate-regex-from-filename filename)))
      (if file-regex
          (push `(,(concat ".*" file-regex) . [,filename yatemplate-expand-yas-buffer]) ;; <----- here is change
                auto-insert-alist)))))

so i can use it too... of course now i lack some expressivness when defining which files to define a template for.

template insert twice

Hi,
Sorry, i'am a newer to yasnippet, when i set the yatemplate, and add a template like this:
00:.*.clj
and its content is
(ns)
when i create a file test.clj
image
found, it insert the content twice.
it's there anything i setted

support lazy loading of yasnippet

thanks for this! I had written something similar myself, but I'm moving over to use your library.

One thing I'd like is to be able to load yasnippet lazily (it's a really heavy package because of all the snippets that need to be parsed), but it seems that filling the alist will force the load of yasnippet.

Is there any chance you could please make your use of yasnippet lazy loading (i.e. no require) and/or assume its loaded already? (you might still need to load some parts of yasnippet to be able to do the alist filling)

Tests prevent byte-compilation

The file test-yatemplate.el currently (require)-s buttercup and shut-up. Neither of these are dependencies of the project, so when yatemplate is installed from melpa, they should not be on the system.

As a result, byte compilation for yatemplate will fail, and this failure will continuously nag every time the user byte compiles their init files.

I believe there was a way to mark files as "do not byte compile" or somesuch, could something like that be done (since nobody really wants to byte compile a test file for a package anyway)?

don't insist on one colon in filenames

it is not possible to have colon in filenames in Windows, which means this breaks if the template is in the user's home directory (which gets contracted to ~ instead of C: or whatever drive it is.).

Workaround is to put the files on another drive, which is a bit awkward.

define some convention variables

it would be good if the following could be included as good practice variables for people to use

(defvar-local yatemplate-owner user-full-name
  "The copyright owner for the buffer.
Particularly useful when combined with `dir-locals.el'.")

(defvar-local yatemplate-license "http://www.gnu.org/licenses/gpl.html"
  "The license (usually a URL) for the buffer.
It is always better to explicitly list the license per file than
to refer to the LICENSE file. Particularly useful when combined
with `dir-locals.el'.")

templates with name XX:*.html not working on Mac OS X

Hi,

Just struggled to figure out why my template wasn't working, and I found that they do not work if the file buffer part begins directly with a *. This tripped me up as I expected it to work. See terminal screenshot below (I restarted Emacs after renaming the file, and opened a new .html file to test it.)

Screen Shot 2021-06-22 at 10 39 07 AM

I'm on Mac OS X 11.4. GNU Emacs 27.2 (build 1, x86_64-apple-darwin18.7.0, Carbon Version 158 AppKit 1671.6)
of 2021-03-27

I can workaround by not using * and just having the extension, eg. "00:.html" but I wonder if it should be documented if this is by design, as it might trip up others as well.

Brakes in cases where `buffer-file-name' is nil

I am not entirely sure why buffer-file-name would be nil, but I have found it consistently breaking the find-file-hook for files using nov-mode. There might be other cases where this happen.

Here is a minimal reproducible setup:

(require 'package)
(setq package-archives
      '(("melpa"    . "https://melpa.org/packages/")
	("gnu" . "http://elpa.gnu.org/packages/")))
(package-initialize)
(package-refresh-contents)

(package-install 'use-package)
(setq use-package-always-ensure t)

(require 'autoinsert)
(auto-insert-mode 1)

(use-package nov :mode ("\\.epub$" . nov-mode))
(use-package yasnippet :hook (after-init . yas-global-mode))
(use-package yatemplate :hook (yas-global-mode . yatemplate-fill-alist))

After that open an epub-file and the following error will be yielded:

Debugger entered--Lisp error: (wrong-type-argument stringp nil)
  find-file-name-handler(nil file-in-directory-p)
  file-in-directory-p(nil "~/.emacs.d/templates")
  yatemplate--find-file-hook()
  run-hooks(find-file-hook)
  after-find-file(nil t)
  find-file-noselect-1(#<buffer to.epub<devops>> "/path/to.epub" nil nil "/path/to.epub" (15755148 65026))
  find-file-noselect("/path/to.epub" nil nil t)
  find-file("/path/to.epub" t)
  funcall-interactively(find-file "/path/to.epub" t)
  call-interactively(find-file nil nil)
  command-execute(find-file)

This is due to the buffer-file-name being nil and find-file-name-handler not supporting nil as an argument. I am not sure why it would be nil since there is a file, but it must somehow run the hook before the buffer-file-name is set up?

Can you expand on the documentation?

Thanks for writing this emacs extension! It's pretty awesome!

But I really don't understand how to use it. For example, I'm not sure how to create a file in yatemplate-dir that is used whenever I open an empty file in web-mode, or if the file ends in "php" or "html".

Can you give a more elaborate code example that shows one how to do something like this?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.