Subject: RE: bug#17284: Host name completion in shell mode take 45 seconds

From: Drew Adams <[email-masked]>
Date: Fri, 3 Apr 2015 09:25:04 -0700 (PDT)

> > > Does it help to replace (looking-back ",") by (eq ?, (char-before)) ?
> > 
> > The function runs quickly with that change.
>
> Couldn't the byte-compiler warn when `looking-back' is used without
> specifying LIMIT?  Making LIMIT non-optional seems too harsh.

1. Is it really about LIMIT?  Or is it instead about looking back
   at a literal string of chars?  And typically a short string.
   I'm guessing that that is the use case to pursue here.

2. Instead of (or in addition to) a byte-compiler warning for
   `looking-back', how about adding a function `chars-before'?

Since I use multiple Emacs versions, some quite old, I use this.
But I'm sure that a much better (including prettier) definition
can be had for recent Emacs.  Or (better) define it in C.

(defun chars-before (chars)
  "Return non-nil if the literal string CHARS is right before point."
  (let* ((len  (length chars))
         (idx  (1- len))
         (pt   (point)))
    (catch 'chars-before
      (dolist (char  (append chars ()))
        (unless (condition-case nil
                    (eq char (char-before (- pt idx)))
                  (error nil))
          (throw 'chars-before nil))
        (setq idx  (1- idx)))
      t)))

Likewise (but may be no better than `looking-at' + `regexp-quote'):

(defun chars-after (chars)
  "Return non-nil if the literal string CHARS is right after point."
  (let* ((len  (length chars))
         (idx  (1- len))
         (pt   (point)))
    (catch 'chars-after
      (dolist (char  (nreverse (append chars ())))
        (unless (condition-case nil
                    (eq char (char-after (+ pt idx)))
                  (error nil))          ; e.g. `eobp'
          (throw 'chars-after nil))
        (setq idx  (1- idx)))
      t)))