Folks,

Attached please find a new version of my control flow add-on for Paul's
JDE.  

[Paul, if you could update the website, I'd be very grateful.]

New in this release
------------

* Control flow abbrevs do not expand in comments or in quoted strings,
  unless you write comments or quoted strings in strange ways.
  [Don't do that.]  This lets you type 'if' and 'else' and 'while' [...]
  in javadoc without getting an annoying surprise.  Users of cperl-mode
  will recognize and delight in this bit of context switching.

* A new defun which clobbers the jde's abbrev table and replaces it
  with an identical one that doesn't expand in comments or in quoted
  strings.  This is for those of you who are sick of typing 'in[space]'
  and 'by[space]' in javadoc and seeing 'int' and 'byte' because you
  forgot to type C-q[space]. 

  Yes, you can easily turn this off if you like typing C-q[space]
  Yes, I think this should become the default behavior in the jde.el
  code (hint hint).

I've only tested this on xemacs linux && solaris.  It's still pretty 
vanilla except for a dependency on a common lisp package that comes
standard with xemacs (and maybe FSF emacs??).  Please let me know if
it's broken.

Usage Notes -

put jde-cflow.el in your load path and add 
(require 'jde-cflow) to your .emacs somewhere after (require 'jde)

As always, a grateful tip o' the hat to Paul for making this possible.
Eric


---- Cut Here and feed the following to sh ----
#!/bin/sh
# This is a shell archive (produced by GNU sharutils 4.2).
# To extract the files from this archive, save it to some FILE, remove
# everything before the `!/bin/sh' line above, then type `sh FILE'.
#
# Made on 2000-05-27 20:38 PDT by <[EMAIL PROTECTED]>.
# Source directory was `/home/eric'.
#
# Existing files will *not* be overwritten unless `-c' is specified.
# This format requires very little intelligence at unshar time.
# "if test", "echo", "mkdir", and "sed" may be needed.
#
# This shar contains:
# length mode       name
# ------ ---------- ------------------------------------------
#  11515 -rw-r--r-- jde-cflow.el
#
echo=echo
if mkdir _sh21963; then
  $echo 'x -' 'creating lock directory'
else
  $echo 'failed to create lock directory'
  exit 1
fi
# ============= jde-cflow.el ==============
if test -f 'jde-cflow.el' && test "$first_param" != -c; then
  $echo 'x -' SKIPPING 'jde-cflow.el' '(file already exists)'
else
  $echo 'x -' extracting 'jde-cflow.el' '(text)'
  sed 's/^X//' << 'SHAR_EOF' > 'jde-cflow.el' &&
X;;; jde-cflow.el -- Control Flow add-ons for Paul Kinnucan's Emacs JDE
X
X;; Author: Eric D. Friedman <[EMAIL PROTECTED]>
X;; Maintainer: Eric D. Friedman
X
X;; Copyright (C) 1998, 1999, 2000 Eric D. Friedman
X
X;; GNU Emacs is free software; you can redistribute it and/or modify
X;; it under the terms of the GNU General Public License as published by
X;; the Free Software Foundation; either version 2, or (at your option)
X;; any later version.
X
X;; GNU Emacs is distributed in the hope that it will be useful,
X;; but WITHOUT ANY WARRANTY; without even the implied warranty of
X;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
X;; GNU General Public License for more details.
X
X;; You should have received a copy of the GNU General Public License
X;; along with GNU Emacs; see the file COPYING.  If not, write to the
X;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
X;; Boston, MA 02111-1307, USA.
X
X;;; Commentary:
X
X;; To use this package effectively, you need to use the Java
X;; Development Environment (JDE) for Emacs.  The latest version of the
X;; JDE is available at <URL:http://sunsite.auc.dk/jde/>.
X
X;; Please send any comments, bugs, or upgrade requests to Eric
X;; Friedman at [EMAIL PROTECTED]
X
X;;; Code:
X
X;;
X;; Tests whether point is in a comment or a quoted string.
X;; Note that "in a comment" here means in a line that has comment
X;; characters.  That is,
X;; // a comment
X;; /**
X;;  * a comment
X;;  */
X;; /* a comment */
X;;
X;; but not
X;; /*
X;;  a comment
X;;  */
X;; because that would be slow and because you shouldn't write comments
X;; like that anyway. ;-)
X;;
X
X(defun jde-cflow-comment-or-quoted ()
X  "Returns t if point is in a comment or a quoted string. nil otherwise"
X  (interactive "p")
X  ;; limit our analysis to the current line.
X  (let ((beg (save-excursion (beginning-of-line) (point))))
X    (if
X        (or
X         ;; are we in a javadoc comment?
X         (save-excursion
X           (re-search-backward
X            "^[ \t]*/?\\*"
X            beg t))
X         ;; are we in a '//' or a '/*' style comment?
X         ;; note that /* or /** on a line with only leading whitespace
X         ;; will have matched in the previous regex.  We check again here
X         ;; because the above case needs to allow for a line with
X         ;; the continuation of a comment (using only a '*') whereas this
X         ;; requires the presence of a '/' in front of the '*' so as to
X         ;; distinguish a comment from a '*' operator.
X         ;; To make a long story short,
X         ;; the first regex matches
X         ;;   /* a comment */
X         ;; and
X         ;; /**
X         ;;  * a comment
X         ;;  */
X         ;; while the second one matches
X         ;; System.out.println(foo); /* a comment */
X         ;; but not
X         ;; i = 3 * 5;
X         ;; if you do something like following, you're on your own:
X         ;; i = 3
X         ;;       * 5; 
X         ;; Suggestions for improving the robustness of this algorithm
X         ;; gratefully accepted.
X         (save-excursion
X           (re-search-backward
X            "\\(//\\|/\\*\\)"
X            beg t))
X         ;; are we in a quoted string?
X         (save-excursion
X           (re-search-backward
X            "\"" ;;
X            beg t)))
X        t ;; return true if we had any matches; nil otherwise
X      nil)))
X
X(defvar jde-tempo-tags nil
X  "Tempo tags for JDE mode")
X
X(defvar jde-mode-abbrev-table nil
X  "Abbrev table for use in JDE-mode buffers.")
X
X(require 'tempo)
X
X;;; JDE-Mode Templates
X
X(tempo-define-template "jde-if"
X                       '((if (jde-cflow-comment-or-quoted)
X                             '(l "if")
X                           '(l > "if (" (p "if-clause: " clause) ") "
X                           "{" > n> r n
X                           "} // end of if (" (s clause) ")" > n>)
X                           )
X                         )
X                       "if"
X                       "Insert a Java if statement"
X                       'jde-tempo-tags)
X
X
X(tempo-define-template "jde-else"
X                       '((if (jde-cflow-comment-or-quoted)
X                             '(l "else")
X                           '(l > "else "
X                               "{" > n> r n
X                               "} // end of else" > n>)
X                               )
X                           )
X                       "else"
X                       "Insert a Java else statement"
X                       'jde-tempo-tags)
X
X(tempo-define-template "jde-if-else"
X                       '((if (jde-cflow-comment-or-quoted)
X                             '(l "ife")
X                           '(l > "if (" (p "if-clause: " clause) ") "
X                               "{" > n> r n
X                               "} // end of if (" (s clause) ")" > n>
X                               > "else "
X                               "{" > n> r n
X                               "} // end of if (" (s clause) ")else" > n>)
X                           )
X                         )
X                       "ifelse"
X                       "Insert a Java if else statement"
X                       'jde-tempo-tags)
X
X(tempo-define-template "jde-while"
X                       '((if (jde-cflow-comment-or-quoted)
X                             '(l "while")
X                           '(l > "while (" (p "while-clause: " clause) ") "
X                               "{" > n> r n
X                               "} // end of while (" (s clause) ")" > n>)
X                           )
X                         )
X                       "while"
X                       "Insert a Java while statement"
X                       'jde-tempo-tags)
X
X(tempo-define-template "jde-for"
X                       '((if (jde-cflow-comment-or-quoted)
X                             '(l "for")
X                           '(l > "for (" (p "for-clause: " clause) ") "
X                               "{" > n> r n
X                               "} // end of for (" (s clause) ")" > n>)
X                           )
X                         )
X                         "for"
X                         "Insert a Java for statement"
X                         'jde-tempo-tags)
X
X(tempo-define-template "jde-for-i"
X                       '((if (jde-cflow-comment-or-quoted)
X                             '(l "fori")
X                           '(l > "for (int " (p "variable: " var) " = 0; "
X                               (s var)
X                               " < "(p "upper bound: " ub)"; " (s var) "++) "
X                               "{" > n> r n
X                               "} // end of for (int " (s var) " = 0; "
X                               (s var) " < " (s ub) "; " (s var) "++)" > n>)
X                           )
X                         )
X                         "fori"
X                         "Insert a Java for loop: for (x = 0; x < ..; x++)"
X                         'jde-tempo-tags)
X
X(tempo-define-template "jde-main"
X                       '((if (jde-cflow-comment-or-quoted)
X                             '(l "main")
X                           '(l > "public static void main (String[] args) "
X                               "{" > n> r n
X                               "} // end of main ()" > n>)
X                           )
X                         )
X                         "main"
X                         "Insert a Java main statement"
X                         'jde-tempo-tags)
X
X(tempo-define-template "jde-switch"
X                       '((if (jde-cflow-comment-or-quoted)
X                             '(l "switch")
X                           '(l > "switch (" (p "switch-condition: " clause) ") "
X                               "{" > n
X                               "case " (p "first value: ") ":" > n> p n
X                               "break;" > n> p n
X                               "default:" > n> p n
X                               "break;" > n
X                               "} // end of switch (" (s clause) ")" > n>)
X                           )
X                         )
X                         "switch"
X                         "Insert a Java switch statement"
X                         'jde-tempo-tags)
X
X(tempo-define-template "jde-case"
X                       '((if (jde-cflow-comment-or-quoted)
X                             '(l "case")
X                           '(l n "case " (p "value: ") ":" > n> p n
X                               "break;" > n> p)
X                           )
X                         )
X                         "case"
X                         "Insert a Java case statement"
X                         'jde-tempo-tags)
X
X
X(defun jde-cflow-overwrite-broken-abbrev-table ()
X  "jde.el contains the following bit of code, which pulls
Xabbrevs and their expansions out of the jde-mode-abbreviations
Xtable and defines them for the mode.
X
X  (mapc (lambda (x)
X          (define-mode-abbrev (car x) (cdr x)))
X        jde-mode-abbreviations)
X
XAs many have noted, this is great except when you're writing a comment
X- then you have to remember to quote whitespace and word delimiters,
Xwhich gets old pretty quickly.
X
XThis defun clears the mode-specific abbrev table and reloads it with a
Xset of abbrevs that invoke an anonymous function instead of expanding
Xto a literal.  That function does the expansion only if point is not
Xin a quoted string or a comment.
X
XHopefully this will someday replace the implementation in jde.el"
X
X  ;; Note the use of lexical-let - must have the common lisp packages
X  ;; around, since the anonymous function needs the closure provided by
X  ;; lexical-let.
X
X  (clear-abbrev-table jde-mode-abbrev-table)
X  (mapc (lambda (x)
X          (lexical-let
X              ((abbrev (car x))         ; this is the abbrev, lexically scoped
X               (expansion (cdr x)))     ; this is the expansion
X            (define-abbrev jde-mode-abbrev-table
X              abbrev
X              ""
X              (lambda ()
X                (if (jde-cflow-comment-or-quoted)
X                    (insert abbrev)     ; insert the abbrev in quote/comment
X                  (insert expansion)    ; proceed with expansion elsewhere
X                  )
X                )
X              0)
X            )
X          )
X        jde-mode-abbreviations)
X  )
X
X(defun jde-cflow-load-cflow-template-abbrevs ()
X  "Defines jde-mode abbrevs for the control flow templates."
X  (define-abbrev jde-mode-abbrev-table "if" "" 'tempo-template-jde-if 0)
X  (define-abbrev jde-mode-abbrev-table "else" "" 'tempo-template-jde-else 0)
X  (define-abbrev jde-mode-abbrev-table "ife" "" 'tempo-template-jde-if-else 0)
X  (define-abbrev jde-mode-abbrev-table "while" "" 'tempo-template-jde-while 0)
X  (define-abbrev jde-mode-abbrev-table "for" "" 'tempo-template-jde-for 0)
X  (define-abbrev jde-mode-abbrev-table "fori" "" 'tempo-template-jde-for-i 0)
X  (define-abbrev jde-mode-abbrev-table "switch" "" 'tempo-template-jde-switch 0)
X  (define-abbrev jde-mode-abbrev-table "case" "" 'tempo-template-jde-case 0)
X  (define-abbrev jde-mode-abbrev-table "main" "" 'tempo-template-jde-main 0)
X  )
X
X
X;;
X;; When JDE mode starts, we
X;; 1) load the template tag last
X;; 2) clear and rewrite the broken abbrev table
X;; 3) load the control flow template abbrevs into that table
X;;
X;; If you like the broken template table, comment out #2 and carry on.
X;;
X
X(add-hook 'jde-mode-hook '(lambda ()
X                            (tempo-use-tag-list 'jde-tempo-tags)
X                            (jde-cflow-overwrite-broken-abbrev-table)
X                            (jde-cflow-load-cflow-template-abbrevs)
X                            ))
X
X(provide 'jde-cflow)
X
X;; end of jde-cflow
SHAR_EOF
  : || $echo 'restore of' 'jde-cflow.el' 'failed'
fi
rm -fr _sh21963
exit 0

Reply via email to