Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression as a trigger

One of the code conventions at work is to include a padding of one space inside square brackets and parenthesis. So list[ index ], not list[index]. Adding those spaces can be annoying, so I tried writing a snippet:

# name: Bracket
# key: [
# --
[ ${1:exp} ]

This works when the opening square bracket is preceded by a space or beginning of a line, but not when its preceded by an identifier. I think one way to do this would be to have the trigger be a regular expression:

# key: "[:ascii:]"[

Or something like that. Is this even possible? Or is there some other clever way to make this work? I'm writing this for python, but I think that shouldn't matter.

Thanks!

like image 383
Arturo E Avatar asked Jan 04 '13 00:01

Arturo E


People also ask

What is a regex trigger?

Regex triggers are a powerful alternative to regular ones, with a few extra features: They let you define triggers using a full-blown regex syntax, enabling use-cases that would be inconvenient or even impossible with regular triggers.

What does '$' mean in regex?

$ means "Match the end of the string" (the position after the last character in the string).

What is regex matching in GTM?

The "matches regex" option allows you to implement regular expressions in Google Tag Manager for matching text patterns. Regex is useful for expressing more complex types of rules such as "fire the tag when the URL starts with A and ends in B or C."


2 Answers

I don't know about YASnippet, but what you want can easily be achieved using autopair (which can be installed via the packaging system):

(defun autopair-space-after-bracket (action pair pos-before)
  (when (and (eq action 'opening)
             (eq pair ?\]))
    (insert "  ")
    (backward-char)))

(setq autopair-handle-action-fns (list #'autopair-default-handle-action
                                       #'autopair-space-after-bracket))

Using this, and starting from the following situation (| marks point position):

list|

then inserting [ yields:

list[ | ]

like image 88
François Févotte Avatar answered Oct 20 '22 09:10

François Févotte


Minor mode solution

I too have had to deal with such a coding standard. My initial solution was identical to @Francesco's based on autopair.

However, I actually wanted to remove this padding in some cases, so made this minor-mode to be more flexible. I definitely recommend using it along with some kind of paired delimiter insertion tool (my favorite is autopair).

See: delim-pad

Using yasnippet

Going down your original track of using yasnippet ... ( BTW autopair and yasnippet are both written by João Távora, both are very powerful and flexible. kudos to that guy! )

Even if you got "[" recognized as a key, you would still have to keep pressing the trigger key to expand the snippet. That can get tired pretty quickly.

Yasnippet also allows you to bind snippets to keys directly, so this will work:

# -*- mode: snippet -*-
# name: beginsquare
# binding: [
# --
[ $0 ]
like image 45
event_jr Avatar answered Oct 20 '22 09:10

event_jr