I am trying to make a simple form that will automate the loading of Slynk on an image.
So far, I have this:
(progn
(load "/home/user/asdf.lisp")
(format t "~% Loaded ASDF")
(push "/home/user/.emacs.d/elpa/sly-20180708.712/slynk/" asdf:*central-registry*)
(format t "~% Added SLYNK path to ASDF:*CENTRAL-REGISTRY*")
(asdf:load-system :slynk)
(format t "~% Loaded ASDF system")
(slynk:create-server :port 4007 :dont-close t)
(format t "~% Created SLYNK server on port 4007")
(setf slynk:*use-dedicated-output-stream* nil)
(format t "~% Set *USE-DEDICATED-OUTPUT-STREAM* to NIL"))
The issue is that whenever I try to evaluate this form at the REPL, I get the following error:
Error: "ASDF" is not a known package.
Restart actions (select using :continue):
0: Return NIL
1: Return to Top Level (an "abort" restart).
2: Abort entirely from this (lisp) process.
[1] CL-USER(4):
No prints yet appear prior to that point. That is, it seems to error on the first push line. My understanding is that it has not yet loaded the package and tries to refer to its symbol.
How can I change the form to make it work? There are similar situations lower in the form.
P.S. When each line is executed by itself in the given order, everything works perfectly.
The error comes from read as
it tries to parse the whole progn form and sees the asdf: package prefix.
You must load asdf before the reader sees the code with the
asdf: package prefix and load slynk before you use the :slynk
prefix, in other words, feed your progn forms to Lisp one by one (as you say in your PS).
Another way is to put everything inside your progn in a separate
file and load it. You would also want to make "20180708.712" and 4007 variables, e.g.,
(defvar *sly-version* "20180708.712")
(defvar *slynk-port* 4007)
(load "load-and-start-slynk.lisp")
and in load-and-start-slynk.lisp:
(load "/home/user/asdf.lisp")
(format t "~% Loaded ASDF")
(push (concatenate 'string "/home/user/.emacs.d/elpa/sly-"
*sly-version* "/slynk/")
asdf:*central-registry*)
(format t "~% Added SLYNK path to ASDF:*CENTRAL-REGISTRY*")
(asdf:load-system :slynk)
(format t "~% Loaded ASDF system")
(slynk:create-server :port *slynk-port* :dont-close t)
(format t "~% Created SLYNK server on port ~D" *slynk-port*)
(setq slynk:*use-dedicated-output-stream* nil)
(format t "~% Set *USE-DEDICATED-OUTPUT-STREAM* to NIL")
PS. Just to show how to abuse Lisp power, here is how to do what you are trying to do:
(defun load-and-start-slynk (port version)
(load "/home/user/asdf.lisp")
(format t "~% Loaded ASDF")
(push (concatenate 'string "/home/user/.emacs.d/elpa/sly-" version "/slynk/")
(symbol-value (find-symbol "*CENTRAL-REGISTRY*" '#:asdf)))
(format t "~% Added SLYNK path to ASDF:*CENTRAL-REGISTRY*")
(funcall (find-symbol "LOAD-SYSTEM" '#:asdf) :slynk)
(format t "~% Loaded ASDF system")
(funcall (find-symbol "CREATE-SERVER" '#:slynk) :port port :dont-close t)
(format t "~% Created SLYNK server on port ~D" port)
(setf (symbol-value (find-symbol "*USE-DEDICATED-OUTPUT-STREAM*" '#:slynk)) nil)
(format t "~% Set *USE-DEDICATED-OUTPUT-STREAM* to NIL"))
Do not do this.
Related
Under Linux, eshell-autojump will do case sensitive matching which I just find a nuisance. I've tried to circumvent this by advising eshell/j with a eshell-under-windows-p that always returns t but to my chagrin eshell-under-windows-p invoked in eshell/j is unaffected by cl-letf. I've modified my eshell/j a bit to give me some debug info:
;; Modified eshell/j inside eshell-autojump.el to this
(defun eshell/j (&rest args) ; all but first ignored
"Jump to a directory you often cd to.
This compares the argument with the list of directories you usually jump to.
Without an argument, list the ten most common directories.
With a positive integer argument, list the n most common directories.
Otherwise, call `eshell/cd' with the result."
(setq args (eshell-flatten-list args))
(let ((path (car args))
(candidates (eshell-autojump-candidates))
(case-fold-search (eshell-under-windows-p))
result)
(when (not path)
(setq path 10))
(message "case-fold-search = %S" case-fold-search)
(message "eshell-under-windows-p returns %s from inside eshell/j" (eshell-under-windows-p))
(if (and (integerp path) (> path 0))
(progn
(let ((n (nthcdr (1- path) candidates)))
(when n
(setcdr n nil)))
(eshell-lisp-command (mapconcat 'identity candidates "\n")))
(while (and candidates (not result))
(if (string-match path (car candidates))
(setq result (car candidates))
(setq candidates (cdr candidates))))
(eshell/cd result))))
My init.el adds the advice to attempt to make eshell/j caseless by trying to trick it to think we are on Windows:
;; Added to init.el
(require 'eshell-autojump)
(advice-add 'eshell/j :around
(lambda (orig-fun &rest xs)
(cl-letf (((symbol-function 'eshell-under-windows-p) (lambda () t)))
(progn (message "eshell-under-windows-p returns %s from lambda" (eshell-under-windows-p)) (apply orig-fun xs)))))
But all I get in Messages buffer when I try to jump in eshell is:
;; I get in *Messages*
eshell-under-windows-p returns t from lambda
case-fold-search = nil
eshell-under-windows-p returns nil from inside eshell/j
My rookie knowledge of elisp is not enough to wrestle with probable scoping issues here. Can anyone decode why eshell-under-window-p is unaffected when called from eshell/j here?
I've found the answer. cl-letf does not work for byte compiled functions. As eshell-autojump is a package it gets byte compiled upon installation and cl-letf cannot be used to modify it's internal behavior. I had to resort to redefining the eshell/j which is a suboptimal solution.
I'm working at the Racket REPL via racket-mode in Emacs, writing code in multiple modules.
Is there a way to execute a single form from a module I'm not currently 'in', in the context of its own module?
For instance:
web.rkt
#lang racket
(require "view.rkt")
(define (display-default-view)
(display (default-view)))
view.rkt
#lang racket
(provide default-view)
(define default-text "Hello")
(define (default-view)
(string-append default-text " world"))
If I call racket-run from web.rkt I get a prompt saying web.rkt>. If I then run (display-default-view) I get "Hello world".
If I then visit view.rkt and change the default-text definition to:
(define default-text "Hi")
and re-evaluate the default-text definition, it evaluates fine, and my prompt still says web.rkt>.
When I enter default-text at the REPL I get "Hi". But when I run (display-default-view) I still get "Hello world". I'm presuming this is because all I've done is define a new default-text in web.rkt.
I'd expect to see output change to "Hi world" --- i.e. the behaviour of the view.rkt module to be updated. Just like I'd see if default-text lived in the web.rkt module.
The idea of dynamically re-evaluating single forms at the repl to change program behaviour is terrific, but it seems to not quite work here.
Is there a way to get this behaving as I would expect in racket-mode? Or if not, a mechanism to just enter a module, without running it, so that I can build something myself to do an enter-execute-exit dance?
Updated, simpler answer:
We can evaluate forms in the REPL in the current file's namespace by entering that namespace in the REPL, evaluating these forms, and then re-entering our original namespace. The easiest way to do this seems to be wrapping these forms with functions to enter the current file's namespace (before) and re-entering the original namespace (after) and then sending all of this into the existing Racket-mode code for evaluating forms in the REPL.
We can do this by building a string of our wrapped commands, writing it to a temporary buffer, marking the whole buffer as our region, and then sending it to racket-send-region.
(defun my-racket-current-namespace-wrapped-commands (buffer-file-string commands)
"generate string containing commands wrapped with Racket functions to enter
the current-namespace and then exit it upon finishing"
(concat "(require (only-in racket/enter enter!))"
"(enter! (file "
buffer-file-string
"))"
commands
"(enter! #f)"))
(defun my-racket--send-wrapped-current-namespace (commands)
"sends wrapped form of commands to racket-send-region function via a temporary buffer"
(let ((buffer-file-string (prin1-to-string buffer-file-name)))
(with-temp-buffer
(insert
(my-racket-current-namespace-wrapped-commands buffer-file-string commands))
(mark-whole-buffer)
(racket-send-region (point-min) (point-max)))))
(defun my-racket-send-region-current-namespace (start end)
"send region to REPL in current namespace"
(interactive "r")
(unless (region-active-p)
(user-error "No region"))
(let ((commands (buffer-substring (region-beginning) (region-end))))
(my-racket--send-wrapped-current-namespace commands)))
(defun my-racket-send-last-sexp-current-namespace ()
"send last sexp to REPL in current namespace"
(interactive)
(let ((commands (buffer-substring (my-racket--repl-last-sexp-start)
(point))))
(my-racket--send-wrapped-current-namespace commands)))
(defun my-racket--repl-last-sexp-start ()
"get start point of last-sexp
permanent (and slightly simplified) copy of racket mode's last-sexp-start private function"
(save-excursion
(progn
(backward-sexp)
(if (save-match-data (looking-at "#;"))
(+ (point) 2)
(point)))))
These functions should mostly be version agnostic - they only depend on racket-send-buffer (which seems likely to remain in future versions).
Edit 1: (Note - this does not seem to work as is for newer versions of Racket-mode. This worked as of the April 01, 2018 release, but newer versions seem to have refactored some of the internals this relied on. In almost all cases, the code above is preferable.)
Sorry, I believe that I originally misunderstood the question. It looks like you mean executing the command straight from view.rkt without having to manually change the namespace in the REPL. I didn't see any built-in functionally in racket-mode that does this, but it's not too hard to write an Elisp wrapper around this process. The following imports in enter!, switches to the current buffer's file's namespace, sends the code in the region, and then switches back to the original namespace. The code used is very similar to what racket-mode uses for racket-send-region and racket-send-last-sexp.
(defun my-racket-send-region-current-namespace (start end)
"Send the current region to the Racket REPL as that namespace"
(interactive "r")
(when (and start end)
(racket-repl t)
(racket--repl-forget-errors)
(let ((proc (racket--get-repl-buffer-process)))
(with-racket-repl-buffer
(save-excursion
(goto-char (process-mark proc))
(insert ?\n)
(set-marker (process-mark proc) (point))))
(comint-send-string proc "(require (only-in racket/enter enter!))")
(comint-send-string proc
(concat "(enter! (file "
(prin1-to-string buffer-file-name)
"))"))
(comint-send-string proc "\n"))
(racket--repl-show-and-move-to-end)
(racket--send-region-to-repl start end)
(let ((proc (racket--get-repl-buffer-process)))
(with-racket-repl-buffer
(save-excursion
(goto-char (process-mark proc))
(insert ?\n)
(set-marker (process-mark proc) (point))))
(comint-send-string proc "(enter! #f)")
(comint-send-string proc "\n"))))
(defun my-racket-send-last-sexp-current-namespace ()
(interactive)
(my-racket-send-region-current-namespace
(save-excursion
(backward-sexp)
(if (save-match-data (looking-at "#;"))
(+ (point) 2)
(point)))
(point)))
Note that if you're using this frequently, this function could probably use more error checking (e.g. the import of require/enter will clobber any previous definition of enter!).
I've also kept the original text below about how to manually switch namespaces in the REPL, in case it helps.
You can use the function enter! in the racket/enter module to switch namespaces to modify definitions in the namespace of the other file.
After calling racket-run in web.rkt, you could do the following in the REPL:
(display-default-view) ;; output is "Hello world"
(require racket/enter)
(enter! "view.rkt") ;; change namespace to view.rkt
(define default-text "Hi")
(enter! #f) ;; return to original namespace
(display-default-view) ;; output is "Hi world"
See the Racket documentation for more details on interactive module loading.
the first time for me to ask something here although I took a lot of benefits as reader since years.
I have a problem regarding loading in emacs 24.4.1 the mail client wanderlust in its newest version fom melpa.org. I solved all dependencies and added all packages (flim, semi, apel) to my load path. But despite of it I got throwing an error during load procedure.
I tried to load emacs in debug mode and got the error focused in the the file semi-def.el, where the following error pop up:
Debugger entered--Lisp error: (void-function static-cond)
(static-cond ((featurep (quote xemacs)) (defalias (quote mime-should-use-popup-menu) (function (lambda nil (and window-system (mouse-event-p last-command-event))))) (defalias (quote mime-select-menu-alist) (function (lambda (title menu-alist) (if (mime-should-use-popup-menu) (let (ret) (popup-menu ...) (recursive-edit) ret) (cdr (assoc ... menu-alist))))))) (t (defalias (quote mime-should-use-popup-menu) (function (lambda nil (and window-system (memq (event-basic-type last-command-event) (quote ...)))))) (defalias (quote mime-select-menu-alist) (function (lambda (title menu-alist) (if (mime-should-use-popup-menu) (x-popup-menu (list ... ...) (list title ...)) (cdr (assoc ... menu-alist))))))))
It seems to me that it can't be an error in the file itself. I guess there is somthing wrong in the interpretation of that function, which my emacs version is still missing for a proper interpretation something like a missing library. I tried also in that file to replace
(eval-when-compile (require 'cl))
with
(eval-when-compile (require 'cl-lib))
But no success so far.
Any help in the issue is much appreciated.
Regards Falk
I have defined a .dir-locals.el file with the following content:
((python-mode . ((cr/virtualenv-name . "saas"))))
In my .emacs I have the following function to retrieve this value and provide a virtualenv path:
(defun cr/virtualenv ()
(cond (cr/virtualenv-name (format "%s/%s" virtualenv-base cr/virtualenv-name))
((getenv "EMACS_VIRTUAL_ENV") (getenv "EMACS_VIRTUAL_ENV"))
(t "~/.emacs.d/python")))
Finally, in my python-mode-hook list, I have this hook function:
(add-hook 'python-mode-hook 'cr/python-mode-shell-setup)
(defun cr/python-mode-shell-setup ()
(message "virtualenv-name is %s" cr/virtualenv-name)
(let ((python-base (cr/virtualenv)))
(cond ((and (fboundp 'ipython-shell-hook) (file-executable-p (concat python-base "/bin/ipython")))
(setq python-python-command (concat python-base "/bin/ipython"))
(setq py-python-command (concat python-base "/bin/ipython"))
(setq py-python-command-args '( "-colors" "NoColor")))
(t
(setq python-python-command (concat python-base "/bin/python"))
(setq py-python-command (concat python-base "/bin/python"))
(setq py-python-command-args nil)))))
When I open a new python file, the message logged by cr/python-mode-shell-setup indicates that cr/virtualenv-name is nil. However, when I C-h v the name, I get "saas" instead.
Obviously there's a load order issue here; is there a way to have my mode hook statements respond to directory-local variables?
This happens because normal-mode calls (set-auto-mode) and (hack-local-variables) in that order.
However hack-local-variables-hook is run after the local variables have been processed, which enables some solutions:
The first is to make Emacs run a new "local variables hook" for each major mode:
(add-hook 'hack-local-variables-hook 'run-local-vars-mode-hook)
(defun run-local-vars-mode-hook ()
"Run a hook for the major-mode after the local variables have been processed."
(run-hooks (intern (concat (symbol-name major-mode) "-local-vars-hook"))))
(add-hook 'python-mode-local-vars-hook 'cr/python-mode-shell-setup)
(Your original function can be used unmodified, with that approach.)
A second option is to utilise the optional LOCAL argument to add-hook that makes the specified function buffer-local. With this approach you could write your hook as follows:
(add-hook 'python-mode-hook 'cr/python-mode-shell-setup)
(defun cr/python-mode-shell-setup ()
(add-hook 'hack-local-variables-hook
(lambda () (message "virtualenv-name is %s" cr/virtualenv-name)
(let ((python-base (cr/virtualenv)))
(cond ((and (fboundp 'ipython-shell-hook) (file-executable-p (concat python-base "/bin/ipython")))
(setq python-python-command (concat python-base "/bin/ipython"))
(setq py-python-command (concat python-base "/bin/ipython"))
(setq py-python-command-args '( "-colors" "NoColor")))
(t
(setq python-python-command (concat python-base "/bin/python"))
(setq py-python-command (concat python-base "/bin/python"))
(setq py-python-command-args nil)))))
nil t)) ; buffer-local hack-local-variables-hook
i.e. python-mode-hook runs first and registers the anonymous function with hack-local-variables-hook for the current buffer only; and that function is then called after the local variables have been processed.
Lindydancer's comment prompts a third approach. It's not nearly as clean as the other two, but proved interesting regardless. I didn't like the idea of causing (hack-local-variables) to be called twice, but I see that if you set the local-enable-local-variables buffer-locally, it prevents (hack-local-variables) from doing anything, so you could do this:
(defun cr/python-mode-shell-setup ()
(report-errors "File local-variables error: %s"
(hack-local-variables)))
(set (make-local-variable 'local-enable-local-variables) nil)
(let ((python-base (cr/virtualenv)))
...))
Obviously that modifies the normal sequence of execution a little, so side effects may be possible. I was worried that if the same major mode is set by a local variable comment in the file, this might cause infinite recursion, but that doesn't actually appear to be a problem.
Local variable header comments (e.g. -*- mode: foo -*-) are handled by (set-auto-mode), so those are fine; but a mode: foo Local Variables: comment seems like it would be an issue as it is handled by (hack-local-variables), and so if the mode is set that way I thought it would cause recursion.
In practice I was able to trigger the problem by using a simple function as a 'mode' which did nothing more than try to run its hooks; however testing with a 'proper' mode did not exhibit the problem, so it's probably safe in reality. I didn't look into this further (as the other two solutions are much cleaner than this), but I would guess the delayed mode hooks mechanism probably explains it?
Is there any way to mitigate this behaviour? It would be best if it returned nil or threw an error.
I.e. suppose I'm trying to connect to an unbound socket on localhost - in that case make-network-process blocks and even if afterwards someone binds the socket, it doesn't realize it happened, so, it is basically stuck.
OK, had to pay more attention to the documentation. There's a :nowait key argument, which will prevent this behaviour. Below is an example code that handles it:
(defun haxe-network-process-sentinel (process input)
(message "haxe-network-process-sentinel <%s>" input)
(when (stringp input)
(cond
((or
(haxe-string-starts-with input "failed with code")
(haxe-string-starts-with input "connection broken by"))
(setq haxe-network-status 'error))
((string= input "open")
(setq haxe-network-status 'open))
(t (setq haxe-network-status 'open)
(haxe-append-server-response input)))))
;;;###autoload
(defun haxe-connect-to-compiler-server ()
"Starts HaXe compilations server and connects to it.
This function is bound to \\[haxe-connect-to-compiler-server]"
(interactive)
(let ((old-proc (get-process haxe-compiler-process)))
(if (and old-proc (equal (process-status old-proc) 'open))
(setq haxe-network-process old-proc)
(haxe-log 3 "Trying to connect to HaXe compiler on %s:%s"
haxe-server-host haxe-server-port)
(while (not (eql haxe-network-status 'open))
(setq haxe-network-process
(make-network-process
:name haxe-compiler-process
:family 'ipv4
:host haxe-server-host
:nowait t
:service haxe-server-port
;; :buffer haxe-network-process-buffer
:sentinel #'haxe-network-process-sentinel
:filter #'haxe-listen-filter))
(sleep-for 1))
(haxe-log 3 "Connected to HaXe compiler"))))