emacsorphanage / helm-swoop

Efficiently hopping squeezed lines powered by Emacs helm interface
GNU General Public License v3.0
688 stars 55 forks source link

feat: Add `swiper`-like font lock ensure mechanism. #165

Closed cireu closed 4 years ago

cireu commented 4 years ago

Emacs's font-lock is lazy. when helm-swoop get contents from buffer, it may be not being fontified. So we force emacs to do it.

But in some buffers, font-lock-ensure will break properties in original buffer, so we need to exclude them in helm-swoop-font-lock-exclude.

Close #85.

conao3 commented 4 years ago

Great. I've tested and it seems works well for me. But defining helm-swoop--should-fontify?, helm-swoop--maybe-fontify! functions seems weird for me. I don't like to make small utility functions that can only be used in one place. For such purposes, it is good to use lambda.

(defun helm-swoop--get-content ($buffer &optional $linum)
  "Get the whole content in buffer and add line number at the head.
If $linum is number, lines are separated by $linum"
  (let* (($buf (get-buffer $buffer))
         (fontify-fn (lambda ()
                       (when (and helm-swoop-speed-or-color
                                  font-lock-mode
                                  (not (or (derived-mode-p 'magit-mode)
                                           (bound-and-true-p magit-blame-mode)
                                           (memq major-mode helm-swoop-font-lock-exclude)
                                           (not (derived-mode-p 'prog-mode))))
                                  (< (buffer-size) 100000))
                         (if (fboundp 'font-lock-ensure)
                             (font-lock-ensure)
                           (with-no-warnings (font-lock-fontify-buffer)))))))
    (when $buf
      (with-current-buffer $buf
        (funcall fontify-fn)
        (let (($bufstr (helm-swoop--buffer-substring (point-min) (point-max))))
          (with-temp-buffer
            (insert $bufstr)
            (goto-char (point-min))
            (let (($i 1))
              (insert (format "%s " $i))
              (while (re-search-forward "\n" nil t)
                (setq $i (1+ $i))
                (if helm-swoop-use-line-number-face
                    (insert (propertize (format "%s" $i) 'font-lock-face 'helm-swoop-line-number-face) " ")
                  (insert (format "%s " $i))))
              ;; Delete empty lines
              (unless $linum
                (goto-char (point-min))
                (while (re-search-forward "^[0-9]+\\s-*$" nil t)
                  (replace-match ""))))
            (helm-swoop--buffer-substring (point-min) (point-max))))))))

And maybe (< (buffer-size) 100000) should customizable by the user.

cireu commented 4 years ago

Merge helm-swoop--should-fontify? into helm-swoop--maybe-fontify!.

I keep last one because it's a single action and deserves a function. Inline it to helm-swoop--get-content will make helm-swoop--get-content more complicated(The original function is already complicated enough). And it's not modular for test.