I define an alist like this:

 (setq major-mode-list
  '(((emacs-lisp-mode inferior-emacs-lisp-mode) . ("Elisp" "Emacs"))
    ((haskell-mode) . ("Haskell" "Haskell"))

And match current major-mode with this function:

 (defun major-mode-icon-3 ()
   (let* ((match (assoc
                  (car (mapcar
                        (lambda (element)
                          (member (buffer-local-value 'major-mode (current-buffer))
                        (map-keys major-mode-list)))
          (name (car (cdr match)))
          (icon (car (cdr (cdr match)))))

Function works correct in ((emacs-lisp-mode inferior-emacs-lisp-mode) . ("Elisp" "Emacs")).

But not correct in ((haskell-mode) . ("Haskell" "Haskell")). The function will return nil because of (car (haskell-mode)).

Question: How to correct this function?

  • I urge you to run the function under edebug (evaluate the defun using C-u C-M-x instead of C-M-x). This will let you step through the function and evaluate individual expressions. – sds Oct 2 '16 at 16:01
  • 1
    (buffer-local-value 'major-mode (current-buffer)) is redundant, just use major-mode instead. – wasamasa Oct 2 '16 at 16:27
  • You're constantly using assoc on the result of the member call of the first element of the key-list. – politza Oct 2 '16 at 19:54
  • @politza Because I need to literate over matched items in the alist. – stardiviner Oct 3 '16 at 2:11
  • 1
    Try cl-assoc-if. – npostavs Nov 2 '16 at 18:03

There are two methods. One simple methods is (remove nil '(nil nil (clojure-mode) nil nil ..). This answer is from IRC channel #emacs.

And another method is to use cl-some which is from Google search:

    (lambda (elem)
      (when (not (null elem))
     (lambda (element)
       (member major-mode element))
     (map-keys major-mode-list)))

Better way is use cl-assoc-if as @npostavs suggest.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.