I would like to set fill-column value for all cider output. I'm using:
(require 'cider)
(setq cider-show-error-buffer nil
cider-docview-fill-column 76
cider-stacktrace-fill-column 76
nrepl-buffer-name-show-port nil
cider-repl-display-in-current-window t
cider-repl-result-prefix ";; => ")
But when I call meta, I get this:
user> (meta #'str)
;; => {:added "1.0", :ns #<Namespace clojure.core>, :name str, :file "clojure/core.clj", :static true, :column 1, :line 511, :tag java.lang.String, :arglists ([] [x] [x & ys]), :doc "With no args, returns the empty string. With one arg x, returns\n x.toString(). (str nil) returns the empty string. With more than\n one arg, returns the concatenation of the str values of the args."}
Everything in one line. I bet there is some variable cider-...-fill-column that will help me. I googled it, but found only cider-docview-fill-column and cider-stacktrace-fill-column.
There's no such REPL fill-column variable, but you can enable pretty-printing of eval results in the REPL (see the REPL's menu). You can enable it from your config like this:
(setq cider-repl-use-pretty-printing t)
Related
How can I substitute a symbol name into a function created in a macro? I think I am missing something obvious here. For example, I am trying to make a macro that defines some variables and functions similar to the following,
(cl-defmacro mac (pkg)
(let (
;; Define some variables
(sym (intern (concat pkg "-file")))
(sym-def "default-file.el")
(sym-doc (concat "A custom var from `" pkg "'."))
;; Define some functions
(symfn (intern (concat pkg "-fn")))
(symfn-doc (concat "A function for `" pkg "'.")))
`(list
(defcustom ,sym ,sym-def ,sym-doc
:group (quote ,(make-symbol pkg))
:type '(choice (const :tag "None" nil)
file))
(defun ,symfn ()
,symfn-doc
(interactive)
(fn ,sym)))))
The function returned makes a call out to another function (fn) with a signature like
(defun fn (var) (symbol-value var))
So, it is expecting a call like (fn 'some-var). And, I want to be able to use the macro like
(mac "pack")
And have the following work,
pack-file ; works: "default-file.el"
(pack-fn) ; error: not a symbol
I have tried things like (quote ,sym), symbol-name, and others... But can't seem to get it right.
You want the call to fn to be (fn ',sym) (which you mention you tried in the question, but I suspect got wrong somehow).
You probably also want the expansion of the macro to be (progn ...) instead of (list ...).
(This was originally a comment: I'm putting it here just so there's an answer.)
How can I conditionally remove a let-binding in a defun created in a lisp macro? Here, I want to remove the first let-bound variable from the resulting function, but am running into a problem where I have a nil that sticks around in the let bindings, causing an error.
For example, the resulting function uses let or let* and should remove the first variable assignment depending on arg. But the way I have it written the problem comes from replacing var1 with nil. I'm not sure how to get rid of the nil.
(cl-defmacro mac (cmd &optional arg)
(let ((fn (intern (concat "fn-" cmd))))
`(defun ,fn ()
(interactive)
(,(if arg 'let* 'let)
(cons
,(when arg
'(var1 1))
(var2 (if (bound-and-true-p var1) (+ var1 1) 1)))
(message "%s" var2)))))
(mac "fun") ; Attempt to set constant: nil
(mac "fun2" t) ; no problem
;; (symbol-function 'fn-fun)
;; (symbol-function 'fn-fun2)
;; (fn-fun)
;; (fn-fun2)
The easiest workaround is probably to use ,#(when arg '((var1 1))). ,# is used to splice a list into the position. Since WHEN returns NIL if the condition fails, and NIL is the same as an empty list, the splice effectively ignores it. When the condition succeeds ((var1 1)) is returned, and (var1 1) is unwrapped by the splice.
I have the following function to print the line where point is to the *scratch* buffer,
(defun print-line ()
(print (thing-at-point 'line) (get-buffer "*scratch*")))
but it prints even the fontified info like this
#(" OFFICE
" 0 2 (fontified t org ...
How to discard the printing of the fontified info.
To expand on Daimrod's mention of buffer-substring-no-properties...
M-x apropos RET no-properties RET
buffer-substring-no-properties
Function: Return the characters of part of the buffer, without the
text properties.
field-string-no-properties
Function: Return the contents of the field around POS, without text
properties.
insert-buffer-substring-no-properties
Function: Insert before point a substring of BUFFER, without text
properties.
match-string-no-properties
Function: Return string of text matched by last search, without text
properties.
minibuffer-contents-no-properties
Function: Return the user input in a minibuffer as a string, without
text-properties.
substring-no-properties
Function: Return a substring of STRING, without text properties.
You can read about text properties in the manual:
M-: (info "(elisp) Text Properties") RET
I needed something similar for eredis when manipulating strings from an org-table. You can use `set-text-properties' to get rid of them when displaying the string.
(defun strip-text-properties(txt)
(set-text-properties 0 (length txt) nil txt)
txt)
(defun print-line ()
(print (strip-text-properties
(thing-at-point 'line))
(get-buffer "*scratch*")))
I've tried some things but it's weird, I don't really understand how text properties work.
For example:
(type-of (thing-at-point 'line)) => string
As you've said if one tries to print it, the properties are printed as well, but if one tries to insert it:
(insert (format "%s" (thing-at-point 'line)))
Only the string is printed, not the properties.
So it seems to me that those properties are just bound to the string but you can manipulate the string as usual:
(lenght (thing-at-point 'line))
(substring (thing-at-point 'line) 0 2)
However, if all you want is the line, and the line only you can use buffer-substring-no-properties:
(defun print-line ()
(print (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
I am trying to create a little Clojure macro that defs a String with a type hint:
(defmacro def-string [name value]
`(def ^String ~name ~value))
(def-string db-host-option "db-host")
When I macroexpand it, the type hint is lost:
(macroexpand '(def-string db-host-option "db-host"))
;=> (def db-host-option "db-host")
Never mind the wisdom of type hinting this.
Why is the macro losing the metadata? How do I write this macro, or any that includes metadata?
^ is a reader macro. defmacro never gets to see it. The hint is put on the list (unquote name). Compare for example (meta ^String 'x) with (meta ' ^String x) to see the effect.
You need to put the hint on the symbol.
(defmacro def-string
[name value]
`(def ~(vary-meta name assoc :tag `String) ~value))
And the usage:
user=> (def-string foo "bar")
#'user/foo
user=> (meta #'foo)
{:ns #<Namespace user>, :name foo, :file "NO_SOURCE_PATH", :line 5, :tag java.lang.String}
Metadata doesn't show up in a macroexpand since it's supposed to be "invisible".
If the macro is correct (which it isn't) you should be able to call (meta #'db-host-option) to inspect the meta data on the var.
Note that (def sym ...) inserts metadata on the var that it receives from the symbol. But ^Tag ~name sets the meta data on ~name (unquote name), not on the passed in symbol bound to name. It can't do anything else since ^Tag ... processing is done by the reader, which is already finished once macro expansion starts.
You want something like
(defmacro def-string [name value]
`(def ~(with-meta name {:tag String}) ~value))
user> (def-string bar 1)
#'user/bar
user> (meta #'bar)
{:ns #<Namespace user>, :name bar, :file "NO_SOURCE_FILE", :line 1, :tag java.lang.String}
In Emacs Lisp, how do I check if a variable is defined?
you may want boundp: returns t if variable (a symbol) is not void; more precisely, if its current binding is not void. It returns nil otherwise.
(boundp 'abracadabra) ; Starts out void.
=> nil
(let ((abracadabra 5)) ; Locally bind it.
(boundp 'abracadabra))
=> t
(boundp 'abracadabra) ; Still globally void.
=> nil
(setq abracadabra 5) ; Make it globally nonvoid.
=> 5
(boundp 'abracadabra)
=> t
In addition to dfa's answer you may also want to see if it's bound as a function using fboundp:
(defun baz ()
)
=> baz
(boundp 'baz)
=> nil
(fboundp 'baz)
=> t
If you want to check a variable value from within emacs (I don't know if this applies, since you wrote "in Emacs Lisp"?):
M-: starts Eval in the mini buffer. Write in the name of the variable and press return. The mini-buffer shows the value of the variable.
If the variable is not defined, you get a debugger error.
Remember that variables having the value nil is regarded as being defined.
(progn (setq filename3 nil) (boundp 'filename3)) ;; returns t
(progn (setq filename3 nil) (boundp 'filename5)) ;; returns nil