From f354cf6de0d277a3b5bb188eb953c8b0842d23f1 Mon Sep 17 00:00:00 2001 From: Lionel Henry Date: Mon, 30 Jul 2018 09:55:59 +0200 Subject: [PATCH] Generate keywords lazily --- doc/newfeat.texi | 5 +++++ lisp/ess-custom.el | 36 +++++++++++++++++------------------ test/literate/fontification.R | 30 +++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 18 deletions(-) diff --git a/doc/newfeat.texi b/doc/newfeat.texi index 3207c9b7d..d0e7ae931 100644 --- a/doc/newfeat.texi +++ b/doc/newfeat.texi @@ -4,6 +4,11 @@ Changes and New Features in development version: @itemize @bullet +@item @ESS: Font-lock keywords are now generated lazily. That means you +can now add or remove keywords from variables like @code{ess-R-keywords} +in your Emacs config after loading ESS (i.e. in the @code{:config} +section for @code{use-package} users). + @item @ESS{[R]}: Interaction with inferior process in non-R files within packages (for instance C or C++ files) has been improved. This is a work in progress. diff --git a/lisp/ess-custom.el b/lisp/ess-custom.el index 5c1f61261..26875ce83 100644 --- a/lisp/ess-custom.el +++ b/lisp/ess-custom.el @@ -2768,46 +2768,46 @@ default or not." ;;; fl-keywords R (defvar ess-R-fl-keyword:modifiers - (cons (concat "\\(" (regexp-opt ess-R-modifyiers 'words) "\\)\\s-*(") - '(1 ess-modifiers-face)) ; modify search list or source (i.e. directives) + '(eval . (cons (concat "\\(" (regexp-opt ess-R-modifyiers 'words) "\\)\\s-*(") + '(1 ess-modifiers-face))) "Font-lock keyword R modifiers.") (defvar ess-R-fl-keyword:fun-defs - (cons ess-R-function-name-regexp - '(1 font-lock-function-name-face nil)) + '(eval . (cons ess-R-function-name-regexp + '(1 font-lock-function-name-face nil))) "Font-lock keyword - function defintions for R.") (defvar ess-r--bare-keywords '("in" "else" "break" "next" "repeat")) (defvar ess-R-fl-keyword:bare-keywords - (cons (regexp-opt ess-r--bare-keywords 'words) - 'ess-keyword-face) + '(eval . (cons (regexp-opt ess-r--bare-keywords 'words) + 'ess-keyword-face)) "Font-lock keywords that do not precede an opening parenthesis.") (defvar ess-R-fl-keyword:keywords - (let ((function-kwords - (delq nil - (mapcar (lambda (k) (unless (member k ess-r--bare-keywords) k)) - ess-R-keywords)))) - (cons (concat "\\(" (regexp-opt function-kwords 'words) "\\)\\s-*(") - '(1 ess-keyword-face))) + '(eval . (let ((function-kwords + (delq nil + (mapcar (lambda (k) (unless (member k ess-r--bare-keywords) k)) + ess-R-keywords)))) + (cons (concat "\\(" (regexp-opt function-kwords 'words) "\\)\\s-*(") + '(1 ess-keyword-face)))) "Font-lock keywords that precede an opening parenthesis.") (defvar ess-R-fl-keyword:control-flow-keywords - (cons (concat "\\(" (regexp-opt ess-R-control-flow-keywords 'words) "\\)\\s-*(") - '(1 ess-r-control-flow-keyword-face))) + '(eval . (cons (concat "\\(" (regexp-opt ess-R-control-flow-keywords 'words) "\\)\\s-*(") + '(1 ess-r-control-flow-keyword-face)))) (defvar ess-R-fl-keyword:signal-keywords - (cons (concat "\\(" (regexp-opt ess-R-signal-keywords 'words) "\\)\\s-*(") - '(1 ess-r-signal-keyword-face))) + '(eval . (cons (concat "\\(" (regexp-opt ess-R-signal-keywords 'words) "\\)\\s-*(") + '(1 ess-r-signal-keyword-face)))) (defvar ess-R-fl-keyword:assign-ops - (cons (regexp-opt ess-R-assign-ops) 'ess-assignment-face) + '(eval . (cons (regexp-opt ess-R-assign-ops) 'ess-assignment-face)) "Font-lock assign operators.") (defvar ess-R-fl-keyword:constants - (cons (regexp-opt ess-R-constants 'words) 'ess-constant-face) + '(eval . (cons (regexp-opt ess-R-constants 'words) 'ess-constant-face)) "Font-lock constants keyword.") (defvar ess-R-fl-keyword:F&T diff --git a/test/literate/fontification.R b/test/literate/fontification.R index 8cf4d40c7..88454f5dc 100644 --- a/test/literate/fontification.R +++ b/test/literate/fontification.R @@ -116,3 +116,33 @@ foo <-¶ foo <<-¶ foo ->¶ foo ->>¶ foo ¶TRUE ¶FALSE ¶NA ¶NULL ¶Inf ¶NaN ¶NA_integer_ ¶NA_real_ ¶NA_complex_ ¶NA_character_ + +### 8 Can modify keywords -------------------------------------------- + +¶foobar foobaz() + +##! (let ((ess-R-keywords (append '("foobaz") ess-R-keywords))) +##> (ess-r-mode) +##> (font-lock-ensure)) +##> (should (not (face-at-point))) +##> (forward-word) +##> (forward-char) + +foobar ¶foobaz() + +##> (should (eq (face-at-point) 'ess-keyword-face)) + +foobar ¶foobaz() + + +### 9 Can set keywords variable to nil ------------------------------- + +¶stop() + +##! (let (ess-R-control-flow-keywords) +##> (ess-r-mode) +##> (font-lock-ensure)) +##> (should (not (face-at-point))) + +¶stop() +