* Condider adding buffer-mode
@ 2013-10-24 14:22 Bozhidar Batsov
2013-10-24 21:46 ` Stefan Monnier
0 siblings, 1 reply; 9+ messages in thread
From: Bozhidar Batsov @ 2013-10-24 14:22 UTC (permalink / raw)
To: emacs-devel
[-- Attachment #1: Type: text/plain, Size: 431 bytes --]
I’ve seen the following code used hundreds of times:
(buffer-local-value 'major-mode buffer)
or
(with-current-buffer buffer ‘major-mode)
Why don’t we add the following function to Emacs and simplify a bit the lives of Elisp hackers:
(defun buffer-mode (buffer) "Return the major mode associated with BUFFER." (buffer-local-value ‘major-mode buffer))
Trivial, but useful.
--
Cheers,
Bozhidar
[-- Attachment #2: Type: text/html, Size: 3204 bytes --]
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Condider adding buffer-mode
2013-10-24 14:22 Condider adding buffer-mode Bozhidar Batsov
@ 2013-10-24 21:46 ` Stefan Monnier
2013-10-29 15:53 ` Bozhidar Batsov
0 siblings, 1 reply; 9+ messages in thread
From: Stefan Monnier @ 2013-10-24 21:46 UTC (permalink / raw)
To: Bozhidar Batsov; +Cc: emacs-devel
> I’ve seen the following code used hundreds of times:
> (buffer-local-value 'major-mode buffer)
> or
> (with-current-buffer buffer ‘major-mode)
Where?
> Why don’t we add the following function to Emacs and simplify a bit
> the lives of Elisp hackers:
> (defun buffer-mode (buffer) "Return the major mode associated with BUFFER."
> (buffer-local-value ‘major-mode buffer))
Most uses of `major-mode' are better replaced with derived-mode-p, so
I don't think we just want to provide `buffer-mode'.
Stefan
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Condider adding buffer-mode
2013-10-24 21:46 ` Stefan Monnier
@ 2013-10-29 15:53 ` Bozhidar Batsov
2013-10-29 16:36 ` Stefan Monnier
0 siblings, 1 reply; 9+ messages in thread
From: Bozhidar Batsov @ 2013-10-29 15:53 UTC (permalink / raw)
To: Stefan Monnier; +Cc: emacs-devel
[-- Attachment #1: Type: text/plain, Size: 1465 bytes --]
On 25 October 2013 00:46, Stefan Monnier <monnier@iro.umontreal.ca> wrote:
> > I’ve seen the following code used hundreds of times:
> > (buffer-local-value 'major-mode buffer)
> > or
> > (with-current-buffer buffer ‘major-mode)
>
> Where?
>
>
One example would be packages like SLIME and CIDER which feature commands
that operate in a different manner when invoked in a REPL or a lisp buffer.
Obviously one could have written different commands for the different
modes, but that doesn't change the fact that people write code like this.
Another example would be selecting all buffers that have some major mode -
maybe you want to enable something in all active elisp buffers, etc.
> > Why don’t we add the following function to Emacs and simplify a bit
> > the lives of Elisp hackers:
>
> > (defun buffer-mode (buffer) "Return the major mode associated with
> BUFFER."
> > (buffer-local-value ‘major-mode buffer))
>
> Most uses of `major-mode' are better replaced with derived-mode-p, so
> I don't think we just want to provide `buffer-mode'.
>
derived-mode-p works well for the current buffer, but it's not particularly
useful if you want to do a mode check on other buffers as you'll still have
to use `with-current-buffer'. I don't see how having something like
`buffer-mode' would be a bad thing for Emacs hackers.
>
>
> Stefan
>
--
Best Regards,
Bozhidar Batsov
http://www.batsov.com
[-- Attachment #2: Type: text/html, Size: 2529 bytes --]
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Condider adding buffer-mode
2013-10-29 15:53 ` Bozhidar Batsov
@ 2013-10-29 16:36 ` Stefan Monnier
2013-10-29 17:28 ` Bozhidar Batsov
0 siblings, 1 reply; 9+ messages in thread
From: Stefan Monnier @ 2013-10-29 16:36 UTC (permalink / raw)
To: Bozhidar Batsov; +Cc: emacs-devel
>> > I’ve seen the following code used hundreds of times:
>> > (buffer-local-value 'major-mode buffer)
>> > or
>> > (with-current-buffer buffer ‘major-mode)
>> Where?
> One example would be packages like SLIME and CIDER which feature commands
> that operate in a different manner when invoked in a REPL or a lisp buffer.
> Obviously one could have written different commands for the different
> modes, but that doesn't change the fact that people write code like this.
This sounds like they check `major-mode', but in the current buffer, so
they wouldn't need something like `buffer-mode'.
> Another example would be selecting all buffers that have some major mode -
> maybe you want to enable something in all active elisp buffers, etc.
Can you point out actual code?
I'm not saying it doesn't exist, I'm just failing to understand why
`buffer-mode' would be useful for those cases.
E.g. using buffer-mode, I guess I'd write a loop like:
(dolist (buf (cl-remove-if-not (lambda (x) (eq 'foo-mode (buffer-mode x)))
(buffer-list)))
(with-current-buffer buf
<do-something))
or maybe
(dolist (buf (buffer-list))
(when (eq 'foo-mode (buffer-mode buf))
(with-current-buffer buf
<do-something)))
neither of which seems any better than:
(dolist (buf (buffer-list))
(with-current-buffer buf
(when (derived-mode-p 'foo-mode)
<do-something)))
> I don't see how having something like `buffer-mode' would be a bad
> thing for Emacs hackers.
I'm not saying it's bad, but I'd like to see compelling use cases first.
BTW, when I said:
> I don't think we just want to provide `buffer-mode'.
I really meant "just buffer-mode", as opposed to "something like
buffer-mode".
Stefan
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Condider adding buffer-mode
2013-10-29 16:36 ` Stefan Monnier
@ 2013-10-29 17:28 ` Bozhidar Batsov
2013-10-29 21:56 ` Stefan Monnier
0 siblings, 1 reply; 9+ messages in thread
From: Bozhidar Batsov @ 2013-10-29 17:28 UTC (permalink / raw)
To: Stefan Monnier; +Cc: emacs-devel
[-- Attachment #1: Type: text/plain, Size: 3637 bytes --]
Here's some real examples:
(defun cider-util--clojure-buffers ()
"Return a list of all existing `clojure-mode' buffers."
(-filter
(lambda (buffer) (eq 'clojure-mode (buffer-local-value 'major-mode
buffer)))
(buffer-list)))
vs
(defun cider-util--clojure-buffers ()
"Return a list of all existing `clojure-mode' buffers."
(-filter
(lambda (buffer) (eq 'clojure-mode (buffer-mode buffer)))
(buffer-list)))
===
(defun cider-remember-clojure-buffer (buffer)
"Try to remember the BUFFER from which the user jumps.
The BUFFER needs to be a Clojure buffer and current major mode needs
to be `cider-repl-mode'. The user can use
`cider-switch-to-last-clojure-buffer'
to jump back to the last Clojure source buffer."
(when (and buffer
(eq 'clojure-mode (with-current-buffer buffer major-mode))
(eq 'cider-repl-mode major-mode))
(setq cider-last-clojure-buffer buffer)))
vs
(defun cider-remember-clojure-buffer (buffer)
"Try to remember the BUFFER from which the user jumps.
The BUFFER needs to be a Clojure buffer and current major mode needs
to be `cider-repl-mode'. The user can use
`cider-switch-to-last-clojure-buffer'
to jump back to the last Clojure source buffer."
(when (and buffer
(eq 'clojure-mode (buffer-mode buffer))
(eq 'cider-repl-mode major-mode))
(setq cider-last-clojure-buffer buffer)))
Seems to me `buffer-mode' makes the code clearer, but that's subjective (as
most things in life :-) )
On 29 October 2013 18:36, Stefan Monnier <monnier@iro.umontreal.ca> wrote:
> >> > I’ve seen the following code used hundreds of times:
> >> > (buffer-local-value 'major-mode buffer)
> >> > or
> >> > (with-current-buffer buffer ‘major-mode)
> >> Where?
> > One example would be packages like SLIME and CIDER which feature commands
> > that operate in a different manner when invoked in a REPL or a lisp
> buffer.
> > Obviously one could have written different commands for the different
> > modes, but that doesn't change the fact that people write code like this.
>
> This sounds like they check `major-mode', but in the current buffer, so
> they wouldn't need something like `buffer-mode'.
>
> > Another example would be selecting all buffers that have some major mode
> -
> > maybe you want to enable something in all active elisp buffers, etc.
>
> Can you point out actual code?
> I'm not saying it doesn't exist, I'm just failing to understand why
> `buffer-mode' would be useful for those cases.
>
> E.g. using buffer-mode, I guess I'd write a loop like:
>
> (dolist (buf (cl-remove-if-not (lambda (x) (eq 'foo-mode (buffer-mode
> x)))
> (buffer-list)))
> (with-current-buffer buf
> <do-something))
>
> or maybe
>
> (dolist (buf (buffer-list))
> (when (eq 'foo-mode (buffer-mode buf))
> (with-current-buffer buf
> <do-something)))
>
> neither of which seems any better than:
>
> (dolist (buf (buffer-list))
> (with-current-buffer buf
> (when (derived-mode-p 'foo-mode)
> <do-something)))
>
> > I don't see how having something like `buffer-mode' would be a bad
> > thing for Emacs hackers.
>
> I'm not saying it's bad, but I'd like to see compelling use cases first.
>
> BTW, when I said:
> > I don't think we just want to provide `buffer-mode'.
>
> I really meant "just buffer-mode", as opposed to "something like
> buffer-mode".
>
>
> Stefan
>
--
Best Regards,
Bozhidar Batsov
http://www.batsov.com
[-- Attachment #2: Type: text/html, Size: 5288 bytes --]
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Condider adding buffer-mode
2013-10-29 17:28 ` Bozhidar Batsov
@ 2013-10-29 21:56 ` Stefan Monnier
2013-10-30 9:34 ` Bozhidar Batsov
0 siblings, 1 reply; 9+ messages in thread
From: Stefan Monnier @ 2013-10-29 21:56 UTC (permalink / raw)
To: Bozhidar Batsov; +Cc: emacs-devel
> Here's some real examples:
> (defun cider-util--clojure-buffers ()
Both uses of this are in something of the form
(dolist (buffer (cider-util--clojure-buffers))
(with-current-buffer buffer
so overall, it's exactly the example I provided. I.e. you can just
throw away cider-util--clojure-buffers and replace the two uses with
(dolist (buffer (buffer-list))
(with-current-buffer buffer
(when (derived-mode-p 'closure-mode)
which also fixes the bug in that code when the user uses a mode derived
from closure-mode.
> (when (and buffer
> (eq 'clojure-mode (with-current-buffer buffer major-mode))
> (eq 'cider-repl-mode major-mode))
> (setq cider-last-clojure-buffer buffer)))
> vs
> (when (and buffer
> (eq 'clojure-mode (buffer-mode buffer))
> (eq 'cider-repl-mode major-mode))
> (setq cider-last-clojure-buffer buffer)))
vs
(when (and buffer
(with-current-buffer buffer (derived-mode-p 'clojure-mode))
(derived-mode-p 'cider-repl-mode))
(setq cider-last-clojure-buffer buffer)))
> Seems to me `buffer-mode' makes the code clearer, but that's subjective (as
> most things in life :-) )
Very marginally so.
Stefan
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Condider adding buffer-mode
2013-10-29 21:56 ` Stefan Monnier
@ 2013-10-30 9:34 ` Bozhidar Batsov
2013-10-30 12:41 ` Stefan Monnier
0 siblings, 1 reply; 9+ messages in thread
From: Bozhidar Batsov @ 2013-10-30 9:34 UTC (permalink / raw)
To: Stefan Monnier; +Cc: emacs-devel
[-- Attachment #1: Type: text/plain, Size: 2681 bytes --]
On 29 October 2013 23:56, Stefan Monnier <monnier@iro.umontreal.ca> wrote:
> > Here's some real examples:
> > (defun cider-util--clojure-buffers ()
>
> Both uses of this are in something of the form
>
> (dolist (buffer (cider-util--clojure-buffers))
> (with-current-buffer buffer
>
> so overall, it's exactly the example I provided. I.e. you can just
> throw away cider-util--clojure-buffers and replace the two uses with
>
> (dolist (buffer (buffer-list))
> (with-current-buffer buffer
> (when (derived-mode-p 'closure-mode)
>
> which also fixes the bug in that code when the user uses a mode derived
> from closure-mode.
>
My point here is mostly that the excessive use of `with-current-buffer'
violates common good programming practices - we're constantly using
functions (and variables) that depend on the current state (namely the
current buffer). While this is OK for interactive code, it seems pretty
obnoxious when done in library code. We can very well have a version of
`derived-mode-p' that operates on an explicit buffer argument as opposed to
an implicit one. I know that Emacs Lisp places little value on functional
programming principles, but I don't see the harm in embracing them at least
occasionally.
>
> > (when (and buffer
> > (eq 'clojure-mode (with-current-buffer buffer major-mode))
> > (eq 'cider-repl-mode major-mode))
> > (setq cider-last-clojure-buffer buffer)))
> > vs
> > (when (and buffer
> > (eq 'clojure-mode (buffer-mode buffer))
> > (eq 'cider-repl-mode major-mode))
> > (setq cider-last-clojure-buffer buffer)))
> vs
> (when (and buffer
> (with-current-buffer buffer (derived-mode-p 'clojure-mode))
> (derived-mode-p 'cider-repl-mode))
> (setq cider-last-clojure-buffer buffer)))
>
> > Seems to me `buffer-mode' makes the code clearer, but that's subjective
> (as
> > most things in life :-) )
>
> Very marginally so.
>
Yeah, I know `derived-mode-p' should have been used, but again - that's not
the point. It's not that I want to see `buffer-mode' that much - it's
rather that I want to see Emacs Lisp APIs that are friendlier to library
authors and functional techniques. Obviously we can write everything we
need with the current tooling, but we should always strive for improvement
IMHO. The problems are amplified by the lack of namespacing in Emacs Lisp.
If this gets fixed someday people would be much less reliant on the
"standard Emacs library", since they'd be able to extend it in a clean way.
Anyways, I got a bit carried away.
> Stefan
>
--
Best Regards,
Bozhidar Batsov
http://www.batsov.com
[-- Attachment #2: Type: text/html, Size: 3980 bytes --]
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Condider adding buffer-mode
2013-10-30 9:34 ` Bozhidar Batsov
@ 2013-10-30 12:41 ` Stefan Monnier
2013-10-31 12:57 ` Bozhidar Batsov
0 siblings, 1 reply; 9+ messages in thread
From: Stefan Monnier @ 2013-10-30 12:41 UTC (permalink / raw)
To: Bozhidar Batsov; +Cc: emacs-devel
> current buffer). While this is OK for interactive code, it seems pretty
> obnoxious when done in library code. We can very well have a version of
> `derived-mode-p' that operates on an explicit buffer argument as opposed to
> an implicit one.
But this applies to many other things. IOW you're complaining about
Emacs's design.
> I know that Emacs Lisp places little value on functional programming
> principles, but I don't see the harm in embracing them at
> least occasionally.
Just adding something like buffer-mode is not going to help enough to
justify adding a function. You're trying to work against the tool.
Stefan
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Condider adding buffer-mode
2013-10-30 12:41 ` Stefan Monnier
@ 2013-10-31 12:57 ` Bozhidar Batsov
0 siblings, 0 replies; 9+ messages in thread
From: Bozhidar Batsov @ 2013-10-31 12:57 UTC (permalink / raw)
To: Stefan Monnier; +Cc: emacs-devel
[-- Attachment #1: Type: text/plain, Size: 865 bytes --]
On 30 October 2013 14:41, Stefan Monnier <monnier@iro.umontreal.ca> wrote:
> > current buffer). While this is OK for interactive code, it seems pretty
> > obnoxious when done in library code. We can very well have a version of
> > `derived-mode-p' that operates on an explicit buffer argument as opposed
> to
> > an implicit one.
>
> But this applies to many other things. IOW you're complaining about
> Emacs's design.
>
Fair enough.
>
> > I know that Emacs Lisp places little value on functional programming
> > principles, but I don't see the harm in embracing them at
> > least occasionally.
>
> Just adding something like buffer-mode is not going to help enough to
> justify adding a function. You're trying to work against the tool.
>
>
> Stefan
>
OK. Consider the matter closed then.
--
Best Regards,
Bozhidar Batsov
http://www.batsov.com
[-- Attachment #2: Type: text/html, Size: 1773 bytes --]
^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2013-10-31 12:57 UTC | newest]
Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-10-24 14:22 Condider adding buffer-mode Bozhidar Batsov
2013-10-24 21:46 ` Stefan Monnier
2013-10-29 15:53 ` Bozhidar Batsov
2013-10-29 16:36 ` Stefan Monnier
2013-10-29 17:28 ` Bozhidar Batsov
2013-10-29 21:56 ` Stefan Monnier
2013-10-30 9:34 ` Bozhidar Batsov
2013-10-30 12:41 ` Stefan Monnier
2013-10-31 12:57 ` Bozhidar Batsov
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs.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).