awk commands

--HG--
branch : pmacs2
This commit is contained in:
moculus 2008-10-04 16:09:15 +00:00
parent bc5f6802d8
commit f48db1edda
3 changed files with 121 additions and 91 deletions

View File

@ -1,84 +1,84 @@
# filename: outline_classic11.awk
# author: Eric Pement - pemente@northpark.edu
# date: 22 March 2001
# version: 1.1
#
# purpose: GNU awk script to modify files created and saved in GNU Emacs
# "outline-mode" into classic indented, outline format. E.g.,
#
# INPUT FILE OUTPUT FILE
# ============== ===================
# * Line 1 | A. Line 1
# ** Line 2 | 1. Line 2
# *** Line 3 | a. Line 3
# *** Line 4 | b. Line 4
# **** Line 5 | (1) Line 5
# ***** Line 6 | (a) Line 6
# ***** Line 7 | (b) Line 7
# ** Line 8 | 2. Line 8
# * Line 9 | B. Line 9
# ** Line 10 | 1. Line 10
#
# NEW! variable "num" determines the amount of increasing indentation.
# Default is 2 (indent by 2, 4, 6, 8... spaces), but this can be
# controlled by using the -v switch from the command line. E.g.,
#
# awk -v num=4 -f outline_classic11.awk yourfile.txt
#
# Note: this script expects a maximum of five asterisks (*) on a line.
# Lines of plain text (no asterisks) in source file are NOT indented.
BEGIN {
if (num == "") num = 2 # if num is not defined, set it to 2
split("ABCDEFGHIJKLMNOPQRSTUVWXYZ",Lev1,"")
split("abcdefghijklmnopqrstuvwxyz",Lev3,"")
split("abcdefghijklmnopqrstuvwxyz",Lev5,"")
}
/^\*/ {
this_len = match($0,/\*([^*]|$)/); # get number of stars in 1st field
array[this_len]++; # increment index of current leaf
if ( this_len - last_len > 1 ) { # check for invalid outline levels
if (FILENAME == "-" )
myfile = "(piped from standard input)"
else
myfile = FILENAME
error_message = "\a\a" \
"************************************************\n" \
" WARNING! The input file has an invalid number \n" \
" of asterisks on line " NR ", below. \n\n" \
" The previous outline level had " last_len " asterisks, \n" \
" but the current/next level has " this_len " asterisks!\n\n" \
" You have inadvertently skipped one level of \n" \
" indentation. Processing halted so you can fix \n" \
" the input file, \x22" myfile "\x22. \n" \
"************************************************\n" \
">>>\n" \
"Error on Line #" NR " :" ;
print error_message, $0 > "/dev/stderr" ;
exit 1;
}
if ( this_len < last_len ) { # if we have moved up a branch...
for (i = this_len + 1; i <= last_len; i++)
array[i] = 0; # .. reset the leaves below us
}
for (j=1; j <= this_len; j++){ # build up the prefix string
if (j == 1) prefix = Lev1[array[j]] "."
else if (j == 2) prefix = array[j] "."
else if (j == 3) prefix = Lev3[array[j]] "."
else if (j == 4) prefix = "(" array[j] ")"
else if (j == 5) prefix = "(" Lev5[array[j]] ")"
}
indent_level = (this_len - 1) * num ;
indentation = sprintf("%+" indent_level "s", "") ;
sub(/^\*+/, indentation prefix) ;
last_len = this_len ;
prefix = "" ;
}
{ print }
# --- end of script ---
# filename: outline_classic11.awk
# author: Eric Pement - pemente@northpark.edu
# date: 22 March 2001
# version: 1.1
#
# purpose: GNU awk script to modify files created and saved in GNU Emacs
# "outline-mode" into classic indented, outline format. E.g.,
#
# INPUT FILE OUTPUT FILE
# ============== ===================
# * Line 1 | A. Line 1
# ** Line 2 | 1. Line 2
# *** Line 3 | a. Line 3
# *** Line 4 | b. Line 4
# **** Line 5 | (1) Line 5
# ***** Line 6 | (a) Line 6
# ***** Line 7 | (b) Line 7
# ** Line 8 | 2. Line 8
# * Line 9 | B. Line 9
# ** Line 10 | 1. Line 10
#
# NEW! variable "num" determines the amount of increasing indentation.
# Default is 2 (indent by 2, 4, 6, 8... spaces), but this can be
# controlled by using the -v switch from the command line. E.g.,
#
# awk -v num=4 -f outline_classic11.awk yourfile.txt
#
# Note: this script expects a maximum of five asterisks (*) on a line.
# Lines of plain text (no asterisks) in source file are NOT indented.
BEGIN {
if (num == "") num = 2 # if num is not defined, set it to 2
split("ABCDEFGHIJKLMNOPQRSTUVWXYZ",Lev1,"")
split("abcdefghijklmnopqrstuvwxyz",Lev3,"")
split("abcdefghijklmnopqrstuvwxyz",Lev5,"")
}
/^\*/ {
this_len = match($0,/\*([^*]|$)/); # get number of stars in 1st field
array[this_len]++; # increment index of current leaf
if ( this_len - last_len > 1 ) { # check for invalid outline levels
if (FILENAME == "-" )
myfile = "(piped from standard input)"
else
myfile = FILENAME
error_message = "\a\a" \
"************************************************\n" \
" WARNING! The input file has an invalid number \n" \
" of asterisks on line " NR ", below. \n\n" \
" The previous outline level had " last_len " asterisks, \n" \
" but the current/next level has " this_len " asterisks!\n\n" \
" You have inadvertently skipped one level of \n" \
" indentation. Processing halted so you can fix \n" \
" the input file, \x22" myfile "\x22. \n" \
"************************************************\n" \
">>>\n" \
"Error on Line #" NR " :" ;
print error_message, $0 > "/dev/stderr" ;
exit 1;
}
if ( this_len < last_len ) { # if we have moved up a branch...
for (i = this_len + 1; i <= last_len; i++)
array[i] = 0; # .. reset the leaves below us
}
for (j=1; j <= this_len; j++){ # build up the prefix string
if (j == 1) prefix = Lev1[array[j]] "."
else if (j == 2) prefix = array[j] "."
else if (j == 3) prefix = Lev3[array[j]] "."
else if (j == 4) prefix = "(" array[j] ")"
else if (j == 5) prefix = "(" Lev5[array[j]] ")"
}
indent_level = (this_len - 1) * num ;
indentation = sprintf("%+" indent_level "s", "") ;
sub(/^\*+/, indentation prefix) ;
last_len = this_len ;
prefix = "" ;
}
{ print }
# --- end of script ---

View File

@ -10,14 +10,16 @@ class Exec(Method):
'''Execute a command in a shell and put the output in a new buffer'''
show_success = True
args = [Argument('cmd', prompt="Exec: ", datatype='shell')]
def _doit(self, w, path, cmd, cmdname=None, bufname=None, cmddir=None):
def _doit(self, w, path, cmd, cmdname=None, bufname=None, cmddir=None, opts={}):
if cmddir:
cmd = "cd %r && %s" % (cmddir, cmd)
d = dict(opts)
if path:
try:
cmd = cmd % {'path': path}
except:
pass
d['path'] = path
try:
cmd = cmd % d
except:
pass
if bufname is None:
bufname = '*%s*' % self.name.title()

View File

@ -1,8 +1,10 @@
import commands
import color, method, mode, tab
import commands, os
import color, default, mode, tab
from lex import Grammar, PatternRule, RegionRule
from mode.python import StringGrammar2
from tab import StackTabber2
from method import Method, Argument, arg
from method.shell import Exec, Pipe
class RegexGrammar(Grammar):
rules = [
@ -62,6 +64,26 @@ class AwkTabber(StackTabber2):
def _is_ignored(self, t):
return t.name in ('spaces', 'eol', 'comment')
class AwkFilterFile(Exec):
show_success = True
args = [arg('path', dt="path", p="Filter File: ", dv=default.path_dirname,
ld=True, h="file to open")]
def _execute(self, w, **vargs):
if not hasattr(w.buffer, 'path'):
w.set_error("Buffer has no program")
return
elif w.buffer.changed():
w.set_error("Buffer is not saved")
return
else:
self._doit(w, w.buffer.path, w.application.config['awk.filter-cmd'],
cmdname='awk', bufname='*Awk-Output*',
opts={'data': vargs['path']})
class AwkFilterBuffer(Method):
pass
class AwkFilterInput(Method):
pass
class Awk(mode.Fundamental):
tabbercls = AwkTabber
@ -78,5 +100,11 @@ class Awk(mode.Fundamental):
'awk_regex.data': ('cyan', 'default', 'bold'),
'awk_regex.end': ('cyan', 'default', 'bold'),
}
config = {
'awk.filter-cmd': "awk -f %(path)r %(data)r",
}
actions = [AwkFilterFile, AwkFilterBuffer, AwkFilterInput]
#format = "%(flag)s %(bname)-18s (%(mname)s) %(indent)s %(cursor)s/%(mark)s %(perc)s [%(func)s]"
install = Awk.install