Hi Paul,

I will merge your changes and roll out a 1.4  version soon (but not too soon, I am a 
little swamped right now).
If time permits I will also roll out the Jalopy jar file into a .zip and make that a 
"true" JDEE plug-in.

Thanks for the new features!

Regards,
  Nascif




-----Original Message-----
From: Paul Landes [mailto:[EMAIL PROTECTED] 
Sent: Monday, March 08, 2004 1:56 PM
To: Nascif Abousalh-Neto
Cc: [EMAIL PROTECTED]
Subject: ANN: jde-jalopy update


I knew I should have gotten this done sooner.  I did some hacking on this because I 
couldn't get Bob's code to work (at least under XEmacs).

Since the thing is in such a flux, I'm just going to attach what I have.  I started 
with what was on your site, Nascif.

Nascif Abousalh-Neto writes:
 > Hi all,
 > 
 > I uploaded version 1.3 of jde-jalopy.el, which incorporates the method 
 > jde-jalopy-buffer posted by Bob Schellink.  > Enjoy,
 >   Nascif
 > 
 > http://home.nc.rr.com/nascifandelaine/emacs.html

----------------------------------------
;;; jde-jalopy.el --- JALOPY interface for JDE

;; Copyright (C) 2002 Nascif A. Abousalh Neto

;; Author: Nascif A. Abousalh Neto <nascif at acm dot org>
;; Maintainer: Nascif A. Abousalh Neto <[EMAIL PROTECTED]>
;; Keywords: java, beautifier, pretty printer, tools
;; Time-stamp: <2002-12-04 00:48:50 nascif>
;; 
;; Version: 1.2
;;
;; This program is free software; you can redistribute it and/or modify ;; it under 
the terms of the GNU General Public License as published by ;; the Free Software 
Foundation; either version 1, or (at your option) ;; any later version.

;; This program is distributed in the hope that it will be useful, ;; but WITHOUT ANY 
WARRANTY; without even the implied warranty of ;; MERCHANTABILITY or FITNESS FOR A 
PARTICULAR PURPOSE.  See the ;; GNU General Public License for more details.

;; A copy of the GNU General Public License can be obtained from the Free ;; Software 
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

;;; Commentary:
;; This package provides an interface from JDE (see http://jde.sunsite.dk/) to ;; 
Jalopy (see http://jalopy.sourceforge.net/).  Jalopy is a `a source code ;; formatter 
for the Sun Java programming language. It layouts any valid Java ;; source code 
according to some widely configurable rules; to meet a certain ;; coding style without 
putting a formatting burden on individual developers.'

;; Installation:
;;
;;  1) Download and install the Jalopy console plug-in package from: ;;  
http://jalopy.sourceforge.net/download.html
;;
;;  2) Put this file on your Emacs-Lisp load path and add the following into ;;  your 
.emacs startup file ;;
;;      (require 'jde-jalopy)
;;
;;  3) Customize the variable `jde-jalopy-option-path' to point to the Jalopy ;;  
installation directory. ;; ;;  4) Make sure JDE is properly configured. In particular 
set the variables ;;  jde-jdk-registry and jde-jdk so that the JVM launcher can be 
found.

;;; Usage:
;;
;;  M-x `jde-jalopy-file' to reformat the Java source file associated to the ;;  
current buffer. ;;

;;; Customization:
;;
;;  To customize the Jalopy-Emacs interface options, use the command: ;;  M-x 
`jde-jalopy-customize' 
;;
;;  The default behavior for Jalopy is to format the code according to the Sun ;;  
Code conventions. If you want to customize the formatting behavior (and ;;  Jalopy 
supports a large number of customization options), use the command: ;;  M-x 
`jde-jalopy-preferences' ;; ;;  This command will launch the Jalopy Preferences editor 
GUI-based tool. You ;;  can use it to create a file with your customized preferences. 
Point the ;;  variable `jde-jalopy-option-preferences-file' to this file. I suggest 
you ;;  save the file in the XML format, so that you can edit the preferences file ;;  
directly in an Emacs buffer later.

;;; Acknowledgements:
;;
;; This code is heavily based on jde-check.el, by Markus Mohnen.

;;; ChangeLog:
;;  1.3 - added jde-jalopy-buffer, which jalopifies a buffer in place ;; ;;  1.2 - 
jde-jalopy-buffer renamed to jde-jalopy-file to clarify its behavior. ;; ;;  1.1 - 
updates to support Jalopy 1.0b10 (changes in location of jar files) ;; ;;  1.0 - first 
version, supporting Jalopy 1.0b8

;;; Code:

(require 'jde-compile)

(if (fboundp 'jde-build-classpath)
    nil
  (require 'jde-run)
  (defalias 'jde-build-classpath 'jde-run-build-classpath-arg)
  )

(defconst jde-jalopy-version "1.0b10")

(defgroup jde-jalopy nil
  "JDE Jalopy Options"
  :group 'jde
  :prefix "jde-jalopy-option-")

(defcustom jde-jalopy-option-class "de.hunsicker.jalopy.plugin.console.ConsolePlugin"
  "*Jalopy console plug-in class.  Specifies the Jalopy console plug-in class. There 
is typically no need to change this variable."
  :group 'jde-jalopy
  :type 'string)

(defcustom jde-jalopy-option-preferences-class 
  "de.hunsicker.jalopy.swing.SettingsDialog"
  "*Jalopy console plug-in class.  Specifies the Jalopy console plug-in class. There 
is typically no need to change this variable."
  :group 'jde-jalopy
  :type 'string)

(defcustom jde-jalopy-option-path ""
  "*Specify installation path of the Jalopy Console plug-in.  This path will be used 
to find the Jalopy .jar files in order to construct a -classpath argument to pass to 
the Java interpreter. This option overrides the `jde-global-classpath' option."
  :group 'jde-jalopy
  :type 'string)

(defcustom jde-jalopy-option-read-args nil
  "*Specify whether to prompt for additional jalopy arguments. If this variable is 
non-nil, the jde-jalopy command prompts you to enter additional jalopy arguments in 
the minibuffer. These arguments are appended to those specified by customization 
variables. The JDE maintains a history list of arguments 
entered in the minibuffer."
  :group 'jde-jalopy
  :type 'boolean)

(defvar jde-interactive-jalopy-args ""
  "String of jalopy arguments entered in the minibuffer.")

(defcustom jde-jalopy-option-jvm-args nil
  "*JVM level options to pass each invocation of Jalopy.  This isn't to be confused 
with `jde-jalopy-option-command-line-args', these are for the JVM, not the Jalopy 
program.

An example of something useful here is `-Duser.longName=John Doe', then put things 
like [EMAIL PROTECTED] $user.longName$' in your class Javadoc templates."
  :group 'jde-jalopy
  :type '(repeat (string :tag "JVM option")))

(defcustom jde-jalopy-option-command-line-args ""
  "*Specify options as a string of command-line arguments.  The value of this variable 
should be a string of switches understood by Jalopy. This variable is intended to be 
used to set options not otherwise defined by this version of jde-jalopy."
  :group 'jde-jalopy
  :type 'string)

(defcustom jde-jalopy-option-encoding "Cp1252"
  "*Encoding of input files. Must be one of the JDK supported encodings."
  :group 'jde-jalopy
  :type 'string)

(defcustom jde-jalopy-option-format "AUTO"
  "*Output file format - one of UNIX, DOS, MAC, AUTO (the default) or DEFAULT (all 
case-insensitive)."
  :group 'jde-jalopy
  :type 'string)

(defcustom jde-jalopy-option-force nil
  "*Force format even if file is up-to-date." 
  :group 'jde-jalopy
  :type 'boolean)  

(defcustom jde-jalopy-option-nobackup nil
  "*If true disable the creation of keep backup files."
  :group 'jde-jalopy
  :type 'boolean)

(defcustom jde-jalopy-option-preferences-file ""
  "*Preferences file. Used to configure the many Jalopy options on how to format the 
code."
  :group 'jde-jalopy
  :type 'file)

(defcustom jde-jalopy-option-thread 1
  "*Specifies the number of processing threads to be used. This settingg should be 
equal to the number of processors your system has."
  :group 'jde-jalopy
  :type 'integer)

(defun jde-jalopy-get-java ()
  "Returns the name of the JVM launcher application."
  (if (string< jde-version "2.2.9")
      (if (eq system-type 'windows-nt)
          jde-run-java-vm-w
        jde-run-java-vm)
    (oref (jde-run-get-vm) :path)))

(defun jde-jalopy-get-options ()
  "Constructs a command-line argument string for jalopy.
The string consists of the contents of the jde-jalopy-options variable concatenated 
with the various jde-jalopy-option settings. "
  (let (options)
    (if (not (string= jde-jalopy-option-encoding ""))
        (setq options (concat options " --encoding=" jde-jalopy-option-encoding)))
    (if (not (string= jde-jalopy-option-format ""))
        (setq options (concat options " --format=" jde-jalopy-option-format)))
    (if jde-jalopy-option-force
        (setq options (concat options " --force")))
    (if jde-jalopy-option-nobackup
        (setq options (concat options " --nobackup")))
    (if (not (string= jde-jalopy-option-preferences-file ""))
        (setq options (concat options " --style=" 
                              (jde-normalize-path 
jde-jalopy-option-preferences-file))))
    (if (not (= jde-jalopy-option-thread 1))
        (setq options (concat options " --thread=" 
                              (int-to-string jde-jalopy-option-thread))))
    
    (if (not (string= jde-jalopy-option-command-line-args ""))
        (setq options (concat options " " jde-jalopy-option-command-line-args)))
    options))

(defun jde-jalopy-get-classpath ()
  "Returns a string with the classpath used to call the Jalopy console plug-in. "
  (let ((classpath) 
        (save-jde-expand-classpath-p jde-expand-classpath-p)
        (save-jde-lib-directory-names jde-lib-directory-names))
    (if jde-jalopy-option-path
        (progn
          (setq jde-expand-classpath-p t)
          (setq jde-lib-directory-names (list "^lib"))
          (setq classpath
                (concat classpath " -classpath \'"
                        (jde-build-classpath (list (jde-jalopy-get-install-path)))
                        "\'"))
          (setq jde-expand-classpath-p save-jde-expand-classpath-p)
          (setq jde-lib-directory-names save-jde-lib-directory-names))
      (if (and (boundp 'jde-global-classpath)
               jde-global-classpath)
          (setq classpath
                (concat classpath " -classpath \'"
                        (jde-build-classpath jde-global-classpath)
                        "\'"))))
    classpath))

(defun jde-jalopy-get-install-path ()
  (concat jde-jalopy-option-path
          (if (string-match ".*[/\\]$" jde-jalopy-option-path)
              "" 
            (if (eq system-type 'windows-nt) "\\" "/"))
          "lib"))

;;;###autoload
(defun jde-jalopy-customize ()
  "Customization of the group jde-jalopy."
  (interactive)
  (customize-group "jde-jalopy"))

(defun jde-jalopy-make-file-command (more-args)
  "Constructs the java jalopy command for \\[jde-jalopy-file] as:
  `jde-jalopy-option-class' + `(jde-jalopy-get-options)' +
     `jde-jalopy-option-jvm-args' MORE-ARGS + `(buffer-file-name)'."
  (concat 
   (jde-jalopy-get-java)
   (jde-jalopy-get-classpath)
   jde-jalopy-option-jvm-args
   (if (not (string= more-args "")) (concat " " more-args))
   " "
   jde-jalopy-option-class 
   " "
   (jde-jalopy-get-options) 
   " "   
   (file-name-nondirectory buffer-file-name)))

(defun jde-jalopy-make-preferences-command ()
  "Constructs the java jalopy Preferences command."
  (concat 
   (jde-jalopy-get-java)
   (jde-jalopy-get-classpath)
   " "
   jde-jalopy-option-preferences-class))

;;;###autoload
(defun jde-jalopy-file ()
  "Formats the Java source code in the current buffer.
This command invokes the code formatter specified by `jde-jalopy-class' with the 
options specified by the JDE customization variables that begin with `jde-jalopy'. If 
the variable `jde-option-read-args' is non-nil, this command reads additional 
compilation options from the minibuffer, with history enabled."
  (interactive)

  (if jde-jalopy-option-read-args
      (setq jde-interactive-jalopy-args
            (read-from-minibuffer 
             "Jalopy args: "
             jde-interactive-jalopy-args
             nil nil
             '(jde-interactive-jalopy-arg-history . 1))))

  (let ((jalopy-command
         (jde-jalopy-make-file-command jde-interactive-jalopy-args)))
    
    ;; Force save-some-buffers to use the minibuffer
    ;; to query user about whether to save modified buffers.
    ;; Otherwise, when user invokes jde-jalopy from
    ;; menu, save-some-buffers tries to popup a menu
    ;; which seems not to be supported--at least on
    ;; the PC.
    (if (and (eq system-type 'windows-nt)
             (not jde-xemacsp)) 
        (let ((temp last-nonmenu-event))
          ;; The next line makes emacs think that jde-jalopy
          ;; was invoked from the minibuffer, even when it
          ;; is actually invoked from the menu-bar.
          (setq last-nonmenu-event t)
          (save-some-buffers (not compilation-ask-about-save) nil)
          (setq last-nonmenu-event temp))
      (save-some-buffers (not compilation-ask-about-save) nil))
    (compile-internal jalopy-command "No more errors")))

(defun jde-jalopy-preferences ()
  "Launches the Jalopy Preferences editor."
  (interactive)

  (let ((jalopy-command
         (jde-jalopy-make-preferences-command)))
    
    (compile-internal jalopy-command "No more errors")))

(defun jde-jalopy-buffer ()
  "Sends the current buffer to jalopy for beautifying.  This is a interactive function 
that sends the contents of the current buffer to jalopy for compilation."
  (interactive)
  (save-some-buffers nil nil)
  (if jde-jalopy-option-read-args
      (setq jde-interactive-jalopy-args
            (read-from-minibuffer
             "Jalopy args: "
             jde-interactive-jalopy-args
             nil nil
             '(jde-interactive-jalopy-arg-history . 1))))
  (let ((path-sep (if (eq system-type 'windows-nt)
                      "\\";;windows file seperator
                    "/"));;unix file seperator
        (jalopy-compilation-buffer "*jalopy-compilation*"))
    (let ((source-file-name (buffer-name))
          (jalopy-script-home (concat jde-jalopy-option-path path-sep "bin"))
          (start (point-min))
          (end (point-max))
          (source-file-point (point))
          (legal-buffer (if (buffer-file-name)
                            "true"
                          nil))
          (jalopy-command
           (jde-jalopy-make-buffer-command jde-interactive-jalopy-args)))
      (if (get-buffer jalopy-compilation-buffer)
          (kill-buffer jalopybuf))
      (setq jalopybuf  (get-buffer-create jalopy-compilation-buffer))
      (copy-to-buffer jalopybuf (point-min) (point-max))
      (save-excursion
        (set-buffer jalopybuf)
        (if legal-buffer
            (compilation-mode jalopy-compilation-buffer))
        (apply 'call-process-region
               start
               end
               (jde-jalopy-get-java)
               t
               t
               nil
               jalopy-command
               )
        (jde-jalopy-setup-buffer)
        (if (not legal-buffer)
            (progn
              (message "The compiled window might not work correctly, because the 
buffer you are trying to beautify is not associated with any
file.")))
        )
      (goto-char source-file-point)
      )
    )
  )

(defun jde-jalopy-setup-buffer()
  "Function that sets up the jalopy buffer.  If any errors occurred, a seperate 
compilation window is displayed. Else the current buffers content are replaced by the 
beautified content."
  (let ((jalopy-compilation-ind "Listing on stdin\.\.\.\n\[ERROR\]")
        (jalopy-ok-ind "Listing on stdin\.\.\."))
    (goto-char (point-min))
    ;;if string was found, this is a jalopy file with errors
    (if (search-forward jalopy-compilation-ind nil t)
        (progn
          (forward-line -1)
          (beginning-of-line)
          (kill-line);;kill the containing the jalopy string
          (kill-line);;kill the containing the jalopy string

          (insert (concat (jde-jalopy-get-java) " "
                          (mapconcat 'identity jalopy-command " ")))
          (newline)
          (replace-regexp (format "%c.+System.in:" directory-sep-char)
                          (concat source-file-name ":"))

          (goto-char (point-min))
          (setq buffer-read-only t)
          (switch-to-buffer-other-window jalopy-compilation-buffer)
          )
      ;;if string was found, this is a jalopy file with NO errors
      (if (search-forward jalopy-ok-ind nil t)
          (let ((warn-buf-name "*jalopy-warnings*")
                (warn-regexp "^\\[WARN\\] .+?System\\.in:")
                warn-buf)
            (beginning-of-line)
            (kill-line);;kill the line containing the jalopy string
            (kill-line);;kill the line containing the jalopy string

            ;; remove warnings (assume log4j logging syntax for now)
            ;; todo: don't assume log4j output and save these in a
            ;; *jalopy-warnings* type buffer
            (save-excursion
              (beginning-of-buffer)
              (while (and (> (buffer-size) 0) (looking-at warn-regexp))

                (when (null warn-buf)
                  
                  (setq warn-buf (get-buffer warn-buf-name))
                  (if warn-buf (kill-buffer warn-buf))
                  (setq warn-buf (generate-new-buffer warn-buf-name))
                  (with-current-buffer warn-buf
                    (compilation-mode)
                    (insert (concat (jde-jalopy-get-java) " "
                                    (mapconcat 'identity jalopy-command " ")))
                    (newline)
                    (insert "jalopy warnings:")
                    (newline)))

                (let ((line (buffer-substring (save-excursion
                                                (re-search-forward
                                                 warn-regexp)
                                                (point))
                                              (save-excursion
                                                (end-of-line)
                                                (point)))))
                  (with-current-buffer warn-buf
                    (insert (concat source-file-name ":" line))
                    (newline)
                    ))

                (kill-line t)
                (beginning-of-line))

              (if warn-buf
                  (with-current-buffer warn-buf
                    (beginning-of-buffer))
                ))
                
            (erase-buffer source-file-name)
            (copy-to-buffer source-file-name (point-min) (point-max))
            (set-buffer source-file-name)

            ;; sometime package font-lock is off, why?
            (when (and (featurep 'font-lock)
                       font-lock-mode)
              (turn-off-font-lock)
              (turn-on-font-lock))

            (kill-buffer jalopy-compilation-buffer)

            (when warn-buf
              (display-buffer warn-buf)
              (with-current-buffer warn-buf
                (goto-char (point-min))
                (setq buffer-read-only t))
              ))
        (message "Error occurred");;else this is a jalopy file with errors
        (switch-to-buffer-other-window jalopy-compilation-buffer)
        ))))

(defun jde-jalopy-make-buffer-command (&optional more-args)
  "Constructs the java jalopy command for \\[jde-jalopy-buffer] as:
  `jde-jalopy-option-class' + `(jde-jalopy-get-options)' +
     `jde-jalopy-option-jvm-args' MORE-ARGS + `(buffer-file-name)'."
    (let ((add-args (cond ((null more-args) nil)
                          ((stringp more-args) (cons more-args nil))
                          ((listp (split-string
                                   (jde-jalopy-trim-options more-args))))
                          (t (error "not a list or string: `%S' more-args"))
                          )))

      (delete ""
        (append (split-string (jde-jalopy-trim-options
                               (jde-jalopy-get-classpath)))
                jde-jalopy-option-jvm-args
                (cons jde-jalopy-option-class nil)
                (split-string (jde-jalopy-trim-options
                               (jde-jalopy-get-options)))
                add-args
                )))))

(defsubst jde-jalopy-trim-options (option-str)
  (replace-in-string option-str "^[ \t\n]*\\(.*?\\)[ \t\n]*$" "\\1"))

(defun jde-jalopy-goto-error ()
  "Goto a line in a file indicated by Jalopy error or warning output.  This is useful 
for when you use the Jalopy from the command line in either an emacs shell or 
subordinate shell.  After Jalopy outputs the error line, simply go to the line and 
execute this command."
  (interactive)
  (let (start end line)
    (save-excursion
      (beginning-of-line)
      (setq start (point))
      (end-of-line)
      (setq end (point)
            line (buffer-substring start end))
      (if (string-match "^\\[[A-Z]+\\] \\([^:]+\\):\\([0-9]+\\)" line)
          (let ((filename (match-string 1 line))
                (lineno (string-to-int (match-string 2 line))))
            (find-file filename)
            (if (integerp lineno) (goto-line lineno)))
        (message "no Jalopy error output found at line")
        ))))

(provide 'jde-jalopy)

;;; jde-jalopy.el ends here


;; (defcustom jde-jalopy-option-recursive 0
;;   "*Recurse into directories, up to NUM levels. If negative, recurses indefinitely"
;;   :group 'jde-jalopy
;;   :type 'integer)

;;     (if (not (= jde-jalopy-option-recursive 0))
;;         (setq options (concat options " --recursive"
;;                               (if (> jde-jalopy-option-recursive 0)
;;                                   (concat "=" (int-to-string 
jde-jalopy-option-recursive))))))

;; (defcustom jde-jalopy-option-dest nil
;;   "*Base output directory for the formatted code.
;; If not specified the formatted code will be sent to the output buffer."
;;   :group 'jde-jalopy
;;   :type '(file :tag "Path"))

;;     (if (not (string= jde-jalopy-option-dest ""))
;;         (setq options (concat options " --dest=" jde-jalopy-option-dest)))
----------------------------------------

-- 
Paul Landes
[EMAIL PROTECTED]

Reply via email to