From mboxrd@z Thu Jan 1 00:00:00 1970 From: Dan Davison Subject: Re: [PATCH] Mode-specific fontification of babel source blocks Date: Mon, 09 Aug 2010 17:35:07 -0400 Message-ID: <87vd7j1nck.fsf@stats.ox.ac.uk> References: <8739uvw0hg.fsf@stats.ox.ac.uk> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Return-path: Received: from [140.186.70.92] (port=32854 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Oia04-00064e-Iy for emacs-orgmode@gnu.org; Mon, 09 Aug 2010 17:35:20 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1Oia00-0006TG-AJ for emacs-orgmode@gnu.org; Mon, 09 Aug 2010 17:35:16 -0400 Received: from markov.stats.ox.ac.uk ([163.1.210.1]:49113) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1Oia00-0006T3-3W for emacs-orgmode@gnu.org; Mon, 09 Aug 2010 17:35:12 -0400 Received: from blackcap.stats.ox.ac.uk (blackcap.stats [163.1.210.5]) by markov.stats.ox.ac.uk (8.13.6/8.13.6) with ESMTP id o79LZ9cn025361 for ; Mon, 9 Aug 2010 22:35:09 +0100 (BST) In-Reply-To: <8739uvw0hg.fsf@stats.ox.ac.uk> (Dan Davison's message of "Tue, 03 Aug 2010 22:55:07 -0400") List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: emacs-orgmode Mailinglist --=-=-= Dan Davison writes: > "David O'Toole" 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. > > Hi David, > > This is great! Here's a patch which allows the src blocks to have > switches and header args, and also uses `org-src-lang-modes' to find the > major mode. I'm resending this as a match against the current master branch, and as an attachment so that it goes into the patchwork system. I am keeping this line of patches in branch `src-block-fontification' at git://github.com/dandavison/org-devel.git Dan --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=src-block-fontification.diff diff --git a/lisp/org.el b/lisp/org.el index af4f793..ef6e769 100644 --- a/lisp/org.el +++ b/lisp/org.el @@ -5017,17 +5017,24 @@ 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]+\\)\\)?\\)\\(.*\\)\\)" + ;; 1 2 3 3 4 5 5 4 2 6 6 7 7 1 + "^\\([ \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) + (language (match-string 6)) + (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 +5054,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 +5064,28 @@ will be prompted for." (add-text-properties beg beg1 '(face org-meta-line)) (add-text-properties end1 end '(face org-meta-line)) (cond + ((and org-src-fontify-natively language) + (let* ((lang-mode + (or (cdr (assoc language org-src-lang-modes)) (intern language))) + (mode-command (intern (concat (symbol-name lang-mode) "-mode"))) + (string (buffer-substring-no-properties block-start block-end)) + (modified (buffer-modified-p)) + (fontified-output + (with-temp-buffer + (insert string) + (message language) + (funcall mode-command) + (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 + (assert (stringp fontified-output)) + (goto-char block-start) + (delete-region block-start block-end) + (insert fontified-output) + (set-buffer-modified-p modified)))) (quoting (add-text-properties beg1 end1 '(face org-block))) ((not org-fontify-quote-and-verse-blocks)) --=-=-= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ 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 --=-=-=--