Is there a simple method for writing traces from multiple sbcl threads to standard output through MCLIDE/swank? - sbcl

Using SBCL, I'm writing a small server and I would like to trace the server thread, but when I use mclide/swank, I do not see any output from the server thread.
? (require 'sb-posix)
NIL
? (sb-thread:make-thread (lambda () (format t "hi from the thread")))
?
When I try the same thing from sbcl directly, I see what I expect:
(require 'sb-posix)
; loading system definition from
; /opt/local/var/macports/software/sbcl/1.0.39_0+html+threads/opt/local/lib/sbcl/sb-grovel/sb-grovel.asd
; into #
; registering # as SB-GROVEL
("SB-POSIX" "SB-GROVEL" "ASDF")
(sb-thread:make-thread (lambda () (format t "hi from the thread")))
hi from the thread#
*
Does swank have issues capturing standard output from non-foreground threads? If I used slime, would this kind of thing work?

Related

Run Lisp functions on load

Whenever I (load "program.lisp") using the following code I get the error: "Error: Unexpected end of #input stream "program.lisp"
(defun theProgram ()
(reset)
(print "Hello Kappa")
(setentries)
(startloop)
(loop for x in mylist collect (splitremove))
(loop for x in numlist collect (getgrades))
(loop for x in namelist collect (getprint))
(loop for x in printlist collect (andprint)))
(theProgram)
I know the last line is the problem and it will work fine if it is not included however I need the program to startup on load, how can I achieve this?
Edit: I should note that setentries calls a (read) and so does startloop. I am using GNU common lisp 2.6 with gcl interpreter.
The actual error was that the (read) part in the functions did not have stream declared so it was reading the input from the script as it was running rather than from the terminal.

Display contents of remote HTML in Emacs

I am aware about w3m integration with Emacs but I am exhausted to make it run on my W7/x64: there is a permanent segmentation fault of w3m binary here.
I wonder if there is an alternative way to display remote HTML in Emacs possibly preliminary filtered in the way it is done by Readability/GetPocket etc. services? I do not need a navigation there so cleared contents would be perfect.
Thanks,
trunk / Emacs 24.4:
M-x eww RET (URL) RET
Emacs 24.1 - 24.3:
M-x browse-url-emacs RET (URL) RET
M-x load-library RET shr RET
M-x shr-render-buffer RET
(defun my-render-url (url)
"Render URL as HTML."
(interactive "sURL: ")
(require 'shr)
(let ((buf (save-window-excursion (browse-url-emacs url))))
(shr-render-buffer buf)))
Edit: or this, which has absolutely no error handling, but is considerably faster (which I attribute to browse-url-emacs using url-retrieve-synchronously, where as this is asynchronous). Feel free to make improvements :)
(defun my-render-url (url)
"Render URL as HTML."
(declare (obsolete eww "24.4"))
(interactive "sURL: ")
(require 'shr)
(url-retrieve
url
(lambda (&optional status cbargs)
(let ((markup (current-buffer)))
(delete-region (point-min) (1+ url-http-end-of-headers))
(shr-render-buffer markup)
(kill-buffer markup)))))

Emacs function automaticly accepting prompt

I am trying to find a way to automatically accept the first proposal from the minibuffer.
(defun find-file-at-point-without-prompt ()
(interactive )
(find-file-at-point)
)
Calling results in the prompt: "Find file or URL: ......". I just want an automatic "yes".
Passing arguments does not work. It might be interesting for other cases as well. I used a macro before that would just call find-file-at-point followed by a RET.
It seems there is no variable to automatically accept the prompt.
You can redefine a function ffap-read-file-or-url by removing a part which is doing the prompt. It remains something like this
(defun ffap-read-file-or-url (prompt guess)
"Read file or URL from minibuffer, with PROMPT and initial GUESS."
(or guess (setq guess default-directory))
(let (dir)
;; Tricky: guess may have or be a local directory, like "w3/w3.elc"
;; or "w3/" or "../el/ffap.el" or "../../../"
(or (ffap-url-p guess)
(progn
(or (ffap-file-remote-p guess)
(setq guess
(abbreviate-file-name (expand-file-name guess))
))
(setq dir (file-name-directory guess))))
;; Do file substitution like (interactive "F"), suggested by MCOOK.
(or (ffap-url-p guess) (setq guess (substitute-in-file-name guess)))
;; Should not do it on url's, where $ is a common (VMS?) character.
;; Note: upcoming url.el package ought to handle this automatically.
guess))

How to yank in a search in an emacs lisp function

(defun search-for-what-is-just-killed ()
(interactive)
(search-forward latestkillringvariable? nil t)
)
How to use "yank" in an emacs lisp function?
You can access directly the kill-ring list to access the latest kill:
(substring-no-properties (car kill-ring))
The substring-no-properties bit is important since text is kept in the kill ring with additional properties (like fontification specific to a particular mode and you'll probably want to strip those).

Emacs mode to edit JSON

Does anybody know a good Emacs mode to edit JSON? An app I am working on uses a JSON based communication protocol and having the data nicely indented and syntax-highlighted would help me a lot in the process of figuring it out.
+1 for Josh's json-mode -- works well for me. I added
(defun beautify-json ()
(interactive)
(let ((b (if mark-active (min (point) (mark)) (point-min)))
(e (if mark-active (max (point) (mark)) (point-max))))
(shell-command-on-region b e
"python -m json.tool" (current-buffer) t)))
and
(define-key json-mode-map (kbd "C-c C-f") 'beautify-json)
to json-mode.el to make the shell command invocation easier.
UPDATE: For those of you with a need/desire to do this with unicode, see my question here. The upshot is rather than using:
python -m json.tool
you will want to use
python -c 'import sys,json; data=json.loads(sys.stdin.read()); print json.dumps(data,sort_keys=True,indent=4).decode("unicode_escape").encode("utf8","replace")'
This both beautifies the JSON as well as preserving the original Unicode content.
js-mode supports syntax highlighting and indentation for json files.
This is as of Emacs 23.2, when espresso-mode was incorporated into Emacs and renamed js-mode.
Check it out:
http://www.nongnu.org/espresso/
Have you tried Steve Yegge's js2-mode for Emacs?
If you want something lightweight try this major-mode I hacked together: https://github.com/joshwnj/json-mode
It's actually no more than some extra syntax highlighting on top of javascript-mode, but for my purposes I've found it to work quite well.
Another common use-case is auto-formatting a JSON file (eg. if it's whitespace-compressed and you want more readability). To do this I'm just piping the buffer through a command-line script: C-u M-|
I've prepared a workaround for js2-mode so it parses json files without errors.
You can find it in my comment: http://code.google.com/p/js2-mode/issues/detail?id=50#c7
(I wanted to post it as a comment do J.F. Sebastian solution, but it seems I'm not allowed to do so (no 'add comment' link))
json.el by Edward O'Connor is part of GNU Emacs since 23.1 (2008).
While it isn't a syntax highlighter, it has a useful function to format JSON:
M-x json-pretty-print-buffer RET
So, if you have a recent version of Emacs, there is no need for jq or python -m json.tool.
Since JSON is a subset of YAML, yaml-mode works too (I don't know how it compares to js-mode and json-mode, though).
Install (from emacs): M-x package-install yaml-mode.
Association of yaml-mode with YAML and JSON files, in ~/.emacs.d/init.el:
(add-to-list 'auto-mode-alist '("\\.yaml$" . yaml-mode))
(add-to-list 'auto-mode-alist '("\\.json$" . yaml-mode))
JSON is supported by espresso-mode
js3-mode:https://github.com/thomblake/js3-mode
js3-mode is an improved js2-mode
This package can be installed by package-list-packages command
I will also second Josh's json-mode, but also recommend flymake-json as an addition. It helps highlight syntax errors.
I don't like using python -mjson.tool because it reorders items in JSON objects.
I find (prog-indent-sexp) works just fine to reindent, and using jsonlint
instead of python -mjson.tool works for pretty printing/reformatting in beautify-json
(eval-after-load "json-mode"
'(progn
(require 'flymake-json)
;; flymake-cursor displays error in minibuffer message area instead of requiring hover
(require 'flymake-cursor)
(add-hook 'json-mode-hook 'flymake-json-load)
(define-key json-mode-map "\C-c\C-n" (function flymake-goto-next-error))
)
)
I've expanded on Mariusz Nowak's workaround, to make it usable as a major mode in its own right. Little modification was required beyond simply deriving the mode; the only change Nowak's work actually needed was the ability to recognize buffers not associated with files, or associated with files whose names don't end in .json, as JSON, which we accomplish with a buffer-local variable.
Here's the augmented workaround:
(make-variable-buffer-local 'js2-parse-as-json)
(defadvice js2-reparse (before json)
(setq js2-buffer-file-name buffer-file-name))
(ad-activate 'js2-reparse)
(defadvice js2-parse-statement (around json)
(if (and (= tt js2-LC)
js2-buffer-file-name
(or js2-parse-as-json
(string-equal (substring js2-buffer-file-name -5) ".json"))
(eq (+ (save-excursion
(goto-char (point-min))
(back-to-indentation)
(while (eolp)
(next-line)
(back-to-indentation))
(point)) 1) js2-ts-cursor))
(setq ad-return-value (js2-parse-assign-expr))
ad-do-it))
(ad-activate 'js2-parse-statement)
(define-derived-mode json-mode js2-mode "JSON"
"Major mode for editing JSON data."
:group 'json
(setq js2-parse-as-json t)
(js2-reparse t))
(add-to-list 'auto-mode-alist '("\\.json$" . json-mode))
If you already use js2-mode, this may be a better option than js-mode plus flymake-json because you need not install anything new (js2-mode already does syntax checking, no need for an external tool), and because this mode will inherit your js2-mode configuration, which js-mode will not.
I would also recommand js2-mode.
JSON stands for JavaScript Object Notation. It's not another language and it's even not a data container like yaml or xml are. JSON could be used as a data container if there's no function (or in this case we should say method) inside a JSON object, but it's not the primary goal of JSON :-)
var myJSObject = {
attr: {foo: "bar", baz: ["quux", "truc", "pouet"]},
fooAlert: function (num) {
alert(this.attr.foo+' '+num);
}
};
myJSObject.fooAlert(42);