[Tip] 在normal state下自动关闭org-fragtog,org-appear

因为normal state一般用于看而不是编辑,不需要展开latex preview,org link等等。

(add-hook 'evil-insert-state-entry-hook
              (lambda ()
                (org-fragtog-mode 1)) nil t)
(add-hook 'evil-normal-state-entry-hook
              (lambda ()
                (org-fragtog-mode -1)
                (when (org-fragtog--cursor-frag)
                  (org-fragtog--enable-frag (org-fragtog--cursor-frag)))) nil t)

另外org-appear自带对evil-mode的支持:

(setq org-appear-trigger 'manual)
(add-hook 'evil-insert-state-entry-hook #'org-appear-manual-start nil t)
(add-hook 'evil-insert-state-exit-hook #'org-appear-manual-stop nil t)
1 个赞

自己之前类似的代码写的太烂了很难用。感谢分享。

这岂不是 org 的 hook 函数搞的全局切状态的时候都调用?

1 个赞

我好像没看懂你说的是啥意思 :joy:,你是说这样可能让不同buffer之间相互影响?
我这里的add-hook都是buffer-local的,事实上我是把这些代码写成一个buffer-local的minor-mode来用的,每个buffer之间不会相互影响

(defun org-expand-preview-enable ()
  "Internal function to enable `org-expand-preview-mode'"
  (when (eq major-mode #'org-mode)
    (when evil-insert-state-minor-mode
      (org-fragtog-mode 1))
    (org-appear-mode 1)
    (add-hook 'evil-insert-state-entry-hook #'org-appear-manual-start nil t)
    (add-hook 'evil-insert-state-exit-hook #'org-appear-manual-stop nil t)
    (add-hook 'evil-insert-state-entry-hook
              (lambda ()
                (org-fragtog-mode 1)) nil t)
    (add-hook 'evil-normal-state-entry-hook
              (lambda ()
                (org-fragtog-mode -1)
                (when (org-fragtog--cursor-frag)
                  (org-fragtog--enable-frag (org-fragtog--cursor-frag)))) nil t)))

(defun org-expand-preview-disable ()
  "Internal function to disable `org-expand-preview-mode'"
  (when org-fragtog-mode
    (org-fragtog-mode -1))
  (when org-appear-mode
    (org-appear-mode -1))
  (remove-hook 'evil-insert-state-entry-hook #'org-appear-manual-start t)
  (remove-hook 'evil-insert-state-exit-hook #'org-appear-manual-stop t)
  (remove-hook 'evil-insert-state-entry-hook
            (lambda ()
              (org-fragtog-mode 1)))
  (remove-hook 'evil-normal-state-entry-hook
            (lambda ()
              (org-fragtog-mode -1)
              (when (org-fragtog--cursor-frag)
                (org-fragtog--enable-frag (org-fragtog--cursor-frag))))))

(define-minor-mode org-expand-preview-mode "Expand the previewable item"
  :lighter " org-ep"
  :init-value nil
  :global nil
  (if org-expand-preview-mode
	  (org-expand-preview-enable)
	(org-expand-preview-disable)))

(add-hook 'org-mode-hook #'org-expand-preview-mode)
2 个赞

嗯,这样是没有问题的。