4

Is it possible to create a regexp that excludes a string? For example, I want to create a regexp that accepts a set of strings with *.*helm.**except a few ones such as *helm help*. It means, *helm help* should be excluded. I want this regexp functionality because I set display-buffer-alist to position Helm windows where I like it to be:

(add-to-list 'display-buffer-alist `("*.*helm.**"
                                    (display-buffer-in-side-window)
                                    (inhibit-same-window . t)
                                    (window-height . 0.4)))

However, doing so would prevent a Helm buffer by default displayed in fullscreen, like *helm help* buffer.

5
  • 1
    A literal star is with two backslashes before it \\*, or enclosed within square brackets [*]. Type M-x re-builder RET and experiment a bit with samples in the buffer above. If you modify your question with a few real real life examples with actual strings you wish to match, I or someone else can take a stab at it. By placing incorrect regexp in your question, it is difficult to know exactly what you want to match. The carrot ^ within square brackets negates.
    – lawlist
    Commented May 19, 2015 at 3:48
  • I mean, I want to exclude a whole string, an exact match, not just any character in any order. Currently [*help] includes help itself, while I want to exclude it.
    – Tu Do
    Commented May 19, 2015 at 3:54
  • 1
    The following link to a thread entitled How to intercept a file before it opens and decide which frame stackoverflow.com/a/18371427/2112489 specifically deals with the display-buffer-alist and it contains custom functions that handle a variety of different situations. I have a few different variables with lists of regexp matching certain buffers, and a function that goes through the lists comparing the current buffer to be displayed with those in the list -- different action is taken depending upon the regexp matched. There is an example function that works out of the box.
    – lawlist
    Commented May 19, 2015 at 4:22
  • 1
    A regexp cannot exclude, by definition. (Well, you can exclude characters from a class, as in [^e], but that's really an abbreviation for specifying any char except e.) It sounds like you are not looking for a regexp that excludes a file, but you are instead looking for some way to exclude a file from display or from some other handling. If so, you should edit your question, including the title and the tags, to clarify it. It sounds like your question is really about how to use display-buffer-alist.
    – Drew
    Commented May 19, 2015 at 6:19
  • @Drew I found my own solution by defadvice the helm-help function to exclude itself. So, yes, I won't use such regexp in this case since it's not supported.
    – Tu Do
    Commented May 19, 2015 at 6:29

2 Answers 2

4

AFAIK Emacs Lisp doesn't support lookahead resp. lookbehind assertions. So a function is needed checking twice - match and not-match.

As display-buffer-alist accepts functions as argument, there should be a way to realize it.

7
  • You are right. found my own solution by defadvice the helm-help function to exclude itself from display-buffer-alist. Or rather, more correctly, restoring the original display-buffer-alist when it enters *Helm Help* buffer.
    – Tu Do
    Commented May 19, 2015 at 6:31
  • 2
    Why not use a function as first element for a display-buffer-alist entry that matches all Helm buffers and then filters helm help? That's the preferable solution in my opinion. Advices should only be your very last resort!
    – user227
    Commented May 19, 2015 at 6:52
  • @lunaryorn Thank you. Works like charm.
    – Tu Do
    Commented May 19, 2015 at 7:37
  • Actually, it was because my old defadvice that it works. I need to find another way.
    – Tu Do
    Commented May 19, 2015 at 7:55
  • It works this time in a simpler way and I updated the answer.
    – Tu Do
    Commented May 19, 2015 at 8:49
0

The accepted answer is the correct answer to my regexp problem, so I left it there. But to solve my own problem, I found a way to do it: instead of trying to negate specific string in the regexp in my original entry added to display-buffer-alist to handle Helm buffers, just add another entry for the case you are trying to exclude. For example, if you want to exclude any Helm help buffer, simply defines another entry for it in display-buffer-alist:

(defvar helm-display-help-buffer-regexp '("*.*Helm.*Help.**"))

(add-to-list 'display-buffer-alist helm-display-help-buffer-regexp)

Note that must be added after your original Helm entry to handle normal Helm buffers, because the display-buffer-alist is scanned from left to right and the one that matches first is selected. This means, before checking all other Helm buffers, we must first check whether the current Helm buffer is a Helm help and if so, use the configuration in helm-display-help-buffer-regexp. Otherwise, the configuration for common Helm buffers is used for Helm help buffer and as a result, the help buffer can't be displayed because it requires a full window.

3
  • The stars on each end of the regexp serve no purpose that I am aware of. They are not literal stars, and there is already a catch-all on each end -- i.e., .* is the catch-all.
    – lawlist
    Commented May 19, 2015 at 16:17
  • So, * is not a literal star but something else? I only want to include all Helm buffers start with * and end with *, not everything. I think it works fine, givent that buffers with filenames like helm.el, helm-buffers.el.. are opened normally.
    – Tu Do
    Commented May 19, 2015 at 17:49
  • The first comment underneath the original question contains two examples of how to match a literal astrisk, and it suggests a built-in library for you to use when writing/testing regexp.
    – lawlist
    Commented May 19, 2015 at 17:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.