This page is no longer maintained — Please continue to the home page at www.scala-lang.org

sbt emacs mode

4 replies
Grey
Joined: 2009-01-03,
User offline. Last seen 42 years 45 weeks ago.
Should have sent to scala-tools initially.  Sorry for double post.

The following is a small emacs elisp script to run sbt as an inferior mode process from emacs.  It uses emacs' compile.el to identify error messages and then jump to errors in scala source files.


;; Support for running sbt in inferior mode.

(eval-when-compile (require 'cl))
(require 'tool-bar)
(require 'compile)

(defgroup sbt nil
  "Run SBT REPL as inferior of Emacs, parse error messages."
  :group 'tools
  :group 'processes)

(defconst sbt-copyright    "Copyright (C) 2008 Raymond Paul Racine")
(defconst sbt-copyright-2  "Portions Copyright (C) Free Software Foundation")

(defconst sbt-version      "0.01")
(defconst sbt-author-name  "Raymond Racine")
(defconst sbt-author-email "ray [dot] racine [at] gamail [dot] com")

(defconst sbt-legal-notice
  "This 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, or (at your option) any later version.  This 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 Emacs;
see the file `COPYING'.  If not, write to the Free Software Foundation, Inc.,
59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.")

(defgroup sbt nil
  "Support for sbt build REPL."
  :group  'sbt
  :prefix "sbt-")

(defcustom sbt-program-name "sbt"
  "Program invoked by the `run-sbt' command."
  :type 'string
  :group 'sbt)

(defun sbt-build-buffer-name (mode)
 "*Scala Build Tool*")

(defun sbt-shell ()
  "Launch the sbt shell."
  (interactive)
  (compile "cd /code/cdap2/cdap2-paxos; sbt" t)
  (pop-to-buffer (sbt-build-buffer-name nil))
  (end-of-buffer))

(defun sbt-clear ()
  "Clear (erase) the SBT buffer."
  (interactive)
  (with-current-buffer (sbt-build-buffer-name nil)
    (let ((inhibit-read-only t))
      (erase-buffer))))

(customize-set-variable 'scala-compile-error-regex 
            '("^\\[error\\] \\([.a-zA-Z0-9/-]+[.]scala\\):\\([0-9]+\\):" 1 2 nil 2 nil)) 
(customize-set-variable 'compilation-buffer-name-function 'sbt-build-buffer-name) 
(customize-set-variable 'compilation-error-regexp-alist (list scala-compile-error-regex))
(customize-set-variable 'compilation-mode-font-lock-keywords
            '(("^\\[error\\] Error running compile:"
               (0 compilation-error-face))
              ("^\\[warn\\][^\n]*"
               (0 compilation-warning-face))
              ("^\\(\\[info\\]\\)\\([^\n]*\\)"
               (0 compilation-info-face)
               (1 compilation-line-face))
              ("^\\[success\\][^\n]*"
               (0 compilation-info-face))))

(customize-set-variable 'comint-prompt-read-only t)
(customize-set-variable 'compilation-buffer-name-function
            'sbt-build-buffer-name)
(customize-set-variable 'compilation-error-regexp-alist
            (list scala-compile-error-regex)) 
(set 'compilation-auto-jump-to-first-error t)

(ansi-color-for-comint-mode-on)

;; Locate the project root directory from the source buffer location.

(defun sbt-project-dir-p (path)
  "Does a project/build.properties exists in the given path."
  (file-exists-p (concat path "/project/build.properties")))

(defun sbt-at-root (path)
  "Determine if the given path is root."
  (equal path (sbt-parent-path path)))

(defun sbt-parent-path (path)
  "The parent path for the given path."
  (file-truename (concat path "/..")))

;; Search up the directory tree until directory with a "project" subdir
;; is found with build.properties
(defun sbt-find-path-to-project ()
  "Move up the directory tree for the current buffer until root or a directory with a project/build.properities is found."
  (interactive)
  (let ((fn (buffer-file-name)))
    (let ((path (file-name-directory fn)))
      (while (and (not (sbt-project-dir-p path))
          (not (sbt-at-root path)))
    (setf path (file-truename (sbt-parent-path path))))
      (print path)
      path)))


L Jantzen
Joined: 2009-01-27,
User offline. Last seen 42 years 45 weeks ago.
Re: sbt emacs mode

Grey wrote:
>
> Should have sent to scala-tools initially. Sorry for double post.
>
> The following is a small emacs elisp script to run sbt as an inferior mode
> process from emacs. It uses emacs' compile.el to identify error messages
> and then jump to errors in scala source files.
>
>

Sorry for awakening this old thread, but I have a question. Its probably
more emacs-related than anything else, but I have a problem with
ansi-control chars when entering sbt-shell mode:

-*- mode: compilation; default-directory: "~/dev/scala/myproject/" -*-
Comint started at Sun Aug 9 21:56:56

cd /home/leif/dev/scala/myproject; sbt
[info] Building project myproject 1.0 using
sbt.DefaultProject
[info]  with sbt 0.4.6 and Scala 2.7.4
[info] No actions specified, interactive session started.
Execute 'help' for more information.
>

How do I get rid of the ansi control characters, or how do I get the
sbt-shell to interpret them correctly?

I have added the following to my .emacs: (snagged from
http://www.grokblok.com/2007/12/13/emacs-shell-ansi-colors.html)

(autoload 'ansi-color-for-comint-mode-on "ansi-color" nil t)
(add-hook 'shell-mode-hook 'ansi-color-for-comint-mode-on)

I have also tried to create a ~/.emacs_bash with the following two lines:

alias ls=ls
unset LS_COLORS

Any pointers?

Mark Harrah
Joined: 2008-12-18,
User offline. Last seen 35 weeks 3 days ago.
Re: sbt emacs mode

On Sunday 09 August 2009, L Jantzen wrote:
> Grey wrote:
> > Should have sent to scala-tools initially. Sorry for double post.
> >
> > The following is a small emacs elisp script to run sbt as an inferior
> > mode process from emacs. It uses emacs' compile.el to identify error
> > messages and then jump to errors in scala source files.
>
> Sorry for awakening this old thread, but I have a question. Its probably
> more emacs-related than anything else, but I have a problem with
> ansi-control chars when entering sbt-shell mode:
>
> -*- mode: compilation; default-directory: "~/dev/scala/myproject/" -*-
> Comint started at Sun Aug 9 21:56:56
>
> cd /home/leif/dev/scala/myproject; sbt
> [info] Building project myproject 1.0 using
> sbt.DefaultProject
> [info]  with sbt 0.4.6 and Scala 2.7.4
> [info] No actions specified, interactive session started.
> Execute 'help' for more information.
>
>
> How do I get rid of the ansi control characters, or how do I get the
> sbt-shell to interpret them correctly?
>
> I have added the following to my .emacs: (snagged from
> http://www.grokblok.com/2007/12/13/emacs-shell-ansi-colors.html)
>
> (autoload 'ansi-color-for-comint-mode-on "ansi-color" nil t)
> (add-hook 'shell-mode-hook 'ansi-color-for-comint-mode-on)
>
> I have also tried to create a ~/.emacs_bash with the following two lines:
>
> alias ls=ls
> unset LS_COLORS
>
> Any pointers?

From the sbt side of things, you can disable the control characters by adding
the option:

-Dsbt.log.noformat=true

to your sbt startup script.

Thanks,
Mark

Grey
Joined: 2009-01-03,
User offline. Last seen 42 years 45 weeks ago.
Re: sbt emacs mode
While you can disable them in sbt and use emacs fontlock to get whatever colors you'd like (that is what I do to colorized Maven output in mvn.el) the sbt ansi colors work fine for me.

I specifically have

(ansi-color-for-comint-mode-on)

in sbt.el and it works fine for me.

I just tried it, if I do a M-x sbt-shell, select the *Scala Build Tool* buffer and then M-x ansi-color-for-comint-mode-off I see the escape codes.  If I then M-x ansi-for-comint-mode-on everything works fine.  I use the latest release of emacs on linux, don't need to autoload any special ansi support just straight emacs.

It should work.

Ray


On Sun, Aug 9, 2009 at 4:15 PM, L Jantzen <ljantzen [at] gmail [dot] com> wrote:



Grey wrote:
>
> Should have sent to scala-tools initially.  Sorry for double post.
>
> The following is a small emacs elisp script to run sbt as an inferior mode
> process from emacs.  It uses emacs' compile.el to identify error messages
> and then jump to errors in scala source files.
>
>

Sorry for awakening this old thread, but I have a question.  Its probably
more emacs-related than anything else, but I have a problem with
ansi-control chars when entering sbt-shell mode:

-*- mode: compilation; default-directory: "~/dev/scala/myproject/" -*-
Comint started at Sun Aug  9 21:56:56

cd /home/leif/dev/scala/myproject; sbt
 [0m[ [0minfo [0m]  [0mBuilding project myproject 1.0 using
sbt.DefaultProject [0m
 [0m[ [0minfo [0m]  [0m   with sbt 0.4.6 and Scala 2.7.4 [0m
 [0m[ [0minfo [0m]  [0mNo actions specified, interactive session started.
Execute 'help' for more information. [0m
>

How do I get rid of the ansi control characters, or how do I get the
sbt-shell to interpret them correctly?

I have added the following to my .emacs: (snagged from
http://www.grokblok.com/2007/12/13/emacs-shell-ansi-colors.html)

(autoload 'ansi-color-for-comint-mode-on "ansi-color" nil t)
(add-hook 'shell-mode-hook 'ansi-color-for-comint-mode-on)

I have also tried to create a ~/.emacs_bash with the following two lines:

alias ls=ls
unset LS_COLORS

Any pointers?


--
View this message in context: http://www.nabble.com/-scala-tools--sbt-emacs-mode-tp24026627p24890724.html
Sent from the Scala - Tools mailing list archive at Nabble.com.


L Jantzen
Joined: 2009-01-27,
User offline. Last seen 42 years 45 weeks ago.
Re: sbt emacs mode

Mark Harrah wrote:
>
> On Sunday 09 August 2009, L Jantzen wrote:
>>
>> Sorry for awakening this old thread, but I have a question. Its probably
>> more emacs-related than anything else, but I have a problem with
>> ansi-control chars when entering sbt-shell mode:
>>
>> How do I get rid of the ansi control characters, or how do I get the
>> sbt-shell to interpret them correctly?
>>
>>
>> Any pointers?
>
> From the sbt side of things, you can disable the control characters by
> adding
> the option:
>
> -Dsbt.log.noformat=true
>
> to your sbt startup script.
>
> Thanks,
> Mark
>
>

Thanks, that will suffice for the moment. :-)

:-lj

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland