Skip to content

Instantly share code, notes, and snippets.

@tstgruby
Created September 27, 2012 15:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tstgruby/3794507 to your computer and use it in GitHub Desktop.
Save tstgruby/3794507 to your computer and use it in GitHub Desktop.
Subject: [PATCH] Refactor macros for proper byte compilation. (ensime issue #162)
diff --git a/src/main/elisp/ensime-config.el b/src/main/elisp/ensime-config.el
index 52dca88..54af9d3 100644
--- a/src/main/elisp/ensime-config.el
+++ b/src/main/elisp/ensime-config.el
@@ -20,6 +20,7 @@
;; MA 02111-1307, USA.
+(eval-and-compile (require 'ensime-macros))
(defvar ensime-config-file-name ".ensime"
"The default file name for ensime project configurations.")
diff --git a/src/main/elisp/ensime-debug.el b/src/main/elisp/ensime-debug.el
index 304b05f..85863c8 100644
--- a/src/main/elisp/ensime-debug.el
+++ b/src/main/elisp/ensime-debug.el
@@ -22,6 +22,8 @@
(eval-and-compile
(require 'cl))
+(eval-and-compile (require 'ensime-macros))
+
(defgroup ensime-db nil
"Customization of ensime debugger support."
:group 'ensime
diff --git a/src/main/elisp/ensime-macros.el b/src/main/elisp/ensime-macros.el
new file mode 100644
index 0000000..9a29cda
--- /dev/null
+++ b/src/main/elisp/ensime-macros.el
@@ -0,0 +1,64 @@
+;;; ensime-macros.el
+;;
+;;;; License
+;;
+;; Copyright (C) 2010 Aemon Cannon
+;;
+;; 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 2 of
+;; the License, 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.
+;;
+;; You should have received a copy of the GNU General Public
+;; License along with this program; if not, write to the Free
+;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
+;; MA 02111-1307, USA.
+
+(eval-when-compile (require 'cl))
+
+(defmacro ensime-with-conn-interactive (conn-sym &rest body)
+ "Surround body forms with a check to see if we're connected.
+If not, message the user."
+ `(let* ((,conn-sym (or (ensime-current-connection)
+ (ensime-prompt-for-connection))))
+ (if conn
+ (progn ,@body)
+ (message
+ "This command requires a connection to an ENSIME server."))))
+
+(defmacro* when-let ((var value) &rest body)
+ "Evaluate VALUE, if the result is non-nil bind it to VAR and eval BODY.
+
+\(fn (VAR VALUE) &rest BODY)"
+ `(let ((,var ,value))
+ (when ,var ,@body)))
+
+(defmacro* ensime-with-popup-buffer ((name &optional connection select)
+ &body body)
+ "Similar to `with-output-to-temp-buffer'.
+Bind standard-output and initialize some buffer-local variables.
+Restore window configuration when closed.
+
+NAME is the name of the buffer to be created.
+CONNECTION is the value for `ensime-buffer-connection'.
+If nil, no explicit connection is associated with
+the buffer. If t, the current connection is taken.
+"
+ `(let* ((vars% (list ,(if (eq connection t) '(ensime-connection) connection)))
+ (standard-output (ensime-make-popup-buffer ,name vars%)))
+ (with-current-buffer standard-output
+ (prog1
+ (progn
+ ,@body)
+ (assert (eq (current-buffer) standard-output))
+ (setq buffer-read-only t)
+ (set-window-point (ensime-display-popup-buffer ,(or select 'nil))
+ (point))))))
+
+
+(provide 'ensime-macros)
diff --git a/src/main/elisp/ensime-refactor.el b/src/main/elisp/ensime-refactor.el
index 4e31567..e0752d8 100644
--- a/src/main/elisp/ensime-refactor.el
+++ b/src/main/elisp/ensime-refactor.el
@@ -20,6 +20,8 @@
;; MA 02111-1307, USA.
+(eval-when-compile (require 'ensime-macros))
+
(defvar ensime-refactor-id-counter 0
"Each refactoring is given a unique id.")
@@ -211,4 +213,4 @@
-(provide 'ensime-refactor)
\ No newline at end of file
+(provide 'ensime-refactor)
diff --git a/src/main/elisp/ensime-search.el b/src/main/elisp/ensime-search.el
index ed97562..65a34b8 100644
--- a/src/main/elisp/ensime-search.el
+++ b/src/main/elisp/ensime-search.el
@@ -19,7 +19,9 @@
;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
;; MA 02111-1307, USA.
-(eval-when-compile (require 'cl))
+(eval-when-compile
+ (require 'cl)
+ (require 'ensime-macros))
(defvar ensime-search-mode nil
diff --git a/src/main/elisp/ensime-test.el b/src/main/elisp/ensime-test.el
index e704964..0af6901 100644
--- a/src/main/elisp/ensime-test.el
+++ b/src/main/elisp/ensime-test.el
@@ -20,6 +20,8 @@
;; MA 02111-1307, USA.
+(eval-and-compile (require 'ensime))
+
(defvar ensime-testing-buffer "*ensime-tests*"
"Contains the output of all the tests. Also tracks all the testing-specific
buffer-local variables.")
@@ -1491,4 +1493,4 @@
-(provide 'ensime-test)
\ No newline at end of file
+(provide 'ensime-test)
diff --git a/src/main/elisp/ensime-undo.el b/src/main/elisp/ensime-undo.el
index 7ea938a..4358714 100644
--- a/src/main/elisp/ensime-undo.el
+++ b/src/main/elisp/ensime-undo.el
@@ -19,6 +19,7 @@
;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
;; MA 02111-1307, USA.
+(eval-and-compile (require 'ensime-macros))
(defvar ensime-undo-info-buffer-name "*ENSIME-Undo*")
@@ -85,4 +86,4 @@
-(provide 'ensime-undo)
\ No newline at end of file
+(provide 'ensime-undo)
diff --git a/src/main/elisp/ensime.el b/src/main/elisp/ensime.el
index 3a33ff4..e962f33 100644
--- a/src/main/elisp/ensime.el
+++ b/src/main/elisp/ensime.el
@@ -33,8 +33,9 @@
(when (<= emacs-major-version 21)
(error "Ensime requires an Emacs version of 21, or above")))
-(eval-and-compile
- (require 'cl))
+(eval-when-compile
+ (require 'cl)
+ (require 'ensime-macros))
(require 'thingatpt)
(require 'comint)
@@ -150,6 +151,66 @@
"Single character offset to convert between emacs and
0-based character indexing.")
+;;; macros
+
+(defmacro ensime-assert-connected (&rest body)
+ "Surround body forms with a check to see if we're connected.
+If not, message the user."
+ `(if (ensime-connected-p)
+ (progn ,@body)
+ (message "This command requires a connection to an ENSIME server.")))
+
+(defmacro ensime-assert-buffer-saved-interactive (&rest body)
+ "Offer to save buffer if buffer is modified. Execute body only if
+buffer is saved."
+ `(if (buffer-modified-p)
+ (if (y-or-n-p "Buffer must be saved to continue. Save now? ")
+ (progn
+ (ensime-save-buffer-no-hooks)
+ ,@body))
+ (progn
+ ,@body)))
+
+(defmacro* ensime-with-connection-buffer ((&optional process) &rest body)
+ "Execute BODY in the process-buffer of PROCESS.
+If PROCESS is not specified, `ensime-connection' is used.
+
+\(fn (&optional PROCESS) &body BODY))"
+ `(with-current-buffer
+ (process-buffer (or ,process (ensime-connection)
+ (error "No connection")))
+ ,@body))
+
+(defmacro* ensime-with-inspector-buffer ((name object &optional select)
+ &body body)
+ "Extend the standard popup buffer with inspector-specific bindings."
+ `(ensime-with-popup-buffer
+ (,name t ,select)
+ (use-local-map ensime-popup-inspector-map)
+ (when (not ensime-inspector-paging-in-progress)
+
+ ;; Clamp the history cursor
+ (setq ensime-inspector-history-cursor
+ (max 0 ensime-inspector-history-cursor))
+ (setq ensime-inspector-history-cursor
+ (min (- (length ensime-inspector-history) 1)
+ ensime-inspector-history-cursor))
+
+ ;; Remove all elements preceding the cursor (the 'redo' history)
+ (setq ensime-inspector-history
+ (subseq ensime-inspector-history
+ ensime-inspector-history-cursor))
+
+ ;; Add the new history item
+ (push ,object ensime-inspector-history)
+
+ ;; Set cursor to point to the new item
+ (setq ensime-inspector-history-cursor 0)
+
+ )
+ ,@body
+ ))
+
;;;;; ensime-mode
(defgroup ensime-mode nil
@@ -615,7 +676,6 @@ Do not show 'Writing..' message."
(ensime-inferior-connect config server-proc)))
)))
-
(defun ensime-reload ()
"Re-initialize the project with the current state of the config file.
Analyzer will be restarted. All source will be recompiled."
@@ -733,23 +793,6 @@ defined."
(let ((config (ensime-config (ensime-current-connection))))
(plist-get config :root-dir))))
-(defmacro ensime-assert-connected (&rest body)
- "Surround body forms with a check to see if we're connected.
-If not, message the user."
- `(if (ensime-connected-p)
- (progn ,@body)
- (message "This command requires a connection to an ENSIME server.")))
-
-(defmacro ensime-with-conn-interactive (conn-sym &rest body)
- "Surround body forms with a check to see if we're connected.
-If not, message the user."
- `(let* ((,conn-sym (or (ensime-current-connection)
- (ensime-prompt-for-connection))))
- (if conn
- (progn ,@body)
- (message
- "This command requires a connection to an ENSIME server."))))
-
(defun ensime-swank-port-file ()
"Filename where the SWANK server writes its TCP port number."
(ensime-temp-file-name (format "ensime_port.%S" (emacs-pid))))
@@ -1072,17 +1115,6 @@ The default condition handler for timer functions (see
(defun ensime-strip-dollar-signs (str)
(replace-regexp-in-string "\\$" "" str))
-(defmacro ensime-assert-buffer-saved-interactive (&rest body)
- "Offer to save buffer if buffer is modified. Execute body only if
-buffer is saved."
- `(if (buffer-modified-p)
- (if (y-or-n-p "Buffer must be saved to continue. Save now? ")
- (progn
- (ensime-save-buffer-no-hooks)
- ,@body))
- (progn
- ,@body)))
-
(defun ensime-assert-executable-on-path (name)
(when (null (executable-find name))
(error (concat name " not found on your emacs exec-path. "
@@ -1094,14 +1126,6 @@ buffer is saved."
str)
-(defmacro* when-let ((var value) &rest body)
- "Evaluate VALUE, if the result is non-nil bind it to VAR and eval BODY.
-
-\(fn (VAR VALUE) &rest BODY)"
- `(let ((,var ,value))
- (when ,var ,@body)))
-
-
(defmacro destructure-case (value &rest patterns)
"Dispatch VALUE to one of PATTERNS.
A cross between `case' and `destructuring-bind'.
@@ -1732,16 +1756,6 @@ This doesn't mean it will connect right after Ensime is loaded."
process))
-(defmacro* ensime-with-connection-buffer ((&optional process) &rest body)
- "Execute BODY in the process-buffer of PROCESS.
-If PROCESS is not specified, `ensime-connection' is used.
-
-\(fn (&optional PROCESS) &body BODY))"
- `(with-current-buffer
- (process-buffer (or ,process (ensime-connection)
- (error "No connection")))
- ,@body))
-
(defun ensime-connect (host port)
"Connect to a running Swank server. Return the connection."
(interactive (list
@@ -3750,37 +3764,6 @@ inspect the package of the current source file."
"Type and package inspector key bindings.")
-(defmacro* ensime-with-inspector-buffer ((name object &optional select)
- &body body)
- "Extend the standard popup buffer with inspector-specific bindings."
- `(ensime-with-popup-buffer
- (,name t ,select)
- (use-local-map ensime-popup-inspector-map)
- (when (not ensime-inspector-paging-in-progress)
-
- ;; Clamp the history cursor
- (setq ensime-inspector-history-cursor
- (max 0 ensime-inspector-history-cursor))
- (setq ensime-inspector-history-cursor
- (min (- (length ensime-inspector-history) 1)
- ensime-inspector-history-cursor))
-
- ;; Remove all elements preceding the cursor (the 'redo' history)
- (setq ensime-inspector-history
- (subseq ensime-inspector-history
- ensime-inspector-history-cursor))
-
- ;; Add the new history item
- (push ,object ensime-inspector-history)
-
- ;; Set cursor to point to the new item
- (setq ensime-inspector-history-cursor 0)
-
- )
- ,@body
- ))
-
-
;; Interface
(defvar ensime-message-function 'message)
@@ -4048,29 +4031,6 @@ See `view-return-to-alist' for a similar idea.")
"So we can query later whether this is a popup buffer."))
;; Interface
-(defmacro* ensime-with-popup-buffer ((name &optional connection select)
- &body body)
- "Similar to `with-output-to-temp-buffer'.
-Bind standard-output and initialize some buffer-local variables.
-Restore window configuration when closed.
-
-NAME is the name of the buffer to be created.
-CONNECTION is the value for `ensime-buffer-connection'.
-If nil, no explicit connection is associated with
-the buffer. If t, the current connection is taken.
-"
- `(let* ((vars% (list ,(if (eq connection t) '(ensime-connection) connection)))
- (standard-output (ensime-make-popup-buffer ,name vars%)))
- (with-current-buffer standard-output
- (prog1
- (progn
- ,@body)
- (assert (eq (current-buffer) standard-output))
- (setq buffer-read-only t)
- (set-window-point (ensime-display-popup-buffer ,(or select 'nil))
- (point))))))
-
-
(defun ensime-make-popup-buffer (name buffer-vars)
"Return a temporary buffer called NAME.
The buffer also uses the minor-mode `ensime-popup-buffer-mode'."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment