perlmeditation
gmax
<p> In a [id://269887|recent post], it was discussed the possibility of having offline previewing for PM nodes.</p>
<p>While everybody agrees that no such thing is possible, I would like to share my personal method for writing posts.</p>
<p>It is based on <a href="http://www.vim.org">Vim</a>, my editor of choice. </p>
<p>Basically, the method consists in creating a new syntax file, where to embed perl syntax into HTML syntax, and adding a few instructions to deal with PM links. Of course, being offline, it does not show the correctness of real links, but at least it can warn you if something is wrong.</p>
<readmore>
<h3>An example</h3>
<p>Here is how my screen looks like when I am writing a PM node. Notice the Perl code happily highlighted in full details, so that I have a minimum of syntax
checking even if the code is untested.</p>
<p>My apologies for the ones who are using the dark theme to view this page. I know that it does not show at its best. However, it is not supposed to be a mandatory color set, just an example.</p>
<table bgcolor="#0ffff"><tr><td>
<pre>
<font color="#008080"><</font><font color="#804040"><b>h3</b></font><font color="#008080">></font><font color="#ff00ff"><b>PM highlighting</b></font><font color="#008080"></</font><font color="#804040"><b>h3</b></font><font color="#008080">></font>
<font color="#008080"><</font><font color="#804040"><b>p</b></font><font color="#008080">></font>This is a chunk of normal text.<font color="#008080"></</font><font color="#804040"><b>p</b></font><font color="#008080">></font>
<font color="#008080"><code></font>
<font color="#0000ff"># This is (untested) Perl code</font>
<font color="#804040"><b>my</b></font> <font color="#008080">$x</font> = <font color="#ff00ff">0xDEADBEEF</font>;
<font color="#804040"><b>my</b></font> <font color="#008080">@many</font> = <font color="#804040"><b>map</b></font> { <font color="#008080">$_</font> * <font color="#008080">$_</font> } (<font color="#ff00ff">1</font> .. <font color="#008080">$x</font>);
</<font color="#008080">code</font>>
<font color="#008080"><</font><font color="#804040"><b>p</b></font><font color="#008080">></font> This is a
<font color="#008080"><</font><font color="#804040"><b>a</b></font><font color="#008080"> </font><font color="#2e8b57"><b>href</b></font><font color="#008080">=</font><font color="#ff00ff">"<A HREF="http://www.nowhere.net">http://www.nowhere.net</A>"</font><font color="#008080">></font><font color="#6a5acd"><u>normal link</u></font><font color="#008080"></</font><font color="#804040"><b>a</b></font><font color="#008080">></font> <font color="#008080"></</font><font color="#804040"><b>p</b></font><font color="#008080">></font>
<font color="#008080"><</font><font color="#804040"><b>p</b></font><font color="#008080">></font> This is a <font color="#6a5acd"><u>[id://123456|PerlMonks tag]</u></font>. <font color="#008080"></</font><font color="#804040"><b>p</b></font><font color="#008080">></font>
<font color="#008080"><</font><font color="#804040"><b>p</b></font><font color="#008080">></font> Notice how this unescaped $array_element<font color="#6a5acd"><u>[0]</u></font> is shown
immediately as a PM link, so that
<font color="#008080"><</font><font color="#804040"><b>i</b></font><font color="#008080">></font><i>it doesn't escape</i><font color="#008080"></</font><font color="#804040"><b>i</b></font><font color="#008080">></font> my attention <font color="#008080"></</font><font color="#804040"><b>p</b></font><font color="#008080">></font>
<font color="#008080"><</font><font color="#804040"><b>p</b></font><font color="#008080">></font> Instead, if I use <font color="#008080"><code></font><font color="#008080">$array</font>[<font color="#ff00ff">0</font>]</<font color="#008080">code</font>>
I know that I am on the right track ... <font color="#008080"></</font><font color="#804040"><b>p</b></font><font color="#008080">></font>
</pre>
</td></tr></table>
<p>This example was made with Vim's <i>TOhtml</i> function.</p>
<h3>How to do it</h3>
<p>In Unix systems, where you usually don't have access to Vim syntax files (normally found at /usr/share/vim/vimXX/syntax), you can create your own syntax files in your home directory.</p>
<ol>
<li>Create two directories "~/.vim" and "~/.vim/syntax".</li>
<li>in ".vim", add a file named filetype.vim, containing the following lines
<code>
" --- cut ----------------------
" my filetype file
if exists("did_load_filetypes")
finish
endif
augroup filetypedetect
au! BufRead,BufNewFile *.PM setfiletype PM
augroup END
" --- cut ----------------------
</code>
</li>
<li>In the directory "~/.vim/syntax", create a file PM.vim, containing
<code>
" ---- cut ---------------------
" Vim syntax file
" Language: PM
" Maintainer: noone
" Last Change: 2003 Jun 26
" Load the HTML syntax.
runtime! syntax/html.vim
" UPDATE - (Thanks to zakb)
unlet b:current_syntax
" add Perl extensions
:syntax include @Perl syntax/perl.vim
:syntax region htmlPerl start="< code >" end="< /code >" contains=@Perl keepend
" remove spaces around code tags !
let b:current_syntax = "html"
:syntax region PMlink start="\[" end ="\]"
if version >= 508 || !exists("did_PM_syn_init")
if version < 508
let did_PM_syn_init = 1
command -nargs=+ HiLink hi link <args>
else
command -nargs=+ HiLink hi def link <args>
endif
HiLink PMlink Underlined
delcommand HiLink
endif
let b:current_syntax = "PM"
" vim: ts=8
" --- cut ----------------------
</code>
Don't forget to <b>remove the spaces</b> that I added around the code tags in this example, to avoid clashes with the real code tags around the code.
</li>
<li>In Windows, unless you are using cygwin, you can't apply this recipe. However, usually you have access to the syntax files (c:\vim\vimXX\syntax\). Therefore you can modify directly html.vim and filetypes.vim (in the upper dir). In html.vim, add the perl extension towards the end, just before the "let b:current_syntax ..." line. In filetype.vim, add a line saying
<code>
au! BufRead,BufNewFile *.PM setfiletype PM
</code>
just close to any other similar line. In Windows, since filenames are not case sensitive, you may want to change *.PM into *.PMonks, to avoid confusion with *.pm, which are Perl modules.
</li>
<li>Now, whenever you create or open a file with extension .PM (uppercase, or .PMonks in Windows), you can enjoy the PerlMonks highlighting.</li>
</ol>
<p>This method has been tested on Linux (Mandrake and Debian) with Vim 6.1 and 6.2 and on Windows XP with Gvim 6.2 and Vim 6.2 under cygwin.</p>
<p><b>Update (1)</b><br>
The method depicted here is not something I have invented overnight. :) It is a standard procedure adopted for syntax files whenever you need to insert the syntax of a language inside another one. For example, C's syntax is embedded into C++'s, POD's into Perl's, and so on.<br>
However, you can use the ~/.vim/syntax directory to override the effects of Vim's syntax when you are not satisfied with the normal one. For example, I have an account on a Debian box, but I don't have root privileges. I am satisfied with the version of Vim (6.1) installed there, but I would like to have the updated syntax files that came with Vim 6.2. Therefore I copied my favorite syntax files into ~/.vim/syntax and I now have full control on them, since Vim searches for syntax files first in my home directory and then in /usr/share/vim.</p>
<h3>further reading</h3>
<p>For more on Vim with Perl, please refer to [TVSET]'s excellent [id://257782|article].</p>
<p><small><b>Note</b> I was not certain where to post this node. It is not [Code Catacombs|Code], nor does belong to [Cool Uses for Perl]. It is not even a [Perl Monks Discussion|Discussion], because I am not proposing something that will change the Monastery. It looks sort of off-topic-but-faintly-related to me, so I guess [Meditations] is OK.</small></p>
<p><b>Update (2) </b><br>
Thanks to [zakb] for the helpful hint. Adding the <i>unlet</i> statement makes it work in a system where it previously didn't. Fixed. </p>
<pre>
_ _ _ _
(_|| | |(_|><
_|
</pre>