Re: BASH lint and now emacs too

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



For those interested, I discovered that emacs already supports closing an opening character if that opening character is one of :   ( { [ " ' ` < 

So if one writes:
   if [
emacs will supply the closing ] and position the cursor between the [] construct. 

That's exactly what I was looking for to help avoid nuisance programming errors that are so difficult to find in BASH. I discovered this as I was attempting to read their LISP code for the way emacs will supply a whole if/else construct. 

After about 6 hours of trial and error I got it working. ( I don't write LISP)

Here is the emacs config file (.emacs) code to turn the feature on for scripting:

(add-hook 'sh-mode-hook '(lambda () (define-key sh-mode-map [return] 'sh-newline-and-indent)))
(defun my-skeleton-setup ()
   (setq skeleton-pair t)
   (setq skeleton-autowrap t)
   (setq skeleton-pair-on-word t)
   (setq skeleton-pair-alist '((?) _ ?() (?] _ ?[) (?} _ ?{) (?> _ ?<) (?/ _ ?\\) (?\\ _ ?/) (?` ?` _ "''") (?' ?' _ "``"))))
(add-hook 'sh-mode-hook 'my-skeleton-setup)

The first line turns on auto indent (Thank You John Haxby) and has nothing to do with the remainder which turns on the character pairing feature.

I tried this for quite some time in xemacs and discovered I was beating a dead horse. It doesn't work in xemacs and is a known bug. It does work in emacs however, so I'm switching to emacs.

I also spent a few hours on a brute force lint filter for bash. Here's the script:

#!/bin/bash

# Written by Bill Gradwohl 10/16/2003
# Anyone can do anything they want with this.
# Please send a copy of enhancements to bill@xxxxxxx

# This is a simple minded bash lint filter. It's designed to locate the 
# trivial errors that are so easy to make and so difficult to locate manually.
# No attempt at efficiency was made; I've got lots of CPU power and a life.
#
# The script will output some statistics, and then will also flag any line that 
# does not have matched pairs of {}, [], (), "", '', ``. The output is color
# coded for easier reading.

BLACK=30
RED=31
GREEN=32
YELLOW=33
BLUE=34
MAGENTA=35
CYAN=36
GRAY=37
WHITE=39

SETCOLOR_BLACK="\\033[0;${BLACK}m"
SETCOLOR_RED="\\033[1;${RED}m"
SETCOLOR_GREEN="\\033[0;${GREEN}m"
SETCOLOR_YELLOW="\\033[1;${YELLOW}m"
SETCOLOR_BLUE="\\033[1;${BLUE}m"
SETCOLOR_MAGENTA="\\033[1;${MAGENTA}m"
SETCOLOR_CYAN="\\033[1;${CYAN}m"
SETCOLOR_NORMAL="\\033[0;${WHITE}m"

syntax() {
   echo -e "${SETCOLOR_GREEN}Syntax: ${0##*/} scriptName"
   echo
   echo    "Example: ${0##*/} /home/bill/.bashrc"
   echo -e "${SETCOLOR_NORMAL}"
   exit 1
}

widthOutput=

width() {
   local x
   if [ -z "$1" -a -z "$2" ]; then 
      x='                '
   else 
      x="$1=$2               "
   fi
   widthOutput="${x:0:$3}"
}

lookfor=('{' '}' '(' ')' '[' ']' '`' '"' "'") # The last two items must be " and '

[ $# -ne 1 ] && syntax

if [ ! -r "${1}" ]; then
   echo
   echo -e "${SETCOLOR_MAGENTA}The file $1 cannot be read.${SETCOLOR_NORMAL}"
   syntax
else
   i=0
   while read -r line; do
      #echo "${line}"
      array[i]="$line"
      savearray[i]="$line"
      masterarray[i++]="$line"
   done <$1

   echo -e "   ${SETCOLOR_GREEN}$1 contains ${#array[*]} lines of code.${SETCOLOR_NORMAL}"
   echo 

   # Set all the counters to 0
   for ((l=0; l<${#lookfor[*]}; ++l)); do
      found[l]=0
   done

   # Get an overall view of things.
   for ((i=0; i<${#array[*]}; ++i)); do
      #echo "$((i+1)) ${array[i]}"
      for ((l=0; l<${#lookfor[*]}; ++l)); do
         for (( ;; )); do
            x="${array[i]/${lookfor[l]}/}"
            if [ "$x" != "${array[i]}" ]; then
               found[l]=$((found[l]+1))
               array[i]="$x"
            else
               break
            fi
         done
      done
   done

   # Report on that overall view.
   for ((i=0; i<${#found[*]}; ++i)); do
      echo "${lookfor[i]}=${found[i]}"
   done

   # Now look at things on a line by line basis.
   for ((i=0; i<${#savearray[*]}; ++i)); do 
      # For each line reset the counters to 0.
      for ((l=0; l<${#lookfor[*]}; ++l)); do
         found[l]=0
      done

      # Find what looks suspicious on the current line.
      for ((l=0; l<${#lookfor[*]}; ++l)); do
         for (( ;; )); do
            x="${savearray[i]/${lookfor[l]}/}"
            if [ "$x" != "${savearray[i]}" ]; then
               found[l]=$((found[l]+1))
               savearray[i]="$x"
            else
               break
            fi
         done
      done

      # Report on what looks suspicious on the current line.
      lineNumber='     '$((i+1))
      lineNumber="${lineNumber:$((-5))}"
      for x in 0 2 4; do 
         width "${lookfor[$x]}" "${found[$x]}" 4
         left="$widthOutput"
         width "${lookfor[$((x+1))]}" "${found[$((x+1))]}" 4
         right="$widthOutput"
         [ ${found[$x]} -ne ${found[$((x+1))]} ] && echo -e "${SETCOLOR_RED}${lineNumber}${SETCOLOR_NORMAL}: ${SETCOLOR_GREEN}${left}${SETCOLOR_NORMAL}, ${SETCOLOR_GREEN}${right}${SETCOLOR_NORMAL} ${masterarray[i]}"
      done
      for x in 6 7 8; do 
         width "" "" 4
         left="$widthOutput"
         width "${lookfor[$x]}" "${found[$x]}" 4
         right="$widthOutput"
         [ ${found[$x]} -ne $((${found[$x]}/2*2)) ] && echo -e "${SETCOLOR_RED}${lineNumber}${SETCOLOR_NORMAL}: ${SETCOLOR_GREEN}${left}${SETCOLOR_NORMAL}  ${SETCOLOR_GREEN}${right}${SETCOLOR_NORMAL} ${masterarray[i]}"
      done
   done
fi



Bill Gradwohl
(817) 224-9400 x211
www.ycc.com
SPAMstomper Protected Email


-- 
Shrike-list mailing list
Shrike-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/shrike-list

[Index of Archives]     [Fedora Users]     [Centos Users]     [Kernel Development]     [Red Hat Install]     [Red Hat Watch]     [Red Hat Development]     [Red Hat Phoebe Beta]     [Yosemite Forum]     [Fedora Discussion]     [Gimp]     [Stuff]     [Yosemite News]

  Powered by Linux