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

`case` doesn't switch correctly with strings

cl-case uses eql for comparisons, so string get tested for identity not for equality in the sense of string=. You can fix this by just switching to pcase (or, if you really want to use case, convert ...
Omar's user avatar
  • 4,832
12 votes
Accepted

Using case to compare with a variable

case is an alias for cl-case, so there is no difference, there. This aspect of the behavior is the same in Common Lisp. There is nothing very weird here. You just need to read the doc, which tells ...
Drew's user avatar
  • 77.9k
9 votes

Using case to compare with a variable

BTW, I recommend the use of pcase, which is more powerful. The equivalent code to what you wrote, would be: (let ((x 1)) (pcase x ('x 'Yes) (_ 'No))) Making it clear that x is not ...
Stefan's user avatar
  • 26.5k
6 votes

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

Emacs Lisp doesn't have any support for automatic type checking, whether in the byte compiler or at runtime. The idiomatic way to report an error at runtime is to check the argument against a ...
Gilles 'SO- stop being evil''s user avatar
5 votes

Using case to compare with a variable

The first part of each clause in a case form is a value (or a list of values), not an expression. The x before 'Yes is syntactically interpreted as a value. The x after case, on the other hand, is ...
Gilles 'SO- stop being evil''s user avatar
5 votes

`case` doesn't switch correctly with strings

Your second version would more typically be written as a cond form: (cond ((string= system-name "HP") (setq package-user-dir (concat user-emacs-directory "packages/hp"))) ((string= ...
phils's user avatar
  • 51.5k
4 votes

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

I just happened to notice this while looking for a an already implemented wsl-browse-url (thanks!), so will offer one other option: (when (string-match "-[Mm]icrosoft" operating-system-release) ;; ...
Chris Bilson's user avatar
4 votes

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

You could also do (string-match-p "Microsoft" (shell-command-to-string "uname -a")) Likely in your my-browse-url-function you're probably depending on a Windows specific path to exist. You could just ...
willbush's user avatar
  • 306
3 votes
Accepted

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

If you wanted this for a function you would use apply, but as or is a special form you can't do that. In particular, or only evaluates as many arguments as it needs to. You could write a macro: (...
phils's user avatar
  • 51.5k
3 votes
Accepted

Specify optional matches with pcase

The backquote pcase pattern `--pcase-macroexpander is implemented using pcase-defmacro in the library pcase.el . There is no pattern included there that does what you discussed. But, it may be added ...
Tobias's user avatar
  • 33.3k
3 votes
Accepted

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

Read about condition-case in C-hig (elisp)Handling Errors (condition-case nil (function1) (error (function2)))
phils's user avatar
  • 51.5k
3 votes

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

I would check for an environment variable using getenv. You may want to check what variables are available in your typical WSL shell, but one option would be to check for a Windows-specific PATH entry,...
glucas's user avatar
  • 20.6k
3 votes
Accepted

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

Standard elisp does not allow you to put restrictions on the arguments in the header in the way you're specifying, but you may wish to check out cl-defun in the Common Lisp extension library to get ...
Dan's user avatar
  • 33.1k
2 votes

Shallowly override functions within a target function

Maybe you did not indicate the full problem you want to solve. But based only on your description, there are simpler ways to handle it. Two obvious ones come to mind (see below). You might want to ...
Drew's user avatar
  • 77.9k
2 votes

Disabling a minor mode in a specific buffer

You should be able to do what you want with something like: (add-to-list 'golden-ratio-exclude-buffer-regexp "*Org Agenda*")
goedson's user avatar
  • 173
2 votes

Nesting conditionals, consolidating functionality in elisp

I think indexing org tables by column and row names is such a general task that it deserves a general function org+-table-get. You could put the following code into your initialization file. (...
Tobias's user avatar
  • 33.3k
2 votes
Accepted

Matching against variable keys in `cl-case`

That behavior is actually described there, albeit in terse form: "Macro: cl-case keyform clause...": cl-case is a macro, macros do not evaluate their arguments. "This macro evaluates ...
wasamasa's user avatar
  • 22.2k
2 votes

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

How about this? (defun or-list (list) (cl-some #'identity list)) Here cl-some takes two arguments: a predicate and a list and returns non-nil if the predicate applied to some member of the list is ...
Fran Burstall's user avatar
2 votes
Accepted

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

I think this is what you want: (let ((flag1 nil) (flag2 nil)) (cl-loop if flag1 if flag2 return 1 else return 0 else return 2)) if flag1 is nil, you get 2 if flag1 is t, and flag2 ...
John Kitchin's user avatar
  • 11.9k
2 votes

Avoiding code duplication with conditional code that uses let bindings

For the sake of providing a more basic version of the answer by @db48x you could do the same thing like this: (defun my-func () (let (v1 v2) (if some-condition (setq v1 2 v2 3) (...
phils's user avatar
  • 51.5k
2 votes

Checking optional function argument

Does the if statement only check for [its first arg] being nil? Yes. And you can just ask Emacs: C-h f if. if is a special form in C source code. (if COND THEN ELSE...) If COND yields non-nil, do ...
Drew's user avatar
  • 77.9k
2 votes

Writing a conditional in a compact way

Only to show that this could be a XY-problem (TLDR: If we knew better what you want to do we could give you better help.): If you are testing var in each of the pcase cases you could include that ...
Tobias's user avatar
  • 33.3k
1 vote

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

Elisp variables aren't declared with a type. You can test a variable to determine the type of its current value; but if you're using setq or similar to assign values, then nothing prevents you from ...
phils's user avatar
  • 51.5k
1 vote
Accepted

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

Are you asking how to check whether the kill-ring is empty? If so, just test whether kill-ring is non-nil: (when kill-ring ...) Or are you asking how to avoid the error that it's empty? If so, just ...
Drew's user avatar
  • 77.9k
1 vote
Accepted

pyvenv conditional on hostname

You can figure this one out on your own by using C-x C-e to evaluate the individual forms. For example if you place point after system-name and hit C-x C-e, it will display your hostname (say, "...
wasamasa's user avatar
  • 22.2k
1 vote

package-installed-p vs fboundp

You should use eval-after-load - the function will not be defined if the package is not loaded, so checking whether it's installed does not help and checking whether the function is defined (the ...
NickD's user avatar
  • 30.5k
1 vote
Accepted

Query-replace stops after first item in while

If you try to match the form (while COND BODY) to what you have above, you will see that COND matches (let* ((pair (pop replacement-list)) (to-find (car pair)) (to-replace (cdr pair))) ...
NickD's user avatar
  • 30.5k
1 vote

Query-replace stops after first item in while

You are using while, not dolist, while stops when the TEST condition returns nil, not sure about the value of query-replace, but it definitely does not do what you want.
xuchunyang's user avatar
  • 14.6k
1 vote

Nesting conditionals, consolidating functionality in elisp

I might be missing the point of the question, but I think the main reason this looks messy is that the cond statement has 5 branches. A nested conditional on its own doesn't look that bad to me, so ...
Karl Erik Holter's user avatar
1 vote

Nesting conditionals, consolidating functionality in elisp

This is an X-Y-problem. Just avoid the multi-branched conditionals if you actually do not need them. I show you one way to do that with the code below. The most important details of the code: If ...
Tobias's user avatar
  • 33.3k

Only top scored, non community-wiki answers of a minimum length are eligible