alias lf !!ls -CF

alias save {
 "write this file, but only if it has been modified
 if modified
 then w!? !*
}

alias w {
 "write this file, but if readonly then check it out first
 if readonly && "!?!%!*" == ""
 then !!co -l %
 then w!!
 else !%w!? !*
}

alias man split +mantweak !man

alias mantweak {
 "convert nroff output back into man source
 let r=report
 set report=0
 1 s/^Reformatting.*ait\.\.\.$//
 %s/\\/\\\\/g
 %s/_\(.\)/\\fI\1\\fR/g
 %s/.\(.\)/\\fB\1\\fR/g
 %s/\\fR\\fB//g
 %s/\\fR\\fI//g
 1 i .nf
 let report=r
 set bd=man nomod
 dis man
}

alias text {
 "like :tag but for any text
 set tagprgonce="grep -nsw '(quote(\"'\",$1))' *.[ch] /dev/null \| sed 's/^\\\\\\(.*\\\\\\):\\\\\\(.*\\\\\\):.*/!^	\\1	\\2/'"
 tag!? !*
}

alias btext {
 "like :browse but for any text
 set tagprgonce="grep -nsw '(quote(\"'\",$1))' *.[ch] /dev/null \| sed 's/^\\\\\\(.*\\\\\\):\\\\\\(.*\\\\\\):.*/!^	\\1	\\2/'"
 browse!! !*
}

if gui=="x11"
then {
 alias courier {
  "use courier fonts of a given size
  set normalfont=*-courier-medium-r-*-!(18)^-*
  set italicfont=*-courier-medium-o-*-!(18)^-*
  set boldfont=*-courier-bold-r-*-!(18)^-*
 }
}

alias copying split (elvispath("license"))

alias cbload {
 "load cut-buffers from a file
 if "!1" == ""
 then error cbload requires a file name
 let b=buffer
 let r = report
 set report=0
 e !1
 %s:^--CBS-- \([a-z]\)$:+;/^--CBS--/-1 y \1:x
 let report = r
 eval buffer (b)
}

alias cbsave {
 "save cut-buffers to a file
 if "!1" == ""
 then error cbsave requires a file name
 let b = buffer
 let r = report
 set report=0
 e !1
 %d
 let a = 'a'
 while a <= 'z'
 do {
  if buffer("Elvis cut buffer "char(a))
  then {
   eval $ a --CBS-- (char(a))
   eval $ put (char(a))
  }
  let a = a + 1
 }
 $ a --CBS--  
 w!?
 let report = r
 eval buffer (b)
}

alias config {
 "describe the configuration of this elvis
 calc "Display modes:     normal"(feature("syntax")?" syntax")(feature("hex")?" hex")(feature("html")?" html man tex")
 calc "Network protocols: "(feature("ftp")?"ftp":"none")(feature("http")?" http")
 calc "Other features:   "(feature("showtag")?" showtag")(feature("lpr")?" lpr")(feature("alias")?" alias")(feature("mkexrc")?" mkexrc")(feature("complete")?" complete")(feature("ram")?" ram")
}

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" The following were contributed by Ian Utley (iu@apertus.uk.com)

alias align {
    "align any = signs (or other given text) in selected line
    "
    " The following if tests to see if we have visually highlighted lines.
    "
    let f=0
    if ( !> !!= "" )
    then {
        !< mark a
        !> mark b
        let f=1
    }
    if ( f == 1)
    then {
        "
        " Initialise i which will store the alignment column. Switch off
        " reporting and mark the current line to return the cursor at the end.
        "
        set i=0
        let j=report
        set report=0
        mark z
        "
        " Remove any whitespace before the alignment character.
        "
        'a,'b s/[ 	]*!(=)\$/!(=)\$/
        "
        " We could be aligning != <= or >= so we want to keep this letter
        " near. Of course we may not be aligning an equals but we commonly do.
        "
        if ( "!(=)\$" == "=" )
        then {
            'a,'b s/[	 ]*\([!!<>]*\)!(=)\$[	 ]*/ \1!(=)\$ /
        } 
        "
        "
        let f=0
        'a,'bglobal /!(=)\$/ {
            " 
            " Special case for the top line as -1 will not work.
            "
            if ( current("line") == 1 )
            then {
                1 insert ""
                let f=1
            }
            -1
            /!(=)\$
            "
            "
            " Remember the largest column number for alignment.
            "
            if (current("column")>i)
            then let i=current("column")
            "
            " Special case removal
            "
            if ( current("line") > 1 && f == 1)
            then {
                1 delete
                let f=0
            }
        }
        "
        " Do the alignment.
        "
        let f=0
        'a,'bglobal /!(=)\$/ {
            " 
            " Special case for the top line as -1 will not work.
            "
            if ( current("line") == 1 )
            then {
                1i ""
                let f=1
            }
            -1
            /!(=)\$
            "
            " Not sure why I need to add +1
            "
            let k=i-current("column")+1
            s/\([!!<>]*\)!(=)\$/                                                                                \1!(=)\$/
            eval s/ *\\\( \{(k)\}[!!<>]*!(=)\$\\\)/\1
            "
            " Special case removal
            "
            if ( current("line") > 1 && f == 1 )
            then {
                1 delete
                let f=0
            }
        }
        "
        " Restore the reporting to its previous level, and return the cursor
        " to the line it was previously on.
        "
        let report=j
        'z
    }
}


alias match {
    " match "if" with "endif", etc.
    "
    if ( ! ignorecase )
    then {
        "
        " Openroad is case-insensitive and so we set the ignorecase option for 
        " searches, but remembering the current setting.
        "
        set k=1
        set ignorecase
    }
    "
    " Initialize variables
    "
    set z=0
    set a=""
    set b=""
    set d=""
    " 
    " Remember where we started.
    "
    mark d
    let a=current("word")
    if (a != "")
    then {
        let a=toupper(a)
        if ( a=="IF" || a=="FOR" || a=="WHILE")
        then {
            let b="END" a
            let d="forward"
        }
        if ( a=="ENDIF" || a=="ENDFOR" || a=="ENDWHILE" )
        then {
             if ( a=="ENDIF" ) 
             then let b="IF"
             if ( a=="ENDFOR" )
             then let b="FOR"
             if ( a=="ENDWHILE" )
             then let b="WHILE"
             let d="backward"
        }
        if ( b=="" )
        then error Not a matching keyword
        if ( d=="forward" )
        then {
            "
            " We are searching forward, so put a sentinal at the end of
            " the file to ensure that the search succeeds.
            "
            eval $ append (b)
            'd
        }
        else {
            "
            " We are searching backwards, so put a sentinal at the start of
            " the file to ensure that the search succeeds.
            "
            eval 1 insert (b)
            'd
        }
        mark c
        set i=1
        "
        " z = 1 when we reach the start, or end of the file.
        "
        while (i != 0 && z==0 )
        do {
            if ( d=="forward" )
            then {
                eval /\<(b)\>
                if (current("line")==buflines)
                then {
                    set z=1
                    $ delete
                }
            }
            else {
                'd
                eval ?\<(b)\>
                if (current("line")==1)
                then {
                    set z=1
                    1 delete
                }
            }
            mark d
            set i=0
            if ( d=="forward" )
            then {
                eval 'c,'d global /\<(a)\>/ let i=i+1
                eval 'c,'d global /\<(b)\>/ let i=i-1
            }
            else {
                eval 'd,'c global /\<(a)\>/ let i=i+1
                eval 'd,'c global /\<(b)\>/ let i=i-1
            }
        }
        if (z==1)
        then {
            'c
            error Match not found!
        }
        else {
            "
            " Clean up the sentinels
            "
            if ( d=="forward" )
            then {
                $ delete
                " Lets attempt to put the cursor on the word.
                'd-1
                eval /\<(b)\>
            }
            else {
                1 delete
                " Lets attempt to put the cursor on the word.
                'd+1
                eval ?\<(b)\>
            }
        }
    }
    "
    " Restore the ignorecase option if necessary 
    "
    if (k==1)
    then {
        set noignorecase
        set k=0
    }
}
