I want to define a bunch of faces for different characters like below:
(defface char-face-a
'((((type tty) (class color)) (:background "yellow" :foreground "black"))
(((type tty) (class mono)) (:inverse-video t))
(((class color) (background dark)) (:background "yellow" :foreground "black"))
(((class color) (background light)) (:background "yellow" :foreground "black"))
(t (:background "gray")))
"Face for marking up A's"
:group 'char-faces)
(defface char-face-b
'((((type tty) (class color)) (:background "red" :foreground "black"))
(((type tty) (class mono)) (:inverse-video t))
(((class color) (background dark)) (:background "red" :foreground "black"))
(((class color) (background light)) (:background "red" :foreground "black"))
(t (:background "gray")))
"Face for marking up B's"
:group 'char-faces)
...
...
Is there anyway to avoid explicitly write all the defface definitions and make the code less redundant? (I know make-face, but it seems deprecated and can't set attributes according to different terminal types as defface does.)
make-face is not at all deprecated, AFAICT.
defface can make use of inheritance -- see face attribute :inherit.
Dunno whether that helps in your particular context.
How about a macro and a loop that operates on a mapping of suffixes <-> colors:
(defmacro brian-def-char-face (letter backgrnd foregrnd)
`(defface ,(intern (concat "brian-char-face-"
letter))
'((((type tty) (class color))
(:background
,backgrnd
:foreground
,foregrnd))
(((type tty) (class color)) (:inverse-video t))
(((class color) (background dark))
(:foreground
,foregrnd
:background
,backgrnd))
(((class color) (background light))
(:foreground
,foregrnd
:background
,backgrnd))
(t (:background "gray")))
,(concat "Face for marking up " (upcase letter) "'s")))
(let ((letcol-alist '((s . (white black))
(t . (black yellow))
(u . (green pink)))))
(loop for elem in letcol-alist
for l = (format "%s" (car elem))
for back = (format "%s" (cadr elem))
for fore = (format "%s" (caddr elem))
do
(eval (macroexpand `(brian-def-char-face ,l ,back ,fore)))))
Gives you new faces:
brian-char-face-s, brian-char-face-t, and brian-char-face-u
Now you just need to maintain the list of letter<->color mappings, and possibly extend the macro to support other face properties (if desired).
Related
How can I color entries in ibuffer accoding to the buffer type ?
Based on which mode the buffer is - for example python mode is blue, lisp mode is yellow etc ...
Is this possible ?
Building on the comment from #lawlist, here's some sample code you could use and tweak to your liking:
(setq ibuffer-fontification-alist
'((1 (eq major-mode 'c++-mode) yellow-face)
(1 (eq major-mode 'fundamental-mode) green-face)
(1 (member major-mode '(shell-mode sh-mode)) purple-face)
(1 (eq major-mode 'tcl-mode) brown-face)))
(defface yellow-face '((t :foreground "yellow")) "")
(defface green-face '((t :foreground "green")) "")
(defface purple-face '((t :foreground "black")) "")
(defface brown-face '((t :foreground "brown")) "")
Obviously you can use existing faces, or create new ones (like in this example). See the manual for faces for more information.
I've been using color-theme for quite awhile to set colors in my .emacs:
(defun custom-theme ()
(interactive)
(color-theme-install
'(custom-theme
((background-color . "black")
(background-mode . dark)
(border-color . "#0d1b2b")
(cursor-color . "#fce94f")
(foreground-color . "#dddddd")
(mouse-color . "black"))
(isearch ((t (:inverse-video t :underline t ))))
(lazy-highlight ((t (:inverse-video t :underline t ))))
(hl-line ((t (:background "gray13" ))))
(fringe ((t (:background "#0d1b2b" ))))
(mode-line ((t (:foreground "#bbbbbb" :background "#353e4b"))))
(region ((t (:background "gray13" ))))
(font-lock-builtin-face ((t (:foreground "#9c56dc" ))))
(font-lock-comment-face ((t (:foreground "#187093" ))))
(font-lock-function-name-face ((t (:foreground "#edd400" :underline t ))))
(font-lock-keyword-face ((t (:foreground "#b47113" ))))
(font-lock-string-face ((t (:foreground "#e65351" ))))
(font-lock-type-face ((t (:foreground "#3257d2" ))))
(font-lock-constant-face ((t (:foreground "#cc1e1e" ))))
(font-lock-variable-name-face ((t (:foreground "#cccccc" ))))
(minibuffer-prompt ((t (:foreground "#729fcf" :bold t ))))
(font-lock-warning-face ((t (:foreground "#BB0000" :inverse-video t :underline "#bbbbbb" ))))
)
)
)
(require 'color-theme)
(eval-after-load "color-theme"
'(progn
(color-theme-initialize)
(custom-theme)))
However, in 24.3 now, for some reason when I open emacs in the terminal (emacs -nw), it's ignoring the background and foreground colors, so I get a white editor background regardless. What's more, emacs is ignoring the -bg and -fg flags passed on the command line.
I've found that I can sort of force the colors on frame setup with something like this:
(defun on-after-init ()
(unless (display-graphic-p (selected-frame))
(progn
(set-face-background 'default "black" (selected-frame))
(set-face-foreground 'default "#dddddd" (selected-frame)))))
(add-hook 'window-setup-hook 'on-after-init)
But this feels like a hack, I'd like to understand why emacs isn't interpreting my color theme correctly. My TERM is set to xterm-256color, though setting back to plain xterm doesn't seem to affect anything.
I'm looking for some assistance, please, further modifying the following already modified excerpt from the highlight-parentheses library: https://github.com/nschum/highlight-parentheses.el [Fn 1.]
GOAL: The goal is to use something like mapcar or dolist to automatically replace INSERT-FACE-HERE with a different face from the variable my-parens-faces each time while does a loop. The visual effect will be a rainbow coloring of parentheses based on the level of nesting.
I am removing the overlays with a post-command-hook and a function similar to remove-overlays, and then subsequently adding new overlays with the parens function below. I will not be moving any overlays -- just creating and deleting. The final version will use variables for the faces and target specific overlays for removal, but here is a sample of what it will look like: (add-hook 'post-command-hook (lambda () (remove-overlays) (parens)))
Each time while does a loop, I want to insert a different face from the variable my-parens-faces -- going in order, like dolist. For example:
while doing loop # 1: (:foreground "black" :background "cyan")
while doing loop # 2: (:foreground "blue" :background "purple")
while doing loop # 3: (:foreground "green" :background "blue")
while doing loop # 4: (:foreground "yellow" :background "purple")
while doing loop # 5: (:foreground "orange" :background "yellow")
while doing loop # 6: (:foreground "red" :background "green")
while doing loop # 7: (:foreground "pink" :background "brown")
while doing loop # 8: (:foreground "blue" :background "beige")
(defun parens ()
(let* (pos1 pos2)
(save-excursion
(condition-case err
(while (setq pos1 (cadr (syntax-ppss pos1)))
(overlay-put (make-overlay pos1 (1+ pos1)) 'face 'INSERT-FACE-HERE)
(when (setq pos2 (scan-sexps pos1 1))
(overlay-put (make-overlay (1- pos2) pos2) 'face 'INSERT-FACE-HERE)))
(error nil)) )))
(defvar my-parens-faces '(
(:foreground "black" :background "cyan")
(:foreground "blue" :background "purple")
(:foreground "green" :background "blue")
(:foreground "yellow" :background "purple")
(:foreground "orange" :background "yellow")
(:foreground "red" :background "green")
(:foreground "pink" :background "brown")
(:foreground "blue" :background "beige")))
[Footnote number 1: Reference to the highlight-parentheses library is not needed to answer this question, but the reference is being included so that proper attribute is made to the author (i.e., Nikolaj Schumacher) who inspired the parens function in this question.]
(defvar parens-mode-command-exclusions '(mwheel-scroll scroll-up scroll-down)
"List of functions that are excluded from triggering the function `parens'.")
(defvar parens-mode-syntax-table
(let ((st (make-syntax-table)))
st)
"Syntax table used while executing the function `parens'.")
(defgroup parens nil
"Faces for highlighting parentheses in `parens-mode'."
:group 'parens)
(defface parens-one-face
'((t (:foreground "magenta")))
"Face for `parens-one-face'."
:group 'parens)
(defface parens-two-face
'((t (:foreground "red")))
"Face for `parens-two-face'."
:group 'parens)
(defface parens-three-face
'((t (:foreground "yellow")))
"Face for `parens-three-face'."
:group 'parens)
(defface parens-four-face
'((t (:foreground "green")))
"Face for `parens-four-face'."
:group 'parens)
(defface parens-five-face
'((t (:foreground "cyan")))
"Face for `parens-five-face'."
:group 'parens)
(defface parens-six-face
'((t (:foreground "orange")))
"Face for `parens-six-face'."
:group 'parens)
(defface parens-seven-face
'((t (:foreground "purple")))
"Face for `parens-seven-face'."
:group 'parens)
(defface parens-eight-face
'((t (:foreground "blue")))
"Face for `parens-eight-face'."
:group 'parens)
(defface parens-nine-face
'((t (:foreground "brown")))
"Face for `parens-nine-face'."
:group 'parens)
(defface parens-ten-face
'((t (:foreground "white")))
"Face for `parens-ten-face'."
:group 'parens)
(defvar parens-overlays-exist-p nil
"Simple test to see whether the parens overlays have been placed.")
(make-variable-buffer-local 'parens-overlays-exist-p)
(defun parens ()
"Portions of this function were borrowed from the library
`highlight-parentheses` written by Nikolaj Schumacher.
https://github.com/nschum/highlight-parentheses.el"
(unless (memq this-command parens-mode-command-exclusions)
(with-syntax-table parens-mode-syntax-table
(let* (
(pt (point))
(pos1 (if
(or
(= pt (point-min))
(eq (preceding-char) 40) ;; open-parentheses
(eq (preceding-char) 91) ;; open-squre-bracket
(eq (preceding-char) 123)) ;; open-wavy-bracket
pt
(1- pt)))
pos2
selected-face
(i 0) )
(remove-parens-overlays)
(save-excursion
(condition-case nil
(while (setq pos1 (cadr (syntax-ppss pos1)))
(if (= i 10)
(setq i 1)
(setq i (1+ i)))
(cond
((= i 1)
(setq selected-face 'parens-one-face))
((= i 2)
(setq selected-face 'parens-two-face))
((= i 3)
(setq selected-face 'parens-three-face))
((= i 4)
(setq selected-face 'parens-four-face))
((= i 5)
(setq selected-face 'parens-five-face))
((= i 6)
(setq selected-face 'parens-six-face))
((= i 7)
(setq selected-face 'parens-seven-face))
((= i 8)
(setq selected-face 'parens-eight-face))
((= i 9)
(setq selected-face 'parens-nine-face))
((= i 10)
(setq selected-face 'parens-ten-face)) )
(overlay-put (make-overlay pos1 (1+ pos1)) 'face selected-face)
(when (setq pos2 (scan-sexps pos1 1))
(overlay-put (make-overlay (1- pos2) pos2) 'face selected-face)))
(error nil) ))
(setq parens-overlays-exist-p t)))))
(defun remove-parens-overlays ()
(when parens-overlays-exist-p
(dolist (face '(
parens-one-face
parens-two-face
parens-three-face
parens-four-face
parens-five-face
parens-six-face
parens-seven-face
parens-eight-face
parens-nine-face
parens-ten-face))
(remove-overlays nil nil 'face face))
(setq parens-overlays-exist-p nil)))
(defun turn-off-parens-mode ()
(parens-mode -1))
(define-minor-mode parens-mode
"A minor-mode for highlighting parentheses."
:init-value nil
:lighter " ‹›"
:keymap nil
:global nil
:group 'parens
(cond
(parens-mode
(add-hook 'post-command-hook 'parens t t)
(add-hook 'change-major-mode-hook 'turn-off-parens-mode nil t)
(when (called-interactively-p 'any)
(message "Turned ON `parens-mode`.")))
(t
(remove-hook 'post-command-hook 'parens t)
(remove-hook 'change-major-mode-hook 'turn-off-parens-mode t)
(remove-parens-overlays)
(when (called-interactively-p 'any)
(message "Turned OFF `parens-mode`.")))))
How can I only highlight the * star, not the entire heading line, to keep texts in same color? In the great emacs org-mode
thanks guys
excample (replace * by #, for cann't bold * in stack-overflow)
(not below)
# heading text
this is text body
(but below)
# heading text
this is text body
Updated Answer
See the variable org-level-color-stars-only, which contains a doc-string that states: "Non-nil means fontify only the stars in each headline. When nil, the entire headline is fontified. Changing it requires restart of `font-lock-mode' to become effective also in regions already fontified."
USAGE: (setq org-level-color-stars-only t)
Previous Answer
You can remove or add stars as you see fit -- this example uses two (2) stars together. If you do just one star, then that would also affect two (2) and three (3) stars together. Maybe one of our forum local regexp experts could please give us the code for one star (but not more than one star) :)
(defvar bumble-bee (make-face 'bumble-bee))
(set-face-attribute 'bumble-bee nil :background "black" :foreground "yellow")
(font-lock-add-keywords 'org-mode (list
(list (concat "\\*\\*")
'(0 bumble-bee t))
))
These control the title of tasks -- you could set them all the same or make them different. Anything you don't want, just set to nil or set to the same color as whatever your regular font is.
(custom-set-faces
'(org-level-1 ((t (:foreground "orange" :bold t))))
'(org-level-2 ((t (:foreground "black" :bold t))))
'(org-level-3 ((t (:foreground "pink" :bold t))))
'(org-level-4 ((t (:foreground "cyan" :bold t))))
)
The other components of the first line are usually: org-tag; org-tag-faces; org-todo-keyword-faces; org-priority-faces; and org-warning:
(setq org-todo-keyword-faces '(
("Active" . (:foreground "red"))
("Next Action" . (:foreground "ForestGreen"))
("Reference" . (:foreground "purple"))
("Someday" . (:foreground "gray65"))
("None" . (:foreground "green"))
("Delegated" . (:foreground "cyan")) ))
(setq org-tag-faces '(
("TODO" . org-warning)
))
(setq org-priority-faces '(
(?A . (:foreground "firebrick" :weight bold))
(?B . (:foreground "orange"))
(?C . (:foreground "green"))
(?D . (:foreground "purple"))
(?E . (:foreground "blue")) ))
(custom-set-faces
'(org-tag ((t (:background "gray97" :foreground "gray50"
:box (:line-width 1 :color "black") :weight regular))))
'(org-warning ((t (:foreground "black"))))
)
I have defined a custom comment environment (through \usepackage{verbatim}):
\newenvironment{customComment}[1]
{
\noindent{\textsc{Commented bloc}: #1}}
\comment}
Commented block
{\endcomment}
What I want to do is to highlight the content of the \begin...\end{customComment} either:
as an already existing environment (font-lock-comment-face maybe but haven't found any working example); or
through a complete font/background etc. customization.
Already tried the defvar... solution from here and (for a similar command this time) this example.
Would be great if you found a solution that only needs editing the .emacs or adding a.el` (I've got a load of these customComments).
To the extent that the original poster (or anyone else) may be interested in highlighting the text that appears between opening/closing LaTeX codes, below are a few examples. The active ingredient is a regexp (\\(.\\|\n\\)+?\\), which could be something else that is also similar, but would need to effectively do the same thing. The following examples are set up to highlight text appearing between quotation marks that LaTeX artists frown upon, a bold command, an underline and a double underline using a tex package called ulem, bold and underline, and I threw in a begin/end document example so that the original poster (or anyone else) can combine the examples to make his / her own.
NOTE: As the definitions become more complex and tend to overlap other definitions, the order in which they appear may be critical -- i.e., one definition may trump another.
(font-lock-add-keywords 'latex-mode (list
(list (concat "\\(\"\\)\\(\\(.\\|\n\\)+?\\)\\(\"\\)")
'(1 lawlist-super-orange t)
'(2 lawlist-super-cyan t)
'(4 lawlist-super-orange t))
(list (concat "\\(\{\\)\\(\\\\bf\\)\\(\\(.\\|\n\\)+?\\)\\(\}\\)")
'(1 lawlist-regular t)
'(2 lawlist-purple t)
'(3 lawlist-bold t)
'(5 lawlist-regular t))
(list (concat "\\(\\\\uline\\|\\\\uuline\\)\\(\{\\)\\(\\(.\\|\n\\)+?\\)\\(\}\\)")
'(1 lawlist-green t)
'(2 lawlist-regular t)
'(3 lawlist-underline t)
'(5 lawlist-regular t))
(list (concat "\\(\{\\)\\(\\\\bf\\)\\(\\\\uline\\)\\(\{\\)\\(\\(.\\|\n\\)+?\\)\\(\}\\)\\(\}\\)")
'(1 lawlist-regular t)
'(2 lawlist-red t)
'(3 lawlist-blue t)
'(4 lawlist-regular t)
'(5 lawlist-bold-underline t)
'(7 lawlist-regular t)
'(8 lawlist-regular t))
(list (concat "\\(\\\\begin\\|\\\\end\\)\\(\{\\)\\(document\\)\\(\}\\)")
'(1 lawlist-super-orange t)
'(2 lawlist-super-SeaGreen t)
'(3 lawlist-super-HotPink1 t)
'(4 lawlist-super-SeaGreen t))
))
(defvar lawlist-regular (make-face 'lawlist-regular))
(set-face-attribute 'lawlist-regular nil
:background "white" :foreground "black")
(defvar lawlist-bold (make-face 'lawlist-bold))
(set-face-attribute 'lawlist-bold nil
:background "white" :foreground "black" :bold t)
(defvar lawlist-bold-underline (make-face 'lawlist-bold-underline))
(set-face-attribute 'lawlist-bold-underline nil
:background "white" :foreground "black" :bold t :underline "black")
(defvar lawlist-underline (make-face 'lawlist-underline))
(set-face-attribute 'lawlist-underline nil
:background "white" :foreground "black" :underline "black")
(defvar lawlist-bumble-bee (make-face 'lawlist-bumble-bee))
(set-face-attribute 'lawlist-bumble-bee nil
:background "black" :foreground "yellow" :bold t :underline "red")
(defvar lawlist-red (make-face 'lawlist-red))
(set-face-attribute 'lawlist-red nil
:background "white" :foreground "red" :bold t)
(defvar lawlist-blue (make-face 'lawlist-blue))
(set-face-attribute 'lawlist-blue nil
:background "white" :foreground "blue" :bold t)
(defvar lawlist-green (make-face 'lawlist-green))
(set-face-attribute 'lawlist-green nil
:background "white" :foreground "green3" :bold t)
(defvar lawlist-orange (make-face 'lawlist-orange))
(set-face-attribute 'lawlist-orange nil
:background "white" :foreground "orange" :bold t)
(defvar lawlist-purple (make-face 'lawlist-purple))
(set-face-attribute 'lawlist-purple nil
:background "white" :foreground "purple" :bold t)
(defvar lawlist-pink (make-face 'lawlist-pink))
(set-face-attribute 'lawlist-pink nil
:background "white" :foreground "pink" :bold t)
(defvar lawlist-super-orange (make-face 'lawlist-super-orange))
(set-face-attribute 'lawlist-super-orange nil
:background "white" :foreground "orange" :bold t :underline nil)
(defvar lawlist-super-cyan (make-face 'lawlist-super-cyan))
(set-face-attribute 'lawlist-super-cyan nil
:background "white" :foreground "cyan" :bold t :underline nil)
(defvar lawlist-super-blue (make-face 'lawlist-super-blue))
(set-face-attribute 'lawlist-super-blue nil
:background "white" :foreground "blue" :bold t :underline nil)
(defvar lawlist-super-red (make-face 'lawlist-super-red))
(set-face-attribute 'lawlist-super-red nil
:background "white" :foreground "red" :bold t :underline nil)
(defvar lawlist-super-purple (make-face 'lawlist-super-purple))
(set-face-attribute 'lawlist-super-purple nil
:background "white" :foreground "purple" :bold t :underline nil)
(defvar lawlist-super-HotPink1 (make-face 'lawlist-super-HotPink1))
(set-face-attribute 'lawlist-super-HotPink1 nil
:background "white" :foreground "HotPink1" :bold t :underline nil)
(defvar lawlist-super-SeaGreen (make-face 'lawlist-super-SeaGreen))
(set-face-attribute 'lawlist-super-SeaGreen nil
:background "white" :foreground "SeaGreen" :bold t :underline nil)
In the tex-mode that's built into Emacs you could do that by adding your environment to tex-verbatim-environments.