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 ...
  • 4,642
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 ...
  • 71k
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 (...
  • 5,550
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 ...
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))))
  • 11k
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 ...
  • 25.5k
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 ...
  • 9,002
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 ...
  • 24.5k
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) (...
  • 12.6k
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 ...
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 ...
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) ;; ...
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= ...
  • 44.3k
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)) ...
  • 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 ...
  • 31.4k
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 ...
  • 71k
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 ...
  • 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,...
  • 19.5k
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)))
  • 44.3k
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: (...
  • 44.3k
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*")
  • 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. (...
  • 30.8k
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 ...
  • 21.3k
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 ...
2 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 ...
  • 30.8k
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 ...
  • 11.1k
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) (...
  • 44.3k
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 ...
  • 71k
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 ...

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