renzmann / treesit-auto

Automatic installation, usage, and fallback for tree-sitter major modes in Emacs 29
GNU General Public License v3.0
369 stars 28 forks source link

org-edit-src-code doesn't respect major-mode-remap-alist #32

Open renzmann opened 1 year ago

renzmann commented 1 year ago

Currently unsure whether this is an Emacs thing or a treesit-auto thing.

Issue: create a #+begin_src python block in org. Use C-c ' to edit the contents. python-mode is activated instead of python-ts-mode.

dragove commented 1 year ago

I'm curious if I can use #+begin_src python or jupyter-python and use the syntax highlighting just like #+begin_src python-ts

dragove commented 1 year ago

Hi, I found I can just add a mapping to org-src-lang-modes e.g.

(add-to-list 'org-src-lang-modes '("python" . python-ts))

and highlighting in code blocks and major mode of C -c ' all gets working

renzmann commented 1 year ago

That's great! I wonder if treesit-auto should be responsible for this too, or leave it as a documentation thing for when people are configuring their Org setup.

ed9w2in6 commented 1 year ago

I think treesit-auto should handle this as well, from a quick glance of treesit-auto.el this does not seems too difficult.
Using org-mode-hook similar mechanism via advice-add can be added to global-treesit-auto-mode to modify org-src-lang-modes.

The org-src-lang-modes list can be build with the current treesit-auto-recipe-list, for each symbol in remap. Something like this:

(mapcar
 (lambda (symbol) (substring (symbol-name symbol) 0 -5))
 '(a-mode b-c-mode d+12djo2i1-mode))
;; ("a" "b-c" "d+12djo2i1")

And for now at least adding a line in README or documentation so that one can get to this issue quicker as sadly I had spend hours on this T^T.


Come to think of it this must be documented as we cannot handle all possible cases as a packages modifies org-edit-src-code directly usually just adds the non-ts modes statically, e.g.:

maybe treesit should have never been implemented in such a way?

A7R7 commented 9 months ago

if not touching org-src-lang-modes, here is an easy hack to make org-edit-src-code respect major-mode-remap-alist. This way org-edit-src-code first searches for a mode in org-src-lang-modes then remap it using major-mode-remap-alist. Can solve the problems that packages append something inside org-src-lang-modes

(defun my/remap-mode (mode)
  (treesit-auto--set-major-remap)
  (alist-get mode major-mode-remap-alist mode)
  )
(advice-add 'org-src-get-lang-mode :filter-return #'my/remap-mode)