Questions tagged [conditionals]
Questions about conditionals: cond, if, when, unless.
40 questions
0
votes
1
answer
50
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 ...
0
votes
1
answer
73
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?
-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 (...
0
votes
1
answer
69
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 ( (...
0
votes
2
answers
136
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-...
0
votes
1
answer
44
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 ...
-2
votes
1
answer
37
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 ...
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 ...
0
votes
1
answer
11
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.
(...
0
votes
2
answers
42
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 (&...
0
votes
2
answers
30
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)
...
0
votes
1
answer
145
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")
(...
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 |
|-...
0
votes
1
answer
60
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 && ...
3
votes
2
answers
296
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
...
4
votes
2
answers
229
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 ...
2
votes
1
answer
289
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 ...
1
vote
1
answer
170
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
(...
1
vote
1
answer
69
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 ...
0
votes
1
answer
36
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 ...
0
votes
1
answer
96
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) ...
0
votes
1
answer
584
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") #'...
0
votes
2
answers
45
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 &...
0
votes
1
answer
73
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
(...
1
vote
1
answer
297
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-...
0
votes
4
answers
289
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 ...
0
votes
1
answer
95
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 ...
4
votes
3
answers
2k
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 ...
0
votes
2
answers
529
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 ...
0
votes
1
answer
148
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-...
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. ...
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 ...
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 ...
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/...
5
votes
2
answers
282
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 ...
7
votes
2
answers
177
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 ...
3
votes
2
answers
265
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)
(...
2
votes
1
answer
672
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)
...
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) (...
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 ...