unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* How to fontify region according to mode
@ 2010-08-14 17:14 Dan Davison
  2010-08-14 17:21 ` Lennart Borgman
  0 siblings, 1 reply; 6+ messages in thread
From: Dan Davison @ 2010-08-14 17:14 UTC (permalink / raw)
  To: help-gnu-emacs

I want to write a function to fontify a region according to a certain
mode, and I tried the following tactic (see function below):

- get the value of `font-lock-defaults' from a buffer in the target mode
  and bind it inside a let
- bind a bunch of other font-lock variable names to stop them being
  overwritten
- call `font-lock-set-defaults'
- call `font-lock-fontify-region'

It didn't seem to work. Could someone suggest how to alter this so that
it works, or is this approach fundamentally flawed?

(defun dan/font-lock-fontify-region (mode start end)
  (let ((font-lock-defaults
	 (with-temp-buffer
	   (funcall mode)
	   font-lock-defaults))
	font-lock-keywords
	font-lock-keywords-only
	font-lock-keywords-case-fold-search
	font-lock-syntax-table
	font-lock-beginning-of-syntax-function)
    (font-lock-set-defaults)
    (font-lock-fontify-region start end)))


I want the rest of the buffer to remain unaltered and, for now, I'm not
looking for a solution involving mumamo/multi-mode etc.

Thanks,

Dan



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: How to fontify region according to mode
  2010-08-14 17:14 How to fontify region according to mode Dan Davison
@ 2010-08-14 17:21 ` Lennart Borgman
  2010-08-14 18:08   ` Dan Davison
  0 siblings, 1 reply; 6+ messages in thread
From: Lennart Borgman @ 2010-08-14 17:21 UTC (permalink / raw)
  To: Dan Davison; +Cc: help-gnu-emacs

On Sat, Aug 14, 2010 at 7:14 PM, Dan Davison <davison@stats.ox.ac.uk> wrote:
> I want to write a function to fontify a region according to a certain
> mode, and I tried the following tactic (see function below):
>
> - get the value of `font-lock-defaults' from a buffer in the target mode
>  and bind it inside a let
> - bind a bunch of other font-lock variable names to stop them being
>  overwritten
> - call `font-lock-set-defaults'
> - call `font-lock-fontify-region'
>
> It didn't seem to work. Could someone suggest how to alter this so that
> it works, or is this approach fundamentally flawed?
>
> (defun dan/font-lock-fontify-region (mode start end)
>  (let ((font-lock-defaults
>         (with-temp-buffer
>           (funcall mode)
>           font-lock-defaults))
>        font-lock-keywords
>        font-lock-keywords-only
>        font-lock-keywords-case-fold-search
>        font-lock-syntax-table
>        font-lock-beginning-of-syntax-function)
>    (font-lock-set-defaults)
>    (font-lock-fontify-region start end)))
>
>
> I want the rest of the buffer to remain unaltered and, for now, I'm not
> looking for a solution involving mumamo/multi-mode etc.


If you want to see how it works then look in mumamo.el. I think you
will find the answers to your questions there. (But why do you want to
reinvent the wheel? What is the purpose?)



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: How to fontify region according to mode
  2010-08-14 17:21 ` Lennart Borgman
@ 2010-08-14 18:08   ` Dan Davison
  2010-08-14 18:35     ` Lennart Borgman
  0 siblings, 1 reply; 6+ messages in thread
From: Dan Davison @ 2010-08-14 18:08 UTC (permalink / raw)
  To: Lennart Borgman; +Cc: help-gnu-emacs

Lennart Borgman <lennart.borgman@gmail.com> writes:

> On Sat, Aug 14, 2010 at 7:14 PM, Dan Davison <davison@stats.ox.ac.uk> wrote:
>> I want to write a function to fontify a region according to a certain
>> mode, and I tried the following tactic (see function below):
>>
>> - get the value of `font-lock-defaults' from a buffer in the target mode
>>  and bind it inside a let
>> - bind a bunch of other font-lock variable names to stop them being
>>  overwritten
>> - call `font-lock-set-defaults'
>> - call `font-lock-fontify-region'
>>
>> It didn't seem to work. Could someone suggest how to alter this so that
>> it works, or is this approach fundamentally flawed?
>>
>> (defun dan/font-lock-fontify-region (mode start end)
>>  (let ((font-lock-defaults
>>         (with-temp-buffer
>>           (funcall mode)
>>           font-lock-defaults))
>>        font-lock-keywords
>>        font-lock-keywords-only
>>        font-lock-keywords-case-fold-search
>>        font-lock-syntax-table
>>        font-lock-beginning-of-syntax-function)
>>    (font-lock-set-defaults)
>>    (font-lock-fontify-region start end)))
>>
>>
>> I want the rest of the buffer to remain unaltered and, for now, I'm not
>> looking for a solution involving mumamo/multi-mode etc.
>
>
> If you want to see how it works then look in mumamo.el. I think you
> will find the answers to your questions there. (But why do you want to
> reinvent the wheel? What is the purpose?)

Hi Lennart,

I was looking at mumamo.el earlier today, but it is 9,101 lines long, so
I would definitely appreciate some guidance. Note that I don't want
different regions to be subject to different major modes; I only want to
change the text properties in a region. That was why I was not using
mumamo and was attempting the simple approach above. I would certainly
consider mumamo if I wanted multiple major modes.

Dan



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: How to fontify region according to mode
  2010-08-14 18:08   ` Dan Davison
@ 2010-08-14 18:35     ` Lennart Borgman
  2010-08-16 13:28       ` Dan Davison
  0 siblings, 1 reply; 6+ messages in thread
From: Lennart Borgman @ 2010-08-14 18:35 UTC (permalink / raw)
  To: Dan Davison; +Cc: help-gnu-emacs

On Sat, Aug 14, 2010 at 8:08 PM, Dan Davison <davison@stats.ox.ac.uk> wrote:
> Lennart Borgman <lennart.borgman@gmail.com> writes:
>
>> On Sat, Aug 14, 2010 at 7:14 PM, Dan Davison <davison@stats.ox.ac.uk> wrote:
>>> I want to write a function to fontify a region according to a certain
>>> mode, and I tried the following tactic (see function below):
>>>
>>> - get the value of `font-lock-defaults' from a buffer in the target mode
>>>  and bind it inside a let
>>> - bind a bunch of other font-lock variable names to stop them being
>>>  overwritten
>>> - call `font-lock-set-defaults'
>>> - call `font-lock-fontify-region'
>>>
>>> It didn't seem to work. Could someone suggest how to alter this so that
>>> it works, or is this approach fundamentally flawed?
>>>
>>> (defun dan/font-lock-fontify-region (mode start end)
>>>  (let ((font-lock-defaults
>>>         (with-temp-buffer
>>>           (funcall mode)
>>>           font-lock-defaults))
>>>        font-lock-keywords
>>>        font-lock-keywords-only
>>>        font-lock-keywords-case-fold-search
>>>        font-lock-syntax-table
>>>        font-lock-beginning-of-syntax-function)
>>>    (font-lock-set-defaults)
>>>    (font-lock-fontify-region start end)))
>>>
>>>
>>> I want the rest of the buffer to remain unaltered and, for now, I'm not
>>> looking for a solution involving mumamo/multi-mode etc.
>>
>>
>> If you want to see how it works then look in mumamo.el. I think you
>> will find the answers to your questions there. (But why do you want to
>> reinvent the wheel? What is the purpose?)
>
> Hi Lennart,
>
> I was looking at mumamo.el earlier today, but it is 9,101 lines long, so
> I would definitely appreciate some guidance. Note that I don't want
> different regions to be subject to different major modes; I only want to
> change the text properties in a region. That was why I was not using
> mumamo and was attempting the simple approach above. I would certainly
> consider mumamo if I wanted multiple major modes.
>
> Dan


Hi Dan,

I am afraid that what you want actually involves the same problem as
using multiple major modes. Why don't you think so?



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: How to fontify region according to mode
  2010-08-14 18:35     ` Lennart Borgman
@ 2010-08-16 13:28       ` Dan Davison
  2010-08-16 14:39         ` Lennart Borgman
  0 siblings, 1 reply; 6+ messages in thread
From: Dan Davison @ 2010-08-16 13:28 UTC (permalink / raw)
  To: Lennart Borgman; +Cc: help-gnu-emacs

Lennart Borgman <lennart.borgman@gmail.com> writes:

> On Sat, Aug 14, 2010 at 8:08 PM, Dan Davison <davison@stats.ox.ac.uk> wrote:
>> Lennart Borgman <lennart.borgman@gmail.com> writes:
>>
>>> On Sat, Aug 14, 2010 at 7:14 PM, Dan Davison <davison@stats.ox.ac.uk> wrote:
>>>> I want to write a function to fontify a region according to a certain
>>>> mode, and I tried the following tactic (see function below):
>>>>
>>>> - get the value of `font-lock-defaults' from a buffer in the target mode
>>>>  and bind it inside a let
>>>> - bind a bunch of other font-lock variable names to stop them being
>>>>  overwritten
>>>> - call `font-lock-set-defaults'
>>>> - call `font-lock-fontify-region'
>>>>
>>>> It didn't seem to work. Could someone suggest how to alter this so that
>>>> it works, or is this approach fundamentally flawed?
>>>>
>>>> (defun dan/font-lock-fontify-region (mode start end)
>>>>  (let ((font-lock-defaults
>>>>         (with-temp-buffer
>>>>           (funcall mode)
>>>>           font-lock-defaults))
>>>>        font-lock-keywords
>>>>        font-lock-keywords-only
>>>>        font-lock-keywords-case-fold-search
>>>>        font-lock-syntax-table
>>>>        font-lock-beginning-of-syntax-function)
>>>>    (font-lock-set-defaults)
>>>>    (font-lock-fontify-region start end)))
>>>>
>>>>
>>>> I want the rest of the buffer to remain unaltered and, for now, I'm not
>>>> looking for a solution involving mumamo/multi-mode etc.
>>>
>>>
>>> If you want to see how it works then look in mumamo.el. I think you
>>> will find the answers to your questions there. (But why do you want to
>>> reinvent the wheel? What is the purpose?)
>>
>> Hi Lennart,
>>
>> I was looking at mumamo.el earlier today, but it is 9,101 lines long, so
>> I would definitely appreciate some guidance. Note that I don't want
>> different regions to be subject to different major modes; I only want to
>> change the text properties in a region. That was why I was not using
>> mumamo and was attempting the simple approach above. I would certainly
>> consider mumamo if I wanted multiple major modes.
>>
>> Dan
>
>
> Hi Dan,
>
> I am afraid that what you want actually involves the same problem as
> using multiple major modes. Why don't you think so?

Hi Lennart,

OK, I see. It looks like I was being naive and thinking it was simpler
than it really is. Seeing as you have worked extensively on this, and as
I have wasted a little bit of time on it, would you mind explaining to
me where my above approach encounters problems?  I.e., inside a let
binding,

1. bind `font-lock-defaults' to the appropriate value
2. bind to nil all the other font-lock related variables that
   `font-lock-set-defaults' will change
3. call `font-lock-set-defaults'
4. call `font-lock-fontify-region'

Thanks,

Dan



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: How to fontify region according to mode
  2010-08-16 13:28       ` Dan Davison
@ 2010-08-16 14:39         ` Lennart Borgman
  0 siblings, 0 replies; 6+ messages in thread
From: Lennart Borgman @ 2010-08-16 14:39 UTC (permalink / raw)
  To: Dan Davison; +Cc: help-gnu-emacs

On Mon, Aug 16, 2010 at 3:28 PM, Dan Davison <davison@stats.ox.ac.uk> wrote:
>>
>> Hi Dan,
>>
>> I am afraid that what you want actually involves the same problem as
>> using multiple major modes. Why don't you think so?
>
> Hi Lennart,
>
> OK, I see. It looks like I was being naive and thinking it was simpler
> than it really is. Seeing as you have worked extensively on this, and as
> I have wasted a little bit of time on it, would you mind explaining to
> me where my above approach encounters problems?  I.e., inside a let
> binding,
>
> 1. bind `font-lock-defaults' to the appropriate value
> 2. bind to nil all the other font-lock related variables that
>   `font-lock-set-defaults' will change
> 3. call `font-lock-set-defaults'
> 4. call `font-lock-fontify-region'


This is essentially the way it is done in mumamo.el, but you have to
do it inside font-lock. Otherwise font-lock will overwrite what you
have done.



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2010-08-16 14:39 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-08-14 17:14 How to fontify region according to mode Dan Davison
2010-08-14 17:21 ` Lennart Borgman
2010-08-14 18:08   ` Dan Davison
2010-08-14 18:35     ` Lennart Borgman
2010-08-16 13:28       ` Dan Davison
2010-08-16 14:39         ` Lennart Borgman

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).