]> git.eshelyaron.com Git - emacs.git/commitdiff
Convert symbol prettification into minor mode and global minor mode.
authorJuanma Barranquero <lekktu@gmail.com>
Tue, 18 Jun 2013 22:13:25 +0000 (00:13 +0200)
committerJuanma Barranquero <lekktu@gmail.com>
Tue, 18 Jun 2013 22:13:25 +0000 (00:13 +0200)
* etc/NEWS: Document new Prettify Symbols mode.

* lisp/progmodes/prog-mode.el (prettify-symbols-alist): Rename from
  `prog-prettify-symbols', and make a local defvar instead of defcustom.
  (prettify-symbols--keywords): Rename from `prog-prettify-symbols-alist'
  and make a local defvar.
  (prettify-symbols--compose-symbol): Rename from
  `prog--prettify-font-lock-compose-symbol'.
  (prettify-symbols--make-keywords): Rename from
  `prog-prettify-font-lock-symbols-keywords' and simplify.
  (prog-prettify-install): Remove.
  (prettify-symbols-mode): New minor mode, based on `prog-prettify-install'.
  (turn-on-prettify-symbols-mode): New function.
  (global-prettify-symbols-mode): New globalized minor mode.

* lisp/emacs-lisp/lisp-mode.el (lisp-mode-variables):
* lisp/progmodes/cfengine.el (cfengine3-mode):
* lisp/progmodes/perl-mode.el (perl-mode): Don't call `prog-prettify-install';
  set `prettify-symbols-alist' instead.

etc/ChangeLog
etc/NEWS
lisp/ChangeLog
lisp/emacs-lisp/lisp-mode.el
lisp/progmodes/cfengine.el
lisp/progmodes/perl-mode.el
lisp/progmodes/prog-mode.el

index 1892d5fbb58e9f75e2f64a76dc9f9ac2cfefb92f..676e67860ccc00a97b299e44026f5690dedf641c 100644 (file)
@@ -1,3 +1,7 @@
+2013-06-18  Juanma Barranquero  <lekktu@gmail.com>
+
+       * NEWS: Document new Prettify Symbols mode.
+
 2013-06-14  Stefan Monnier  <monnier@iro.umontreal.ca>
 
        * NEWS (utf-8 for el): Move to the incompatible section.
index a2ef1c4fdd05e5aae9d4b3036339be2b78698725..a2f96f4bf92ad3c62b0a253785ee4917b9f80286 100644 (file)
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -2873,10 +2873,10 @@ should be derived.
 modes, e.g. (add-hook 'prog-mode-hook 'flyspell-prog-mode) to enable
 on-the-fly spell checking for comments and strings.
 
-**** New option, `prog-prettify-symbols' lets the user control symbol
-prettify (replacing a string like "lambda" with the Greek lambda
-character).  The mode derived from `prog-mode' must call
-`prog-prettify-install' with its own custom alist, which can be empty.
+**** New minor modes `prettify-symbols-mode' and
+`global-prettify-symbols-mode' let the user enable symbol
+prettification (replacing a string like "lambda" with the Greek lambda
+character).
 
 *** New hook `change-major-mode-after-body-hook', run by
 `run-mode-hooks' just before any other mode hooks.
index abd9e9da3aef9478642399b38ad00eedf26bf23f..44d8a2a82bd0edc6cbc1e73d7686f2dca5afde41 100644 (file)
@@ -1,3 +1,26 @@
+2013-06-18  Juanma Barranquero  <lekktu@gmail.com>
+
+       Convert symbol prettification into minor mode and global minor mode.
+
+       * progmodes/prog-mode.el (prettify-symbols-alist): Rename from
+       `prog-prettify-symbols', and make a local defvar instead of defcustom.
+       (prettify-symbols--keywords): Rename from
+       `prog-prettify-symbols-alist' and make a local defvar.
+       (prettify-symbols--compose-symbol): Rename from
+       `prog--prettify-font-lock-compose-symbol'.
+       (prettify-symbols--make-keywords): Rename from
+       `prog-prettify-font-lock-symbols-keywords' and simplify.
+       (prog-prettify-install): Remove.
+       (prettify-symbols-mode): New minor mode, based on
+       `prog-prettify-install'.
+       (turn-on-prettify-symbols-mode): New function.
+       (global-prettify-symbols-mode): New globalized minor mode.
+
+       * emacs-lisp/lisp-mode.el (lisp-mode-variables):
+       * progmodes/cfengine.el (cfengine3-mode):
+       * progmodes/perl-mode.el (perl-mode): Don't call
+       `prog-prettify-install'; set `prettify-symbols-alist' instead.
+
 2013-06-18  Juri Linkov  <juri@jurta.org>
 
        * files-x.el (modify-file-local-variable-message): New function.
index b8e12b630615a07779cf6d66edf5fee7914f71de..af30deca4cc534bd49a7f7a93b49378d00da33bd 100644 (file)
@@ -231,7 +231,7 @@ font-lock keywords will not be case sensitive."
          (font-lock-mark-block-function . mark-defun)
          (font-lock-syntactic-face-function
           . lisp-font-lock-syntactic-face-function)))
-  (prog-prettify-install lisp--prettify-symbols-alist))
+  (setq-local prettify-symbols-alist lisp--prettify-symbols-alist))
 
 (defun lisp-outline-level ()
   "Lisp mode `outline-level' function."
index 01b5faef5b344e6756b87456bdd758c4b41de48c..55d5b8b0be7f966bbbc6f5849d4a2b5bf95016ad 100644 (file)
@@ -547,7 +547,7 @@ to the action header."
   (setq font-lock-defaults
         '(cfengine3-font-lock-keywords
           nil nil nil beginning-of-defun))
-  (prog-prettify-install cfengine3--prettify-symbols-alist)
+  (setq-local prettify-symbols-alist cfengine3--prettify-symbols-alist)
 
   ;; Use defuns as the essential syntax block.
   (set (make-local-variable 'beginning-of-defun-function)
index 1d5052bede44ce09e375f14c58d01e8c5bd61ba6..8955c64aa9e8b5c042ec9a27e1c462b7dbb0d1d3 100644 (file)
@@ -658,7 +658,7 @@ Turning on Perl mode runs the normal hook `perl-mode-hook'."
                              nil nil ((?\_ . "w")) nil
                              (font-lock-syntactic-face-function
                               . perl-font-lock-syntactic-face-function)))
-  (prog-prettify-install perl--prettify-symbols-alist)
+  (setq-local prettify-symbols-alist perl--prettify-symbols-alist)
   (setq-local syntax-propertize-function #'perl-syntax-propertize-function)
   (add-hook 'syntax-propertize-extend-region-functions
             #'syntax-propertize-multiline 'append 'local)
index 6498ba51e459f639770e8774f069534c6f255680..b3ed328bd72e9abcce6be2aeb08f9e6227de1a12 100644 (file)
@@ -54,21 +54,14 @@ instead."
          (end (progn (forward-sexp 1) (point))))
       (indent-region start end nil))))
 
-(defvar prog-prettify-symbols-alist nil)
-
-(defcustom prog-prettify-symbols nil
-  "Whether symbols should be prettified.
-When set to an alist in the form `((STRING . CHARACTER)...)' it
-will augment the mode's native prettify alist."
-  :type '(choice
-          (const :tag "No thanks" nil)
-          (const :tag "Mode defaults" t)
-          (alist :tag "Mode defaults augmented with your own list"
-                 :key-type string :value-type character))
-  :version "24.4")
-
-(defun prog--prettify-font-lock-compose-symbol (alist)
-  "Compose a sequence of ascii chars into a symbol.
+(defvar-local prettify-symbols-alist nil
+  "Alist of symbol prettifications.
+Each element looks like (SYMBOL . CHARACTER), where the symbol
+matching SYMBOL (a string, not a regexp) will be shown as
+CHARACTER instead.")
+
+(defun prettify-symbols--compose-symbol (alist)
+  "Compose a sequence of characters into a symbol.
 Regexp match data 0 points to the chars."
   ;; Check that the chars should really be composed into a symbol.
   (let* ((start (match-beginning 0))
@@ -88,28 +81,60 @@ Regexp match data 0 points to the chars."
   ;; Return nil because we're not adding any face property.
   nil)
 
-(defun prog-prettify-font-lock-symbols-keywords ()
-  (when prog-prettify-symbols
-    (let ((alist (append prog-prettify-symbols-alist
-                         (if (listp prog-prettify-symbols)
-                             prog-prettify-symbols
-                           nil))))
-      `((,(regexp-opt (mapcar 'car alist) t)
-         (0 (prog--prettify-font-lock-compose-symbol ',alist)))))))
-
-(defun prog-prettify-install (alist)
-"Install prog-mode support to prettify symbols according to ALIST.
-
-ALIST is in the format `((STRING . CHARACTER)...)' like
-`prog-prettify-symbols'.
-
-Internally, `font-lock-add-keywords' is called."
-  (setq-local prog-prettify-symbols-alist alist)
-  (let ((keywords (prog-prettify-font-lock-symbols-keywords)))
-    (when keywords
-      (font-lock-add-keywords nil keywords)
-      (setq-local font-lock-extra-managed-props
-                  (cons 'composition font-lock-extra-managed-props)))))
+(defun prettify-symbols--make-keywords ()
+  (if prettify-symbols-alist
+      `((,(regexp-opt (mapcar 'car prettify-symbols-alist) t)
+         (0 (prettify-symbols--compose-symbol ',prettify-symbols-alist))))
+    nil))
+
+(defvar-local prettify-symbols--keywords nil)
+
+;;;###autoload
+(define-minor-mode prettify-symbols-mode
+  "Toggle Prettify Symbols mode.
+With a prefix argument ARG, enable Prettify Symbols mode if ARG is
+positive, and disable it otherwise.  If called from Lisp, enable
+the mode if ARG is omitted or nil.
+
+When Prettify Symbols mode and font-locking are enabled, symbols are
+prettified (displayed as composed characters) according to the rules
+in `prettify-symbols-alist' (which see), which are locally defined
+by major modes supporting prettifying.  To add further customizations
+for a given major mode, you can modify `prettify-symbols-alist' thus:
+
+  (add-hook 'emacs-lisp-mode-hook
+            (lambda ()
+              (push '(\"<=\" . ?≤) prettify-symbols-alist)))
+
+You can enable this mode locally in desired buffers, or use
+`global-prettify-symbols-mode' to enable it for all modes that
+support it."
+  :init-value nil
+  (if prettify-symbols-mode
+      ;; Turn on
+      (when (setq prettify-symbols--keywords (prettify-symbols--make-keywords))
+        (font-lock-add-keywords nil prettify-symbols--keywords)
+        (setq-local font-lock-extra-managed-props
+                    (cons 'composition font-lock-extra-managed-props))
+        (font-lock-fontify-buffer))
+    ;; Turn off
+    (when prettify-symbols--keywords
+      (font-lock-remove-keywords nil prettify-symbols--keywords)
+      (setq prettify-symbols--keywords nil))
+    (when (memq 'composition font-lock-extra-managed-props)
+      (setq font-lock-extra-managed-props (delq 'composition
+                                                font-lock-extra-managed-props))
+      (with-silent-modifications
+        (remove-text-properties (point-min) (point-max) '(composition nil))))))
+
+(defun turn-on-prettify-symbols-mode ()
+  (when (and (not prettify-symbols-mode)
+             (local-variable-p 'prettify-symbols-alist))
+    (prettify-symbols-mode 1)))
+
+;;;###autoload
+(define-globalized-minor-mode global-prettify-symbols-mode
+  prettify-symbols-mode turn-on-prettify-symbols-mode)
 
 ;;;###autoload
 (define-derived-mode prog-mode fundamental-mode "Prog"