Appendix C. Text Editors That Understand Mason

In order to write Mason components, you don’t need anything fancy. You can do just fine with any old ramshackle text editor — even (horrors!) a word processor that can save as plain text will do.

However, an editor that helps you manage the details of the document you’re creating can be a nice mini-luxury. Several different editors can easily be trained to understand the structure of Mason components, with varying degrees of assistance (or interference, depending on how much help you’re looking for). In most cases, this is due to the work of Mason developers who created syntax definitions and offered them to the general public. If you find shortcomings, the polite thing to do is to drop a note to the developer, offering a fix if possible.

Emacs

Both authors of this book are enthusiastic users of Emacs and its wonderful cperl-mode for editing plain Perl code, courtesy of Ilya Zakharevich. However, while one can just turn cperl-mode on for an entire Mason component and get some basic syntax highlighting, brace matching, and indentation, the result is necessarily clumsy, since large parts of the component aren’t Perl code.

Michael Abraham Shulman, a Mason contributor from way back, developed a much nicer solution. His Multiple-Major-Mode Mode (mmm-mode) is a way to combine cperl-mode and html-mode (or sgml-mode, if you prefer), each in the correct region of the component. Of course, like a true hubristic programmer, Michael addressed the larger problem: mmm-mode is actually a framework for allowing several major modes to coexist in a single buffer. Mason syntax definitions are provided, but there are also definitions for embedding JavaScript in HTML documents, embedding HTML code in Perl here-docs, embedding Perl in HTML using Embperl, and more.

Note that since this is a book on Mason and not a book on Emacs and Lisp, we’re just going to give you installation and configuration instructions as if they were magical incantations. If you know alternate ways to achieve the same ends, you’ll probably be fine using them.

To use mmm-mode’s mason-mode, you’ll need to download mmm-mode from http://mmm-mode.sourceforge.net/ and install the .el files as described in the README file.[28] Create a file called .emacs in your home directory if it doesn’t already exist, and add the statement (require 'mmm-auto) to it. Then you can manually invoke mason-mode while editing a component by first turning on html-mode (type Meta-x html-mode [29] and press Return) for the outer region, then turning on mason-mode for the Mason sections (type Meta-x mmm-ify-by-class, press Return, type mason, and press Return). mmm-mode will then scan your component to figure out which sections are which, turning on cperl-mode in the sections that contain Perl code.

If you want mason-mode to start up automatically whenever you edit files with a certain suffix, say .mas, you can add a few lines to your .emacs file after the (require 'mmm-auto) line, as in Example C-1.

Example C-1. mmm-mode configuration for Mason by extension
(require 'mmm-auto)
(setq mmm-global-mode 'maybe)
(add-to-list 'auto-mode-alist '("\\.mas\\'" . html-mode))
(mmm-add-mode-ext-class 'html-mode "\\.mas\\'" 'mason)

Alternately, if you’d like to enable mmm-mode for all HTML files in a certain directory, you could use the code in Example C-2.

Example C-2. mmm-mode configuration for Mason by directory
(require 'mmm-auto)
(setq mmm-global-mode 'maybe)
(add-to-list 'auto-mode-alist '("/usr/local/apache/htdocs" . sgml-mode))
(mmm-add-mode-ext-class 'sgml-mode "/usr/local/apache/htdocs" 'mason)

As of this writing, mmm-mode doesn’t scan your Mason component continually, so if you type <& &> in your component, the inside of that tag won’t be treated as a cperl-mode region until you rescan the file. You can rescan by typing Control-c % Control-b. If you know of a way to automatically keep the regions current while typing, be sure to drop a line to Michael. His email address is in the mmm-mode README file.

You can download Emacs from several different places on the Internet, but the original distribution is available at http://www.gnu.org/software/emacs/. Precompiled binaries for your system may also be available from other sources, greatly simplifying the installation process if you’re not using Unix.

Vim

Not to be outdone in the so-called editor wars, Vim has some pretty slick Mason syntax highlighting of its own. Vim is a widely used variant of the standard Unix editor vi. Vim stands for "vi improved,” and syntax highlighting is one of several major improvements offered by Vim.

The Mason syntax module is part of the standard distribution of Vim, so if you have a fairly modern version of Vim (Version 6.0 or later should be fine), you should already have all the necessary files. To invoke Mason highlighting while you’re editing a Mason component, you can enter the command :set syntax=mason. This will turn on Perl syntax for the Perl portions of the component and HTML syntax for the other parts.

If you want Mason syntax highlighting to start automatically when you edit Mason components, you have a few options. First, Vim will automatically turn on Mason highlighting when you begin editing a file that starts with the string <%args>. This is nice, because it both simplifies configuration and encourages you to standardize the structure of your components.

Another alternative is to configure Vim to invoke Mason highlighting when you begin editing files ending with a certain suffix like .mas. To do this, you can add the following directives to your own .vimrc file (usually located in your home directory):

au syntax mason so /usr/local/share/vim/vim60ax/syntax/mason.vim
au BufNewFile,BufRead *.mas set ft=mason

Alternatively, you could edit Vim’s filetype.vim file (usually located in /usr/local/vim60/share/vim/vim60/, though it depends on where Vim is installed on your system) to contain the following line:

au BufNewFile,BufRead *.mas setf mason

For more information on Vim configuration, consult the thorough Vim documentation. If you add something significant to the Vim Mason support, be sure to contact the maintainer of the Mason syntax file to see whether he’d be interested in integrating your changes into the main distribution.[30]



[28] Debian users can find an mmm-mode package in Woody and Sid.

[29] If you don’t know what the Meta key is, you can probably use your keyboard’s Escape key instead.

[30] The maintainer is currently Andrew Smith, .

Get Embedding Perl in HTML with Mason now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.