【问题标题】:How to delete a list of elements from another list in ELisp?如何从 ELisp 中的另一个列表中删除元素列表?
【发布时间】:2014-07-29 06:35:49
【问题描述】:
(add-hook 'org-mode-hook
          (lambda ()
            (setq-local ac-sources
                        (delq '(ac-source-dictionary ac-source-words-in-same-mode-buffers) ac-sources))))

我在这里发现delq 只能从列表中删除元素,它不能从另一个列表中删除一个列表。既不是delete。

那么如何归档我的效果。

【问题讨论】:

  • 我很惊讶delete 不会从列表中删除列表。在我的 emacs 暂存缓冲区中,(delete '(first 2) '(first 2 (first 2) third 4)) 的结果是 (first 2 third 4)。

标签: list emacs elisp


【解决方案1】:

在循环中使用delq:

(mapcar (lambda (x) (setq ac-sources (delq x ac-sources)))
        '(ac-source-dictionary ac-source-words-in-same-mode-buffers))

【讨论】:

  • 如果您不需要mapcar 的累积结果,使用mapc 可能会更好。
  • 当然,为什么不呢。我很惊讶我不得不使用setq。你知道为什么普通的delq 不起作用吗?
  • @abo-abo 变量指向列表中的第一个 cons 单元格,其中存储了第一个列表元素。如果 delq 删除该元素及其 cons 单元格,则该变量仍保留 old cons 单元格。
  • 哦,我明白了,这对我有帮助,我用 (1 2 3) 和 ((1 2 3) 4 5 6) 做了一些测试。我徘徊为什么 (1 2 3) 不能被删除。现在我明白了。
【解决方案2】:

你要找的函数叫做cl-set-difference:

(add-hook 'org-mode-hook
  (lambda ()
    (setq-local ac-sources
                (cl-set-difference 
                 ac-sources
                 '(ac-source-dictionary ac-source-words-in-same-mode-buffers)))))

另见How to calculate difference between two sets in emacs lisp,the sets should be lists。

【讨论】:

    猜你喜欢
    • 2022-11-13
    • 1970-01-01
    • 2015-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-28
    相关资源
    最近更新 更多