Skip to content

jpkotta/auto-yasnippet

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 

Repository files navigation

Main idea

This is a hybrid of keyboard macro and yasnippet. You create the snippet on the go, usually to be used just in the one place. It's fast, because you're not leaving the current buffer, and all you do is enter the code you'd enter anyway, just placing "~" where you'd like yasnippet fields and mirrors to be.

Functions

aya-create

Removes "~" from current line or region (if mark is active) yielding valid code. The created snippet is recorded into `aya-current'.

aya-expand

Expands whatever is currently in `aya-current'

aya-open-line

Generic expansion function. It will either expand or move to the next field depending on the context.

Setup

  1. Download yasnippet from https://github.com/capitaomorte/yasnippet and set it up.
  2. Put `auto-yasnippet.el' into your elisp folder.
  3. In your .emacs file:
     (require 'auto-yasnippet)
     (global-set-key (kbd "H-w") 'aya-create)
     (global-set-key (kbd "H-y") 'aya-expand)

Usage examples

JavaScript

     field~1 = document.getElementById("field~1");
     // Since this just one line,
     // just call `aya-create' (from anywhere on this line).
     // The ~ chars disappear, yielding valid code.
     // `aya-current' becomes:
     // "field$1 = document.getElementById(\"field$1\");"
     // Now by calling `aya-expand' multiple times, you get:

     field1 = document.getElementById("field1");
     field2 = document.getElementById("field2");
     field3 = document.getElementById("field3");
     fieldFinal = document.getElementById("fieldFinal");

Java

     class Light~On implements Runnable {
       public Light~On() {}
       public void run() {
         System.out.println("Turning ~on lights");
         light = ~true;
       }
     }
     // This differs from the code that you wanted to write only by 4 ~ chars.
     // Since it's more than one line, select the region and call `aya-create'.
     // Again, the ~ chars disappear, yielding valid code.
     // `aya-current' becomes:
     // "class Light$1 implements Runnable {
     //   public Light$1() {}
     //   public void run() {
     //     System.out.println(\"Turning $2 lights\");
     //     light = $3;
     //   }
     // }"

     // Now by calling `aya-expand', you can quickly fill in:

     class LightOff implements Runnable {
       public LightOff() {}
       public void run() {
         System.out.println("Turning off lights");
         light = false;
       }
     }

C++

    const Point<3> curl(grad[~2][~1] - grad[~1][~2],

Select the region between the paren and the comma and call `aya-create'. You can easily obtain the final code:

    const Point<3> curl(grad[2][1] - grad[1][2],
                        grad[0][2] - grad[2][0],
                        grad[1][0] - grad[0][1]);

Note how annoying it would be to triple check that the indices match. Now you just have to check for one line.

JavaScript - aya-one-line:

     // `aya-one-line' works as a combination of `aya-create' and `aya-expand'
     // for one-line snippets. It's invoked by `aya-create' in case
     // there's no `aya-marker' (default ~) on the line, but there's
     // `aya-marker-one-line' (default $). Or you can invoke it on its own.
     field$ = document.getElementById("");
     // call `aya-create' and the rest is as before:

     field1 = document.getElementById("field1");
     field2 = document.getElementById("field2");
     field3 = document.getElementById("field3");
     fieldFinal = document.getElementById("fieldFinal");

Generating comments

Here's a yasnippet that makes use of aya-tab-position. You need to call aya-open-line if you want to use it.

# -*- mode: snippet -*-
# name: short comment
# key: sc
# --
//———$1${1:$(make-string (- 47 aya-tab-position (length yas-text)) ?—)}$0

Comments generated with this will always end in same column position, no matter from which indentation level they were invoked from.

About

quickly create disposable yasnippets

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Emacs Lisp 100.0%