0

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 associated with symbol.

  (defvar armgv 0)
  (defvar cia '(norm norm))

  (defun mbcomplt-authentis (cookie)
     "DOC."

   (let ( (armg (car cookie))
          (seqr (car (cdr cookie))) )
  
     (cond
      ((eq armg 'auto)
          (setq mbcomplt 0))
      ((eq armg 'icomplt-horz)
          (setq mbcomplt 1))
      ((eq armg 'icomplt-vert)
          (setq mbcomplt 2))
      ((eq armg 'ivy)
          (setq mbcomplt 3))
      ((eq armg 'vertico)
          (setq mbcomplt 4))
      ((eq armg 'helm)
          (setq mbcomplt 5)))))
Dilna
  • 1,173
  • 3
  • 10

1 Answers1

1

Your code sets mbcomplt to a number 1 to 5 depending on the value of the variable armg (which is supposed to be a symbol like 'ivy). A more efficient way to do the same thing is this:

    (setq mbcomplt
      (cl-case armg
        ('auto 0)
        ('icomplt-horz 1)
        ('icomplt-vert 2)
        ('ivy 3)
        ('vertico 4)
        ('helm 5)))

You can do something similar with cond: the point is that conditionals in lisp are expressions that return a value.

Fran Burstall
  • 3,665
  • 10
  • 18
  • Would you be kind enough to explain on use of symbols and values? Here you set `mbcomplt`, but need assistance on the meaning of the rest and how to use them. – Dilna Oct 30 '22 at 09:32
  • I don't know what you are asking here. Are you asking how `cl-case` works? (If so, do `C-h f cl-case`). Or are you asking how evaluation works in emacs-lisp, in which case you should read an introduction to emacs-lisp ( `C-h R eintr`). – Fran Burstall Oct 30 '22 at 09:41
  • Am asking how I would be using your construct. If I want to set `mbcomplt` to symbol `auto` with value `0`, what would I do. I would then have to check whether an argument passed to a function is `auto` and then use it value, according to value, then do something. – Dilna Oct 30 '22 at 10:02
  • How can I set a `symbol` and set a value to it? – Dilna Oct 30 '22 at 10:04
  • I am not sure you understand what your original code does. I have edited my answer to make things a bit clearer. However, I have no idea what you mean by "How can I set a `symbol` and set a value to it?" – Fran Burstall Oct 30 '22 at 10:31
  • Have seen things like `symbol-value`. Perhaps using this has no purpose for this case. When one sets a symbol to a variable. Could that symbol also include a value besides the symbol name? – Dilna Oct 30 '22 at 10:35
  • Ok, you compare `armg` against each symbol key, then set the numeric value. – Dilna Oct 30 '22 at 10:46
  • Correct: this is exactly what the `cond` expression in your post did. – Fran Burstall Oct 30 '22 at 12:25
  • You can also use [pcase](https://www.gnu.org/software/emacs/manual/html_node/elisp/pcase-Macro.html) so that you do not require 'cl-lib'. But I guess that [you knew about that](https://emacs.stackexchange.com/questions/74301/having-pcase-conditions-do-nothing) already. – dalanicolai Oct 30 '22 at 12:49