Optional lines in Ultisnips using parameters - vim

I am trying to have some additional lines inserted in snippets based on a parameter. I am not sure how design such snippet.
snippet 'mysnip' 'snippets with optional lines'
This snippet line1 is inserted by default
<This line1a should be inserted if parameter1 is true>
This snippet line2 is inserted by default
<This line2a should be inserted if parameter1 is true>
endsnippet

It is not very clear to me how/where you want to enter your parameters.
One option is to define two snippets, one called mysnip and the other one mysnip1 - in this case you pass the parameter in the snippet name, and the definition of these two snippets should be straightforward.
Another option is to just define one snippet mysnip, and pass the parameter somewhere within this snippet. A working example could look like this:
snippet mysnip1
${1:Change this snippet line to have the text "True" (without quotes).}
This line is always present. `!p
if t[1]=="True":
snip += "A line displayed when $1 has the text True.
`
endsnippet

You can fake this using regular expression triggers. It only works if you do not want to have tabstops in your optional arguments though:
snippet /mysnip([a-z]*)/ "Optionals" r
this is always here!`!p
if "a" in match.group(1):
snip += "only when a"
if "b" in match.group(1):
snip += "only when b"`
endsnippet
If you type mysnip it will just be the first line, mysnipb the first and thirdm and mysnipab will be all of it.

Can't you put your optional lines in a variable that your snippet engine will expand?
In case it doesn't join automatically empty lines produced from a empty variable, you may have to have your variable contain a newline character and put it or the line before/after.

Related

May I know how to create / generate XML using elementtree

...
para1= ET.SubElement(root,'para')
anchortag=ET.SubElement(para1,'anchor')
anchortag.set()
para1.text= " sometexthere"
I tried with the above code snippet,but couldn't get the expected output.I don't want to create a new para tag that would take the text to newline.
result of above code :
<para> sometexthere<anchor> xyxyy</anchor></para>
Expected code
<para><anchor> xyxyy</anchor> sometexthere </para>
You have to make a couple of changes, and it should work:
First, change
anchortag.set()
to
anchortag.text='xyxy'
and then, given that you want sometexthere to be at the tail end of the element,
change
para1.text= " sometexthere"
to
para1.tail= "sometexthere"

Using REGEX to grab the information after the match

I ran a PDF through a series of processes to extra the text from it. I was successful in that regard. However, now I want to extract specific text from documents.
The document is set up as a multi lined string (I believe. when I paste it into Word the paragraph character is at the end of each line):
Send Unit: COMPLETE
NOA Selection: 20-0429.07
#for some reason, in this editor, despite the next line having > infront of it, the following line (Pni/Trk) keeps wrapping up to the line above. This doesn't exist in the actual doc.
Pni/Trk: 3 Panel / 3 Track
Panel Stack: STD
Width: 142.0000
The information is want to extract are the numbers following "NOA Selection:".
I know I can do a regex something to the effect of:
pattern = re.compile(r'NOA\sSelection:\s\d*-\d*\.\d*)
but I only want the numbers after the NOA selection, especially because NOA Selection will always be the same but the format of the numbers/letters/./-/etc. can vary pretty wildly. This looked promising but it is in Java and I haven't had much luck recreating it in Python.
I think I need to use (?<=...), but haven't been able to implement it.
Also, several of the examples show the string stored in the python file as a variable, but I'm trying to access it from a .txt file, so I might be going wrong there. This is what I have so far.
with open('export1.txt', 'r') as d:    
contents = d.read()    
p = re.compile('(?<=NOA)')
s = re.search(p, contents)
print(s.group())
Thank you for any help you can provide.
With your shown samples, you could try following too. For sample 20-0429.07 I have kept .07 part optional in regex in case you have values 20-0429 only it should work for those also.
import re
val = """Send Unit: COMPLETE
NOA Selection: 20-0429.07"""
matches = re.findall(r'NOA\s+Selection:\s+(\d+-\d+(?:\.\d+)?)', val)
print(matches)
['20-0429.07']
Explanation: Adding detailed explanation(only for explanation purposes).
NOA\s+Selection:\s+ ##matching NOA spaces(1 or more occurrences) Selection: spaces(1 or more occurrences)
(\d+-\d+(?:\.\d+)?) ##Creating capturing group matching(1 or more occurrences) digits-digits(1 or more occurrences)
##and in a non-capturing group matching dot followed by digits keeping it optional.
Keeping it simple, you could use re.findall here:
inp = """Send Unit: COMPLETE
NOA Selection: 20-0429.07"""
matches = re.findall(r'\bNOA Selection: (\S+)', inp)
print(matches) # ['20-0429.07']

Makefile variable expansion syntax

Please explain how does this TARGET get expanded?
PROJECT=helloworld
TARGET = $(PROJECT:=.so)
For example, I have generated helloworld.so.1.2.3, helloworld.so.1 and helloworld.so
This would get messy in the comments, reiterating the description from docs with this example. For:
PROJECT=helloworld
TARGET = $(PROJECT:=.so)
Value of $(TARGET) would be helloworld.so. As an empty string '' at the end of the word would gets replaced with .so. This "end of a word" is the difference from:
TARGET = $(PROJECT).so
With PROJECT being helloworld, the resulting value would look the same, but call make PROJECT="hello world" and in case of the former you end up with hello.so and world.so in TARGET, for the latter, it would be "hello world.so". Or to quote the docs:
$(var:a=b)’ ... replace every a at the end of a word with b ... “at the end of a word”, we mean that a must appear either followed by whitespace or at the end of the value in order to be replaced

how to search on the folded range in gvim?

Here is the material to test the search function:
move cursor in the line 5
zfG to fold line 5 until the end of the file. (please see the
attachment1)
When I input ?case, I can get the attachment2
Now, let me add a line which contains only one word case in the first line, and fold line 6 until the end of the file too. When I input ?case, I can only get the line 1, can not search line7?
How can I search on the folded lines?
The following is the raw material to test:
Note the use of ( ) with the pipe symbol to specify the 'or' condition
/[0-9]*/MATches if there are zero or more numbers in the line
/^[^#]/ Matches if the first character is not a # in the line
Notes:
1. Regular expressions are case sensitive
2. Regular expressions are to be used where pattern is specifiedYou can jump back to beginning of file by typing any one of the following command

Add a number of '=' in a rest (reStructuredText) document that equals to characters from last line?

I want to use a shortcut to add needed = (from Section/Title reStructuredText syntax) according to the last line.
So, suppose (being | the cursor position)
Title
|
and pressing an specific mapping mapped to a function, add a number of = that equals to the last line (where Title is), becoming:
Title
=====|
This sequence will get you close:
kyyp:.s/./=/g
Duplicate the previous line, then in that line, change every character to an equals sign. Map that to a key sequence you like, and try it out.
Another way:
:execute "normal " . strlen(getline(line(".") - 1)) . "i="
strlen(getline(line(".") - 1)) returns the lenght of the line above the current position. The result is that the command Ni= is executed, inserting = N times.
For a mapping I would have used:
put=repeat('=', col('$')-1)
For something more interactive, I would have use the same solution as Ned's.
(I don't like my mappings to change the various registers like #" or #/)
My vim-rst-sections vim plugin will convert lines to section headings:
http://www.vim.org/scripts/script.php?script_id=4486
In your case, you'd put the cursor on the line, and type <leader><leader>d to get a top-level heading like this:
#####
Title
#####
A few repeats of <leader><leader>d will take you down to the standard hierarchy of Python ReST sections to the =.

Resources