[英]How can I insert into the middle of a list in ELisp?
我有以下数据结构作为 ELisp 变量( ispell-tex-skip-alists
):
((("---" ispell-tex-arg-end 2)
("---" ispell-tex-arg-end)
("---" ispell-tex-arg-end)
("---" ispell-tex-arg-end)
("---" ispell-tex-arg-end 0)
("---" ispell-tex-arg-end)
("---" . "---"))
(("---" ispell-tex-arg-end 0)
("---" ispell-tex-arg-end 2)
("env1" . "endenv1")
("env2" . "endenv2")))
我可以使用cddadr
隔离我需要的列表部分,但我需要向此列表添加一个元素:
((("---" ispell-tex-arg-end 2)
("---" ispell-tex-arg-end)
("---" ispell-tex-arg-end)
("---" ispell-tex-arg-end)
("---" ispell-tex-arg-end 0)
("---" ispell-tex-arg-end)
("---" . "---"))
(("---" ispell-tex-arg-end 0)
("---" ispell-tex-arg-end 2)
("env1" . "endenv1")
("env2" . "endenv2")
("env3" . "endenv3")))
我试过add-to-list
,但显然我需要add-to-list
的cddadr
部分的符号名称(我可以在技术上优化为cadr
)才能使add-to-list
工作。
我怎样才能达到我需要的结果?
最佳尝试:
(defun LaTeX-ispell-skip-environment (env &optional star)
(let ((start (concat "%s" (if star "\\*?")))
(end (concat "\\\\end[ \n]*{[ \n]*%s" (if star "\\*?") "[ \n]*}")))
(let ((env-list (cddadr ispell-tex-skip-alists)))
(setcdr (last env-list)
(cons (format start env)
(format end env))))))
我假设您的方法中缺少一个list
:
(defun LaTeX-ispell-skip-environment (env &optional star)
(let ((start (concat "%s" (if star "\\*?")))
(end (concat "\\\\end[ \n]*{[ \n]*%s" (if star "\\*?") "[ \n]*}")))
(let ((env-list (cddadr ispell-tex-skip-alists)))
(setcdr (last env-list)
(list
(cons (format start env)
(format end env)))))))
在您的版本中,相关列表的结构发生了变化。 在修改之前,它是一个缺点列表。 修改后是一个混合列表。 大多数条目都是 conses,但最后一个 car 是一个字符串,最后一个 cdr 也是一个字符串。
或者setcdr
和last
,您可以使用nconc
:
(defun LaTeX-ispell-skip-environment (env &optional star)
(let ((start (concat "%s" (if star "\\*?")))
(end (concat "\\\\end[ \n]*{[ \n]*%s" (if star "\\*?") "[ \n]*}")))
(let ((env-list (cddadr ispell-tex-skip-alists)))
(nconc env-list
(list
(cons (format start env)
(format end env)))))))
接下来是一个测试。 为了与原始发布的想要的结果进行比较,我将"\\\\\\\\end[ \\n]*{[ \\n]*%s"
替换为"end%s"
和"[ \\n]*}"
""
在LaTeX-ispell-skip-environment
。 在的端progn
的结果(LaTeX-ispell-skip-environment "env3")
与测试equal
针对想要的结果。 测试返回t
。
(progn
(defun LaTeX-ispell-skip-environment (env &optional star)
(let ((start (concat "%s" (if star "\\*?")))
(end (concat "end%s" (if star "\\*?") "")))
(let ((env-list (cddadr ispell-tex-skip-alists)))
(setcdr (last env-list)
(list
(cons (format start env)
(format end env)))))))
(setq ispell-tex-skip-alists
'((("---" ispell-tex-arg-end 2)
("---" ispell-tex-arg-end)
("---" ispell-tex-arg-end)
("---" ispell-tex-arg-end)
("---" ispell-tex-arg-end 0)
("---" ispell-tex-arg-end)
("---" . "---"))
(("---" ispell-tex-arg-end 0)
("---" ispell-tex-arg-end 2)
("env1" . "endenv1")
("env2" . "endenv2"))))
(LaTeX-ispell-skip-environment "env3")
(equal ispell-tex-skip-alists
'((("---" ispell-tex-arg-end 2)
("---" ispell-tex-arg-end)
("---" ispell-tex-arg-end)
("---" ispell-tex-arg-end)
("---" ispell-tex-arg-end 0)
("---" ispell-tex-arg-end)
("---" . "---"))
(("---" ispell-tex-arg-end 0)
("---" ispell-tex-arg-end 2)
("env1" . "endenv1")
("env2" . "endenv2")
("env3" . "endenv3")))))
为了更笼统地回答标题中提到的问题,这里有一个函数,它在列表的第 n 个位置插入一个元素:
(defun insert-into-list (list el n)
"Insert into list LIST an element EL at index N.
If N is 0, EL is inserted before the first element.
The resulting list is returned. As the list contents is mutated
in-place, the old list reference does not remain valid."
(let* ((padded-list (cons nil list))
(c (nthcdr n padded-list)))
(setcdr c (cons el (cdr c)))
(cdr padded-list)))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.