I'm trying to create an Org mode capture template that writes each entry to a time-based filename.
First, there is a helper function that works in the scratch buffer:
;; Example input: (capture-date-based-file "~/path/to/logs/" "log")
;; Expected output: "~/path/to/logs/2017-11-27-monday-log.org"
(defun capture-date-based-file (path entry-type)
"Create a date-based file name based on the given type."
(downcase (concat path (format-time-string "%Y-%m-%d-%A-") entry-type ".org")))
Then, it's used in the capture templates list:
(setq org-capture-templates
'(("l" "Log" entry (file+headline ,(capture-date-based-file "~/path/to/logs/" "log"))
"* %?\n%U\n" :clock-in t :clock-resume t) ))
I get an error: Symbol's function definition is void: \,
It's difficult to find an answer in Google, because of the comma character. I've looked over the docs, and I'm not sure what I'm doing wrong.
The comma suggests that you're wanting to evaluate the call to capture-date-based-file, but the surrounding form is quoted rather than backquoted, so that won't work.
i.e. these are two quite different things:
'(foo ,(bar) baz)
`(foo ,(bar) baz)
See C-hig (elisp)Backquote RET
In a backquoted form, the comma causes the form which follows to be evaluated immediately, and the result of that evaluation is then substituted into the backquoted form. In a quoted form, ,(bar) simply remains as a literal ,(bar).
The reason for the specific error you saw is that the lisp reader produces the following:
ELISP> (read ",(bar)")
(\, (bar))
Therefore any attempt to evaluate ,(bar) is actually calling the non-existent function \,
(One of the less-obvious errors you'll encounter, FWIW.)
In your scenario I presume that org pulls that particular form out of the template structure and evaluates it. M-x toggle-debug-on-error would most likely show you exactly where and when this happens.
Related
What I'm trying to do is pretty simple, I have a routine and I want that, when someone loads it into autocad, a pop up appears on the screen with a little explaining of what it does. I know how to do the popup, but I have no idea on how to make it run specifically when the routine is loaded, any suggestions ?
This is actually very easily achieved: in short, you would simply include an alert expression outside of any defun expression within the AutoLISP file, such that the alert expression is evaluated immediately when the content of the AutoLISP file is evaluated on load.
For example:
(defun c:test ( )
(princ "\nThis is the main function.")
(princ)
)
(alert "Type \"test\" to run the main function.") ;; This will be displayed on load
(princ)
When the above AutoLISP file is loaded, the interpreter will read the content of the AutoLISP file and will immediately evaluate all AutoLISP expressions contained therein.
As a result, the defun expression will be evaluated first and will define a function c:test which may then be executed at the AutoCAD command line as a result of the c: prefix.
The alert expression will then be evaluated and will display a message box to the user, as desired.
Finally, the closing (princ) expression will be evaluated and will return a null symbol to the command line so as to achieve a 'clean load'. If the final (princ) expression were to be omitted, the alert expression would return a value of nil to the command line.
I need to generate a space-padded string with a variable string length. The not-so-clever solution that works involved nesting of format:
(format nil (format nil "~~~d,,a" 10) "asdf")
Now, I wanted to make it a bit more clever by using format's ~? for recursive processing. I would expect that something like this should do what I want:
(format nil "~#?" "~~~d,,a" 10 "asdf")
but what I get is just the formatting string, i.e. ~10,,a, not the padded asdf string. Perhaps I misunderstood the word 'recursive' here, but I would expect that having formed the inner format string, CL should proceed to actually use it. Am I missing something?
Variable arguments to format directives
FORMAT allows you to use v as an argument to a directive in the control string to pop arguments from the argument list.
CL-USER> (format nil "~va" 10 "asdf")
"asdf "
There may be multiple vs used.
CL-USER> (format nil "~v,,,va" 10 #\- "asdf")
"asdf------"
Recursive processing with ~?
The recursive processing directive is meant for embedding a different "call" to FORMAT inside a control string. For example, a function to prompt for a yes or no answer might be implemented with it.
(defun y-or-n-prompt (control-string &rest args)
(format t "~&~? [y/n]: " control-string args)
;;...
)
The caller can now format a prompt with this as they would with FORMAT without having to worry about the details of what the prompt should look like to the user (adding a new line at the beginning or the [y/n]: prompt at the end).
CL-USER> (y-or-n-prompt "foo ~d bar" 12)
foo 12 bar [y/n]:
NIL
The result of ~? will not be processed by FORMAT, so it cannot be used to build a control string. In general, building control strings at run time is a bad idea, because it's error prone (for example, you must escape any unwanted tildes in the string) and prevents the implementation from processing the control string at compile time.
I am reading an article which uses Scheme for describing an implementation. I know a bit of Common Lisp but no Scheme.
I am hoping you will be so kind as to explain two Scheme code samples and show me how they correspond to Common Lisp.
First, what does this Scheme mean:
(define (content cell)
(cell ’content))
I believe it means this: define a function named content which has one argument named cell. In Common Lisp it is written as:
(defun content (cell)
(...))
Am I right so far?
I am uncertain what the function's body is doing. Is the argument (cell) actually a function and the body is invoking the function, passing it a symbol, which happens to be the name of the current function? Is this the corresponding Common Lisp:
(defun content (cell)
(funcall cell ’content))
Here is the second Scheme code sample:
(define nothing #(*the-nothing*))
I believe it is creating a global variable and initializing it to #(*the-number*)). So the corresponding Common Lisp is:
(defvar nothing #(*the-nothing*))
Is that right? Does the pound symbol (#) have a special meaning? I'm guessing that *the-nothing* is referring to a global variable, yes?
Broadly speaking: yes to both, with one major caveat. More specifically, the first one is accepting an argument called cell and calling it with the symbol 'content. (BTW, your unicode quotation mark is freaking me out a bit. Is that just a copy-paste issue?)
In the second case, the hash is a shortcut for defining a vector. So, for instance:
(vector? #(abc)) ;; evaluates to #t
However, the hash also has quoting behavior. Just as the first element of
'(a b c)
is the symbol 'a (and not the value of the variable named a), the first value in the vector
#(*the-nothing*)
is the symbol '*the-nothing*, rather than the value of a global variable.
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.
I've started trying to learn the innards of Scheme evaluation, and one aspect of quasiquotation, unquoting, evaluation and cons-cells is confusing me. If you can recommend any good references on the subject I'd be very grateful.
The R7RS draft has this example in section 4.2.8 on quasiquotation.
`(( foo ,(- 10 3)) ,#(cdr '(c)) . ,(car '(cons)))
(It's in the R4RS spec too, so this isn't a new thing.)
According to the spec this should evaluate to:
((foo 7) . cons)
I'm having some trouble understanding why. To my mind, the . removes the unquote from the start of the inner list, meaning it won't be evaluated as a procedure.
Here's a simpler expression that demonstrates the same problem:
`(foo . ,(car '(bar)))
Using the same logic as above, this should evaluate to:
(foo . bar)
And indeed it does evaluate to that on the Scheme systems I've tried.
However, to my understanding it shouldn't evaluate to that, so I want to find out where I'm going wrong.
My understanding of Scheme evaluation is (OK, simplified) if it's the first keyword after an open-bracket, call that procedure with the remainder of the list as the parameters.
My understanding of the spec is that ',' is exactly equivalent to wrapping the next expression in an '(unquote' procedure.
My understanding of the dot notation is that, for general display purposes, you remove the dot and opening parenthesis (and matching closing parenthesis), as described here:
In general, the rule for printing a pair is as follows: use the dot
notation always, but if the dot is immediately followed by an open
parenthesis, then remove the dot, the open parenthesis, and the
matching close parenthesis.
So:
`(foo . ,(car '(bar)))
Could equally be rendered as:
(quasiquote (foo unquote (car (quote (bar)))))
(In fact, this is how jsScheme will render the input in its log window.)
However, when it comes to evaluating this:
(quasiquote (foo unquote (car (quote (bar)))))
Why is the 'unquote' evaluated (as a procedure?), unquoting and evaluating the (car...) list? Surely it should just be treated as a quoted symbol, since it's not after an opening bracket?
I can think of a number of possible answers - 'unquote' isn't a regular procedure, the 'unquote' is evaluated outside of the regular evaluation process, there's a different way to indicate a procedure to be called other than a '(' followed by the procedure's symbol - but I'm not sure which is right, or how to dig for more information.
Most of the scheme implementations I've seen handle this using a macro rather than in the same language as the evaluator, and I'm having difficulty figuring out what's supposed to be going on. Can someone explain, or show me any good references on the subject?
You are correct in that there are macros involved: in particular, quasiquote is a macro, and unquote and unquote-splicing are literals. None of those are procedures, so normal evaluation rules do not apply.
Thus, it's possible to give (quasiquote (foo bar baz unquote x)) the special treatment it needs, despite unquote not being the first syntax element.