From mboxrd@z Thu Jan 1 00:00:00 1970 From: Dan Davison Subject: Re: [PATCH] Mode-specific fontification of babel source blocks Date: Sun, 15 Aug 2010 02:33:52 -0400 Message-ID: <87r5i0crlb.fsf@stats.ox.ac.uk> References: Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Return-path: Received: from [140.186.70.92] (port=43965 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OkWn8-0002og-OB for emacs-orgmode@gnu.org; Sun, 15 Aug 2010 02:34:00 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1OkWn7-0000JE-1n for emacs-orgmode@gnu.org; Sun, 15 Aug 2010 02:33:58 -0400 Received: from markov.stats.ox.ac.uk ([163.1.210.1]:65170) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1OkWn6-0000J3-RM for emacs-orgmode@gnu.org; Sun, 15 Aug 2010 02:33:57 -0400 In-Reply-To: (David O'Toole's message of "Tue, 3 Aug 2010 19:12:42 -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: David O'Toole Cc: emacs-orgmode Mailinglist --=-=-= "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. 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. --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=src-block-fontification.diff 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)) --=-=-= 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 --=-=-= 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 --=-=-=--