Debugging Maxima CAS Lisp code on Emacs - debugging

Is it possible to debug Maxima CAS Lisp code in Emacs?
It's a pain to use so many print statements all the time.

I've used two approaches over the years.
Run slime using the Maxima core file. See this email for how to do it
http://article.gmane.org/gmane.comp.mathematics.maxima.general/36029
Run Maxima but add code in the initialisation file to create a swank server then connect to that with slime-connect.
http://article.gmane.org/gmane.comp.mathematics.maxima.general/44533
Someone (Leo Butler, maybe?) on the list then suggested a neater approach than what's in that email. Unfortunately, my searching-fu has failed me and I can't find the conversation so I'll just paste what's in my ~/.maxima/swank.lisp nowadays:
(eval-when (:compile-toplevel :load-toplevel :execute)
(defvar *swank-asd*
(car (directory #P"~/.emacs.d/elpa/slime*/swank.asd")))
(when *swank-asd*
(load *swank-asd*)
(require :swank)))
(when (find-package :swank)
(swank:create-server :port 56789 :dont-close t)
;; Hack to make "q" not kill Maxima outright. Only applies from console
(in-package :maxima)
(defvar *real-continue-function* (symbol-function 'continue))
(setf (symbol-function 'continue)
(lambda (&rest args)
(let ((swank::*sldb-quit-restart* 'maxima::macsyma-quit))
(apply *real-continue-function* args))))
(format t "Swank loaded successfully"))
It starts by trying to load up swank from my Emacs directory (I install slime using Elpa). On success, or if swank was loaded anyway for some reason, it creates a server and then does the nifty "make the q key not really annoying" hack described in the second email.

Related

How to always show inline images?

I'm trying to work with inline images (e.g. for plotting data by gnuplot), and have the problem: images always inserted as links by default. I need to do some keypresses to "force" emacs to show actual image inline, instead of just file link.
E.g. I start with gnuplot code:
#+BEGIN_SRC gnuplot :file plot.png
plot sin(x)
#+END_SRC
When I press C-c C-c on this code block, it runs, and shows me results as link to image file:
#+RESULTS:
[[file:plot.png]]
If I press C-c C-x C-v (org-toggle-inline-images) twice -- link does replaced with inline image
If I run M-x org-redisplay-inline-images -- again, link does replaced with image
If I run (org-display-inline-images t t) -- again, image is shown
and so on (those options were taken from Emacs org-display-inline-images and Inline images in org-mode questions)
But I don't want to press anything special: I want images to be displayed inline by default. I've found and tried following variables:
(setq org-startup-with-inline-images t) in .emacs config
#+STARTUP: inlineimages header
(setq org-display-inline-images t)
But neither got me the behavior I want. I'm puzzled -- do I want something so unnatural?
P.S. Im' using GNU Emacs v26.1 on MacOS X, org mode v9.1.9-65, if it matters
P.P.S. Although it seems like a bug in my emacs/orgmode version, and I'm yet to report it, but meanwhile I've found following trick: (add-hook 'org-babel-after-execute-hook 'org-display-inline-images 'append) (thanks to ob-ipython authors) -- it fixes issue for me right now. Maybe will be useful for somebody else
I can reproduce the problem with:
Org mode version 9.1.9 (release_9.1.9-65-g5e4542 # /home/xyz/.emacs.d/elpa/org-plus-contrib-20190415/)
Reproduction:
Start Emacs 26.3 with emacs -Q.
M-x load-library RET org RET
Add Gnuplot to org-babel-load-languages via M-x customize-option.
Load gnuplot.el
Open the Org file with the following content and press C-c C-c on the source block.
#+STARTUP: inlineimages
Some text.
#+BEGIN_SRC gnuplot :file plot.png :results graphics
plot sin(x)
#+END_SRC
I have a similar solution as you suggested in your question, but a bit more differentiated.
Re-displaying images in large Org documents can take some time. So I do it only if the source block has the results-parameter graphics:
(require 'subr-x)
(defun org+-babel-after-execute ()
"Redisplay inline images after executing source blocks with graphics results."
(when-let ((info (org-babel-get-src-block-info t))
(params (org-babel-process-params (nth 2 info)))
(result-params (cdr (assq :result-params params)))
((member "graphics" result-params)))
(org-display-inline-images)))
(add-hook 'org-babel-after-execute-hook #'org+-babel-after-execute)
#Tobias is the best answer. I have tweaked the #Tobias code to further optimize by bounding the re-display to the current subtree and setting the REFRESH parameter to t to redisplay only if necessary.
(require 'subr-x)
(defun org+-babel-after-execute ()
"Redisplay inline images in subtree if cursor in source block with :result graphics."
(when (org-in-src-block-p)
(let (beg end)
(save-excursion
(org-mark-subtree)
(setq beg (point))
(setq end (mark)))
(when-let ((info (org-babel-get-src-block-info t))
(params (org-babel-process-params (nth 2 info)))
(result-params (cdr (assq :result-params params)))
((member "graphics" result-params)))
(org-display-inline-images nil t beg end)))))
(add-hook 'org-babel-after-execute-hook #'org+-babel-after-execute)

How to run (interpret) a Scheme program stored in a file?

At the moment I am using the REPL-feature of Petite-Chez Scheme. This is working fine for small examples etc.
However, how can I store an entire program in a file ".scm", and then run (interpret) it from the command-line ? I am familiar with the (load "C:/..") command, however this only load definitions from a file into REPL.
How do I run programs using Scheme like programs in C/C++ where I compile and then execute the binary ".exe" ?
Thanks.
Briefly, you just write your program in a file, put #!/usr/bin/scheme --script as the first line of the program, mark it executable, and run it. Here's a sample script that emulates the Unix echo command:
#!/usr/bin/scheme --script
(let ([args (cdr (command-line))])
(unless (null? args)
(let-values ([(newline? args)
(if (equal? (car args) "-n")
(values #f (cdr args))
(values #t args))])
(do ([args args (cdr args)] [sep "" " "])
((null? args))
(printf "~a~a" sep (car args)))
(when newline? (newline)))))
See section 2.6 of Using Chez Scheme for details.
If you want an actual executable there are several implementations that supports compilation to native executable. Racket is one of them and it supports many different scheme versions and dialects (R5RS, R6RS, Racket, ...). There are many more. Chicken (R5RS + SRFIs), Gambit (R5RS + SRFIs) and Bigloo (R5RS, + SRFIs) to name a few.

ANSI Coloring in Compilation Mode

Have anyone added support for ansi-color in compilation-mode Emacs? If so what property/attribute does the color-writing program have to check for in order to make sure its active terminal supports ANSI-escape coloring.
There's already a function for applying color to comint buffers. You simply need to enable it on compilation buffers:
(require 'ansi-color)
(defun colorize-compilation-buffer ()
(toggle-read-only)
(ansi-color-apply-on-region compilation-filter-start (point))
(toggle-read-only))
(add-hook 'compilation-filter-hook 'colorize-compilation-buffer)
Color writing programs should check the TERM environment variable and the terminfo database to check if the terminal supports color. In practice, a lot of programs ignore this and rely on a user setting. Emacs will set the compilation terminal type to dumb by default but this can be overriden by setting the compilation-environment variable.
Update: Note that in Emacs 24.5 the two calls to (toggle-read-only) in the code above are not needed.
My optimized solution which don't pollute M-x grep (only for M-x compile):
(ignore-errors
(require 'ansi-color)
(defun my-colorize-compilation-buffer ()
(when (eq major-mode 'compilation-mode)
(ansi-color-apply-on-region compilation-filter-start (point-max))))
(add-hook 'compilation-filter-hook 'my-colorize-compilation-buffer))
As of emacs 28.1, this is now a built in !
(require 'ansi-color)
(add-hook 'compilation-filter-hook 'ansi-color-compilation-filter)
Or with use-package
(use-package ansi-color
:hook (compilation-filter . ansi-color-compilation-filter))
Riffing on #gavenkoa's solution:
(when (require 'ansi-color nil t)
(defun my-colorize-compilation-buffer ()
(when (eq major-mode 'compilation-mode)
(ansi-color-apply-on-region compilation-filter-start (point-max))))
(add-hook 'compilation-filter-hook 'my-colorize-compilation-buffer))
This will not block errors but will still not raise an error if ansi-color is unavailable. Personally, I find the wildcard catch semantics of ignore-error distasteful.
Riffing on #stribb's solution, which riffs on #gavenkoa's solution, this is how to set it up with the awesome use-package:
(use-package ansi-color
:config
(defun my-colorize-compilation-buffer ()
(when (eq major-mode 'compilation-mode)
(ansi-color-apply-on-region compilation-filter-start (point-max))))
:hook (compilation-filter . my-colorize-compilation-buffer))
As of 2020, the most modern way appears to be the xterm-color Emacs package.
See my answer on the duplicate question for details.

slime-fancy not loading with emacs sbcl slime windows configuration

I have slime with sbcl working in emacs 24.1 but can not get a slime repl to open.
I can use M-x slime to make a connection to sbcl in a inferior-lisp buffer but I can not invoke the slime-repl or get a nice lisp auto-indent when editing lisp files even though I am loading the slime-fancy contrib in .emacs. I don't get any error messages during start-up.
When I try M-x slime-repl I get [No match].
my .emacs file:
(setq inferior-lisp-program "sbcl")
(add-to-list 'load-path "c:/home/bin/emacs/site-lisp/slime/")
(require 'slime)
(require 'slime-autoloads)
(slime-setup '(slime-fancy))
I used this method for the installation:
http://www.pchristensen.com/blog/articles/installing-sbcl-emacs-and-slime-on-windows-xp
I have noticed a pattern that almost everything I try with Python and Clojure works as described and almost nothing I try related to common lisp works. I have also tried cusp with eclipse. I am willing to try yet another approach if there is something more recent for common lisp in windows.
After playing with Sujoy's answer and trimming it down to get it to work, I realized my original problem was caused by the (require 'slime) statement. The following .emacs file gets the slime repl to open as expected.
(setq inferior-lisp-program "sbcl")
(require 'slime-autoloads)
(slime-setup '(slime-fancy))
Only 'slime-fancy will not setup the REPL. Try the below snippet. Put it in a buffer and eval.
Of course, you do not need to setup the hyperspec root as well, but that helps a lot :)
EDIT: missed out on the autoloads I am using, so here's the full config.
the keybinding (using minor-mode keymap, global mapping can be used just as easily)
(define-key my-keys-map (kbd "<f5>") 'slime)
the autoloads
;; slime mode
(autoload 'slime "my-slime" "Slime mode." t)
(autoload 'slime-connect "my-slime" "Slime mode." t)
Here's my-slime.el
(provide 'my-slime)
(eval-after-load "slime"
(setq slime-lisp-implementations
(slime-setup '(slime-asdf
slime-autodoc
slime-editing-commands
slime-fancy
slime-fontifying-fu
slime-fuzzy
slime-indentation
slime-mdot-fu
slime-package-fu
slime-references
slime-repl
slime-sbcl-exts
slime-scratch
slime-xref-browser))
(slime-autodoc-mode)
(setq slime-complete-symbol*-fancy t
slime-complete-symbol-function 'slime-fuzzy-complete-symbol
slime-when-complete-filename-expand t
slime-truncate-lines nil
slime-autodoc-use-multiline-p t)
(add-hook 'lisp-mode-hook (lambda () (slime-mode t)))))
(require 'slime)

How do you load a file into racket via command line?

I have been trying to launch a racket program from the commandline (via 'racket') but have not been having success. According to the documentation (here http://docs.racket-lang.org/reference/running-sa.html#%28part._mz-cmdline%29) passing -f followed by a file should evaluate that file. However, I can't seem to get this to work. As a test, I made the following file:
;test.rkt
#lang racket
(define a 1)
Then, running it in racket (supposedly loading the file) and attempting to recall the value of a:
racket -f test.rkt -i
Welcome to Racket v5.1.1.
> a
reference to undefined identifier: a
My end goal is to be able to launch a different program from a shell script using the --main option combined with loading the definitions with -f to start up execution, just have become a bit baffled since I can't seem to get this trivial bit working.
Removing the #lang line works, but it means that your code is no longer a module, which makes it a pretty bad idea. To start racket on a given module file, all you need is to just run racket on the file, nothing else is needed. For example, put this in test.rkt:
#lang racket/base
(printf "Hi\n")
and just run it with racket test.rkt. If you want to have command-line flags, you can use (current-command-line-arguments) to get a vector of additional command-line arguments, but there's also the racket/cmdline library that makes it much easier to have standard kinds of flag processing. Here's an example for that:
#lang racket/base
(require racket/cmdline)
(define excitedness "")
(define mode "Hi")
(command-line
#:multi
[("-e" "--excited") "add excitedness levels"
(set! excitedness (string-append excitedness "!"))]
#:once-each
[("-b" "--bye") "turn on \"bye\" mode"
(set! mode "Bye")])
(printf "~a~a\n" mode excitedness)
and you can now run it with racket test.rkt <flags>. See also the Racket Guide's section on scripts for making your test.rkt even easier to run.
Finally, there is the --main approach that you've seen -- to use that, your module needs to provide a main function that receives all the command-line flags as arguments. For example:
#lang racket/base
(require racket/string)
(provide main)
(define (main . xs)
(printf "You gave me ~s flags: ~a\n"
(length xs) (string-join xs ", ")))
and to run it:
racket -t /tmp/y -m -- foo bar baz
The flag breakdown is: -t requires your module, -m causes racket to run your main function, and -- means that the following flags are all passed to your program. You can combine the flags like so:
racket -tm- /tmp/y foo bar baz
and that would be something that you'd usually put in your script trampoline as described in that guide section.
And, of course, this is all described in great details in the reference manual.
Remove the #lang racket header from your file:
;test.rkt
(define a 1)

Resources