[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Condider adding buffer-mode

From: Bozhidar Batsov
Subject: Re: Condider adding buffer-mode
Date: Wed, 30 Oct 2013 11:34:11 +0200

On 29 October 2013 23:56, Stefan Monnier <address@hidden> 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)))
   (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. 


Best Regards,
Bozhidar Batsov


reply via email to

[Prev in Thread] Current Thread [Next in Thread]