emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Dan Davison <davison@stats.ox.ac.uk>
To: David O'Toole <dto1138@gmail.com>
Cc: emacs-orgmode Mailinglist <emacs-orgmode@gnu.org>
Subject: Re: [PATCH] Mode-specific fontification of babel source blocks
Date: Sun, 15 Aug 2010 02:33:52 -0400	[thread overview]
Message-ID: <87r5i0crlb.fsf@stats.ox.ac.uk> (raw)
In-Reply-To: <AANLkTimrVKE2wzz+T2fwQuDFLiB_ZNW8OC3X4SNRfn7V@mail.gmail.com> (David O'Toole's message of "Tue, 3 Aug 2010 19:12:42 -0400")

[-- Attachment #1: Type: text/plain, Size: 1706 bytes --]

"David O'Toole" <dto1138@gmail.com> writes:

> I've got a preliminary patch that adds optional "native" fontification
> for source blocks. It uses the block's declared mode to fontify the
> block text. So now blocks look the way they should, and this opens the
> way to further enhancements.

I've tested David's patch, and made some additions. It's really nice to
have fontified code in the Org buffer. It's not that smooth when editing
code in the Org buffer, but there seems to be no easy way round that
(happy to be proved wrong). In the continued absence of an alternative,
I think we should consider adding this. I've prepared a patch which does
the following:

- David's original patch
- Extend regexp to match full Org code src block syntax
- I've moved the fontification code out into a separate function
- I've provided a standalone manual fontification function
  `org-src-fontify-block'.

On my system, I have not managed to eliminate some point movement during
fontification. This occurs because of the heavy-duty method of
fontification (delete the code, re-insert fontified code generated in a
separate buffer). On the other hand, Org users are not really encouraged
to *edit* code in the Org buffer, and the slightly clunky fontification
is only experienced by those who go against that. David's variable
`org-src-fontify-natively' determines whether font-lock automatically
fontifies code blocks. Even if that variable is nil, it is still
possible to call `org-src-fontify-block' for manual fontification.

Buffer movement is especially problematic with org-indent-mode.

Patch against master attached, and also in branch org-src-fontification
at git@github.com:dandavison/org-devel.git.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: src-block-fontification.diff --]
[-- Type: text/x-diff, Size: 3755 bytes --]

diff --git a/lisp/org-src.el b/lisp/org-src.el
index baa2b11..3198439 100644
--- a/lisp/org-src.el
+++ b/lisp/org-src.el
@@ -654,6 +654,43 @@ the language, a switch telling if the content should be in a single line."
 
 (org-add-hook 'org-src-mode-hook 'org-src-mode-configure-edit-buffer)
 
+(defun org-src-font-lock-fontify-block (lang start end)
+  (let* ((lang-mode (org-src-get-lang-mode lang))
+	 (string (buffer-substring-no-properties start end))
+	 (modified (buffer-modified-p))
+	 (fontified-output
+	  (with-temp-buffer
+	    (insert string)
+	    (funcall lang-mode)
+	    (font-lock-fontify-buffer)
+	    (add-text-properties
+	     (point-min) (point-max)
+	     '(font-lock-fontified t fontified t font-lock-multiline t))
+	    (buffer-substring (point-min) (point-max)))))
+    (when fontified-output
+      (goto-char start)
+      (delete-region start end)
+      (insert fontified-output)
+      (set-buffer-modified-p modified)))
+  t) ;; Tell `org-fontify-meta-lines-and-blocks' that we fontified
+
+(defun org-src-fontify-block ()
+  "Fontify code block at point"
+  (interactive)
+  (let ((org-src-fontify-natively t)
+	(info (org-edit-src-find-region-and-lang))
+	(point (point)))
+    (font-lock-fontify-region (nth 0 info) (nth 1 info))
+    (goto-char point)))
+
+(defun org-src-get-lang-mode (lang)
+  "Return major mode that should be used for LANG.
+LANG is a string, and the returned major mode is a symbol."
+  (intern
+   (concat
+    ((lambda (l) (if (symbolp l) (symbol-name l) l))
+     (or (cdr (assoc lang org-src-lang-modes)) lang)) "-mode")))
+
 (provide 'org-src)
 
 ;; arch-tag: 6a1fc84f-dec7-47be-a416-64be56bea5d8
diff --git a/lisp/org.el b/lisp/org.el
index a05f2bf..b381ad0 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -5017,17 +5017,23 @@ will be prompted for."
 				'(display t invisible t intangible t))
 	t)))
 
+(defvar org-src-fontify-natively nil
+  "When non-nil, fontify source blocks like their major mode would.")
+
 (defun org-fontify-meta-lines-and-blocks (limit)
   "Fontify #+ lines and blocks, in the correct ways."
   (let ((case-fold-search t))
     (if (re-search-forward
-	 "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)\\(.*\\)\\)"
+	 "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
 	 limit t)
-	(let ((beg (match-beginning 0))
-	      (beg1 (line-beginning-position 2))
-	      (dc1 (downcase (match-string 2)))
-	      (dc3 (downcase (match-string 3)))
-	      end end1 quoting block-type)
+	(let* ((beg (match-beginning 0))
+	       (block-start (match-end 0))
+	       (block-end nil)
+	       (lang (match-string 7))
+	       (beg1 (line-beginning-position 2))
+	       (dc1 (downcase (match-string 2)))
+	       (dc3 (downcase (match-string 3)))
+	       end end1 quoting block-type)
 	  (cond
 	   ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
 	    ;; a single line of backend-specific content
@@ -5047,6 +5053,7 @@ will be prompted for."
 		   (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
 		   nil t)  ;; on purpose, we look further than LIMIT
 	      (setq end (match-end 0) end1 (1- (match-beginning 0)))
+	      (setq block-end (match-beginning 0))
 	      (when quoting
 		(remove-text-properties beg end
 					'(display t invisible t intangible t)))
@@ -5056,6 +5063,8 @@ will be prompted for."
 	      (add-text-properties beg beg1 '(face org-meta-line))
 	      (add-text-properties end1 end '(face org-meta-line))
 	      (cond
+	       ((and lang org-src-fontify-natively)
+		(org-src-font-lock-fontify-block lang block-start block-end))
 	       (quoting
 		(add-text-properties beg1 end1 '(face org-block)))
 	       ((not org-fontify-quote-and-verse-blocks))

[-- Attachment #3: Type: text/plain, Size: 276 bytes --]


Dan




> Anyone up for an icons theme standard
> discussion?
>
> _______________________________________________
> Emacs-orgmode mailing list
> Please use `Reply All' to send replies to the list.
> Emacs-orgmode@gnu.org
> http://lists.gnu.org/mailman/listinfo/emacs-orgmode

[-- Attachment #4: Type: text/plain, Size: 201 bytes --]

_______________________________________________
Emacs-orgmode mailing list
Please use `Reply All' to send replies to the list.
Emacs-orgmode@gnu.org
http://lists.gnu.org/mailman/listinfo/emacs-orgmode

      parent reply	other threads:[~2010-08-15  6:34 UTC|newest]

Thread overview: 56+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-08-03 23:12 [PATCH] Mode-specific fontification of babel source blocks David O'Toole
2010-08-03 23:14 ` David O'Toole
2010-08-03 23:23   ` Erik Iverson
2010-08-04  2:55 ` Dan Davison
2010-08-04  5:55   ` David O'Toole
2010-08-09 21:35   ` Dan Davison
2010-09-02 15:51     ` Org now fontifies code blocks Dan Davison
2010-09-02 17:04       ` Erik Iverson
2010-09-02 19:06         ` Native TAB in code blocks [WAS Re: Org now fontifies code blocks] Dan Davison
2010-09-02 19:18           ` Carsten Dominik
2010-09-02 20:36             ` Native TAB in code blocks Dan Davison
2010-09-03  8:11               ` Julien Fantin
2010-09-02 20:26       ` Org now fontifies " Sébastien Vauban
2010-09-03 17:30       ` Eric S Fraga
2010-09-03 19:10         ` Thomas S. Dye
2010-09-06 16:49           ` David O'Toole
2010-09-07 13:23           ` Dan Davison
2010-09-07 13:55             ` Richard Riley
2010-09-07 16:05             ` Thomas S. Dye
2010-09-08 16:36               ` Darlan Cavalcante Moreira
2010-09-08 17:41                 ` Dan Davison
2010-09-09 13:02                   ` Darlan Cavalcante Moreira
2010-09-09 21:40                     ` Thomas S. Dye
2010-09-09 22:35                       ` Dan Davison
2010-09-09 23:03                         ` Thomas S. Dye
2010-10-28 11:10                           ` Dan Davison
2010-10-28 12:08                             ` Jules Bean
2010-10-28 12:47                               ` Jambunathan K
2010-10-28 13:35                                 ` Dan Davison
2010-10-28 16:24                             ` Thomas S. Dye
2010-09-06 16:59         ` Richard Riley
2010-09-06 17:53           ` David O'Toole
2010-09-06 18:30             ` Bastien
2010-09-06 18:52               ` David O'Toole
2010-09-06 18:59                 ` Richard Riley
2010-09-07 13:43                 ` Dan Davison
2010-09-07 14:22                   ` Carsten Dominik
2010-09-07 14:33                     ` Bastien
2010-09-07 14:37                       ` Carsten Dominik
2010-09-07 14:41                       ` Sebastian Rose
2010-09-07 16:20                         ` Dan Davison
2010-09-07 15:03                       ` Sébastien Vauban
2010-09-07 16:54                     ` Dan Davison
2010-09-08 16:30                       ` Bastien
2010-09-08 18:35                         ` Sébastien Vauban
2010-09-08 18:42                           ` Erik Iverson
2010-09-08 19:17                             ` Dan Davison
2010-09-07 14:24                   ` Bastien
2010-09-07 14:33                   ` Tom Short
2010-09-07 14:47                     ` Richard Riley
2010-09-06 17:59           ` Erik Iverson
2010-09-06 18:23           ` Dan Davison
2010-09-06 18:49             ` Richard Riley
2010-09-07 13:33               ` Dan Davison
2010-09-07  7:24           ` Sébastien Vauban
2010-08-15  6:33 ` Dan Davison [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87r5i0crlb.fsf@stats.ox.ac.uk \
    --to=davison@stats.ox.ac.uk \
    --cc=dto1138@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).