using save-some-buffers to offer saving only the current buffer - elisp

In some elisp code I want to ask whether the user will save the current buffer before I proceed further.
For this, I can use save-some-buffers and pass an extra args to select only the current buffer, as in:
(let ((buff (current-buffer)))
(save-some-buffers nil (function (lambda () (eq (current-buffer) buff)))))
Is there already an elisp function that does (more or less) the same thing? I'd rather use standard library functions than maintain my own code.
In particular, I like using save-some-buffers because it understands several answers and does more checks than I would do myself.

Related

Call finish-output from format

I noticed that there is no
format directive which would
call force-output/finish-output.
Why?
It does seem to be useful in user interaction, cf.
Lisp format and force-output.
E.g., ~= could translate to finish-output, and ~:= to force-output.
I don't think clear-output makes much sense in this context, but we
might map ~#= to it for completeness.
PS. Cf. CLISP RFE.
Summary from comp.lang.lisp:
An explanation from Steven Haflich
The language defines no portable way to extend the set of format
directives (other then ~/.../) but that's not really the issue here.
The real problem is that it is not well defined to call finish-output or similar functions at arbitrary places during printing.
If pretty-printing is in progress, the stream received by a
pprint-dispatch or print-object method may be an encapsulating stream --
one that delays output temporarily until it can make decisions about
white space and line breaks. (There are also potential problems if
finish-output were called inside a ~< justification, but that directive
is a hairball!) What would one expect finish-output to do if called
inside a pretty print operation? I don't think it's well defined.
The problem isn't particular to format, of course, but a directive for
finish-output from format would just add another sharp edge to the
language. finish-output etc. are only safe to call when completely
outside an actual or implied call to cl:write. Call it as a function
at an appropriate point in your code (where you know execution isn't
inside a nested write) so the intention is clear and you don't mess up
printer internals.
A suggestion from Rob Warnock
Actually, no changes to format are needed. Just add this function somewhere in the COMMON-LISP-USER package:
(defun fo (stream arg colon-p atsign-p &rest params)
(declare (ignore arg params))
(cond
(colon-p (force-output stream))
(atsign-p (clear-output stream))
(t (finish-output stream))))
Then:
(progn
(format t "enter var: ~/fo/" nil)
(read))
enter var: 456
456
The problems with this (portable!) approach are
verbosity (~/fo/ instead of ~=)
need to consume a format argument (nil in the example above)

Change program code while running in Chicken Scheme

Is it possible to update the program code while it is being interpreted by csi, the Chicken Scheme Interpreter? If so, how?
So that I can interactively change part of the code and immediately see the effects of that changes.
For example, suppose I have written the following program:
(define (loop)
(print "Ciao")
(rest 1)
(loop))
(loop)
(assume (rest 1) has the effect of pausing the program for a second).
If I run this program, trough csi, it prints the string "Ciao" every second. If I change the string "Ciao" into something else, for example into "else", and I save the program code file, then csi continue interpreting the old program code, so I continuously see the string "Ciao". I'd like, in this case, that when I save the modified code with the string "Ciao" replaced by "else", csi continue its interpretation job by looking into the modified file, instead of the old file.
So that I obtain as output some "Ciao" followed by some "else": the "else" start to appear when I replace "Ciao" by "else" in the source code.
In general, the answer is "don't". The way you're supposed to use the REPL is by evaluating piecemeal changes against it, then evaluating a function or two to make sure everything went as expected. Part of this approach is structuring your program so that it can be easily tested in pieces, which implies not automatically starting any infinite loops. In the specific case you're asking about, you could instead write
(define (do-stuff)
(print "Ciao"))
(define (main-loop)
(do-stuff)
(rest 1)
(main-loop))
(define (start) (main-loop))
You can now incrementally develop do-stuff, periodically evaluating new versions to your interpreter and calling them to make sure they work, then eventually calling start once you're confident that it's doing the right thing.
You may get mileage out of this similar question asked about Common Lisp.
As an aside, if you were using Common Lisp and SLIME, you could now do more or less what you proposed:
(defun do-stuff ()
(format t "Ciao~%"))
(defun main-loop ()
(loop (progn (do-stuff)
(sleep 1))))
(main-loop)
Starting that up would start printing Ciao on separate lines in your SLIME REPL. If you changed do-stuff to
(defun do-stuff ()
(format t "else~%"))
then hit it with C-c C-c (the default binding for slime-compile-defun), you'd see your SLIME REPL start printing else lines in-flight.
CL-USER> (main-loop)
Ciao
Ciao
Ciao
; compiling (DEFUN DO-STUFF ...)else
else
else
else
else
else
; Evaluation aborted on NIL. User break.
CL-USER>
I'm not sure how to accomplish the same thing in Scheme, but I'm reasonably sure it's possible.
All that being said, you sometimes want to run a program part of which is an infinite loop. A real world example would be while testing a TCP server of some sort. If you're in such a situation, and your desired workflow is
Write file
Run csi my-file.scm
Edit file
Kill csi
Run csi my-file.scm
goto 3
and you basically just want to automate steps 4 through 6, you'll need an external tool to do it for you. Take a look at entr or hsandbox (the latter doesn't have Scheme support out of the box, but it doesn't look like it would be too hard to add).
There is no common way to make a running program check its source for changes but you there seems to be enough feratures available in Chicken to roll your own:
(use posix)
(use srfi-18)
(define (watch-reload! file)
(define (tsleep n)
(thread-sleep! (seconds->time (+ n (time->seconds (current-time))))))
(define (get-time)
(file-modification-time file))
(thread-start!
(lambda ()
(let loop ((filetime '()))
(let ((newtime (get-time)))
(when (not (equal? filetime newtime))
(load file))
(tsleep 10)
(loop newtime))))))
Now all you have to do is to use watch-reload! instead of load and it will check and reload every 10 seconds if the file has been modified.
If you save when the file is not valid scheme it stops working until you call watch-reload! on it again.
It may be that chicken programmers might have a better solution.

How do I get a list of functions defined in an emacs-lisp file

Is it possible to get a list of functions defined in an emacs-lisp file? I found this sort of related answer: How do I get a list of Emacs lisp non-interactive functions?, but it involves a map over all of the atoms defined, not just what is in a file.
If the file in question has already been loaded, then you can modify the code in the question you link to filter out the symbols defined in other files:
(let ((funclist ()))
(mapatoms
(lambda (x)
(when (and (fboundp x) ; does x name a function?
(let ((f (symbol-file x)))
(and f (string= (file-name-base f) "my-file.el"))))
(push x funclist))))
funclist)
If the file has not been loaded, you would have to scan it with scan-sexps and find defun forms.
You might, however, prefer to use etags or imenu instead of scanning the file yourself.
Maybe a faster way is to look for the file in load-history, which will then give you the list of variables and functions defined therein.
Not sure if your asking for a non interactive approach.
With M-x occur ENT (defun.* ENT you get a buffer with more or less all function-definitions found in (current-buffer).
The quick&dirty way: extract all defuns via regex. It works instantly on a buffer with 5000 lines.
(-map 'cadr (s-match-strings-all "defun \\(.*?\\) " (buffer-string)))
This returns a list of function names that are defined via defun in the current open buffer. buffer-string returns content of a current buffer in a string, -map and s-match-string-all are taken from dash and s third party libraries (their GitHub pages explain how to install them), cadr returns a 2nd element of a list.
-map is analogous to Emacs built-in mapcar, it applies a function to each element of a list and returns a new list, s-match-string-all returns all possible regex matches in a string, parentheses in a regex denote a group (read more how to form Emacs regular expressions from EmacsWiki).
If you run it in eval-expression (Alt+:), it will just throw it into echo area, but that's not what you need. So below are variations that work with custom buffer or file. with-current-buffer allows to temporarily switch a buffer, while some code does actions inside it, f-read is a file reading function form another third-party library f.
(defun list-defined-functions (buffer)
(with-current-buffer buffer
(-map 'cadr (s-match-strings-all "defun \\(.*?\\) "
(buffer-string)))))
(defun list-defined-functions-in-file (file)
(-map 'cadr (s-match-strings-all "defun \\(.*?\\) "
(f-read file))))
Read Emacs Lisp manual and try to come up with whatever is useful for you.

How to obtain my function definition in MIT Scheme?

In JavaScript, I can retrieve the "source code" definition of a function, for example:
​function alert_Hi() {
alert("Hi");
}
alert(alert_Hi);
will return exactly what I typed. http://jsfiddle.net/DuCqJ/
How can I do this in MIT Scheme?
I remember seeing something that returns #compound-procedure or something, but what I really want is the "source code".
You might try pp
(define (display-hi) (display "Hi"))
(pp display-hi) =>
(named-lambda (display-hi)
(display "Hi"))
MIT-Scheme debugging aids
JavaScript is fully interpreted, so it has full function definitions lying around even after you've defined them. Scheme is not actually fully interpreted; it compiles functions (and a few other constructs, I think) down to a non-readable representation and throws away the initial code.
You could probably get it to store the initial textual representation of a function at runtime using some macro tricks, but I'm inclined to believe that this would be more trouble than it's worth.
If you don't mind me asking, why do you need the textual representation of a defined function at runtime?

How do you find the name of the current running funciton: current-function, this-function, current-defun, this-defun

I am trying to dynamically find the name of the current function (this-function) running i.e.
(defun my-func ()
(remove-hook 'some-hook this-function)
(do-something))
I haven't tested this, but why not write a macro to encapsulate what you want? Something like the following, maybe?
(defmacro one-shot-hook (name hook &rest body)
`(defun ,name ()
(remove-hook ',hook ',name)
,#body)
Then, for example
(macroexpand-all-1
'(one-shot-hook test c-mode-hook
(message "Yay!")))
gives
(defun test nil
(remove-hook (quote c-mode-hook) (quote test))
(message "Yay!"))
(when I've reformatted it).
This removes the problem of needing to know the name of the function you're in, which would need nasty macrology anyway (I'm not sure whether it's possible).
One more thing, I'd probably suggest just having a flag variable set to nil initially which your code tests to decide whether to run. Then you don't have to mess around adding and removing hooks all the time: the result will probably be much easier to customise and understand for anyone else using your code.

Resources