From ec8ae0bedc8e5613b61727db6fdea62d189497c6 Mon Sep 17 00:00:00 2001 From: Henrik Lissner Date: Sat, 16 Jun 2018 19:32:25 +0200 Subject: [PATCH] Add :ui pretty-code & set-pretty-symbols! autodef Along with defaults for C/C++, elm, elisp, js, typescript, web-mode, and org-mode. Thanks to @ar1a for inspiration. --- init.example.el | 1 + modules/lang/cc/config.el | 16 ++++++ modules/lang/elm/config.el | 11 +++- modules/lang/emacs-lisp/config.el | 5 +- modules/lang/javascript/config.el | 36 ++++++++++++- modules/lang/org/config.el | 10 +++- modules/lang/python/config.el | 18 +++++++ modules/ui/pretty-code/autoload.el | 86 ++++++++++++++++++++++++++++++ modules/ui/pretty-code/packages.el | 5 ++ 9 files changed, 183 insertions(+), 5 deletions(-) create mode 100644 modules/ui/pretty-code/autoload.el create mode 100644 modules/ui/pretty-code/packages.el diff --git a/init.example.el b/init.example.el index 37c63ef5a..60ac24e02 100644 --- a/init.example.el +++ b/init.example.el @@ -39,6 +39,7 @@ (popup ; tame sudden yet inevitable temporary windows +all ; catch all popups that start with an asterix +defaults) ; default popup rules + ;pretty-code ; replace bits of code with pretty symbols ;tabbar ; FIXME an (incomplete) tab bar for Emacs ;unicode ; extended unicode support for various languages vi-tilde-fringe ; fringe tildes to mark beyond EOB diff --git a/modules/lang/cc/config.el b/modules/lang/cc/config.el index d99f9aa34..4b4ba614b 100644 --- a/modules/lang/cc/config.el +++ b/modules/lang/cc/config.el @@ -60,6 +60,22 @@ compilation database is present in the project.") :config (set-electric! '(c-mode c++-mode objc-mode java-mode) :chars '(?\n ?\})) + (set-pretty-symbols! '(c-mode-hook c++-mode-hook) + ;; Functional + ;; :def "void " + ;; Types + :null "nullptr" + :true "true" :false "false" + :int "int" :float "float" + :str "std::string" + :bool "bool" + ;; Flow + :not "!" + :and "&&" :or "||" + :for "for" + :return "return" + :yield "#require") + ;;; Better fontification (also see `modern-cpp-font-lock') (add-hook 'c-mode-common-hook #'rainbow-delimiters-mode) (add-hook! '(c-mode-hook c++-mode-hook) diff --git a/modules/lang/elm/config.el b/modules/lang/elm/config.el index 35a5f303e..500d28d61 100644 --- a/modules/lang/elm/config.el +++ b/modules/lang/elm/config.el @@ -6,7 +6,16 @@ (after! elm-mode (add-hook 'elm-mode-hook #'rainbow-delimiters-mode) (set-company-backend! 'elm-mode 'company-elm) - (set-repl-handler! 'elm-mode #'run-elm-interactive)) + (set-repl-handler! 'elm-mode #'run-elm-interactive) + (set-pretty-symbols! 'elm-mode + :null "null" + :true "true" :false "false" + :int "Int" :str "String" + :float "Float" + :bool "Bool" + + :not "not" + :and "&&" :or "||")) (def-package! flycheck-elm diff --git a/modules/lang/emacs-lisp/config.el b/modules/lang/emacs-lisp/config.el index fe015ff0f..7ac57c3cb 100644 --- a/modules/lang/emacs-lisp/config.el +++ b/modules/lang/emacs-lisp/config.el @@ -8,6 +8,9 @@ (set-lookup-handlers! 'emacs-lisp-mode :documentation 'info-lookup-symbol) (set-docset! '(lisp-mode emacs-lisp-mode) "Emacs Lisp") + (set-pretty-symbols! 'emacs-lisp-mode + :lambda "lambda") + (set-rotate-patterns! 'emacs-lisp-mode :symbols '(("t" "nil") ("let" "let*") @@ -31,8 +34,6 @@ (font-lock-add-keywords nil `(;; Highlight custom Doom cookies ("^;;;###\\(autodef\\|if\\)[ \n]" (1 font-lock-warning-face t)) - ;; Display "lambda" as λ - ("(\\(lambda\\)" (1 (ignore (compose-region (match-beginning 1) (match-end 1) ?λ #'decompose-region)))) ;; Highlight doom/module functions ("\\(^\\|\\s-\\|,\\)(\\(\\(doom\\|\\+\\)[^) ]+\\|[^) ]+!\\)[) \n]" (2 font-lock-keyword-face))))) diff --git a/modules/lang/javascript/config.el b/modules/lang/javascript/config.el index 3a7e17245..a145a24a6 100644 --- a/modules/lang/javascript/config.el +++ b/modules/lang/javascript/config.el @@ -1,5 +1,23 @@ ;;; lang/javascript/config.el -*- lexical-binding: t; -*- +(after! (:any js2-mode web-mode) + (set-pretty-symbols! '(js2-mode web-mode) + '(;; Functional + :def "function" + :lambda "() =>" + :composition "compose" + ;; Types + :null "null" + :true "true" :false "false" + ;; Flow + :not "!" + :and "&&" :or "||" + :for "for" + :return "return" + ;; Other + :yield "import"))) + + ;; ;; Major modes ;; @@ -66,7 +84,23 @@ (after! typescript-mode (add-hook! 'typescript-mode-hook #'(flycheck-mode rainbow-delimiters-mode)) (set-electric! 'typescript-mode - :chars '(?\} ?\)) :words '("||" "&&"))) + :chars '(?\} ?\)) :words '("||" "&&")) + (set-pretty-symbols! 'typescript-mode + ;; Functional + :def "function" + :lambda "() =>" + :composition "compose" + ;; Types + :null "null" + :true "true" :false "false" + :int "number" + :str "string" + :bool "boolean" + ;; Flow + :not "!" + :and "&&" :or "||" + :for "for" + :return "return" :yield "import")) ;; `coffee-mode' diff --git a/modules/lang/org/config.el b/modules/lang/org/config.el index 3a351a229..0c006e6fc 100644 --- a/modules/lang/org/config.el +++ b/modules/lang/org/config.el @@ -48,7 +48,8 @@ +org|setup-popups-rules +org|setup-agenda +org|setup-keybinds - +org|setup-hacks)) + +org|setup-hacks + +org|setup-pretty-code)) (add-hook! 'org-mode-hook #'(doom|disable-line-numbers ; org doesn't really need em @@ -151,6 +152,13 @@ unfold to point on startup." ((size . 0.2)) ((quit) (select . t)))))) +(defun +org|setup-pretty-code () + "Setup the default pretty symbols for" + (set-pretty-symbols! 'org-mode + :name "#+NAME:" + :src_block "#+BEGIN_SRC" + :src_block_end "#+END_SRC")) + (defun +org|setup-ui () "Configures the UI for `org-mode'." (setq-default diff --git a/modules/lang/python/config.el b/modules/lang/python/config.el index c2b7a11d0..48ba4f077 100644 --- a/modules/lang/python/config.el +++ b/modules/lang/python/config.el @@ -28,6 +28,24 @@ is loaded.") (set-electric! 'python-mode :chars '(?:)) (set-repl-handler! 'python-mode #'+python/repl) + (set-pretty-symbols! 'python-mode + ;; Functional + :def "def" + :lambda "lambda" + ;; Types + :null "None" + :true "True" :false "False" + :int "int" :str "str" + :float "float" + :bool "bool" + :tuple "tuple" + ;; Flow + :not "not" + :in "in" :not-in "not in" + :and "and" :or "or" + :for "for" + :return "return" :yield "yield") + (when (executable-find "ipython") (setq python-shell-interpreter "ipython" python-shell-interpreter-args "-i --simple-prompt --no-color-info" diff --git a/modules/ui/pretty-code/autoload.el b/modules/ui/pretty-code/autoload.el new file mode 100644 index 000000000..7954207eb --- /dev/null +++ b/modules/ui/pretty-code/autoload.el @@ -0,0 +1,86 @@ +;;; ui/pretty-code/autoload.el -*- lexical-binding: t; -*- + +;;;###autoload +(defvar +pretty-code-enabled-modes + '(c++-mode-hook + c-mode-hook + elm-mode + emacs-lisp-mode + js2-mode + org-mode + python-mode + typescript-mode + web-mode) + "List of major modes in which `prettify-symbols-mode' should be enabled.") + +;;;###autoload +(defvar +pretty-code-symbols + '(;; org + :name "»" + :src_block "»" + :src_block_end " " + ;; Functional + :lambda "λ" + :def "ƒ" + :composition "∘" + ;; Types + :null "∅" + :true "𝕋" + :false "𝔽" + :int "ℤ" + :float "ℝ" + :str "𝕊" + :bool "𝔹" + ;; Flow + :not "¬" + :in "∈" + :not-in "∉" + :and "∧" + :or "∨" + :for "∀" + :some "∃" + :return "⟼" + :yield "⟻" + ;; Other + :tuple "⨂" + :pipe "") + "Options plist for `pretty-code-get-pairs'.") + +;; When you get to the right edge, it goes back to how it normally prints +;;;###autoload +(setq prettify-symbols-unprettify-at-point 'right-edge) + +;;;###autodef +(defun set-pretty-symbols! (modes &rest plist) + "Associates string patterns with icons in certain major-modes. + + MODES is a major mode symbol or a list of them. + PLIST is a property list whose keys must match keys in `+pretty-code-symbols', +and whose values are strings representing the text to be replaced with that +symbol. + +For example, the rule for emacs-lisp-mode is very simple: + + (set-pretty-symbols! 'emacs-lisp-mode + :lambda \"lambda\") + +This will replace any instances of \"lambda\" in emacs-lisp-mode with the symbol +assicated with :lambda in `+pretty-code-symbols'." + (declare (indent 1)) + (dolist (mode (doom-enlist modes)) + (let ((fn (intern (format "+pretty-code|init-%s" mode)))) + (fset fn + (lambda () + (when (and (eq major-mode mode) + (memq major-mode +pretty-code-enabled-modes)) + (let (results prop icon) + (while plist + (let ((prop (pop plist)) + (sym (pop plist))) + (when-let* ((icon (plist-get +pretty-code-symbols prop))) + (push (cons sym (prettify-utils-string icon)) + results)))) + (setq prettify-symbols-alist results)) + (prettify-symbols-mode -1) + (prettify-symbols-mode +1)))) + (add-hook (intern (format "%s-hook" mode)) fn)))) diff --git a/modules/ui/pretty-code/packages.el b/modules/ui/pretty-code/packages.el new file mode 100644 index 000000000..80e5b016d --- /dev/null +++ b/modules/ui/pretty-code/packages.el @@ -0,0 +1,5 @@ +;; -*- no-byte-compile: t; -*- +;;; ui/pretty-code/packages.el + +(package! prettify-utils + :recipe (:fetcher github :repo "Ilazki/prettify-utils.el"))