minad / cape

🦸cape.el - Completion At Point Extensions
GNU General Public License v3.0
584 stars 20 forks source link

Add a version of cape-line that collects candidates from all buffers with the same major mode #36

Closed daanturo closed 2 years ago

daanturo commented 2 years ago

I think we can implement this by writing another version of cape--line-list that operate on relevant buffers, or extend the current one.

(defun my-cape--line-list (&optional all)
  "Return all lines from buffer.
With non-nil ALL, return all lines from buffers with the same
major mode as the current one."
  (let ((ht (make-hash-table :test #'equal))
        end lines
        (mode major-mode))
    (dolist (buf (if all
                     (buffer-list)
                   (list (current-buffer))))
      (with-current-buffer buf
        (when (equal mode major-mode)
          (let ((beg (point-min))
                (max (point-max))
                (pt (point)))
            (save-excursion
              (while (< beg max)
                (goto-char beg)
                (setq end (line-end-position))
                (unless (<= beg pt end)
                  (let ((line (buffer-substring-no-properties beg end)))
                    (unless (or (string-blank-p line) (gethash line ht))
                      (puthash line t ht)
                      (push line lines))))
                (setq beg (1+ end))))))))
    (nreverse lines)))
minad commented 2 years ago

Sounds good. I propose this approach:

I am happy to accept a patch which implements this. See https://github.com/minad/cape/#contributions.

daanturo commented 2 years ago

Thank you.