Run Lisp functions on load - function

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.

Related

Function argument as part of path to a file in Clojure

Suppose I have the same file name, placed in five different sub-folders. The file in question has the same name in all folders but otherwise has different values.
Suppose my file is called something like test.csv. My folder are A1,A2,A3,A4 and A5.
My data-read structure looks as so:
(defn my-data [folder]
(loop [SUB-FOLDER (str 'folder)
X []
data (with-open [file (clojure.java.io/reader "./resources/SUB-FOLDER/test.csv")]
(doall (csv/read-csv file)))]
(def row (first data))
(if (nil? row)
[(lazy-seq X)]
(recur (conj X (map #(Float/parseFloat %) row))
(rest data)))))
I would call this function as so
(def Y (map vec (get (my-data A1) 0)))
Where I am trying to access the file test.csv in the sub-folder A1 which I am passing as an argument to my csv-read function.
Now obviously the above code does not work, but it provides an overview of what I am trying to achieve.
How could one make the minimal example work?
Thanks.
Something like this should put your data from one folder into a sequence of sequences:
(defn read-data [folder]
(let [data (csv/read-csv (slurp (str "./resources/" folder "/test.csv")))]
(for [line data]
(map #(Float/parseFloat %) line))))
Change map to mapv if you want to have vectors, and wrap the for with into [] if you want a vector of vectors. This assumes folder is a string. Because your files are very small you don't need to open them with a reader, it's easier to slurp them and then parse them.
A fe issues with your original code:
You're always trying to read a file called "./resources/SUB-FOLDER/test.csv" instead of replacing the name of your folder.
You don't want to use def inside a function. See let.
Calling lazy-seq on an already realized sequence doesn't do anything.
By calling doall on your reader you're forcing all your data into memory instead of reading it and processing it as you go (that's why I replaced it with slurp). If you're going to process it in a loop as you read it, you want the with-open outside the loop (and no doall).

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).

Is there a apply-function-to-region-lines in emacs?

A lot of my work involves searching and deleting unnecessary lines of code. So I create a macro, and then select all lines (C-x h) and then run the command (apply-macro-to-region-lines). I managed to save that command and placed it in my .emacs file; I called it cut_it_now. But now my function is not a macro anymore, so I can't use the (apply-macro-to-region-lines) function anymore.
Do you know if there is (apply-function-to-region-lines) implemented somewhere?
Many thanks,
D
The following function should do what you want:
(defun apply-function-to-region-lines (fn)
(interactive "aFunction to apply to lines in region: ")
(save-excursion
(goto-char (region-end))
(let ((end-marker (copy-marker (point-marker)))
next-line-marker)
(goto-char (region-beginning))
(if (not (bolp))
(forward-line 1))
(setq next-line-marker (point-marker))
(while (< next-line-marker end-marker)
(let ((start nil)
(end nil))
(goto-char next-line-marker)
(save-excursion
(setq start (point))
(forward-line 1)
(set-marker next-line-marker (point))
(setq end (point)))
(save-excursion
(let ((mark-active nil))
(narrow-to-region start end)
(funcall fn)
(widen)))))
(set-marker end-marker nil)
(set-marker next-line-marker nil))))
So, if you have the following function that you want to apply against lines in a buffer:
(defun test()
(insert "> "))
And, if your buffer contains the following contents:
Line 1: blah, blah
Line 2: blah, blah
Line 3: blah, blah
Line 4: blah, blah
If you select a region enclosing just lines 2 & 3, enter "M-x apply-function-to-region-lines", and enter "test" as the function name when prompted, you will get the following result in your buffer:
Line 1: blah, blah
> Line 2: blah, blah
> Line 3: blah, blah
Line 4: blah, blah
Note that you can still use apply-macro-to-region-lines with a macro generated from code, provided the macro is defined as a vector or string. With a custom apply-named-macro-to-region-lines[2], you can select the macro to use interactively.
Emacs has two ways of generating code from a keyboard macro, depending upon the method used to name it.
If you use kmacro-name-last-macro (bound to C-xC-kn), then Emacs generates a function from the macro, which is not directly useful for this particular purpose [1].
If you use name-last-kbd-macro to name your macro, it will be generated as a vector or string.
In either case, you then use insert-kbd-macro to obtain the code.
In fact the vector/string format is the default, so you could bypass the naming step and immediately ask for the code (typing RET at the name prompt to indicate the most recently-defined macro), and then manually edit the default name of the inserted code.
[1]: The vector form does appear to simply be embedded in the function definition, so you should be able to extract that from the code to manually re-define a macro function in vector format.
[2]: When I originally wrote this reply, I'd forgotten that this was a custom function. Sorry about that.
(defun apply-named-macro-to-region-lines (top bottom)
"Apply named keyboard macro to all lines in the region."
(interactive "r")
(let ((macro (intern
(completing-read "kbd macro (name): "
obarray
(lambda (elt)
(and (fboundp elt)
(or (stringp (symbol-function elt))
(vectorp (symbol-function elt))
(get elt 'kmacro))))
t))))
(apply-macro-to-region-lines top bottom macro)))
A simple solution is to define a macro that calls your function then use the good ol' apply-macro-to-region-lines.
Apart from that, I think that you could write a loop in a few lines of elisp that does exactly what you ask for. If you would like to be fancy, you can even prompt the user for the name of the function. I think this is a good exercise for elisp, I can help you with some pointers if you feel like you would like to try it yourself.
I agree with #Lindydancer's answer, and I'd also add that there might be an easier way to accomplish your goal. e.g. the built-in function delete-matching-lines. :-)
You could always copy the source to apply-macro-to-region-lines and tweak it to call a passed in function, and thus make your own version.

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

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?