Elisp (void-variable\]) when trying to evaluate a function in property list - debugging

In my Spacemacs configuration, I configure my org layer to scale any latex it generates like so
(org :variables
org-format-latex-options '(:foreground "#90ee90" :background default :scale 2.0
:html-foreground default
:html-background "Transparent" :html-scale 1 :matchers
("begin" "$1" "$" "$$" "\\(" "\\["))
I use this configuration on multiple machines, and I like different scaling's for different displays, so I wrote a little function
(defun switch-scale ()
(cond
((equal (system-name) "WMachine") 5.0)
(t 2.0) ;; default
)
)
And rewrote the above code to call the function in the :scale property, like so
org-format-latex-options '(:foreground "#90ee90" :background default :scale (switch-scale)
...
When I test switch-scale in a scratch buffer, it works fine (returns 5.0), but when I add it to my configuration it triggers the following error when trying to generate latex in org mode
Debugger entered--Lisp error: (void-variable \])
eval(\] nil)
elisp--eval-last-sexp(nil)
eval-last-sexp(nil)
funcall-interactively(eval-last-sexp nil)
call-interactively(eval-last-sexp nil nil)
command-execute(eval-last-sexp)
I'm stuck as to what's happening, it seems like (switch-scale) just isn't being evaluated???

Answer was I don't understand elisps evaluation system ... so because it was in a quoted list everything inside that list was literal, so I actually needed to use a backquote to quote the list and then use a comma to evaluate the (switch-scale) function, as explained here https://www.gnu.org/software/emacs/manual/html_node/elisp/Backquote.html

Related

Elisp - How to run a shell command and make buffer to be in markdown-mode?

(defun jira-view-git-branch ()
(interactive)
(markdown-mode)
(shell-command (format "./jira-view.sh &")))
So how to make the output buffer to be in markdown mode?
I tried the following
(defun jira-view-git-branch ()
(interactive)
(with-output-to-temp-buffer "*jira*"
(shell-command (format "./jira-view.sh &") "*jira*" "*Messages*")
(pop-to-buffer "*jira*"))
(with-current-buffer "*jira*"
(markdown-mode)))
but got this in *Messages*
error in process filter: read-from-minibuffer: Wrong type argument: markerp, nil
error in process filter: Wrong type argument: markerp, nil
Without knowing what your shell command jira-view.sh does exactly, I find it hard to come up with a good solution for this.
At least, the following should give you some pointers:
(defun jira-md (buffer)
(interactive "Bbuffer name: ")
(let ((b (get-buffer-create buffer)))
(switch-to-buffer b)
(markdown-mode)
(insert (shell-command-to-string "echo '# title'"))))
You can ask for a (possibly not yet existing) buffer when calling this function by having B be the first character in the argument to interactive
Once you have a buffer name, you can switch to that buffer and then set the major mode.
You could also make the setting of major mode optional by first examining if the major mode is not already set to markdown-mode. Something like:
(unless (eq major-mode 'markdown-mode)
(markdown-mode))

How to add and remove function to after-save-hook for specific major mode?

I wrote a simple major mode for a configuration file.
I would like to check the syntax upon saving.
How to make sure the my-check-syntax is added to after-save-hook iff it is under my-config-mode.
(defun my-check-syntax ()
;; code: print a message to show whether syntax is correct
)
(define-derived-mode my-config-mode nil "my-config"
(setq-local font-lock-defaults '(my-config-font-lock-keywords))
;; (add-hook 'after-save-hook #'my-check-syntax)
)
(add-to-list 'auto-mode-alist '("\\.myconfigure\\'" . my-config-mode))
Use the LOCAL argument to add-hook and remove-hook.

js/console.log in ClojureScript

I want to implement a function with ClojureScript to simplify js/console.log like this:
(defn log [& args]
(apply js/console.log args))
Calling it : (log "foo" "bar")
throws: TypeError: Illegal invocation
but this works : (js/console.log "foo" "bar")
What is the problem ?
js/something is for accessing a js object, but you shouldnt nest dots after that, since it is not clojure compatible syntax and it was going to be removed. In older versions of the compiler (2138) your code works, but it is possible that it has been deprecated in newer versions of the compiler. Which version are you using?
The correct way would be using straightforward js interop like this: [Caveat: see comment below from David Nolen, ClojureScript lead developer]
(defn log [& args] (apply (.-log js/console) args))
And even shorter, since console.log is already variadic (just make an alias):
(def log (.-log js/console))
You can also just use println if you first put this at top of your file: (enable-console-print!).
And pprint has been ported:
:require [cljs.pprint :refer [pprint]]
I found the actual answer
(.apply (.-log js/console) js/console (clj->js args))
Here is a working code for your function (tested with [org.clojure/clojurescript "1.7.228"]):
; working
(defn log [& args]
(.apply js/console.log js/console (to-array args)))
; not working
; (defn log [& args] (apply (.-log js/console) args))
; (defn log [& args] (apply js/console.log args))
; (def log js/console.log)
Here is an article that describes why (apply...) is not playing well with JS functions.
http://clojurescriptmadeeasy.com/blog/how-to-apply-with-the-console-api.html
With console.log makes sense to use a macro instead of a function. If you implement log as a function all the messages will be logged with the line number of where your log function is defined.
A macro solves this problem by generating inline code during compilation, it's important to understand that macros run at compile time.
Define this macro in macros.cljc:
(ns test.macros)
(defmacro log
[& msgs]
`(.log js/console ~#msgs))
`: is like ' or quote but:
Symbols are auto-resolved to include their namespace, preventing ambiguous symbols at the time of evaluation.
Evaluated forms can be inserted using ~ or unquote, as i did for msgs adding # to unpack multiple arguments: ~#msgs. More info about syntax quote.
Then call it from core.cljs:
(ns test.core
(:require-macros [test.macros :refer [log]]))
(log "foo" "bar")

LTK: removing character echo in Entry widget

Is there anyway to have remove echoing of characters being typed in the Entry widget of LTK?
For the CLI interface I use the c-string function (alien routine)
(sb-alien:define-alien-routine getpass sb-alien:c-string (prompt sb-alien:c-string))
Not quite sure how to apply this in LTK.
Set the -show (:show) option of the entry to the placeholder character you want. You can do it during widget creation:
(make-instance 'ltk:entry ...... :show "*")
or later:
(ltk:configure my-entry :show "*")

Clojure compile-time escape mechanism

The language Forth offers a "compile-time" escape mechanism where code can be executed immediately, while the compiler is running (not at run-time). You can include print statements, for example, to debug tricky syntax or type errors).
Does Clojure have anything similar? I am getting a compile-time IllegalArgumentException in one of my function calls and would like to add a compile-time print statement to determine the argument type ((.getClass)).
Thanks.
UPDATE: Here is the complete defn that is failing compilation:
(ns my.ns.name
(:gen-class
:main true)
(:use
[clojure.contrib.str-utils2 :only (join)])
(:import
[java.io PrintWriter]
[java.net URL]
[java.util.concurrent Executors]
[java.util.jar Manifest]
[org.apache.commons.cli CommandLine HelpFormatter Options Option ParseException PosixParser]))
(defn set-version
"Set the version variable to the build number."
[]
(def version
(-> (str "jar:" (.. my.ns.name (getProtectionDomain)
(getCodeSource)
(getLocation))
"!/META-INF/MANIFEST.MF")
(URL.)
(.openStream)
(Manifest.)
(.. getMainAttributes)
(.getValue "Build-number"))))
This defn works:
(defn set-version
"Set the version variable to the build number."
[]
(println (str (.getClass my.ns.name)))
(def version
(-> (str "jar:" (-> my.ns.name (.getProtectionDomain)
(.getCodeSource)
(.getLocation))
"!/META-INF/MANIFEST.MF")
(URL.)
(.openStream)
(Manifest.)
(.. getMainAttributes)
(.getValue "Build-number"))))
Printing the class of things during compilation is pretty restricted to special cases. You will mostly get Symbols and Seqs. Only literals have a meaningful type during compilation. You can execute arbitrary code during compilation via macros.
(defmacro debug-type
[x]
(println (type x))
x)
However as I said: this will normally not be very helpful. And no: in general you cannot wrap x in an eval, eg. if x is a symbol refering to a let-local.
EDIT: Update for updated question.
(def version
(-> (str "jar:" (-> *ns* (.getProtectionDomain)
(.getCodeSource)
(.getLocation))
"!/META-INF/MANIFEST.MF")
(URL.)
(.openStream)
(Manifest.)
(.getMainAttributes)
(.getValue "Build-number")))
Try this. There is no need for a function. def inside defn should ring alarm bells.

Resources