如何用生成字符串的函数覆盖defcustom of type: string?下面是详细的设置:
电流设置
在..dir locals.el文件中,创建了python-shell-virtualenv-root和python-pytest-executable两个变量。
((nil . ((eval . (let ((direnv-python ".direnv/python-3.7.2"))
(setq-local python-shell-virtualenv-root (expand-file-name direnv-python projectile-project-root)
python-pytest-executable (expand-file-name (concat direnv-python "/bin/pytest") projectile-project-root)))
))))上面的代码片段使用direnv-python构建这两个变量。
首选设置
全局地定义构建两个变量python-shell-virtualenv-root和python-pytest-executable的函数,最好是在init.el中。然后,在.dir-locals.el中定义direnv-python变量。
然后使用python-shell-virtualenv-root变量动态地创建python-pytest-executable和direnv-python。
动机
创建python-shell-virtualenv-root和python-pytest-executable的逻辑对于每个项目都是相同的。direnv-python是特定于项目的。我只想指定后面的每个项目。
编辑
1 defvar应为defcustom
发布于 2021-08-27 10:12:49
@Drew在评论中回答了这个问题。简而言之,如果您确保在已经存在的定义之前对其进行评估,则可以使用所需的defcustom定义您自己的type。
但是,正如Drew所提到的,它可能不希望您想要这样做,因为其他代码期望原始type,因此很可能会中断。
首选设置
为了获得问题中提到的首选设置,我做了以下操作:
;; In config.el
(defcustom python-projectile-environment-directory ".direnv/python-3.7.2"
"The python environment within a projectile project"
:type 'string
:group 'python)
;; Run a hook after local vars are read
;; Source: https://stackoverflow.com/questions/5147060/how-can-i-access-directory-local-variables-in-my-major-mode-hooks
(defun run-local-vars-mode-hook ()
"Run a hook for the major-mode after the local variables have been processed."
(run-hooks (intern (concat (symbol-name major-mode) "-local-vars-hook"))))
(add-hook 'hack-local-variables-hook 'run-local-vars-mode-hook)
;; Set-up the python shell
(defun config/python-mode-shell-setup ()
(message "project python environment is %s" python-projectile-environment-directory)
(setq-local python-shell-virtualenv-root (expand-file-name python-projectile-environment-directory (projectile-project-root))
python-pytest-executable (expand-file-name (concat python-projectile-environment-directory "/bin/pytest") (projectile-project-root))))
(add-hook 'python-mode-local-vars-hook 'config/python-mode-shell-setup)run-local-vars-mode-hook允许您在读取局部变量之后运行钩子(有关详细信息,请参阅前面提到的链接)。这是允许在钩子中使用局部变量的缺失链接。
之后,config/python-mode-shell-setup使用局部变量python-projectile-environment-directory设置python。
;; .dir-locals.el
((python-mode . ((python-projectile-environment-directory . ".direnv/python-3.7.9"))))如果不存在该局部变量,则使用defcustom的默认值“..direnv/python-3.7.2”。
https://stackoverflow.com/questions/68906610
复制相似问题