logical not condition in ibuffer config

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

logical not condition in ibuffer config

Hikaru Ichijyo
I've been setting up ibuffer to group my buffer listing into
categories.  I've encountered a situation where I may want to make an
exception to one of the rules.  This is my config:

--8<---------------cut here---------------start------------->8---
      ibuffer-saved-filter-groups
      (quote (("Default"
               ("IRC" (mode . erc-mode))
               ("Web" (mode . w3m-mode))
               ("Mail & News" (or
                        (mode . message-mode)
                        (mode . mail-mode)
                        (mode . gnus-group-mode)
                        (mode . gnus-summary-mode)
                        (mode . gnus-article-mode)))
               ("Dired" (mode . dired-mode))
               ("Text" (mode . text-mode))
               ("HTML/CSS" (or
                        (mode . html-mode)
                        (mode . css-mode)))
               ("Config" (or
                        (mode . conf-space-mode)
                        (mode . conf-unix-mode)
                        (mode . conf-xdefaults-mode)))
               ("Shell" (mode . shell-script-mode))
               ("Perl" (mode . perl-mode))
               ("Lisp & Emacs" (or
                        (mode . emacs-lisp-mode)
                        (mode . bookmark-bmenu-mode)
                        (name . "^\\*Packages\\*$")))
               ("Docs" (or
                        (name . "^\\*info\\*$")
                        (name . "^\\*Man "))))))
--8<---------------cut here---------------end--------------->8---

I am finding that the "Lisp & Emacs" group catches my bbdb buffer,
because it is in Emacs-Lisp mode, but I would rather this went to
Default, since I rarely edit it directly.

So, I want to use a 'not' condition in these rules, to say, "Put
anything in 'Lisp & Emacs' that's in emacs-lisp-mode,
bookmark-bmenu-mode, or is named '*Packages*', except for buffers named
'bbdb'."

The problem is, I'm not really sure how I can use "or" operators here to
begin with, aside from seeing that they work, having based this config
on common configurations I've seen in other people's ~/.emacs files.
This whole section seems to define an alist.  How does it get executed?
When I try to use "and" or "not", it doesn't work, but for some reason,
"or" does work.  Something more is going on here beyond just an alist
definition.  I am still learning Emacs Lisp, obviously.

Is there a way I can use other operators like "not" and "and" in this
ibuffer setup?  It would be useful for cases where you want a rule exception.

--
He that would make his own liberty secure must guard even his enemy from
oppression; for if he violates this duty, he establishes a precedent
that will reach to himself.
                                        --Thomas Paine
Reply | Threaded
Open this post in threaded view
|

Re: logical not condition in ibuffer config

Emanuel Berg-4
Hikaru Ichijyo wrote:

> I've been setting up ibuffer to group my
> buffer listing into categories.
> I've encountered a situation where I may want
> to make an exception to one of the rules.
> This is my config:
>
>       ibuffer-saved-filter-groups
>       (quote (("Default"
>       ("IRC" (mode . erc-mode))
>       ("Web" (mode . w3m-mode))
>       ("Mail & News" (or
> (mode . message-mode)
> (mode . mail-mode)
> (mode . gnus-group-mode)
> (mode . gnus-summary-mode)
> (mode . gnus-article-mode)))
>       ("Dired" (mode . dired-mode))
>       ("Text" (mode . text-mode))
>       ("HTML/CSS" (or
> (mode . html-mode)
> (mode . css-mode)))
>       ("Config" (or
> (mode . conf-space-mode)
> (mode . conf-unix-mode)
> (mode . conf-xdefaults-mode)))
>       ("Shell" (mode . shell-script-mode))
>       ("Perl" (mode . perl-mode))
>       ("Lisp & Emacs" (or
> (mode . emacs-lisp-mode)
> (mode . bookmark-bmenu-mode)
> (name . "^\\*Packages\\*$")))
>       ("Docs" (or
> (name . "^\\*info\\*$")
> (name . "^\\*Man "))))))
>
> I am finding that the "Lisp & Emacs" group
> catches my bbdb buffer, because it is in
> Emacs-Lisp mode, but I would rather this went
> to Default, since I rarely edit it directly.

This is not exactly Lisp but rather a common
computer data hierarchy that is expressed as
list and then parsed as configuration data by
the Lisp program... whatever.

The easiest way to do what you want is probably
to find out the priority *order* by which the
data structure gets traversed.

Then, first put the general case (all Elisp)
somewhere, only after that explicitly put the
exception/the specific case (bbdb) wherever you
want it instead, so that rule overwrites the
general case rule (which otherwise applies to
both cases).

By the way, this level of perfectionism...
start digging into Elisp today as it'll just
get worse.

--
underground experts united
http://user.it.uu.se/~embe8573
Reply | Threaded
Open this post in threaded view
|

Re: logical not condition in ibuffer config

Hikaru Ichijyo
Emanuel Berg <[hidden email]> writes:

> This is not exactly Lisp but rather a common
> computer data hierarchy that is expressed as
> list and then parsed as configuration data by
> the Lisp program... whatever.
>
> The easiest way to do what you want is probably
> to find out the priority *order* by which the
> data structure gets traversed.
>
> Then, first put the general case (all Elisp)
> somewhere, only after that explicitly put the
> exception/the specific case (bbdb) wherever you
> want it instead, so that rule overwrites the
> general case rule (which otherwise applies to
> both cases).
>
> By the way, this level of perfectionism...
> start digging into Elisp today as it'll just
> get worse.

It sounds like you're explaining the algorithm.  I understand that.  I
need to know why Elisp will understand "or" operators in an ibuffer
configuration but not "and" or "not"...or suggestions of how "and" or
"not" could be coded that would work.

--
He that would make his own liberty secure must guard even his enemy from
oppression; for if he violates this duty, he establishes a precedent
that will reach to himself.
                                        --Thomas Paine
Reply | Threaded
Open this post in threaded view
|

Re: logical not condition in ibuffer config

Emanuel Berg-4
Hikaru Ichijyo wrote:

> It sounds like you're explaining the
> algorithm. I understand that. I need to know
> why Elisp will understand "or" operators in
> an ibuffer configuration but not "and" or
> "not"...or suggestions of how "and" or "not"
> could be coded that would work.

If it is Lisp all Lisp should work; check
your syntax.

If it isn't LISP but a LIST any program can do
whatever they want with it, including
implementing "or" but not "not", so check the
program (function) that is using it and see
what it does with it, and how.

--
underground experts united
http://user.it.uu.se/~embe8573
Reply | Threaded
Open this post in threaded view
|

Re: logical not condition in ibuffer config

Emanuel Berg-4
> If it is Lisp all Lisp should work; check
> your syntax.

But even so, most likely it is faster to assign
two times (the first time incorrectly) than to
check every file in Elisp mode if it is
Big Brother or not...

--
underground experts united
http://user.it.uu.se/~embe8573
Reply | Threaded
Open this post in threaded view
|

Re: logical not condition in ibuffer config

Yuri Khan-2
In reply to this post by Hikaru Ichijyo
On Mon, Mar 12, 2018 at 3:17 AM, Hikaru Ichijyo <[hidden email]> wrote:

> I've been setting up ibuffer to group my buffer listing into
> categories.  I've encountered a situation where I may want to make an
> exception to one of the rules.  This is my config:
>
> --8<---------------cut here---------------start------------->8---
>                ("Lisp & Emacs" (or
>                         (mode . emacs-lisp-mode)
>                         (mode . bookmark-bmenu-mode)
>                         (name . "^\\*Packages\\*$")))
> --8<---------------cut here---------------end--------------->8---
>
> I am finding that the "Lisp & Emacs" group catches my bbdb buffer,
> because it is in Emacs-Lisp mode, but I would rather this went to
> Default, since I rarely edit it directly.

The filtering/grouping feature in Ibuffer is written and documented
with interactive use, rather than long-term init-file-based
configuration, in mind. But you can use that to find out how it
represents filter intersection and inversion.

I will use predicates that are easier to test in an uncustomized
emacs, but the results should apply to your case as well.

So, first, I want a few buffers in emacs-lisp-mode, with a secondary
difference that I could intersect and invert.

    $ emacs -Q
    C-x C-f ~/.emacs.d/init.el RET
    M-x find-library RET ibuffer RET
    M-x ibuffer RET

Now, I have two buffers using emacs-lisp-mode. What’s more, one of
them has a file name ending in “.gz”. Let’s see how we can combine
these conditions.

    <f1> m, scroll to Filtering commands

I see a command to combine two filters with “or”, and a command to
invert a filter. I do not see a command to combine with “and”. Let’s
try adding a filter.

    / m emacs-lisp-mode RET

Something happened. I now see only buffers in emacs-lisp-mode. (The
header line shows the currently applied filter as [major mode in use:
emacs-lisp-mode].) So far so good. Now let’s add another filter, with
a condition on file name.

    / f \.gz\' RET

Now I only see the buffer that is in emacs-lisp-mode *and* with file
name ending in “.gz”. (The header line displays this as [filename:
\.gz\'] [major mode in use: emacs-lisp-mode].) Let’s try inverting.

    / !

Now I see the buffer that is in emacs-lisp-mode and with file name
*not* ending “.gz”. (The header line updates to [NOT [filename:
\.gz\']] [major mode in use: emacs-lisp-mode].)

To see how this is represented, I will save this filter:

    / s test1 RET

Emacs says: “Setting ‘ibuffer-saved-filter-groups’ temporarily since
"emacs -q" would overwrite customizations”. I understand it as “your
changes were not saved into your init file and will only be active in
this session”. Okay, let’s see the variable.

    M-: ibuffer-saved-filters RET
    ⇒ (("test1" (not filename . "\\.gz\\'")
                (used-mode . emacs-lisp-mode))
       …more filters…)

And that should answer your questions:

* For an “and”, you just write several filter expressions one after another.
* You can put “not” before the filter predicate.

> So, I want to use a 'not' condition in these rules, to say, "Put
> anything in 'Lisp & Emacs' that's in emacs-lisp-mode,
> bookmark-bmenu-mode, or is named '*Packages*', except for buffers named
> 'bbdb'."

This should be it:

    ("Lisp & Emacs"
      (not name . "^bbdb$")
      (or (mode . emacs-lisp-mode)
          (mode . bookmark-bmenu-mode)
          (name . "^\\*Packages\\*$")))

Reply | Threaded
Open this post in threaded view
|

Re: logical not condition in ibuffer config

Hikaru Ichijyo
In reply to this post by Hikaru Ichijyo
Yuri Khan <[hidden email]> writes:

[...]

>
>> So, I want to use a 'not' condition in these rules, to say, "Put
>> anything in 'Lisp & Emacs' that's in emacs-lisp-mode,
>> bookmark-bmenu-mode, or is named '*Packages*', except for buffers named
>> 'bbdb'."
>
> This should be it:
>
>     ("Lisp & Emacs"
>       (not name . "^bbdb$")
>       (or (mode . emacs-lisp-mode)
>           (mode . bookmark-bmenu-mode)
>           (name . "^\\*Packages\\*$")))
>

Now I see what Emanuel meant by assigning twice.  I never would have
suspected that would work.  Thank you.

--
He that would make his own liberty secure must guard even his enemy from
oppression; for if he violates this duty, he establishes a precedent
that will reach to himself.
                                        --Thomas Paine
Reply | Threaded
Open this post in threaded view
|

Re: logical not condition in ibuffer config

Emanuel Berg-4
Hikaru Ichijyo wrote:

> Now I see what Emanuel meant by assigning
> twice. I never would have suspected that
> would work. Thank you.

It is neater to do it Kahn's way but it depends
on how many Elisp files one typically has.
If one has say a dozen checking 12 times is OK.
But if one has 1 000 checking 1 000 times
because of ONE exception is poor style IMO.
Then either don't do it or find some other way.

    "Do it today, in a different way."

--
underground experts united
http://user.it.uu.se/~embe8573
Reply | Threaded
Open this post in threaded view
|

Re: logical not condition in ibuffer config

Emanuel Berg-4
In reply to this post by Hikaru Ichijyo
Another idea is to change the major mode of the
BBDS file ...

(pause for brain to think)

I have a book on knots:

    @book{ultimate-encyclopedia-of-knots-and-ropework,
      author     = {Geoffrey Budworth},
      ISBN       = 1844768910,
      publisher  = {Southwater},
      title      = {The Ultimate Encyclopedia of Knots and Ropework},
      year       = 2010
    }

And there is a section on the "Granny's Knot",
if only to tell why it sucks and that no one
should use it. I was very impressed by this.

--
underground experts united
http://user.it.uu.se/~embe8573