help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: emacs lisp. Is there a rassoc-default for getting ALL elementsmatchi


From: WJ
Subject: Re: emacs lisp. Is there a rassoc-default for getting ALL elementsmatching a VALUE?
Date: Wed, 28 Mar 2012 19:18:46 -0000
User-agent: XanaNews/1.18.1.6

Xah Lee wrote:

> 
> emacs lisp. Is there a rassoc-default for getting ALL elements
> matching a VALUE?
> 
> emas lisp. Is there a rassoc-default function? i.e. similar to assoc-
> default but get all items of a alist of a give value.
> 
> i want to be able to properly setup cperl-mode to load instead of perl-
> mode. Here's detail.
> 
> easy way is just
>  (defalias 'perl-mode 'cperl-mode)
> 
> but the problem with that is you can't call perl-mode anymore, if you
> still want it on occasion.
> 
> So, i went to set the auto-mode-alist. Like this:
> 
>  (setq auto-mode-alist (rassq-delete-all 'perl-mode auto-mode-alist))
>  (add-to-list 'auto-mode-alist '("\\.\\([pP]\\([Llm]\\|erl\\|od\\)\\|al
> \\)\\'" . cperl-mode))
> 
> that turns out doesn't do it, because there's also interpreter-mode-
> alist. So i wrote:
> 
> (when
>     (rassoc 'perl-mode interpreter-mode-alist)
>   (let ((mykey (car (rassoc 'perl-mode interpreter-mode-alist)) ))
>     (setq interpreter-mode-alist (rassq-delete-all 'perl-mode
> interpreter-mode-alist))
>     (add-to-list 'interpreter-mode-alist (mykey . 'cperl-mode))
>     )
>   )
> 
> but i discovered there are actually several elements for perl in that
> alist:
> 
>  ("perl" . perl-mode)
>  ("perl5" . perl-mode)
>  ("miniperl" . perl-mode)
> 
> So my code above won't work.
> 
> So, the proper way is to query for the value 'perl-mode, get ALL
> results, then remove them all, then add them all back with 'cperl-
> mode.
> 
> Ι found the function "assoc-default", which gets all elements by KEY,
> not value.
> 
> so, my question is, is there a "rassoc-default" that gets by value?
> Or, is there any simpler proper way to setup cperl-mode instead of
> perl-mode?

Use map.

An example using Ruby:

require "pp"  # Pretty printing.

list = [
  ["foo", :bar],
  ["perl",  :perl_mode],
  ["perl5",  :perl_mode],
  ["miniperl",  :perl_mode],
  ["ah-ha", :ho_ho]]


pp list.map{|key,val|
  if :perl_mode == val
    [key, :cperl_mode]
  else
    [key, val]
  end
}

=== output ===

[["foo", :bar],
 ["perl", :cperl_mode],
 ["perl5", :cperl_mode],
 ["miniperl", :cperl_mode],
 ["ah-ha", :ho_ho]]


reply via email to

[Prev in Thread] Current Thread [Next in Thread]