首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用生成字符串的函数覆盖类型为‘string’的“Defcustom‘”

用生成字符串的函数覆盖类型为‘string’的“Defcustom‘”
EN

Stack Overflow用户
提问于 2021-08-24 11:19:28
回答 1查看 68关注 0票数 0

如何用生成字符串的函数覆盖defcustom of type: string?下面是详细的设置:

电流设置

..dir locals.el文件中,创建了python-shell-virtualenv-rootpython-pytest-executable两个变量。

代码语言:javascript
复制
((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-rootpython-pytest-executable的函数,最好是在init.el中。然后,在.dir-locals.el中定义direnv-python变量。

然后使用python-shell-virtualenv-root变量动态地创建python-pytest-executabledirenv-python

动机

创建python-shell-virtualenv-rootpython-pytest-executable的逻辑对于每个项目都是相同的。direnv-python是特定于项目的。我只想指定后面的每个项目。

编辑

1 defvar应为defcustom

EN

回答 1

Stack Overflow用户

发布于 2021-08-27 10:12:49

@Drew在评论中回答了这个问题。简而言之,如果您确保在已经存在的定义之前对其进行评估,则可以使用所需的defcustom定义您自己的type

但是,正如Drew所提到的,它可能不希望您想要这样做,因为其他代码期望原始type,因此很可能会中断。

首选设置

为了获得问题中提到的首选设置,我做了以下操作:

代码语言:javascript
复制
;; 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。

代码语言:javascript
复制
;; .dir-locals.el

((python-mode . ((python-projectile-environment-directory . ".direnv/python-3.7.9"))))

如果不存在该局部变量,则使用defcustom的默认值“..direnv/python-3.7.2”。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68906610

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档