aboutsummaryrefslogtreecommitdiff
path: root/helpful.el
diff options
context:
space:
mode:
authorWilfred Hughes <me@wilfred.me.uk>2018-05-10 08:42:18 +0100
committerWilfred Hughes <me@wilfred.me.uk>2018-05-10 08:44:46 +0100
commitb97947b2033e33eb5fddb71ec840a3221b27e87c (patch)
treea7ccfe2f89e474430c1e09fbdbfab1d4ff9ab1b2 /helpful.el
parentc87f865c7a3c15ed0c049099694ee6cc4643b684 (diff)
Don't highlight very large code snippets
This stops big code excerpts hanging Emacs when using helpful. Fixes #107.
Diffstat (limited to 'helpful.el')
-rw-r--r--helpful.el47
1 files changed, 28 insertions, 19 deletions
diff --git a/helpful.el b/helpful.el
index c82c3b1..3b55f4f 100644
--- a/helpful.el
+++ b/helpful.el
@@ -82,6 +82,14 @@ To disable cleanup entirely, set this variable to nil. See also
:type 'function
:group 'helpful)
+;; TODO: explore whether more basic highlighting is fast enough to
+;; handle larger functions. See `c-font-lock-init' and its use of
+;; font-lock-keywords-1.
+(defconst helpful-max-highlight 5000
+ "Don't highlight code with more than this many characters.
+This is particularly important for large pieces of C code, such
+as describing `this-command'.")
+
(defun helpful--kind-name (symbol callable-p)
"Describe what kind of symbol this is."
(cond
@@ -905,25 +913,26 @@ hooks.")
(with-temp-buffer
(insert source)
- ;; Switch to major-mode MODE, but don't run any hooks.
- (delay-mode-hooks (funcall mode))
-
- ;; `delayed-mode-hooks' contains mode hooks like
- ;; `emacs-lisp-mode-hook'. Build a list of functions that are run
- ;; when the mode hooks run.
- (let (hook-funcs)
- (dolist (hook delayed-mode-hooks)
- (let ((funcs (symbol-value hook)))
- (setq hook-funcs (append hook-funcs funcs))))
-
- ;; Filter hooks to those that relate to highlighting, and run them.
- (setq hook-funcs (-intersection hook-funcs helpful--highlighting-funcs))
- (-map #'funcall hook-funcs))
-
- (if (fboundp 'font-lock-ensure)
- (font-lock-ensure)
- (with-no-warnings
- (font-lock-fontify-buffer)))
+ (when (< (length source) helpful-max-highlight)
+ ;; Switch to major-mode MODE, but don't run any hooks.
+ (delay-mode-hooks (funcall mode))
+
+ ;; `delayed-mode-hooks' contains mode hooks like
+ ;; `emacs-lisp-mode-hook'. Build a list of functions that are run
+ ;; when the mode hooks run.
+ (let (hook-funcs)
+ (dolist (hook delayed-mode-hooks)
+ (let ((funcs (symbol-value hook)))
+ (setq hook-funcs (append hook-funcs funcs))))
+
+ ;; Filter hooks to those that relate to highlighting, and run them.
+ (setq hook-funcs (-intersection hook-funcs helpful--highlighting-funcs))
+ (-map #'funcall hook-funcs))
+
+ (if (fboundp 'font-lock-ensure)
+ (font-lock-ensure)
+ (with-no-warnings
+ (font-lock-fontify-buffer))))
(buffer-string)))
(defun helpful--source (sym callable-p)