2022 Developer Survey is open! Take survey.
11 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 ...
user avatar
  • 69.2k
11 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 ...
user avatar
  • 4,582
10 votes

`cond` with less redundancy

Traditional Emacs Lisp "in my time we didn't have that new-fangled CL stuff" style: (catch 'foo (dolist (str (list str1 str2 str3 str4)) (if (firstFunction str) (throw 'foo (...
user avatar
  • 5,480
8 votes

`cond` with less redundancy

Another way using dash.el. You could also do this with cl-lib functions. (-when-let (found (-find 'firstFunction (list str1 str2 str3 str3))) (secondFunction found)) Or, if you know at least one ...
user avatar
8 votes

`cond` with less redundancy

Wouldn't a loop do it? (cl-dolist (str (list str1 str2 str3 ...)) (when (first-function str) (cl-return (second-function str))))
user avatar
  • 10.8k
7 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 ...
user avatar
  • 25.2k
7 votes
Accepted

Elisp code execution order?

Forms in the function body are executed in order, just as you would expect. When you choose "y" if does not "jump" past the first message call. If looks like it did because the first message gets ...
user avatar
  • 8,922
6 votes

"and" vs "when" for conditionals

Let me start off by saying that (and a b) and (when a b) in your example do the same thing: First a is evaluated. b is evaluated if a is true#. But and and when are used for different things. You ...
user avatar
  • 24.3k
5 votes
Accepted

How execute multiple lists in a true statement?

Use progn or prog1. progn takes a list of body forms, evaluates them one by one, then returns the value of the last one. prog1 does the same but returns the value of the first one. (if (true) (...
user avatar
  • 11.4k
5 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 ...
user avatar
5 votes
Accepted

Underscore in if-let

Since Emacs 26, the underscore (or any variable symbol in general) can be omitted: if-let is a Lisp macro in `subr-x.el'. (if-let SPEC THEN &rest ELSE) Probably introduced at or before Emacs ...
user avatar
  • 11.3k
4 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 ...
user avatar
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) ;; ...
user avatar
4 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= ...
user avatar
  • 43.1k
4 votes

`cond` with less redundancy

Just for fun... (funcall (lambda (f l) (funcall f l f)) (lambda (l f) (if l (if (firstFunction (car l)) (secondFunction (car l)) ...
user avatar
  • 568
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 ...
user avatar
  • 31k
3 votes

Elisp code execution order?

It's not being skipped. The second message is printed after the first message is printed, but there is no wait between the two, so you do not notice the first message. Look in buffer *Messages* and ...
user avatar
  • 69.2k
3 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 ...
user avatar
  • 186
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,...
user avatar
  • 19.1k
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)))
user avatar
  • 43.1k
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: (...
user avatar
  • 43.1k
3 votes

Underscore in if-let

Apologies for not helpfully answering the question posed, but hoping this might help anyway... Instead of something "similar to the first", I'd likely use something that (I think) is simpler ...
user avatar
  • 69.2k
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*")
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. (...
user avatar
  • 29.9k
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 ...
user avatar
  • 21k
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 ...
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 ...
user avatar
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 ...
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 ...
user avatar
  • 29.9k

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