Skip to main content
Share Your Experience: Take the 2024 Developer Survey

Questions tagged [conditionals]

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

Filter by
Sorted by
Tagged with
18 votes
3 answers
5k views

"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 ...
Clément's user avatar
  • 3,995
8 votes
3 answers
3k views

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. ...
Ruy's user avatar
  • 839
8 votes
2 answers
3k views

`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/...
NVaughan's user avatar
  • 1,481
7 votes
2 answers
164 views

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 ...
The Unfun Cat's user avatar
6 votes
7 answers
1k views

`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) (...
Name's user avatar
  • 7,871
5 votes
2 answers
281 views

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 ...
ebpa's user avatar
  • 7,499
4 votes
3 answers
1k views

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 ...
nephewtom's user avatar
  • 2,261
4 votes
2 answers
206 views

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 ...
Linus Arver's user avatar
3 votes
2 answers
264 views

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) (...
147pm's user avatar
  • 2,969
3 votes
2 answers
278 views

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 ...
Aquaactress's user avatar
  • 1,453
2 votes
1 answer
646 views

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) ...
ReneFroger's user avatar
  • 3,880
2 votes
2 answers
5k views

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 ...
Louis15's user avatar
  • 263
1 vote
1 answer
156 views

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 (...
outis's user avatar
  • 188
1 vote
1 answer
245 views

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 ...
Paul Gowder's user avatar
1 vote
1 answer
65 views

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 ...
Phoenix's user avatar
  • 351
1 vote
1 answer
281 views

Opening files in new frame

I'm using a fairly frame-oriented workflow (or trying to). I've run into something strange. If I write this: (defun find-file-existing (filename) "Edit the existing file FILENAME. Like \\[find-...
singpolyma's user avatar
1 vote
0 answers
107 views

Constrain Eclim to java files

I am using eclim and I am loving it! But I had to disable eclimd-autostart because it keep trying to fire up eclimd when I pop a .js buffer. Autostarting of eclimd is disabled, please start eclimd ...
CESCO's user avatar
  • 374
0 votes
4 answers
277 views

Nesting conditionals, consolidating functionality in elisp

Working in an org-mode file, I have this table #+tblname: site-charac-matrix | | glaze | orn | color | thin | |---+-------+-----+-------+------| | A | 0 | 1 | 0 | 0 | | B | 1 | 0 ...
147pm's user avatar
  • 2,969
0 votes
2 answers
28 views

Avoiding code duplication with conditional code that uses let bindings

This is the procedural logic I want to program in Elisp: def my_func(): v1; v2; if (some_condition): v1 = 2 v2 = 3 else: v1 = 4 v2 = 5 foo(v1) ...
Gradient's user avatar
  • 211
0 votes
1 answer
61 views

How to invoke another function only if there is some error with a given function?

Assume that I have two functions, say function1 and function2. I would like to execute function2 only if there is some error during the execution of function1. So far, I have tried the below code (...
Name's user avatar
  • 7,871
0 votes
2 answers
35 views

Checking optional function argument

If I have an optional function argument, how can I determine if name has been supplied. And if it has been supplied, how can I test its validity (not nil or not empty string). (defun myfunc (&...
Dilna's user avatar
  • 1
0 votes
1 answer
58 views

Alternative way to disambiguate "dangling else" in a cl-loop

I need to achieve in a cl-loop the following conditional written in pseudocode. if (flag1) { if (flag2) {return ...} } else { return ... } Note that it is equivalent to if (flag1 && ...
Firmin Martin's user avatar
0 votes
2 answers
42 views

Query-replace stops after first item in while

Following this StackOverflow question, I coded this function to clean up text pasted from other sources, such as Google Docs: (defun sanitize-md () "Replace characters." (interactive &...
emonigma's user avatar
  • 1,907
0 votes
2 answers
520 views

Disabling a minor mode in a specific buffer

Running spacemacs 0.200.13 on emacs 25.2.2 on Kubuntu 18.04. I have globally enabled golden-ratio-mode (a minor mode). But I would like to disable it in a particular buffer, *Org Agenda* to be ...
deshmukh's user avatar
  • 1,912
0 votes
2 answers
90 views

How to make optional parameters default to true?

In the question Optional parameter defaults, the suggested way to set default value for the parameter of an elisp function is along the lines of: (defun my-command (a &optional b) (command-...
tinlyx's user avatar
  • 1,364
0 votes
1 answer
43 views

How can I return a list from this function given :str nil and 1 argument

I could not figure out how to get a list out of this (my/tf :str nil "foo" ). This function returns a concatenated string of directories or a list. Function Must: Return a string when one ...
Rick's user avatar
  • 25
0 votes
1 answer
61 views

Setting numeric value associated with symbol

I have the following condition to set mbcomplt according to the symbol stored in armg. Is there a more convenient way (e.g a shorter way of setting mbcomplt) to set the numeric value for mbcomplt ...
Dilna's user avatar
  • 1
0 votes
1 answer
121 views

Hide category string in org-agenda-prefix-format for a particular category/filename

My current org-agenda-prefix-format looks like this: (setq org-agenda-prefix-format '((agenda . "%?-2 i%12:c %?-12 t%?12 s ") (todo . "%?-2 i%-12:c") (...
onlycparra's user avatar
0 votes
1 answer
44 views

Emacs orgmode table formula to calculate different per km tariffs for trips under 100 km ans over 100km

I am new to Emacs Lisp. I have this price table in Org mode. A small portion of it is shown here: *** Destinations en France | Destination | Distance Km | Temps de trajet | Prix | |-...
alikgator's user avatar
0 votes
1 answer
29 views

pyvenv conditional on hostname

I'm trying to setup activating virtual environments conditional on hostname. Until recently I've been using... (pyvenv activate "~/.virtualenv/default") ...across all hosts, but because ...
slackline's user avatar
  • 345
0 votes
1 answer
545 views

package-installed-p vs fboundp

What should I use: (when (package-installed-p 'ivy) (global-set-key (kbd "C-x b") #'ivy-switch-buffer)) or: (when (fboundp 'ivy-switch-buffer) (global-set-key (kbd "C-x b") #'...
gavenkoa's user avatar
  • 3,452
0 votes
1 answer
83 views

Adding conditional control to function argument

How do we add conditional control to function arguments? (defun f (x) (let (x) (if (eq (x (or 1 2))) (print x) (print "Hey!")))) I would like to call: (f 1) ; 1 (f 2) ; 2 (f ...
Sati's user avatar
  • 815
0 votes
1 answer
137 views

Cursor position variable

Here's my function: (defun scroller-down () "Scrolling down conditional for buffer and minibuffer." (interactive) (if (active-minibuffer-window) (ivy-scroll-down-command) (scroll-down-...
John DeBord's user avatar
0 votes
1 answer
38 views

Different outputs from minor-mode depending on major-mode

My minor-mode is inserting text in the current buffer. I want the default format of the text to be different depending on what major-mode the buffer is in, and to allow the user to override this ...
pelle's user avatar
  • 1
0 votes
1 answer
47 views

require 'package vs with-eval-after-load 'package

(require 'eglot) (do-something) There's another.. (with-eval-after-load 'eglot (do-something) ) They seem to do the same, are there any difference?
eugene's user avatar
  • 489
0 votes
1 answer
60 views

Combining commands for two major modes

I want to extend the execution of the body of emacs-lisp-mode also to lisp-interaction-mode. How can I combine two conditions together? (cond ((eq major-mode 'emacs-lisp-mode) (let ( (...
Dilna's user avatar
  • 1
0 votes
0 answers
21 views

Edebug conditional breakpoint when a value that contains a certain string appears?

Is it possible to set a conditional breakpoint in edebug such that it gets hit only when a symbol that contains a string "download:" gets hit? I've tried running edebug-set-conditional-...
Daniel Krajnik's user avatar
0 votes
1 answer
10 views

Resetting value when modulus computes to 0

Am doing the following computation where dmax is 8, whilst dcur start at 1 and increases bf increments of 1. I would like that when (mod dcur dmax) is 0, I use the value 8 rather than the value 0. (...
Dilna's user avatar
  • 1
0 votes
1 answer
94 views

Setting `scroll-margin` for `term-mode` using `unless` is not working

I want to set default value of scroll-margin to 3 but for ansi-term I want it to be 0. To do this I have added a hook to term-mode-hook which worked fine. (setq scroll-margin 3) ...
Chinmay Chhajed's user avatar
-1 votes
2 answers
52 views

Writing a conditional in a compact way

Could I write the if condition on the same line. Is there any other way to include the equivalent of the if conditional in a more compact way ? (pcase grapheme ("dinkus" (if (...
user avatar
-2 votes
1 answer
36 views

cond clause for optional arguments

Am using an optional argument and want to use cond to execute (do-this) if actm is present and matches 'regexp, and (do-that) otherwise. Is the following the correct way to do it, or am I missing ...
Dilna's user avatar
  • 1