ob-tmux icon indicating copy to clipboard operation
ob-tmux copied to clipboard

No syntax highlight in tmux source block

Open randomizedthinking opened this issue 3 years ago • 3 comments

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 avatar Jul 22 '22 20:07 randomizedthinking

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

Simon-chevolleau avatar Jul 24 '23 13:07 Simon-chevolleau

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 avatar Oct 20 '23 16:10 TimotheeMathieu

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.

TimotheeMathieu avatar Dec 16 '23 15:12 TimotheeMathieu