# HG changeset patch # User Richard Cheng # Date 1334583503 -3600 # Node ID a8dd2fa6d24c1838cc198740849e5cb529322313 # Parent dd723c5dadcd193c61ca2677cfa29d78d530d383# Parent ee7e65e9658e25af8affe00eb58d087e8e55dd4f Merge remote-tracking branch 'upstream/master' diff -r dd723c5dadcd -r a8dd2fa6d24c chapters/50.markdown --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/chapters/50.markdown Mon Apr 16 14:38:23 2012 +0100 @@ -0,0 +1,115 @@ +Section Movement Theory +======================= + +If you've never used Vim's section movement commands (`[[`, `]]`, `[]` and `][`) +take a second and read the help for them now. Go ahead and read `:help section` +as well. + +Confused yet? That's okay, so was I the first time I read that stuff. We're +going to take a quick detour from writing code to learn about how these +movements work, and then in the next chapter we'll make our Potion plugin +support them. + +Nroff Files +----------- + +The four "section movement" commands are conceptually meant to move around +between "sections" of a file. + +All of these commands are designed to work with [nroff files][] by default. +Nroff is a language like LaTeX or Markdown -- it's used to write text that will +be reformatted later (it's actually the format used by UNIX man pages). + +Nroff files use a certain set of "macros" to define "section headings". For +example, here's an excerpt from the `awk` man page: + + :::nroff + .SH NAME *** + awk \- pattern-directed scanning and processing language + .SH SYNOPSIS *** + .B awk + [ + .BI \-F + .I fs + ] + [ + .BI \-v + .I var=value + ] + [ + .I 'prog' + | + .BI \-f + .I progfile + ] + [ + .I file ... + ] + .SH DESCRIPTION *** + .I Awk + scans each input + .I file + for lines that match ... + +The lines starting with `.SH` are section headings. I've marked them with +`***`. The four section movement commands will move your cursor between these +section heading lines. + +Vim considers any line starting with `.` and one of the nroff heading macros to +be a section header, *even when you're not editing an nroff file*! + +You can change the macros by changing the `sections` setting, but Vim still +requires a period at the beginning of the line, and the macros must be pairs of +characters, so that setting doesn't add enough flexibility for Potion files. + +Braces +------ + +Section movement commands *also* look for one more thing: an opening or closing +curly brace (`{` or `}`) as the first character on a line. + +`[[` and `]]` look for opening braces, while `[]` and `][` look for closing +braces. + +This extra "hack" allows you to move between sections of C-like languages +easily. However, these rules are always the same no matter what type of file +you're in! + +Put the following into a buffer: + + :::text + Test A B + Test + + .SH Hello A B + + Test + + { A + Test + } B + + Test + + .H World A B + + Test + Test A B + +Now run `:set filetype=basic` to tell Vim that this is a BASIC file, and try the +section movement comments. + +The `[[` and `]]` commands will move between the lines marked `A`, while `[]` +and `][` move between the lines marked `B`. + +This shows us that Vim always uses these same two rules for section movement, +even for languages where neither one makes sense (like BASIC)! + +[nroff files]: http://en.wikipedia.org/wiki/Nroff + +Exercises +--------- + +Read `:help section` again, now that you know the story of section movement. + +Read `:help sections` just for the fun of it. diff -r dd723c5dadcd -r a8dd2fa6d24c chapters/51.markdown --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/chapters/51.markdown Mon Apr 16 14:38:23 2012 +0100 @@ -0,0 +1,372 @@ +Potion Section Movement +======================= + +Now that we know how section movement works, let's remap the commands to work +in a way that makes sense for Potion files. + +First we need to decide what "section" should mean for a Potion file. There are +two pairs of section movement commands, so we can come up with two "schemes" and +our users can choose the one they prefer. + +Let's use the following two schemes to define where Potion sections start: + +1. Any line following a blank line that contains non-whitespace as the first + character, or the first line in the file. +2. Any line that contains non-whitespace as the first character, an equal sign + somewhere inside the line, and ends with a colon. + +Using a slightly-expanded version of our sample `factorial.pn` file, here's +what these rules will consider to be section headers: + + :::text + # factorial.pn 1 + # Print some factorials, just for fun. + + factorial = (n): 1 2 + total = 1 + + n to 1 (i): + total *= i. + + total. + + print_line = (): 1 2 + "-=-=-=-=-=-=-=-\n" print. + + print_factorial = (i): 1 2 + i string print + '! is: ' print + factorial (i) string print + "\n" print. + + "Here are some factorials:\n\n" print 1 + + print_line () 1 + 10 times (i): + print_factorial (i). + print_line () + +Our first definition tends to be more liberal. It defines a section to be +roughly a "top-level chunk of text". + +The second definition is more restrictive. It defines a section to be +(effectively) a function definition. + +Custom Mappings +--------------- + +Create a `ftplugin/potion/sections.vim` file in your plugin's repo. This +is where we'll put the code for section movement. Remember that this code will +be run whenever a buffer's `filetype` is set to `potion`. + +We're going to remap all four section movement commands, so go ahead and create +a "skeleton" file: + + :::vim + noremap