How to get a comprehensive listing of all defined variables (and their values)? - elisp

How can one list all the globally defined variables (ideally with their global-scope values) for the current Emacs session?

Looking at the source code for describe-variable and obarray, it seems that the following should give you what you want.
(defun global-bindings ()
(let (res)
(mapatoms (lambda (vv)
(when (and (boundp vv)
(not (keywordp vv))
(get vv 'variable-documentation))
(push (cons vv (symbol-value vv)) res))))
res))

Related

Guile/Scheme - redefine another module's internal function

Let's say I have the following two files:
;; demo.scm
(define-module (demo)
#:export (f))
(define (g x) 1)
(define (f x) (g x))
... and in the same directory:
;; use-demo.scm
(add-to-load-path ".")
(use-modules (demo))
(define (g x) (+ x 1))
(display (f 5))
(newline)
Running use-demo.scm in Guile (2), I get the output 1. So it looks like the function f has 'closed over' the function g that's defined in module demo. Is there any way to get around this? I really want to use the version of g that I've redefined in use-demo.scm.
OK, just for the record, I did some research and am posting the solution to this specific problem in case it helps someone.
The trick is to not redefine g locally, but rather to 'inject' the new function into the demo module's mapping of names to values.
(add-to-load-path ".")
(use-modules (demo))
(module-define! (resolve-module '(demo)) 'g
(lambda (x) (+ x 1)))
(display (f 5))
(newline)
If you have specific functions that you'd like to be able to override, you could make them configurable using parameters. This has some advantages:
You don't need to call reload-module to put the module back in its original configuration.
The changes only apply for the scope of the code which needs the modified behaviour.
It works properly when using multiple threads.
Obviously, the main disadvantage is that you need to add some boilerplate for each function that you want to allow to be overridden (although that's what hygienic macros are for, hehe).
The following code may work. I haven't run it.
;; demo.scm
(define-module (demo)
#:export (f))
(define (default-g x) 1)
(define p (make-parameter default-g))
(define (f x) ((p) x))
;; use-demo.scm
(add-to-load-path ".")
(use-modules (demo))
(define (my-g x) (+ x 1))
(parameterize ((## (demo) p) my-g)
(display (f 5))
(newline))
Obviously, if you can provide some additional information about what the application for this capability is, I might be able to suggest alternative approaches (there are a few others).

Scheme - run two procedures from arguments sequentially?

I am trying to create a function that takes two functions as arguments and executes both of them.
I tried using cond, but it only executes action1.
(define seq-action
(lambda (action1 action2)
(cond
((procedure? action1) (action1))
((procedure? action2) (action2)))))
I feel like it shouldn't be too hard to run one after the other. They don't need to run at the same time.
I have tried simply (action1) (action2) side-by-side, but it only returns action2. Here is what I define for action1 and action2:
(define ax
(lambda ()
(+ 1 2)))
(define bx
(lambda ()
(+ 5 2)))
Executing one procedure after the other is as simple as this:
(define seq-action
(lambda (action1 action2)
(action1)
(action2)))
However, the above will only return the result of the last procedure. If you need both results then return a list with the values, like this:
(define seq-action
(lambda (action1 action2)
(list (action1) (action2))))
Alternatively, you could return multiple values simultaneously using the values procedure:
(define seq-action
(lambda (action1 action2)
(values (action1) (action2))))
For retrieving both values after calling the last version, you need to use let-values.
A Scheme function will return whatever the last thing it evaluated is. If you need both returns you could try packing them into a list and returning it.
If you want to check arguments against procedure? before calling, you may use the following solution.
(define seq-action
(lambda (action1 action2)
(and (procedure? action1) (action1))
(and (procedure? action2) (action2))))

In Scheme, when a recursive function returns a list, I cannot assign it to a variable

I have the following code:
(define (get-data)
(define port (open-input-file "../data/problem11.txt"))
(define field 0)
(define (get-fields)
(define field (read port))
(cond ((not (eof-object? field))
(cons field (get-fields)))
(else '())))
(define returned (get-fields))
(close-input-port port)
returned)
(get-data)
I can't find any problem with my code according to the manuals, and nothing comes up on google when searched, but when I run the code, SCM (my chosen scheme interpreter) gives me the following error:
;ERROR: "/usr/lib/scm/Iedline.scm": unbound variable: get-fields
; in expression: (get-fields)
; in scope:
; (returned get-fields field port . ##define)
; () procedure get-data
;STACK TRACE
1; (##define ((returned (get-fields)) (get-fields (##lambda () (# ...
2; (##get-data)
But when I make my code look like this, it works fine:
(define (get-data port)
(define field 0)
(define (get-fields)
(define field (read port))
(cond ((not (eof-object? field))
(cons field (get-fields)))
(else '())))
(get-fields))
(define port (open-input-file "../data/problem11.txt"))
(define alfa (get-data port))
(close-input-port port)
Why is it that when I attempt to define returned as the returned list of get-fields that I get an unbound variable error, but when I define alfa as the returned list of get-data (in the second code block), it works okay? Is it that get-fields is recursive? I don't get it. Any answers here would be awesome, thanks.
Implementations are allowed to implement internal defines using one of two possible semantics: letrec and letrec*.
SCM has obviously chosen to use letrec semantics. That means that, given a bunch of internal defines, none of them can immediately refer to the value of another internal define in that same bunch: the same restriction that applies to letrec.
Some implementations, like Racket, use letrec* semantics. That means that any variable defined in an earlier internal define can be used directly by later internal defines. (In other words, your code will work fine in any letrec*-based implementation.)
Since you're using a letrec-based implementation, do this:
(let ((returned (get-fields)))
(close-input-port port)
returned)

Emacs, ruby: convert do end block to curly braces and vice versa

I often find myself converting code like this:
before do
:something
end
to
before { :something }
Is there a way to automate this task in emacs? I use ruby-mode and rinary, but they're not too helpful here.
ruby-mode in Emacs 24.3 and newer has the command ruby-toggle-block.
The default binding is C-c {.
I am sure it can be made shorter and better, but for now I've got the following:
(defun ruby-get-containing-block ()
(let ((pos (point))
(block nil))
(save-match-data
(save-excursion
(catch 'break
;; If in the middle of or at end of do, go back until at start
(while (and (not (looking-at "do"))
(string-equal (word-at-point) "do"))
(backward-char 1))
;; Keep searching for the containing block (i.e. the block that begins
;; before our point, and ends after it)
(while (not block)
(if (looking-at "do\\|{")
(let ((start (point)))
(ruby-forward-sexp)
(if (> (point) pos)
(setq block (cons start (point)))
(goto-char start))))
(if (not (search-backward-regexp "do\\|{" (point-min) t))
(throw 'break nil))))))
block))
(defun ruby-goto-containing-block-start ()
(interactive)
(let ((block (ruby-get-containing-block)))
(if block
(goto-char (car block)))))
(defun ruby-flip-containing-block-type ()
(interactive)
(save-excursion
(let ((block (ruby-get-containing-block)))
(goto-char (car block))
(save-match-data
(let ((strings (if (looking-at "do")
(cons
(if (= 3 (count-lines (car block) (cdr block)))
"do\\( *|[^|]+|\\)? *\n *\\(.*?\\) *\n *end"
"do\\( *|[^|]+|\\)? *\\(\\(.*\n?\\)+\\) *end")
"{\\1 \\2 }")
(cons
"{\\( *|[^|]+|\\)? *\\(\\(.*\n?\\)+\\) *}"
(if (= 1 (count-lines (car block) (cdr block)))
"do\\1\n\\2\nend"
"do\\1\\2end")))))
(when (re-search-forward (car strings) (cdr block) t)
(replace-match (cdr strings) t)
(delete-trailing-whitespace (match-beginning 0) (match-end 0))
(indent-region (match-beginning 0) (match-end 0))))))))
There are two functions to be bound to keys: ruby-goto-containing-block-start and ruby-flip-containing-block-type.
Either command works anywhere inside a block, and hopefully they can skip blocks that should be skipped - although that shouldn't be an issue if you are converting to a short block format.
The ruby-flip-containing-block-type collapses three line do .. end blocks to single line {} and vice versa. If the blocks are not exactly 3 lines and 1 line long, it should leave them alone.
I am using this on my ruby setup now, so I would appreciate improvements.
You could use a regular expression that crosses newlines.
/do(C-q C-j\?)*(.*)(C-q C-j\?)*end/
and replace with
{\2 }
Something like that could work. You could then customize it until it does exactly what you need and bind it to a macro so that you can whip it out and impress your friends anytime!
I tested the above regexes in vi (my editor of choice) and they worked. So something similar should work for you.
For more information, make sure to checkout the emacs wiki!
Here is a function. I am an elisp beginner. It only goes one way; from do to {. let me know if it works for you.

How can i overload a function at run time in Scheme?

rt.
I want to redefine a function at run time so that i can change the behavior of the system at run time.
thanks.
(define (foo x) ...stuff...)
(set! foo (lambda (x) ...different stuff...))
It might be advisable to use let to do this locally, this can also apply to keywords in this sense:
(let ((define +))
(define 2 3)) ; ===> 5
Or even redefine them to constants, remember, Scheme is a lisp-1:
(let ((define 2) (+ 4))
(- define +)) ; ===> -2
Or even:
(let ((quote /))
'3) ===> 1/3
Doing it only locally preserves the functional style.
Assuming you want to overload a function you defined earlier, simply define it again. This also works for redefining functions such as car and cdr, e.g. to make car into cdr:
(define (car x) (cdr x))
However, I think you won't be able to affect other already defined functions with such a redefinition, so a system function which uses car will still use the original system car and not yours:
(define (test x) (car x))
(define (car x) (cdr x))
(test '(1 2 3))
1
I guess the reason for this is that internally the symbols disappear once a function gets read or evaluated and the symbols are replaced by what they're bound to; in this case, the actual code of the function. So rebinding a symbol to a different function won't affect the rest of your already defined code. This is usually a good thing because it helps uphold referential transparency.
If you want to redefine scheme keywords such as lambda or cond, use let-syntax (see http://community.schemewiki.org/?scheme-faq-language)

Resources