关于自己编写layer的问题(youdao-dictionary、org配置 )

我参照山人spacemacs视频中自己写layer的方式没有成功安装有道。 Message buffer中可以看到: package youdao-dictionary not initialized

只有把youdao-dictionary 放到 additional()里:

 dotspacemacs-additional-packages '(youdao-dictionary)

并将快捷键放在dotspacemacs/user-config ()里,才能正常使用。

1 个赞

那可能是你的 layer 的编写方式有问题,我自己写的 layer 是可以正确安装的。你可以把你的 layer 的配置代码贴上来看看。

我的spacemacs配置放在这里了。

我觉得也应该是我配置的问题,但是我看了好几遍,也没发现问题。youdao词典在上述的配置下已经能工作了。 不过又出现了: File mode specification error: (file-error Cannot open load file No such file or directory ob-C++) org-babel-execute-src-block: No org-babel-execute function for C++! 这个错误。

layer的主要配置我也写在下面,大神有空时帮我看看: package.el:

(defconst ziyuan-packages
  '(company
    org-ref
    org-bullets
    htmlize
    auctex
    anaconda-mode
    (org-download :location (recipe :fetcher github :repo "abo-abo/org-download"))
    (org :location built-in))
 )

;;一个在org中方便插入图片的package, see: https://github.com/abo-abo/org-download
(defun ziyuan/init-org-download()
  (use-package org-download
    :init))

(defun ziyuan/init-org-ref()
  (use-package org-ref
  :init))

(defun ziyuan/init-org-bullets()
  (use-package org-bullets
  :init))

(defun ziyuan/init-htmlize()
  (use-package htmlize
  :init))

(defun ziyuan/init-anaconda-mode()
  (use-package anaconda-mode
  :init))

(defun ziyuan/init-org()
    (setq org-hide-emphasis-markers t)  ;;隐藏字体样式标志
    (setq org-agenda-files '("~/Nutstore/gtd"))  ;; 设置默认 Org Agenda 文件目录
    (setq org-use-sub-superscripts nil)  ;;上下标默认不作用,需要时加{}
    (setq org-confirm-babel-evaluate nil) ;;在用C-c C-c执行代码块时,不再提示“Do you want to execute”
    (setq org-src-fontify-natively t) ;; Org-mode 文本内语法高亮
    ;;Let's have pretty source code blocks : 语法高亮、保留原生tab等
    (setq org-edit-src-content-indentation 0
          org-src-tab-acts-natively 1
          org-src-fontify-natively t  ;; Org-mode 文本内语法高亮
          org-confirm-babel-evaluate nil
          org-support-shift-select 'always)

    (setq org-export-babel-evaluate (quote inline-only))

  (require 'ox-latex)
  (add-to-list 'org-latex-classes
               '("ctexart"
                 "\\documentclass[UTF8]{ctexart}
        \\usepackage{amsmath,latexsym,amssymb,mathrsfs,pifont}
        \\usepackage[T1]{fontenc}
        \\usepackage{fixltx2e}
        \\usepackage{graphicx}
        \\usepackage{subfig} 
        \\usepackage{grffile}
        \\usepackage{longtable}
        \\usepackage{wrapfig}
        \\usepackage{rotating}
        \\usepackage{minted}
        \\usepackage[colorlinks=true]{hyperref}
        \\tolerance=1000
        [NO-DEFAULT-PACKAGES]
        [NO-PACKAGES]" 
                 ("\\section{%s}" . "\\section*{%s}")
                 ("\\subsection{%s}" . "\\subsection*{%s}")
                 ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
                 ("\\paragraph{%s}" . "\\paragraph*{%s}")
                 ("\\subparagraph{%s}" . "\\subparagraph*{%s}"))) 

  ;; 使用xelatex一步生成PDF
  (setq-default org-latex-pdf-process
                '("xelatex -shell-escape -interaction nonstopmode -output-directory %o %f"
                  "xelatex -shell-escape -interaction nonstopmode -output-directory %o %f"
                  "xelatex -shell-escape -interaction nonstopmode -output-directory %o %f"
                  "rm -fr %b.out %b.log %b.tex auto"
                  ))

  ;; Tell the latex export to use the minted package for source code coloration.
  (setq org-latex-listings 'minted)
  )

(defun ziyuan/post-init-org()
    (setq org-babel-load-languages
      (quote
       ((emacs-lisp . t)
        (python . t)
        (sh . t)
        (C++ . t)
        (C . t)
        (dot . t)
        (latex . t)
        (matlab . t))))

    ;; 加密文章
    ;; "http://coldnew.github.io/blog/2013/07/13_5b094.html"
    ;; org-mode 設定
    (require 'org-crypt)

    ;; 當被加密的部份要存入硬碟時,自動加密回去
    (org-crypt-use-before-save-magic)

    ;; 設定要加密的 tag 標籤為 secret
    (setq org-crypt-tag-matcher "secret")

    ;; 避免 secret 這個 tag 被子項目繼承 造成重複加密
    ;; (但是子項目還是會被加密喔)
    (setq org-tags-exclude-from-inheritance (quote ("secret")))

    ;; 用於加密的 GPG 金鑰
    ;; 可以設定任何 ID 或是設成 nil 來使用對稱式加密 (symmetric encryption)
    (setq org-crypt-key nil)

)

(defun ziyuan/post-init-org-ref()
  (setq reftex-default-bibliography '("~/Refrences/bibliography/refs.bib"))
  ;; see org-ref for use of these variables
  (setq org-ref-bibliography-notes "~/Refrences/bibliography/notes.org"
        org-ref-default-bibliography '("~/Refrences/bibliography/refs.bib")
        org-ref-pdf-directory "~/Refrences/bibliography/bibtex-pdfs/")
  )

(defun ziyuan/post-init-company()
    (setq global-company-mode 1) ; 开启全局 Company 补全
    )

(defun ziyuan/post-init-org-bullets()
  (add-hook 'org-mode-hook (lambda () (org-bullets-mode 1)))
  (add-hook 'org-mode-hook 'turn-on-visual-line-mode)
  (setq-default org-bullets-bullet-list '("⓪" "①" "②" "③"
                                          "④" "⑤" "⑥" "⑦"
                                          "⑧" "⑨" "⑩" "⑪"
                                          "⑫" "⑬" "⑭"
                                          "⑮" "⑯" "⑰"
                                          "⑱" "⑲" "⑳"))
  )

.spacemacs.d/init.el:

(defun dotspacemacs/layers ()
  (setq-default
   dotspacemacs-distribution 'spacemacs
   dotspacemacs-configuration-layer-path '()
   dotspacemacs-configuration-layers
   '(
     ivy
     helm
     auto-completion
     (chinese :variables
              chinese-enable-youdao-dict t
              chinese-enable-fcitx t)
     (better-defaults :variables better-defaults-move-to-end-of-code-first t)   ;;test
     emacs-lisp
     git
     markdown
     org
     spell-checking
     syntax-checking
     ziyuan
     )
   dotspacemacs-additional-packages '()
   dotspacemacs-excluded-packages '(chinese-wbim
                                    chinese-pyim)
   dotspacemacs-delete-orphan-packages t))

(defun dotspacemacs/init ()
  (setq-default
   dotspacemacs-elpa-https t
   dotspacemacs-elpa-timeout 5
   dotspacemacs-check-for-update t
   dotspacemacs-editing-style 'vim
   dotspacemacs-verbose-loading nil
   dotspacemacs-startup-banner 'official
   dotspacemacs-startup-lists '(recents projects)
   dotspacemacs-startup-recent-list-size 10
   dotspacemacs-scratch-mode 'text-mode
   dotspacemacs-themes '(monokai
                         spacemacs-dark
                         spacemacs-light
                         solarized-light
                         solarized-dark
                         leuven
                         zenburn)
   dotspacemacs-colorize-cursor-according-to-state t
   dotspacemacs-default-font '("Consolas"
                               :size 20
                               :weight normal
                               :width normal
                               :powerline-scale 1.2)
   dotspacemacs-leader-key "SPC"
   dotspacemacs-emacs-leader-key "M-m"
   dotspacemacs-major-mode-leader-key ","
   dotspacemacs-major-mode-emacs-leader-key "C-M-m"
   dotspacemacs-distinguish-gui-tab nil
   dotspacemacs-command-key ":"
   dotspacemacs-remap-Y-to-y$ t
   dotspacemacs-default-layout-name "Default"
   dotspacemacs-display-default-layout nil
   dotspacemacs-auto-resume-layouts nil
   dotspacemacs-auto-save-file-location 'cache
   dotspacemacs-max-rollback-slots 5
   dotspacemacs-use-ido nil
   dotspacemacs-helm-resize nil
   dotspacemacs-helm-no-header nil
   dotspacemacs-helm-position 'bottom
   dotspacemacs-enable-paste-micro-state nil
   dotspacemacs-which-key-delay 0.4
   dotspacemacs-which-key-position 'bottom
   dotspacemacs-loading-progress-bar t
   dotspacemacs-fullscreen-at-startup nil
   dotspacemacs-fullscreen-use-non-native nil
   dotspacemacs-maximized-at-startup t
   dotspacemacs-active-transparency 90
   dotspacemacs-inactive-transparency 90
   dotspacemacs-mode-line-unicode-symbols t
   dotspacemacs-smooth-scrolling t
   dotspacemacs-line-numbers t
   dotspacemacs-smartparens-strict-mode nil
   dotspacemacs-highlight-delimiters 'all
   dotspacemacs-persistent-server nil
   dotspacemacs-search-tools '("ag" "pt" "ack" "grep")
   dotspacemacs-default-package-repository nil
   dotspacemacs-whitespace-cleanup nil
   dotspacemacs-startup-lists '((recents . 8)
                                bookmarks
                                (projects . 8))
   ))

(defun dotspacemacs/user-init ()
  (setq configuration-layer--elpa-archives
        '(("melpa-cn" . "http://elpa.zilongshanren.com/melpa/")
          ("org-cn"   . "http://elpa.zilongshanren.com/org/")
          ("gnu-cn"   . "http://elpa.zilongshanren.com/gnu/")
          ("elpy" . "https://jorgenschaefer.github.io/packages/")
          ))

  (setq tramp-ssh-controlmaster-options
        "-o ControlMaster=auto -o ControlPath='tramp.%%C' -o ControlPersist=no")
  )

(defun dotspacemacs/user-config ()
  (linum-relative-global-mode t)
  (setcdr evil-insert-state-map nil)
  (define-key evil-insert-state-map [escape] 'evil-normal-state)
  (setq ns-use-srgb-colorspace nil)
  (setq powerline-default-separator 'arrow)
  (setq-default dotspacemacs-configuration-layers '((chinese :variables
                                                            chinese-enable-fcitx t)))
  (setq-default dotspacemacs-configuration-layers '((chinese :variables
                                                            chinese-enable-youdao-dict t)))
  (setq org-format-latex-options
        (plist-put org-format-latex-options :scale 2.0));调整 LaTeX 预览图片的大小
  (spacemacs/set-leader-keys "oy" 'youdao-dictionary-search-at-point+)
  ;;中文输入退出到normal模式时,自动变为英文
  (require 'fcitx)
  (setq fcitx-active-evil-states '(insert emacs hybrid)) ; if you use hybrid mode
  (fcitx-aggressive-setup)
  (fcitx-prefix-keys-add "M-m") ; M-m is common in Spacemacs
  (setq fcitx-use-dbus t) ; uncomment if you're using Linux
)

(setq custom-file (expand-file-name "custom.el" dotspacemacs-directory))
(load custom-file 'no-error 'no-message)
1 个赞

org的这个错误,你要查询一下 org-babel 的配置,你的配置使用的是 init-org,这样 spacemacs 本身的 org 配置就失效了,建议使用 post-init-org 来定义你自己的扩展。

我把org的配置放到了 post-init-org里。当我在org文件里执行C++代码时,还是报org-babel-execute-src-block: No org-babel-execute function for C++!的错误。

如果说是 org-babel 的配置问题,那为什么其他语言的代码却是可以执行的呢? 我有点怀疑是spacemacs develop分支的bug

    (org-babel-do-load-languages
       'org-babel-load-languages
       '((perl . t)
         (ruby . t)
         (sh . t)
         (dot . t)
         (js . t)
         (latex .t)
         (python . t)
         (emacs-lisp . t)
         (plantuml . t)
         (C . t)
         (ditaa . t)))

上面是我的 org babel 配置,你需要自己添加 cpp 的支持。 (貌似写上 C 就默认是支持 c/c++了)

这问题挺奇怪的。我在照着视频学习的时候,中间有一段时间 org的C++代码是可以执行的,后来不知道从什么时候开始就不行了,应该还是我加入配置的问题。

另外我刚刚试了一下切到master分支,(把ivy,helm去掉后)发现连python的代码段也出现了和C++同样的错误。是不是在develop分支下hack spacemacs更加方便些?

换成了你的写法就可以了!