Running a function when the routine is loaded through appload (autoLISP) - autocad

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.

Related

Add extra action (recenter) to `org-forward-element`?

In org-mode, strike M-} which invoke org-forward-element is very handy to jump around.
However, the matching line always stay at the bottom. So I have to manually execute (recenter).
How could add the action (recenter) to org-forward-element?
I tried a decorator solution, but does not work.
(defun add-recenter(func)
(lambda ()
(func)
(recenter)))
(setq org-element-forward (add-recenter org-element-forward))
This seems to work:
(advice-add 'org-forward-element :after (lambda () (recenter)))
I first tried (advice-add 'org-forward-element :after 'recenter), but that didn't work, because if the advice function being added (in this case recenter) is interactive, its interactive spec overrides that of the old function (org-forward-element), which causes an error since recenter generates one argument while org-forward-element takes zero arguments.
The reason your code doesn't work is that in Emacs Lisp variables and functions are in separate namespaces, just like Common Lisp and unlike Scheme. This is sometimes described as "Lisp-2", as opposed to "Lisp-1". See e.g. this article: http://ergoemacs.org/emacs/lisp1_vs_lisp2.html
Basically every symbol has both a "value cell" and a "function cell". setq changes the symbol's value cell, and calling a symbol, like (foo), accesses its function cell. The corresponding Emacs Lisp code would be something like:
(defun add-recenter(func)
(lambda ()
(funcall func)
(recenter)))
(fset 'org-element-forward (add-recenter (symbol-function 'org-element-forward)))
That is essentially what the advice-add call above does.

Dynamically changing file name in org-agenda-export

I would like to export the agenda view to separate file with a unique name based on the current date. Based on this example code:
(setq org-agenda-custom-commands
'(("X" agenda "" nil ("agenda.ps")))
)
The last argument is the file name so I thought I can put the output of the concat function, e.g.:
(setq org-agenda-custom-commands
'(("X" agenda "" nil (concat (format-time-string "%Y-%m-%d") "-agenda.html")))
)
Unfortunately, this approach fails since the interpreter takes the concat literally, and a string (expected data type) is not generated. I’m not very familiar with LISP, so any help is greatly appreciated.
First of all the last argument is not a filename but rather a list of names, so you must add some extra parentheses.
As you already noted, because of the quote the list is not evaluated, and it's fine since you don't want to evaluate it, except the last element (concat function). To do so you can use backquote:
(setq org-agenda-custom-commands
`(("X" agenda "" nil (,(concat (format-time-string "%Y-%m-%d") "-agenda.html"))))
)
As a side note, I'm not a specialist of the org-mode and I'm just answering the question you asked, but I have a feeling that it is possible to achieve your goal in a more simple way. Not sure how, but maybe you can dig in the documentation of org-mode and probably you'll find something interesting.

Org Mode: Symbol's function definition is void: \,

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.

How do you inspect the type of (*) on OCaml's toplevel?

I wanted to see the type of the multiplication function (*), so I tapped it into the OCaml toplevel.
# (*)
However, the toplevel echoed:
(*);; 1: this is the start of a comment.
and then consumed any further input I put in. I figured that I had to get out of the comment mode by pressing Ctrl+d to send EOF. Great. But surely, I should be able to query the type of any function, including our mysterious multiplication function (*)?!
I would be incredibly disappointed if that is a limitation of the toplevel.
It does recognize *) as the end of the comment, but it's still waiting for the end of the expression. I.e. if you enter two semicolons, it will give you a syntax error and allow you to enter another expression.
To get the function * type ( * );; with spaces to distinguish it from comment symbols.

Identifiers and Binding in Scheme - how to interpret the function?

I am reading DrRacket document http://docs.racket-lang.org/guide/binding.html
There is a function
(define f
(lambda (append)
(define cons (append "ugly" "confusing"))
(let ([append 'this-was])
(list append cons))))
> (f list)
'(this-was ("ugly" "confusing"))
I see that we define function f, inside we define lambda that takes (append), why ?
Procedure (body) for lambda is another function called cons, that appends two strings.
I don't understand this function at all.
Thanks !
The section that you're referring to demonstrates lexical scope in Racket. As in other Scheme implementations, the main point is that you can "shadow" every binding in the language. Unlike most "mainstream" languages, there are no real keywords that are "sacred" in the sense that they can never be shadowed by a local binding.
Note that a really good tool to visualize what is bound where is DrRacket's "check syntax" button: click it, and you'll see your code with highlights that shows which parts are bindings, which are special forms -- and if you hover the mouse over a specific name, you'll see an arrow that tells you where it came from.
Scheme takes some getting used to :)
f is assigned the function returned by the lambda.
lambda defines the function that takes a parameter (called append).
(define cons (append "ugly" "confusing")) is not a function per se, but calls append with the two strings as parameter and assigns the result to cons.
inside the let block, append is re-assigned a different value, the symbol this-was.
the let block creates a list of append (which now contains 'this-was) and cons (which contains '("ugly" "confusing") from 3 above
since 5 is the last statement that value is returned by the whole function which is called f
f is called with the parameter list (the list function). which gets passed as the parameter append. And this is why 3 above creates a list '("ugly" "confusing") which gets assigned to cons.
Hope that cleared up things a bit.
Cheers!

Resources