0

Is it possible to hook evil-insert-enter-state and other states only within org-mode with the org-toggle-latex-fragment?

p.s.

finally the suggestions work. I currently have the following pieces that make the org mode automatically render the content when you are in the normal mode, but expand the content when you are in other modes.

(defun org-preview-all-latex-fragments ()
  "Toggle all the latex fragments."
     (org-toggle-latex-fragment '(16)))


(add-hook 'org-mode-hook (lambda ()
                           (add-hook 'evil-normal-state-entry-hook 'org-preview-all-latex-fragments nil t)
                           (add-hook 'evil-normal-state-exit-hook 'org-remove-latex-fragment-image-overlays nil t)))
darwinsenior
  • 486
  • 1
  • 5
  • 13
  • I think something like `(when (eq major-mode 'org-mode) ...` in a 'global' `evil-insert-enter-state` hook is the best way to do this. – Gordon Gustafson Jun 26 '16 at 05:44

1 Answers1

1

You could add buffer local hooks inside a org-mode hook:

(add-hook 'org-mode-hook
  (lambda ()
    (add-hook 'evil-insert-state-entry-hook 'org-toggle-latex-fragment nil t)))

The third argument of add-hook is the buffer-local flag.

(add-hook HOOK FUNCTION &optional APPEND LOCAL)

The problem with the above is that org-toggle-latex-fragment works differently depending on where the cursor is. If you mean to activate all latex fragments across the file, you may need to modify my suggestion slighty:

(defun org-toggle-all-latex-fragments ()
  (org-toggle-latex-fragment '(16)))

(add-hook 'org-mode-hook
  (lambda ()
    (add-hook 'evil-insert-state-entry-hook 'org-toggle-all-latex-fragments nil t)))
Henrik
  • 982
  • 1
  • 8
  • 10
  • what does '(16) mean? – darwinsenior Jun 26 '16 at 15:57
  • `org-toggle-latex-fragment`'s first argument is called the universal argument. '(16) forces `org-toggle-latex-fragment` to toggle all latex previews in the current file. – Henrik Jun 26 '16 at 21:31
  • Should also mention: passing '(16) to it is the equivalent of typing `C-u C-u M-x org-toggle-latex-fragment` – Henrik Jun 26 '16 at 21:37
  • I made it work. It is now great. Just one more comment. I am not sure why org-preview-latex-fragment is deprecated. Should it be normal that people want to control explicitly if they want to switch the image overlay on or off? – darwinsenior Jun 29 '16 at 04:21