How to clear SML Buffer in Emacs when using SML mode. - sml

I am using Emacs with SML mode. Sometimes I need to clean the SML buffer. How can I do that.

Standard ML of New Jersey is a functional programming language that is a variant of ML. Usually it's used in EMACS with a plugin. Since I usually just kill the buffer and start a new one there wasn't the need for a clearscreen. However this might be useful:
http://www.standardml.org/Basis/manpages.html.

(let ((sml-process (get-process "sml")))
(when sml-process
(quit-process sml-process)))
(sleep-for 0.25)
(let ((sml-buffer (get-buffer "*sml*")))
(when sml-buffer
(kill-buffer sml-buffer)))

Related

How can I get readline/rlwrap-like functionality when using clojure.main/repl?

How can I get readline-like (or rlwrap-like) functionality from my REPL when I use the repl function from clojure.main?
The background to this is that I'm utilizing and customizing the break function from The Joy of Clojure, First Edition. I'm using it from inside the lein repl REPL. When my "breakpoint" kicks in, the readline-like functionality of Leiningen's REPL is gone, which is kind of inconvenient. My muscle memory makes me hit ↑ followed quickly by Enter. Before I can stop myself, I've got this in my terminal:
debug=> ^[[A
CompilerException java.lang.RuntimeException: Unable to resolve symbol: in this context, compiling:(/tmp/form-init13211381000659590518.clj:1:1)
And now my REPL is stuck and I have to kill the terminal or the process to get out. I'd like very much if I could either get readline working in this second-level REPL or at least prevent this common issue from derailing my debug sessions.
You should use rebel readline, a new repl for clojure developed by bhauman the same guy who brought is figwheel.
https://github.com/bhauman/rebel-readline
It has rlwrap features, syntax highlighting and multi line code editing all in the terminal.
I'm not sure the rlwrap utility would help there, because the inner REPL is held by the outer one. So the input is being controlled by Java code, not the rlwrap tool.
You are causing an exception since you input a wrong value. I remember, the clojure.main/repl function might take an additional argument to handle exceptions. Probably, you could handle it somehow and just print a string "wrong input value" instead. Take a look at the documentation for REPL.
Also, you may implement your own REPL for debugging. Long ago, I used to write some kind of it, here what I've got:
(defn repl []
(let [input (read)]
(if (= input 'q)
nil
(do
(try
(let [result (eval input)]
(println result))
(catch Exception e
(println e)))
(recur)))))
That function just prompts for a proper Clojure expression in an endless loop, evaluates it and prints the result. In case of a wrong input, it prints the error and carries on. To leave the REPL, input q.
Example:
(repl)
(+ 1 2) ;; 2
fsdf8 9_fsd ;; prints a stack trace
q ;; returns nil and exit
Try Emacs with Cider as your repl. When you (break) you'll be bumped out of the Cider repl and into the Emacs Minibuffer, where your standard emacs editing shortcuts (upon which readline is modeled) continue to apply.

List buffers associated with files?

I'm very new to using lisp, so I'm sorry if this is a trivial question. I haven't been able to find solutions after a while googling, though I'm sure that this is fault on my part.
So. I'm trying to write a command which will revert all open buffers. Simple. I just do
(setq revert-without-query (buffer-list))
(mapc 'revert-buffer (buffer-list))`
Unfortunately, this ends up failing if there are any buffers which aren't associated with files- which is to say, always.
Doing C-x C-b to list-buffers prints something like
CRM Buffer Size Mode File
init.el 300 Emacs-lisp ~/.spacemacs.d/init.el
%scratch% 30 Test
Ok. Easy enough. If I was allowed to mix lisp and python, I'd do something like
(setq revert-without-query [b for b in buffer-list if b.File != ""])
;; Or would I test for nil? Decisions, decisions...
Upon some digging, I found that there exists remove-if. Unfortunately, being completely new to lisp, I have no idea how to access the list, their attributes, or... well... anything. Mind helping me out?
One possibility would be checking buffer-file-name which will return nil if the buffer isn't visiting a file, eg.
(cl-loop for buf in (buffer-list)
if (buffer-file-name buf)
collect buf)
or
(cl-remove-if-not 'buffer-file-name (buffer-list))
You probably want to revert dired directories also. Any type of buffer can have its own specialized revert (see revert-buffer-function). So you probably want to check for both buffer-file-name and dired-directory being non-nil.
(dolist (b (buffer-list))
(when (buffer-live-p b)
(with-current-buffer b
(when (or buffer-file-name dired-directory)
(revert-buffer 'ignore-auto 'noconfirm)))))
You can also use the ignore-errors hammer, but you're probably better off fixing corner cases as you encounter them.

How do I interactively read the input text for this Emacs function?

I am new to Emacs functions. Today is my first attempt to create a function.
I know that count-matches will tell me how many times a regex appears in the rest of the buffer, but most of the time I need to count from the beginning of the buffer. So I tried this:
(defun count-matches-for-whole-buffer (text-to-count)
"Opens the ~/.emacs.d/init.el file"
(interactive "sText-to-count:")
(beginning-of-buffer)
(count-matches text-to-count))
I put this in ~/.emacs.d/init.el and then do "eval-buffer" on that buffer.
So now I have access to this function. And if I run it, it will ask me for text to search for.
But the function only gets as far as this line:
beginning-of-buffer
I never get the count. Why is that?
Two things.
You should use (goto-char (point-min)) instead of beginning-of-buffer.
count-matches will not display messages when called from lisp code unless you provide a parameter indicating so.
Try this code:
(defun count-matches-for-whole-buffer (text-to-count)
(interactive "sText-to-count:")
(count-matches text-to-count (point-min) (point-max) t))

How to autoformat (not just auto-indent) C++ code in emacs?

How can I auto-format C++ code in emacs following GNU-style?
There's this auto-newlines thing: https://www.gnu.org/software/emacs/manual/html_node/ccmode/Auto_002dnewlines.html, but even when I set style to GNU it doesn't put the return value on a separate line from the function name.
I also want something that adds spaces between arguments in an argument-list. And something I can "run" on code after I've written (not just moves things around as I type)
Already been answered here. There's a tool called astyle (Artistic Style) that formats the code in C++.
(defun astyle-this-buffer (pmin pmax)
(interactive "r")
(shell-command-on-region pmin pmax
"astyle" ;; add options here...
(current-buffer) t
(get-buffer-create "*Astyle Errors*") t))

Clojurescript very slow on compiling

I am trying clojurescript and find it takes a long time to compile a very simple clojurescript source file into js. I can't believe this.
time cljsc hello.cljs '{:optimizations :advanced}' > hello.js
real 1m27.324s
user 1m2.412s
sys 0m0.676s
The snippet is from Clojurescript's github quick start page:
(ns hello)
(defn ^:export greet [n]
(str "Hello " n))
Leaving out the :optimizations option, I still find it takes a long time:
time cljsc hello.cljs > hello.js
real 0m10.867s
user 0m22.301s
sys 0m0.412s
Is that normal ? Or how can I speed up that ?
By calling cljsc you are firing up a JVM every single time you compile, which has to load tons of code, and then do the actual compiling. The JVM startup time alone is painful.
The general workflow is to not use cljsc, but keep a JVM open and compile with it every time. A common way to do this is to use lein-cljsbuild, which I highly recommend.
Taking ten seconds to compile doesn't seem totally crazy to me, because it has to compile all of the standard cljs library to javascript as well as your trivial program. As for the advanced optimizations, that's probably just how long it takes to do anything. It's reading and optimizing thousands of lines of cljs.core javascript, and probably also the google closure libraries that (I think) are used by cljs.core.