Friday, December 16, 2011

sh (bash) syntax for here-document strings: embedding other languages

Hello

I often use here-documents in the shell scripts (lovely language, by the
way) I write, and it so happens that the here-document is almost always
a script meant for some (other) interpreter. Most often than not,
though, the here-document is actually a (nested) shell script !

For those not familiar with the notion, here-documents are a nice way to
include (or embed) a fragment of (generic) text within a script, for
whatever usage the scrip author sees fit. With the sh language, the
here-document is always used as input to a command or program. Within
other languages, it may be just a string literal (a string constant).

The problem is the sh syntax highlighting in vim will always highlight
the here-document as a plain string. If the here-doc is a script it can
easily get to be quite long and having no highlighting for it can be so
inconvenient !

I often feel like splitting my script into many small scripts just to
get the highlighting.

Is there please a way to embed syntax highlighting from other languages
into shell here-documents ?

For example in HTML the syntax many languages can be included: CSS,
JavaScript, ASP, Python, Perl... But in HTML every element to include
scripts is properly marked with the language being used, or there is a
well-known default language otherwise.

In a shell here-document there is no indication of the script language
for the embedded document, and there might actually be no language for
the document. But if I could include something like a modline in the
heredoc, to specify a syntax to be used, could syntax/sh.vim then
include the syntax for the specified language and use it for the
here-document ?

For example, if I want to check in my script that php currently has the
memcache extension and can connect to a server I can write

php <<-"php_script_delimiter"
<?php
/* vi:syntax=php */
$memcache = new Memcache();
if ($memcache)
if ($memcache->connect("server_name", 11211))
exit(0);
else
exit(1);
else
exit(2);
?>
php_script_delimiter

Is there a way than to use the syntax highlighting commands (probably
regmatch) and find the modline-like line embedded there (with
"vi:syntax=php"), and load the php syntax for the here-doc region ?

For nested shell scripts (the majority of my here-documents) I found a
quick-and-dirty trick to circumvent sh syntax highlighting: use sh
line-spliting within the here-doc delimiter, like this

ssh webserver<<-true\
"_script_delimiter"
# here the delimiter shall be "true_script_delimiter"
# but the default vim sh syntax highlighting will
# (incorrectly) see just "true\" as the delimiter

true\
# vim here-document syntax highlighting stops here
# sh will really execute the `true´ command here

# embedded remote shell script (for ssh) follows
if test -d "/var/www/html/htdocs"
then
echo "Found htdocs..."
# plain sh syntax here ...
fi
...

#
#embedded script really ends here
true_script_delimiter

echo "webserver ssh session completed..."
echo "Main script resumes."


While I am happy this works for me, it is only a trick, it works only
with sh syntax and it would still be nice if I could start my
embedded script with a
# vi:syntax=sh
line and get things done right ... ! :)

Thank you,
Timothy Madden

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments: