Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No syntax highlight in tmux source block #16

Open
randomizedthinking opened this issue Jul 22, 2022 · 3 comments
Open

No syntax highlight in tmux source block #16

randomizedthinking opened this issue Jul 22, 2022 · 3 comments

Comments

@randomizedthinking
Copy link

ob-tmux is great but it does not highlight the code in the source block. Can we add a :src variable to specify the source type, then tmux can use ob-src to highlight the code in the block? Not sure how hard it is to implement it.

@randomizedthinking randomizedthinking changed the title No syntax in tmux source block No syntax highlight in tmux source block Jul 22, 2022
@Simon-chevolleau
Copy link

That would be a great feature !
Did you find any workaround to have obtain syntax highlighting ?

@TimotheeMathieu
Copy link

I was also interested in this feature and I managed to do it after some tests.
Here is the code heavily copy-pasted from org.el, it can most definitely be improved but for now it works for me.

(defun t/org-tmux-fonts (limit)
    (interactive)
    (save-excursion
      (let ((case-fold-search t))
        (goto-char (point-min))
        (while (re-search-forward
	        (rx bol (group (zero-or-more (any " \t")) "#"
			       (group (group (or (seq "+" (one-or-more (any "a-zA-Z")) (optional ":"))
					         (any " \t")
					         eol))
				      (optional (group "_" (group (one-or-more (any "a-zA-Z"))))))
			       (zero-or-more (any " \t"))
			       (group (group (zero-or-more (not (any " \t\n"))))
				      (zero-or-more (any " \t"))
				      (group (zero-or-more any)))))
	        limit t)
          (let ((beg (match-beginning 0))
	        (end-of-beginline (match-end 0))
	        ;; Including \n at end of #+begin line will include \n
	        ;; after the end of block content.
	        (block-start (match-end 0))
	        (block-end nil)
	        (lang-ob (match-string 7)) ; The language, if it is a source block.
	        (dc3 (downcase (match-string 3)))
                quoting block-type)
	    (cond
	     ((and (match-end 4) (equal dc3 "+begin") (equal lang-ob "tmux"))
	      ;; Truly a block
	      (setq block-type (downcase (match-string 5))
		    ;; Src, example, export, maybe more.
		    quoting (member block-type org-protecting-blocks))
	      (when (re-search-forward
		     (rx-to-string `(group bol (or (seq (one-or-more "*") space)
					           (seq (zero-or-more (any " \t"))
						        "#+end"
						        ,(match-string 4)
						        word-end
						        (zero-or-more any)))))
		     ;; We look further than LIMIT on purpose.
		     nil t)
	        (setq block-end (match-beginning 0)) ; Include the final newline.
                (cond
	         ((and org-src-fontify-natively
                       (string= block-type "src"))
                  (save-excursion 
                    (goto-char block-start)
                    (let* ((element (org-element-at-point))
                           (block-info
                            ;; (org-with-point-at (org-element-property :begin src-block)
                            (org-babel-get-src-block-info)) ;;)
                           (my-lang (assoc-default :lang (elt block-info 2))))
                      (org-src-font-lock-fontify-block my-lang (+ 1 block-start) block-end)))))))))))))

  (advice-add 'org-fontify-meta-lines-and-blocks :after #'t/org-tmux-fonts ) ;; add fontification to org src blocks fontification

The usage is to add a parameter :lang in the header of the source block. Result in image:
image

@TimotheeMathieu
Copy link

Update: I found that an even better solution is to use poly-mode.
By making a polymode for ob-tmux I was able to get, in addition to proper syntax highlighting, also the right indentation, lsp-mode, snippets and all sorts of language dependent keybindings. My primary usage here is shell and python.

The code for this is in a fork of ob-tmux I did here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants