org mode 启动的时候有点问题

环境:

emacs:29.1

问题:

Debugger entered--Lisp error: (void-variable org-mode-map)
  (define-key org-mode-map (kbd "C-c h") 'org-habit-stats-view-habit-at-point)
  eval-buffer(#<buffer  *load*-631604> nil "/Users/mmmmmcclxxvii/.emacs.d/org/init-org.el" nil t)  ; Reading at buffer position 95
  load-with-code-conversion("/Users/mmmmmcclxxvii/.emacs.d/org/init-org.el" "/Users/mmmmmcclxxvii/.emacs.d/org/init-org.el" nil t)
  #<subr require>(init-org)
  apply(#<subr require> init-org nil)
  (prog1 (apply orig feature args) (if (and (not already-loaded) (memq feature features)) (progn (let ((time (sanityinc/time-subtract-millis (current-time) require-start-time))) (add-to-list 'sanityinc/require-times (list feature require-start-time time) t)))))
  (let* ((already-loaded (memq feature features)) (require-start-time (and (not already-loaded) (current-time)))) (prog1 (apply orig feature args) (if (and (not already-loaded) (memq feature features)) (progn (let ((time (sanityinc/time-subtract-millis ... require-start-time))) (add-to-list 'sanityinc/require-times (list feature require-start-time time) t))))))
  sanityinc/require-times-wrapper(#<subr require> init-org)
  apply(sanityinc/require-times-wrapper #<subr require> init-org)
  require(init-org)
  eval-buffer(#<buffer  *load*> nil "/Users/mmmmmcclxxvii/.emacs.d/init.el" nil t)  ; Reading at buffer position 4813
  load-with-code-conversion("/Users/mmmmmcclxxvii/.emacs.d/init.el" "/Users/mmmmmcclxxvii/.emacs.d/init.el" t t)
  load("/Users/mmmmmcclxxvii/.emacs.d/init" noerror nomessage)
  startup--load-user-init-file(#f(compiled-function () #<bytecode 0x155dede9da33cd>) #f(compiled-function () #<bytecode -0x1f3c61addc0da035>) t)
  command-line()
  normal-top-level()

配置:

出现问题地方的配置 init-org.el

;;org-habit-stats
(define-key org-mode-map (kbd "C-c h") 'org-habit-stats-view-habit-at-point)
(define-key org-agenda-mode-map (kbd "H") 'org-habit-stats-view-habit-at-point-agenda)

但是我感觉这里应该没有问题,因为是从 org-habit-stats 这里直接拷贝过来的

想了一下,感觉是加载顺序的问题

我的 init.el会去 require其他的配置,包括 init-org.el

我把 (require 'init-org) 语句放在 (package-initialize)语句后面,之前是放在前面,但是好像还是不行,还是报错

现在的init.el

(defun add-subdirs-to-load-path (search-dir)
  (interactive)
  (let* ((dir (file-name-as-directory search-dir)))
    (dolist (subdir
             ;; 过滤出不必要的目录,提升 Emacs 启动速度
             (cl-remove-if
              #'(lambda (subdir)
                  (or
                   ;; 不是目录的文件都移除
                   (not (file-directory-p (concat dir subdir)))
                   ;; 父目录、 语言相关和版本控制目录都移除
                   (member subdir '("." ".." 
                                    "dist" "node_modules" "__pycache__" 
                                    "RCS" "CVS" "rcs" "cvs" ".git" ".github")))) 
              (directory-files dir)))
      (let ((subdir-path (concat dir (file-name-as-directory subdir))))
        ;; 目录下有 .el .so .dll 文件的路径才添加到 `load-path' 中,提升 Emacs 启动速度
        (when (cl-some #'(lambda (subdir-file)
                           (and (file-regular-p (concat subdir-path subdir-file))
                                ;; .so .dll 文件指非 Elisp 语言编写的 Emacs 动态库
                                (member (file-name-extension subdir-file) '("el" "so" "dll"))))
                       (directory-files subdir-path))

          ;; 注意:`add-to-list' 函数的第三个参数必须为 t ,表示加到列表末尾
          ;; 这样 Emacs 会从父目录到子目录的顺序搜索 Elisp 插件,顺序反过来会导致 Emacs 无法正常启动
          (add-to-list 'load-path subdir-path t))

        ;; 继续递归搜索子目录
        (add-subdirs-to-load-path subdir-path)))))

;; Comment/uncomment this line to enable MELPA Stable if desired.  See `package-archive-priorities`
;; and `package-pinned-packages`. Most users will not need or want to do this.
;;(add-to-list 'package-archives '("melpa-stable" . "https://stable.melpa.org/packages/") t)

;;straight
(defvar bootstrap-version)
(let ((bootstrap-file
       (expand-file-name "straight/repos/straight.el/bootstrap.el" user-emacs-directory))
      (bootstrap-version 5))
  (unless (file-exists-p bootstrap-file)
    (with-current-buffer
        (url-retrieve-synchronously
         "https://raw.githubusercontent.com/raxod502/straight.el/develop/install.el"
         'silent 'inhibit-cookies)
      (goto-char (point-max))
      (eval-print-last-sexp)))
  (load bootstrap-file nil 'nomessage))




;;for geiser
(add-to-list 'package-archives
	     '("nongnu" . "https://elpa.nongnu.org/nongnu/") t)
(add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/") t)

(add-to-list 'load-path "~/.emacs.d/org" t)
(add-to-list 'load-path (expand-file-name "common" user-emacs-directory) t)
(add-to-list 'load-path (expand-file-name "life" user-emacs-directory) t)
(add-to-list 'load-path "~/.emacs.d/common/lsp-bridge" t)
(add-subdirs-to-load-path "~/.emacs.d/site-lisp")



(eval-when-compile
  (require 'use-package))
;;(require 'use-package-ensure)
;;(setq use-package-always-ensure t)


(package-initialize)


(custom-set-variables
 ;; custom-set-variables was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 '(custom-safe-themes
   '("a56cc18045d90be8f770ae409fc86274f8e5de2999a16b604ff84f8015e8d1e5" "9cdb010e4840d45440681f3ae76043e2b17972e7b3af7549cf310c8f087a05d2" "b6c43bb2aea78890cf6bd4a970e6e0277d2daf0075272817ea8bb53f9c6a7f0a" "37c8c2817010e59734fe1f9302a7e6a2b5e8cc648cf6a6cc8b85f3bf17fececf" "e9d47d6d41e42a8313c81995a60b2af6588e9f01a1cf19ca42669a7ffd5c2fde" "f366d4bc6d14dcac2963d45df51956b2409a15b770ec2f6d730e73ce0ca5c8a7" "573182354a59c672f89b8a7ea98ef020a54ff3fc93cf67bbfae4aa5bd41fdd5d" default))
 '(org-agenda-files
   '("/Users/mmmmmcclxxvii/Documents/cybertheye's life/gain-insights.org" "/Users/mmmmmcclxxvii/Documents/cybertheye's life/inbox.org" "/Users/mmmmmcclxxvii/Documents/cybertheye's life/recipe.org" "/Users/mmmmmcclxxvii/Documents/cybertheye's life/whoami.org"))
 '(org-modules
   '(ol-bbdb ol-bibtex ol-docview ol-doi ol-eww ol-gnus org-habit ol-info ol-irc ol-mhe ol-rmail ol-w3m))
 '(package-selected-packages
   '(vue-mode lsp-pyright rust-mode typescript-mode pdf-tools auctex vmd-mode flatland-theme qtcreator-theme markdown-mode interaction-log treemacs log4j-mode log4e helm-lsp dap-java which-key lsp-ui yasnippet flycheck projectile quickrun helm weibo geiser-racket geiser-mit ample-theme evil monokai-theme geiser cyberpunk-theme zenburn-theme ob-ipython idea-darkula-theme org org-modern company)))
(custom-set-faces
 ;; custom-set-faces was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 )


(require 'init-benchmarking) ;; Measure startup time

;;basic setting
(require 'basic)

;;latex setting
(require 'init-latex)

;;developement setting 
(require 'init-dev)

;; other module setting
(require 'init-org)

;; interesting plugin about life
(require 'init-life)


;; manage packages
(require 'package)

;; writegood mode
(require 'writegood-mode)


(load-theme 'flatland t)

把org-mode相关的配置写在org-hook里

(add-hook  'org-mode-hook 
   (lambda ()
   ;;这里写org-mode-map的
))

1 个赞

Or just (eval-after-load 'org ...)