playground:plan9_tutorial_sam
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
playground:plan9_tutorial_sam [2021/04/19 18:05] – created sam tutorial page dsp | playground:plan9_tutorial_sam [2021/04/20 04:59] (current) – adding external command section dsp | ||
---|---|---|---|
Line 63: | Line 63: | ||
notice that if we rename an entry that doesn' | notice that if we rename an entry that doesn' | ||
issue **w** on the renamed entry it will create a new file on disk. | issue **w** on the renamed entry it will create a new file on disk. | ||
+ | |||
+ | === Examples for sam regex === | ||
+ | The benefit of sam is that it allows for regex tasks to be chained and also debugged | ||
+ | easily from within the editor. We will consider a toy example to indent properly some | ||
+ | code of the form | ||
+ | < | ||
+ | if foo { | ||
+ | bar | ||
+ | } | ||
+ | </ | ||
+ | We would like bar to be indented at 8 spaces in, one more level than //if// is. | ||
+ | We can start with a regex to detect the opening brace | ||
+ | < | ||
+ | should detect, in the whole file (,) these braces. | ||
+ | Now we can match backwards to find the prepending white space in that line we matched | ||
+ | by just chaining the next regex. | ||
+ | < | ||
+ | now that matches the white space before. | ||
+ | We would like to add at least as much white space to the line below and | ||
+ | then some (4 spaces for example). | ||
+ | But for that to work the line below must be free of leading white space. | ||
+ | Therefore we will remove it from any line that is below an opening brace. The search is | ||
+ | < | ||
+ | and to remove the white space between the start of line and the first character we chain it like | ||
+ | < | ||
+ | The final form of our regex chain is | ||
+ | < | ||
+ | , x/{\n[ ]*/ s/^[ ]*/ / | ||
+ | , x/{$/ -/^[ ]*/ t .+0 | ||
+ | </ | ||
+ | |||
+ | === Interacting with external commands === | ||
+ | Of course creating a indentation program with just regexp would be duplication of work. | ||
+ | For most languages pretty printers exist like gofmt and cb (C beautifier) let' try | ||
+ | to run cb on some ugly-fied C. | ||
+ | < | ||
+ | if (foo < | ||
+ | do(bxm,as); | ||
+ | wrong; | ||
+ | } | ||
+ | </ | ||
+ | To pass all this code through cb we can just select it and then | ||
+ | issue the command |cb . that would replace the dot by | ||
+ | < | ||
+ | if (foo < | ||
+ | do(bxm, | ||
+ | wrong; | ||
+ | } | ||
+ | </ | ||
+ | similarly we can read input from a command | ||
+ | using < and write to a command using > if we want to count all the | ||
+ | characters in a selection for example we can highlight it and issue >wc | ||
+ | , observe that this doesn' | ||
+ | |||
+ | We could also imagine a command where it replaces every | ||
+ | occurrence of //{DATE}// with the current system date in the whole document | ||
+ | < | ||
+ | , x/{DATE}/ <date | ||
+ | </ | ||
+ | would find any occurrence of the string //{DATE}// and then replace that | ||
+ | dot with the output of the date command. | ||
=== Self guided discoveries === | === Self guided discoveries === | ||
- | * create two files (file1 and file2) add text to both, use n and b to control which file has the focus. | + | * create two files (file1 and file2) add text to both, use **n** and **b** to control which file has the focus. |
- | * on file2 for exaple | + | * on file2 for example |
r file2 | r file2 | ||
0 | 0 | ||
- | </ | + | </ |
* can you specify a reverse range like #10,#5 ? | * can you specify a reverse range like #10,#5 ? | ||
- | * highlight a word. then issue m+0. why is this happening? what is t+0 doing? what if you keep issuing t+0? | + | * highlight a word. then issue **m+0**. why is this happening? what is **t+0** doing? what if you keep issuing |
+ | * Improve the toy indent example by | ||
+ | * making lines align when no opening brace is detected in the end of line | ||
+ | * remove 4 leading spaces if the line has a closing brace | ||
+ | |||
+ | === Documentation and more === | ||
+ | *[[http:// | ||
+ | *[[http:// | ||
+ | *[[http:// | ||
+ | *[[http:// | ||
+ | *[[https:// | ||
+ | *[[https:// |
playground/plan9_tutorial_sam.1618855517.txt.gz · Last modified: 2021/04/19 18:05 by dsp