大家好,我正试图找到存储和重置系统变量列表的最短方法。但是,我不是列表操作专家,因此我需要一些mapcar和lambda函数的帮助:
(defun C:test ( / vars ); declare the list of variable names where each item is: (var-name-X new-var-val-X):(setq vars '(("FILLETRAD" 10) ("CLIPROMPTLINES" 1) ("OSMODE" 0) ("CMDECHO" 0))); this should reconstruct the list where each item is: (var-name-X old-varval-X new-var-val-X)):(setq vars (mapcar '(lambda (x) (list (car x) (getvar (car x)) (cadr x)) vars))); set the new values:(mapcar '(lambda (x) (setvar (car x) (caddr x)) vars)); perform var-check:(foreach x vars (princ (strcat (car x) " with value: " (getvar (car x)))))(alert "\nThe main function starts!"); restore the variable values:(mapcar '(lambda (x) (setvar (car x) (cadr x)) vars)); perform var-check:(foreach x vars (princ (strcat (car x) " with value: " (getvar (car x)))))(princ)); defun
目标是只设置一个名为“vars”的引号,而不是为每个系统变量设置一组引号。所以我们都可以从中受益
换句话说(一些测试是从visual lisp控制台复制的):
_$ ; declare the list of variable names "vars" where each item is: (var-name-X new-var-val-X):(setq vars '(("FILLETRAD" 10) ("CLIPROMPTLINES" 1) ("OSMODE" 0) ("CMDECHO" 0)))(("FILLETRAD" 10) ("CLIPROMPTLINES" 1) ("OSMODE" 0) ("CMDECHO" 0))_$ ; this should reconstruct the list to "nvars" where each item is: (var-name-X old-varval-X new-var-val-X)):(setq nvars (list))nil_$ _$ (foreach itm vars (if (not (member (car itm) (mapcar 'car nvars))) (setq nvars (cons (list (car itm) (getvar (car itm)) (cadr itm)) nvars))))(("CMDECHO" 1 0) ("OSMODE" 15359 0) ("CLIPROMPTLINES" 4 1) ("FILLETRAD" 52.0 10))_$ (setq nvars (reverse nvars))(("FILLETRAD" 52.0 10) ("CLIPROMPTLINES" 4 1) ("OSMODE" 15359 0) ("CMDECHO" 1 0))_$ ; set the new values:(foreach itm nvars(setvar (car itm) (caddr itm))(princ (strcat "\n" (car itm) " : " (rtos (getvar (car itm))))))FILLETRAD : 10.0000CLIPROMPTLINES : 1.0000OSMODE : 0.0000CMDECHO : 0.0000"\nCMDECHO : 0.0000"_$ (alert "\nThe main function starts!")nil_$ ; restore the variable values:(foreach itm nvars(setvar (car itm) (cadr itm))(princ (strcat "\n" (car itm) " : " (rtos (getvar (car itm))))))FILLETRAD : 52.0000CLIPROMPTLINES : 4.0000OSMODE : 15359.0000CMDECHO : 1.0000"\nCMDECHO : 1.0000"_$
但在这里,我使用了两个引号“vars”和“nvars”-我的问题是,是否可以将其缩短为仅一个“vars”并可能进行一些覆盖(使用mapcar/lambda而不是我的新手foreach方法)。