Vim Macros for Editing DocBook Documents
DocBook is a vast markup language offering many tags, and most projects use only a subset of the available tags. The bindings above were developed for marking up LJ articles. Your use of DocBook probably will focus on a different set of tags, so you probably need to change the templates and key bindings.
The file tagtmps.vim contains the templates inserted by the <F2> key. Listing 2 shows the template for the article tag.
Listing 2. Article Tag Template
let g:Template_article ="<?xml version=\"1.0\" encoding=\"ISO-8859-1\" standalone=\"no\"?><CR>" \."<!DOCTYPE article SYSTEM \"docbookx.dtd\"><CR><CR>" \."<article><CR><CR>" \."<simplesect><title/><CR>" \."<para><CR>-:-<CR>" \."</para><CR>" \."</simplesect><CR><CR>" \."<simplesect><title></title><CR>" \."<para><CR>" \."</para><CR>" \."</simplesect><CR><CR>" \."</article>"
All templates are stored in global string variables whose names start with Template_ and end with the tag to which the template corresponds. The g: prefix in the variable name makes it a global variable. Long strings can be placed on multiple lines by prefixing the second and subsequent lines with a backslash (\) and using the dot (.) concatenation operator to append the lines together. Each string piece is contained within double quotes. Double-quoted strings understand the usual C escape sequences. You can modify the existing templates or add new ones for other tags by following the described naming sequence. Within a template the sequence -:- is used to specify where you want the cursor to be placed after the template is inserted. The macro automatically removes this string after inserting the macro.
If you want to change the function keys used to execute the macros, modify the following lines in the file mfuncs.vim:
let s:InsertTagTemplateKey = "<F2>" let s:InsertSymbolKey = "<F3>" let s:InsertForeignCharKey = "<F4>" let s:InsertStartTagKey = "<F5>" let s:InsertEndTagKey = "<F6>" let s:TagWordKey = "<F7>" let s:TagRangeKey = "<F7>" let s:ChangeTagKey = "<F8>"
If you want to change the tag-keys, symbol-keys or foreign-char-keys that follow the functions keys or change the tags associated with the keys, change the corresponding lines in maps.vim. For example, to change b so it is associated with the tag <book> rather than the tag <emphasis role="bold">, look for the following line in maps.vim:
call MapTagKey("b", 0, 0, "emphasis", " role=\"bold\"")
and change it to:
call MapTagKey("b", 1, 1, "book", "")
The function MapTagKey is defined in mfuncs.vim. Its prototype is:
function! MapTagKey(key, snewline, enewline, tag, stagx)
Its parameters are explained in Table 5.
Table 5. MapTagKey Parameters
|key||Keystroke(s) to bind the tag key to (for example, <F2>key).|
|snewline||One if the start tag should be placed on a new line, zero otherwise.|
|enewline||One if the end tag should be placed on a new line, zero otherwise.|
|tag||The tag associated with the key.|
|stagx||Extra attributes that should be placed in the start tag.|
The function MapTagKey merely sets up and executes a number of Vim nmap and imap commands to make the appropriate key bindings. Similarily, symbol-keys and foreign-char-keys are mapped by the functions MapSymbolKey and MapForeignCharKey. These functions each take two arguments, the key and the text to insert. For example:
call MapSymbolKey("3", "¾") call MapForeignCharKey("b", "β")
Near the bottom of the file maps.vim are a handful of Vim nmap commands for binding the tag manipluation and movement keys, including delete tag, change tag and move tag. All of these bindings call the functions defined in the file tfuncs.vim.
nmap <S-F8> :call DeleteTag()^M " Delete tag at cursor. nmap <F9> :call CursorLeftByTag()^M " Move left by tags. nmap <F10> :call MoveTagLeft()^M " Move tag left of preceding word. nmap <F11> :call MoveTagRight()^M " Move tag right of following word. nmap <F12> :call CursorRightByTag()^M " Move right by tags. nmap <S-F9> :call TightenTagLeft()^M " Delete whitespace left of tag. nmap <S-F10> :call InsertStringLeftOfTag(" ")^M " Insert space to the left of tag. nmap <S-F11> :call InsertStringRightOfTag(" ")^M " Insert space to the right of tag. nmap <S-F12> :call TightenTagRight()^M " Delete whitespace right of tag.
If you cat this file, these lines are going to look strange, and in some editors, all the lines are going to break right after the closing parentheses in the call. If you look at it with vim, you can see that the closing parentheses are followed by ^M, a carriage return. When you cat the file, this causes part of the line to be erased. Some editors cause a line break here. These mappings work in command mode and the ^M ends the command.
Mitch Frazier is an Associate Editor for Linux Journal.
Fast/Flexible Linux OS Recovery
On Demand Now
In this live one-hour webinar, learn how to enhance your existing backup strategies for complete disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible full-system recovery solution for UNIX and Linux systems.
Join Linux Journal's Shawn Powers and David Huffman, President/CEO, Storix, Inc.
Free to Linux Journal readers.Register Now!
- Peppermint 7 Released
- Download "Linux Management with Red Hat Satellite: Measuring Business Impact and ROI"
- Sony Settles in Linux Battle
- Libarchive Security Flaw Discovered
- Using and Writing Java Servlets
- Create User Interfaces with Glade
- Integrating a Linux Cluster into a Production High-Performance Computing Environment
- Maru OS Brings Debian to Your Phone
- Understanding Ceph and Its Place in the Market
- Profiles and RC Files
With all the industry talk about the benefits of Linux on Power and all the performance advantages offered by its open architecture, you may be considering a move in that direction. If you are thinking about analytics, big data and cloud computing, you would be right to evaluate Power. The idea of using commodity x86 hardware and replacing it every three years is an outdated cost model. It doesn’t consider the total cost of ownership, and it doesn’t consider the advantage of real processing power, high-availability and multithreading like a demon.
This ebook takes a look at some of the practical applications of the Linux on Power platform and ways you might bring all the performance power of this open architecture to bear for your organization. There are no smoke and mirrors here—just hard, cold, empirical evidence provided by independent sources. I also consider some innovative ways Linux on Power will be used in the future.Get the Guide