配置文件中的变量没被初始化

刚用emacs/spacemacs, 折腾配置文件就遇到下面这个问题. 在~/.spacemacs中, 最后一行load-file了这个文件.

;; Jiaxing Zhang's GNU/Emacs file
;; Note: This is for org-mode related settings

(require 'org)
; (define-key global-map "\C-cl" 'org-store-link)
; (define-key global-map "\C-ca" 'org-agenda)
(setq org-log-done t)
(setq org-log-done 'time)
(setq org-columns-default-format "%7TODO(To Do) %30ITEM(Task) %6EFFORT(estimate){:} %SCHEDULED %DEADLINE %6CLOCKSUM(Clock)")
; (setq org-log-done 'note)

; (setq org-emphasis-alist
;   '(("*" (bold :foreground "Orange" ))
;     ("/" italic)
;     ("_" underline)
;     ("=" (:background "maroon" :foreground "white"))
;     ("~" (:background "deep sky blue" :foreground "MidnightBlue"))
;     ("+" (:strike-through t))))

; (setq org-todo-keywords
;   '((sequence "TODO" "IN-PROGRESS" "WAITING" "DONE")))

(setq org-todo-keywords
    '((sequence "TODO(t)" "NEXT(n)" "INFLIGHT(f)" "DONE(d)")))

;; flyspell-mode on for org-mode
; (add-hook 'org-mode-hook 'turn-on-flyspell)

;; flyspell-mode to ignore in code block
(add-to-list 'ispell-skip-region-alist '(":\\(PROPERTIES\\|LOGBOOK\\):" . ":END:"))
(add-to-list 'ispell-skip-region-alist '("#\\+BEGIN_SRC" . "#\\+END_SRC"))


(setq org-agenda-files (list "/Users/quebec/Library/Mobile Documents/iCloud~com~appsonthemove~beorg/Documents/org/todo.org"))


; (setq org-capture-templates
;       '(("t" "Todo" entry (file+headline "~/notes/org/gtd.org" "Tasks")
;          "* TODO %?\n  %i\n  %a")
;         ("s" "Scratch" entry (file+datetree "~/notes/org/scratch.org")
;          "* %?\nEntered on %U\n  %i\n  %a")))

(global-set-key (kbd "C-<right>") 'org-do-demote)
(global-set-key (kbd "C-<left>") 'org-do-promote)
(define-key global-map "\C-ct"
  (lambda () (interactive) (org-capture nil "t")))

(define-key global-map "\C-cs"
  (lambda () (interactive) (org-capture nil "s")))
;;; To save the clock history across Emacs sessions, use
; (if (file-exists-p org-clock-persist-file)
;     ;; (setq org-clock-persist 'history)
;     (org-clock-persistence-insinuate)
;   (shell-command (concat "touch " org-clock-persist-file)))
(setq org-clock-persist 'history)
(org-clock-persistence-insinuate)


; {{{3 latex
(setq org-latex-pdf-process '("xelatex --shell-escape -interaction nonstopmode %f"))
  ;; 生成PDF后清理辅助文件
  ;; https://answer-id.com/53623039
  (setq org-latex-logfiles-extensions 
    (quote ("lof" "lot" "tex~" "tex" "aux" 
      "idx" "log" "out" "toc" "nav" 
      "snm" "vrb" "dvi" "fdb_latexmk" 
      "blg" "brf" "fls" "entoc" "ps" 
      "spl" "bbl" "xdv")))

  ;; 图片默认宽度
  (setq org-image-actual-width '(300))

  (setq org-export-with-sub-superscripts nil)

  ;; 不要自动创建备份文件
  (setq make-backup-files nil)

  ;; elegantpaper.cls
  ;; https://github.com/ElegantLaTeX/ElegantPaper/blob/master/elegantpaper.cls
  (with-eval-after-load 'ox-latex
  ;; http://orgmode.org/worg/org-faq.html#using-xelatex-for-pdf-export
  ;; latexmk runs pdflatex/xelatex (whatever is specified) multiple times
  ;; automatically to resolve the cross-references.
  ; (setq org-latex-pdf-process '("latexmk -xelatex -quiet -shell-escape -f %f"))
  (setq org-latex-listings t)
  (add-to-list 'org-latex-classes
                '("elegantpaper"
                  "\\documentclass[lang=cn]{elegantpaper}
                  [NO-DEFAULT-PACKAGES]
                  [PACKAGES]
                  [EXTRA]"
                  ("\\section{%s}" . "\\section*{%s}")
                  ("\\subsection{%s}" . "\\subsection*{%s}")
                  ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
                  ("\\paragraph{%s}" . "\\paragraph*{%s}")
                  ("\\subparagraph{%s}" . "\\subparagraph*{%s}")))
  )

; {{{3 fragtog
(use-package org-fragtog
  :hook
  (org-mode . org-fragtog-mode)
  :config
  (setq org-startup-with-latex-preview t)
  (setq org-latex-create-formula-image-program 'dvisvgm)
  (setq org-format-latex-options (plist-put org-format-latex-options :scale 2.0))
  :custom
  (org-fragtog-preview-delay 1.0))

; {{{3 export
;; Define a function for turning a single subtree into a top-level tree
;; (:export: headings might be located at an arbitrary nesting level,
;; so a single call to "org-promote-subtree" is not enough):
(defun org-promote-to-top-level ()
  "Promote a single subtree to top-level."
  (let ((cur-level (org-current-level)))
    (loop repeat (/ (- cur-level 1) (org-level-increment))
          do (org-promote-subtree))))

;; Define a function that applies "org-promote-to-top-level" 
;; to each :export: subtree:
(defun org-export-trees-to-top-level (backend)
  "Promote all subtrees tagged :export: to top-level.
BACKEND is the export back-end being used, as a symbol."
  (org-map-entries 'org-promote-to-top-level "+export"))

;; Make org-mode run "org-export-subtrees-to-top-level" as part of the export
;; process:
(add-hook 'org-export-before-parsing-hook 'org-export-trees-to-top-level)

其中初始化了(setq org-image-actual-width '(300)), 但是, 启动emacs后发现并没有生效, 于是用describle-variable查看, 发现它并没有被正确设置.

org-image-actual-width is a variable defined in ‘org.el’.
Its value is nil
Original value was t

但我又用describle-symbol查看org-export-trees-to-top-level, 发现这个已经被设置了, 说明这个文件是被正确执行了. 那么为什么org-image-actual-width的设置完全没起作用呢? 而且, 当我启动emacs, 再次load-file这个文件, 它又被正确设置了:

org-image-actual-width is a variable defined in ‘org.el’.
Its value is (300)
Original value was t

那么启动的时候怎么就没被正确设置呢? 一头雾水. 平台是mac, emacs是27.2.

不是没初始化,是被覆盖了。从 describle-variable 看,这个变量值至少经历了三个阶段的变化:

t -> '(300) -> nil

找一找是不是配置或包里有其他地方还有设置这个变量。能改则改,不能改就想办法让你的 setq 最后执行,比如放在某个 hook 里面,或在某个包加载之后。

这个文件就是最后一个被执行的(它对应的load-file是最后一行), 并且我曾试过在这个文件的末尾再次执行(setq org-image-actual-width '(300)). 还是不起作用. 这么看起来, 覆盖就一定是某个add-hook的作用?

不要着急辩论,在你的代码库里 rg org-image-actual-width 再来讨论对策。

不是某个文件它就一定最后执行。有很多种手段都可以改变执行顺序。

解决了, 我用的是spacemacs, 我之前的配置方法就是错的, 我直接把load-file放在~/.spacemacs最后, 但应该放在~/.spacemacs的user-config函数下, 这样才是最后执行的. 感谢回复!