Questions tagged [conditionals]

Questions about conditionals: cond, if, when, unless.

This tag is used for questions about the usage of Lisp conditionals: cond, if, when, unless.

41 questions
17
votes
2 answers

"and" vs "when" for conditionals

This is a follow-up on the comments on this answer. The following bits of code seem to be equivalent: (and a b) (when a b) Of course and lets you put more conditions: (and a b c d) means (when (and a b c) d) I tend to use when only to express…
Clément
  • 3,924
  • 1
  • 22
  • 37
8
votes
2 answers

`case` doesn't switch correctly with strings

I have the following code which doesn't work: (eval-when-compile (require 'cl)) (setq mymachine (system-name)) (case mymachine ("HP" (setq package-user-dir (concat user-emacs-directory "packages/hp"))) ("DELL" (setq package-user-dir (concat…
NVaughan
  • 1,481
  • 12
  • 27
7
votes
2 answers

Is there a way to only accept certain arguments to a function?

I want to write a function where the only two valid values for the input argument are "dag" or "rulegraph". Is there a way to specify that only these two arguments are accepted this in the elisp function header? If this is not possible, I'd accept…
The Unfun Cat
  • 2,393
  • 16
  • 32
6
votes
3 answers

Using case to compare with a variable

I expected the outcome of the following small program: (progn (setq x 1) (case x (x 'Yes) (t 'No))) to be Yes, given that case compares things with eql, and surely x is eql to itself. However the outcome is No and I am completely…
Ruy
  • 787
  • 4
  • 11
6
votes
7 answers

`cond` with less redundancy

Is there a better way to write the below code with less redundancy? (cond ((firstFunction str1) (secondFunction str1)) ((firstFunction str2) (secondFunction str2)) ((firstFunction str3) (secondFunction str3)) ((firstFunction str4) (secondFunction…
Name
  • 7,689
  • 4
  • 38
  • 84
5
votes
2 answers

Shallowly override functions within a target function

I'm exploring options for monkey-patching and I'm wondering if the following is within reason. I would like to override a particular function within some target function so that calls within the target function are overridden, but functions called…
ebpa
  • 7,319
  • 26
  • 53
4
votes
2 answers

How do I splat the arguments to the `or` function?

I would like to evaluate a list of booleans like (nil nil t) into a single boolean, such that if any element is true, then the expression evaluates to true. I first looked at the (or) function in elisp, but it does not take a list of booleans as an…
Linus Arver
  • 152
  • 1
  • 6
4
votes
3 answers

Is there a way Emacs can infer is running on WSL (Windows Subsystem for Linux)?

I would like to identify when Emacs is running on WSL (Windows Subsystem for Linux). Normally I use system-type variable to check if Emacs is on Linux, Windows or MacOS X. Though in this case it returns gnu/linux: system-type is a variable defined…
nephewtom
  • 2,219
  • 17
  • 29
3
votes
2 answers

Specify optional matches with pcase

Consider the following form: (pcase '(:def "k" #'foo :wk "ho") (`(:def ,(and (or (pred stringp) (pred vectorp)) key) ,(and (pred xl-sharp-quoted-symbol-p) def) :wk ,(and (pred stringp) desc)) (format "%s | %s | %s" key def…
Aquaactress
  • 1,393
  • 8
  • 11
3
votes
2 answers

Elisp code execution order?

I cannot understand the behavior of this simple code: (defun foo (str bool) (interactive (list (read-string "Some text: ") (y-or-n-p "Yes or no? "))) (message "%s is first" str) (if bool (message "%s is in if" str))) When I call…
147pm
  • 2,907
  • 1
  • 18
  • 39
2
votes
2 answers

In Spacemacs, how to customize keybindings that are condition on language file?

Here is what I would like to do. I would like to customize key bindings that point to a different command depending on the file type on which the user is operating. For instance, if one presses f5 in a Python file .py, I would like f5 to call the…
Louis15
  • 263
  • 3
  • 9
2
votes
1 answer

How execute multiple lists in a true statement?

For this simple question, I never found any answer. Let assume I have the following: (if (true) (foo1) ;; When true (bar) ;; When false) So I would like to combine foo1 and foo2 (if (true) (foo1)(foo2) ;; When true (bar) ;; When…
ReneFroger
  • 3,855
  • 22
  • 63
1
vote
1 answer

Is there a tagged union/sum type in elisp? If not, what to use instead?

In many programming languages, if one wants a variable that can take a discrete number of values, one has the capacity to define the permissible values in advance, using something like an enum. Does elisp have any functionality like that? My use…
Paul Gowder
  • 147
  • 6
1
vote
1 answer

Matching against variable keys in `cl-case`

In cl-case clauses (though naturally not the initial keyform), keys are apparently quoted: (let ((mark ?.)) (case mark (mark 'mark) (?. ?.))) ; 46 (?.) (let ((mark ?.)) (case 'mark (mark 'mark) (?. ?.))) ; mark My reading of…
outis
  • 178
  • 8
1
vote
1 answer

A way to check if "(current-kill 0)" will run onto an error before it happens

I have an Elisp part involving (current-kill 0) to copy the current clipboard content into a variable. This works flawlessly as long as the kill-ring has content. However, if I just started the computer and did not copy anything yet (or use the…
Phoenix
  • 341
  • 1
  • 8
1
2 3