unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* How to add property for a buffer object
@ 2012-09-12 15:30 York Zhao
  2012-09-12 15:58 ` Drew Adams
  0 siblings, 1 reply; 7+ messages in thread
From: York Zhao @ 2012-09-12 15:30 UTC (permalink / raw)
  To: help-gnu-emacs

I like longlines-mode and refill-mode, but unfortunately there are
always times I have to turn them off. Therefor, I binded a key to
toggle the `refill-mode'. But I also want to toggle the
`longlines-mode', however, I want to use only one key to toggle either
`refill-mode', or `longlines-mode'. My thought is to add a property to
current buffer so that the toggle command knows which one to toggle.
The problem is that the `get' and `put' function require a symbol not
a buffer object. So, is there any way to "get" and "put" property on a
buffer? Or, is there any other solution to have one function toggle
either `longlines-mode' or 'refill-mode'? Here is my function (not
working) for your reference.

(defun york/toggle-longlines-or-refill-mode ()
  (interactive)
  (if (null (get (current-buffer) 'longlines-mode-turned-off-p))
      (refill-mode)
    (put (current-buffer) 'longlines-mode-turned-off-p
         (if (eq (get (current-buffer) 'longlines-mode-turned-off-p)
                 'Yes)
             'No
           'Yes))
    (longlines-mode 1)))


Thanks in advance

York



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

* RE: How to add property for a buffer object
  2012-09-12 15:30 How to add property for a buffer object York Zhao
@ 2012-09-12 15:58 ` Drew Adams
  2012-09-12 16:36   ` York Zhao
  0 siblings, 1 reply; 7+ messages in thread
From: Drew Adams @ 2012-09-12 15:58 UTC (permalink / raw)
  To: 'York Zhao', help-gnu-emacs

> is there any way to "get" and "put" property on a buffer?

You can use `get' and `put' on the symbol that is the value of variable
`major-mode'.

Or you can simply make some variable buffer-local for a given mode.

> Or, is there any other solution to have one function toggle
> either `longlines-mode' or 'refill-mode'?

Test something specific to the buffer or its mode (see above).
Or just use a prefix arg (and test that).




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

* Re: How to add property for a buffer object
  2012-09-12 15:58 ` Drew Adams
@ 2012-09-12 16:36   ` York Zhao
  2012-09-12 17:10     ` Drew Adams
  0 siblings, 1 reply; 7+ messages in thread
From: York Zhao @ 2012-09-12 16:36 UTC (permalink / raw)
  To: Drew Adams; +Cc: help-gnu-emacs

Thank you very much Drew for the reply, I have been so impressed by your warm
hearted on this list, and your knowledge on Emacs lisp (of course). And your
trick of turning on `debug-on-quit' had been a life saver for me to figure out
which part of which function was holding up the CPU. Thanks a lot, really.

> You can use `get' and `put' on the symbol that is the value of variable
> `major-mode'.

I don't think this would help because I need to attach something to the buffer,
not all the buffers in the major-mode.

> Or you can simply make some variable buffer-local for a given mode.

Yes, I think this would work, but I then have to create a variable for this,
have no idea why Emacs doesn't allow assigning property to the buffer object
itself, just like assigning property to a symbol.

> Test something specific to the buffer or its mode (see above).
> Or just use a prefix arg (and test that).

I'm sure this would work, but I'm still not happy with having to do it this way.
Maybe the ultimate solution is to fix the `refill-mode' itself so that I can
alway have it turned on (tired of having to hit "M-q" almost all the time). The
major problem with `refill-mode' however, is that it intercepts the
"fill-function" therefor was not able to handle refilling properly in org-mode,
especially, it would mess up org tables. Also, if you have `refill-mode' turned
on, you will never be able to break a paragraph into two by hitting <RET> while
the point is in the middle of the paragraph. On the other hand, the
`auto-fill-mode' never "auto-fill" when inserting text in the middle of a line,
because of this, I have to hit "M-q" all the time. Do you know of any other way
to really handle "auto-fill" property?


Thanks again,
York



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

* RE: How to add property for a buffer object
  2012-09-12 16:36   ` York Zhao
@ 2012-09-12 17:10     ` Drew Adams
  2012-09-12 18:49       ` York Zhao
  0 siblings, 1 reply; 7+ messages in thread
From: Drew Adams @ 2012-09-12 17:10 UTC (permalink / raw)
  To: 'York Zhao'; +Cc: help-gnu-emacs

> > You can use `get' and `put' on the symbol that is the value 
> > of variable `major-mode'.
> 
> I don't think this would help because I need to attach 
> something to the buffer, not all the buffers in the major-mode.

Or you could keep track of which buffers in the mode you care about this way.
IOW, work with a list of buffers instead of setting and getting a property for
each of those buffers.

> > Or you can simply make some variable buffer-local for a given mode.
> 
> Yes, I think this would work, but I then have to create a 
> variable for this, have no idea why Emacs doesn't allow
> assigning property to the buffer object itself, just like
> assigning property to a symbol.

Emacs Lisp has:

* symbol properties for symbols

* text properties for buffer text and strings

* overlay properties for overlays (which apply to buffers, and by extension to a
buffer's window)

* button properties for buttons

That's about it, IIRC.  But usually other Lisp objects that you might want to
assign a property to are somehow associated with one or more of those things, so
you can just assign the property to the associated thing (e.g., to a symbol).

For a buffer, as an alternative to a buffer-local variable or a list of affected
buffers, you could put a property on a symbol whose name is the same as the
buffer name.  Or you could put a text property on a string that is `string=' to
the buffer name.  Or if you can depend on some of the buffer text remaining the
same, you could put a text property on some buffer position (e.g. (point-min)).

> > Test something specific to the buffer or its mode (see above).
> > Or just use a prefix arg (and test that).
> 
> I'm sure this would work, but I'm still not happy with having 
> to do it this way.

> Maybe the ultimate solution is to fix the `refill-mode' itself
> so that I can alway have it turned on (tired of having to hit "M-q" almost 
> all the time).

I don't use `refill-mode', but the doc string says it is a (buffer-local) minor
mode.  (I guess there is no globalized equivalent, but you could define one if
you needed it.)

> The major problem with `refill-mode' however, is that it intercepts
> the "fill-function" therefor was not able to handle refilling 
> properly in org-mode, especially, it would mess up org tables.
> Also, if you have `refill-mode' turned on, you will never be able
> to break a paragraph into two by hitting <RET> while
> the point is in the middle of the paragraph.

Sounds like there should be a bug report or enhancement request in there
somewhere. ;-)  `M-x report-emacs-bug'.

> On the other hand, the `auto-fill-mode' never "auto-fill" when
> inserting text in the middle of a line, because of this, I have
> to hit "M-q" all the time. Do you know of any other way
> to really handle "auto-fill" property?

No, I'm probably not the right one to ask.  But hopefully someone else will have
a suggestion.  I use `M-q' and `C-M-q' (e.g. in Lisp code).

There are also other fill commands, which operate on multiple paragraphs in the
region: `fill-region', `fill-individual-paragraphs', and
`fill-nonuniform-paragraphs'.  When one of them does what you want, you might be
able to use it in place of several uses of `M-q'.




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

* Re: How to add property for a buffer object
  2012-09-12 17:10     ` Drew Adams
@ 2012-09-12 18:49       ` York Zhao
  2012-09-12 20:55         ` Drew Adams
  0 siblings, 1 reply; 7+ messages in thread
From: York Zhao @ 2012-09-12 18:49 UTC (permalink / raw)
  To: Drew Adams; +Cc: help-gnu-emacs

>> > You can use `get' and `put' on the symbol that is the value
>> > of variable `major-mode'.
>>
>> I don't think this would help because I need to attach
>> something to the buffer, not all the buffers in the major-mode.
>
> Or you could keep track of which buffers in the mode you care about this way.
> IOW, work with a list of buffers instead of setting and getting a property for
> each of those buffers.

Yes I could do this, but this will be a lot more work than if we could set
property directly to buffer object.

> That's about it, IIRC.  But usually other Lisp objects that you might want to
> assign a property to are somehow associated with one or more of those things, so
> you can just assign the property to the associated thing (e.g., to a symbol).

> For a buffer, as an alternative to a buffer-local variable or a list of affected
> buffers, you could put a property on a symbol whose name is the same as the
> buffer name.  Or you could put a text property on a string that is `string=' to
> the buffer name.

But I think you will have to make a symbol for the buffer, and delete the symbol
when the buffer gets killed, right? Again, a lot more work.

> Or if you can depend on some of the buffer text remaining the
> same, you could put a text property on some buffer position (e.g. (point-min)).

I think this is feasible.

>> The major problem with `refill-mode' however, is that it intercepts
>> the "fill-function" therefor was not able to handle refilling
>> properly in org-mode, especially, it would mess up org tables.
>> Also, if you have `refill-mode' turned on, you will never be able
>> to break a paragraph into two by hitting <RET> while
>> the point is in the middle of the paragraph.
>
> Sounds like there should be a bug report or enhancement request in there
> somewhere. ;-)  `M-x report-emacs-bug'.

I will probably send the bug, but I some times sort of lack the motivation to
report bug to a large project, as I'm not sure how long their "TODO" list is,
and I'm afraid that once it gets into somebody's "TODO" list, chances are that
it will never be touched :-).

>> On the other hand, the `auto-fill-mode' never "auto-fill" when
>> inserting text in the middle of a line, because of this, I have
>> to hit "M-q" all the time. Do you know of any other way
>> to really handle "auto-fill" property?
>
> No, I'm probably not the right one to ask.  But hopefully someone else will have
> a suggestion.  I use `M-q' and `C-M-q' (e.g. in Lisp code).
>
> There are also other fill commands, which operate on multiple paragraphs in the
> region: `fill-region', `fill-individual-paragraphs', and
> `fill-nonuniform-paragraphs'.  When one of them does what you want, you might be
> able to use it in place of several uses of `M-q'.

I think you missed my point here. I don't have problem with "M-q", it actually
works perfectly. But my problem is that I have to hit "M-q" all the time. What
I'm looking for is something like "refill-mode" so that I don't have to always
hit "M-q" while inserting or deleting text in the middle of a line. My fingers
hurt a lot so I always try to hit as less keys as possible.


Thanks,
York



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

* RE: How to add property for a buffer object
  2012-09-12 18:49       ` York Zhao
@ 2012-09-12 20:55         ` Drew Adams
  2012-09-13  1:43           ` York Zhao
  0 siblings, 1 reply; 7+ messages in thread
From: Drew Adams @ 2012-09-12 20:55 UTC (permalink / raw)
  To: 'York Zhao'; +Cc: help-gnu-emacs

> I will probably send the bug, but I some times sort of lack 
> the motivation to report bug to a large project, as I'm not
> sure how long their "TODO" list is, and I'm afraid that once
> it gets into somebody's "TODO" list, chances are that
> it will never be touched :-).

If you never report a bug or enhancement request then it is probably even less
likely that it will be taken care of.

> > There are also other fill commands, which operate on 
> > multiple paragraphs in the region: `fill-region',
> > `fill-individual-paragraphs', and `fill-nonuniform-paragraphs'.
> > When one of them does what you want, you might be
> > able to use it in place of several uses of `M-q'.
> 
> I think you missed my point here. I don't have problem with 
> "M-q", it actually works perfectly. But my problem is that I
> have to hit "M-q" all the time.

I was suggesting that you might be able to just select several paragraphs and
use one of the commands I mentioned, instead of hitting `M-q' for each
paragraph.


> What I'm looking for is something like "refill-mode" so that I 
> don't have to always hit "M-q" while inserting or deleting text
> in the middle of a line. My fingers hurt a lot so I always try
> to hit as less keys as possible.

Why bother to fill immediately?  Why not select all your paragraphs when you're
done editing or at a stopping point, and then use one of the region filling
commands?  That was my suggestion, anyway, FWIW.




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

* Re: How to add property for a buffer object
  2012-09-12 20:55         ` Drew Adams
@ 2012-09-13  1:43           ` York Zhao
  0 siblings, 0 replies; 7+ messages in thread
From: York Zhao @ 2012-09-13  1:43 UTC (permalink / raw)
  To: Drew Adams; +Cc: help-gnu-emacs

> I was suggesting that you might be able to just select several paragraphs and
> use one of the commands I mentioned, instead of hitting `M-q' for each
> paragraph.

> > What I'm looking for is something like "refill-mode" so that I > don't have
>to always hit "M-q" while inserting or deleting text > in the middle of a line.
>My fingers hurt a lot so I always try > to hit as less keys as possible. Why
>bother to fill immediately? Why not select all your paragraphs when you're done
>editing or at a stopping point, and then use one of the region filling
>commands? That was my suggestion, anyway, FWIW.

Looks at that I actually missed you point :), but on the other hand, you
actually don't have to use any other filling command to fill the selection, at
least in Emacs 24.2, you could use "M-q" (`fill-paragraph') to fill the region
because `fill-paragraph' now checks whether there is an active region,
if so, it will fill the region instead of filling the paragraph. But my problem
is not here, it is that if I insert enough text in the middle of a line, the
current line will be broken into two lines, but unfortunately, not two physical
lines, it just becomes two visual lines, I mean, it is still one line, and
since I'm using Vim key bindings (the wonderful Evil package), if I hit "j" I
will be moving to next physical line not visual line, and I have to hit "gj" if
I want to move to next visual line which is a pain. Maybe I really need to
configure my "Evil" to move visual lines when "j" and "k" are pressed.

Thanks,
York



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

end of thread, other threads:[~2012-09-13  1:43 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-09-12 15:30 How to add property for a buffer object York Zhao
2012-09-12 15:58 ` Drew Adams
2012-09-12 16:36   ` York Zhao
2012-09-12 17:10     ` Drew Adams
2012-09-12 18:49       ` York Zhao
2012-09-12 20:55         ` Drew Adams
2012-09-13  1:43           ` York Zhao

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