Are there any standard techniques, packages or the like for managing font settings in emacs?
I would like to define default fixed and variable width fonts somewhere once, and set the fonts of faces in modes using those defaults. Eg, set org-mode's default font to the variable width default, and its code face to fixed the fixed width default.
Here's an adaptation of the system that I have. I've since defaulted to using just fixed pitch, but these calls will allow you to define the font to use for modes using the mode hook.
I call the method 'toggle-pitch to switch between fixed and variable pitch fonts on the fly when necessary as well.
(set-face-font 'default "Source Code Pro Semibold-9")
(set-face-font 'variable-pitch "Segoe UI Semibold-9")
(copy-face 'default 'fixed-pitch)
;;============================================================
;; toggle between variable pitch and fixed pitch font for
;; the current buffer
(defun fixed-pitch-mode ()
(buffer-face-mode -1))
(defun variable-pitch-mode ()
(buffer-face-mode t))
(defun toggle-pitch (&optional arg)
"Switch between the `fixed-pitch' face and the `variable-pitch' face"
(interactive)
(buffer-face-toggle 'variable-pitch))
;; enable buffer-face mode to provide buffer-local fonts
(buffer-face-mode)
;; Set the fonts to format correctly
;(add-hook 'text-mode-hook 'fixed-pitch-mode)
;(add-hook 'dired-mode-hook 'fixed-pitch-mode)
;(add-hook 'calendar-mode-hook 'fixed-pitch-mode)
;(add-hook 'org-agenda-mode-hook 'fixed-pitch-mode)
;(add-hook 'shell-mode-hook 'fixed-pitch-mode)
;(add-hook 'eshell-mode-hook 'fixed-pitch-mode)
;(add-hook 'bs-mode-hook 'fixed-pitch-mode)
;(add-hook 'w3m-mode-hook 'variable-pitch-mode)
;(add-hook 'org-mode-hook 'variable-pitch-mode)
(add-hook 'eww-mode-hook 'variable-pitch-mode)
A really useful way to set faces is to use the M-x list-faces-display function. This function will pop up a buffer with the face name on the left and an example on the right. This buffer then lets you customize any of the faces and to see what the face definition is. The one possible 'gotcha' is that it will only display the faces it knows about at the time you run the function. This means that if you haven't loaded org-mode for example, you may not see any org-mode face definitions.
One of the useful options when defining a face is the inherit option, which tells the face to inherit the settings of another face. Some of the inherited values can be overridden (such as face colour) in the 'main' face definition.
So, in your case, I would set the appropriate values for the default fixed and variable width fonts, then set the other faces i.e. org-mode faces to inherit from those definitions.
doing it this way shold eliminate the need to do any add-hook jiggery pokery
I finally solved it like this:
First set fixed-pitch and variable-pitch fonts for use in general:
(custom-set-faces
'(fixed-pitch ((t (:family "Input Mono Narrow"))))
'(variable-pitch ((t (:family "Arial")))))
Then in the hook for your mode:
(add-hook 'org-mode-hook nil :inherit 'variable-pitch)
Related
hi: Each time I insert some text in emacs , it will highlight the newly added text. I wonder how to change the background color of the highlight, because the highlight background color is very close to the font color , as a result, I can hardly recognize the code that I am writing.
thank you soooo much
For issues with fonts (which Emacs calls faces) inside of Emacs, it is often helpfull to know the function 'list-faces-display'. You can call this with M-x and it will list all faces defined in the current frame. This can be helpfull both identifying which face is problematic, it will also give you its name which can be used to modify the face. For instance to change the foreground colour of the face named "button" you can call something like this:
(set-face-foreground 'button "cyan")
The effect will be immediately visible. Many aspects of faces can be changed, including colour, font familiy and font size.
Obviously, this will not help you if the problematic behaviour stems from the terminal emulator you are using, as it would appear from some of the comments to your question, then the problem is outside of Emacs and cannot be fixed from inside of Emacs. Even so, knowing about 'list-faces-display' is usefull.
I had this exact question and managed to solve it using the following ways. But I also had another thing in mind: a marker to show which lines are modified.
For tracking changes between the saved file and the buffer, we should use the highlight-changes-mode. But before enabling that mode, we need to prepare some stuff as explained beautifully here for the line marks:
;; a part to add the fringe marks to the gutter. To change the shape, read the explanation of it in this code.
(eval-after-load "hilit-chg"
'(progn
(defvar highlight-fringe-mark 'filled-square
"The fringe bitmap name marked at changed line.
Should be selected from `fringe-bitmaps'.")
(defadvice hilit-chg-make-ov (after hilit-chg-add-fringe activate)
(mapc (lambda (ov)
(if (overlay-get ov 'hilit-chg)
(let ((fringe-anchor (make-string 1 ?x)))
(put-text-property 0 1 'display
(list 'left-fringe highlight-fringe-mark)
fringe-anchor)
(overlay-put ov 'before-string fringe-anchor))
))
(overlays-at (ad-get-arg 1))))))
;; make the highlight-changes-mode reset when the file is saved
(add-hook 'local-write-file-hooks 'highlight-changes-rotate-faces)
(add-hook 'after-save-hook
(lambda ()
(when highlight-changes-mode
(save-restriction
(widen)
(highlight-changes-remove-highlight (point-min) (point-max))))))
make sure it is enabled globally except for buffers that start with ' and *
(setq highlight-changes-global-modes t)
(global-highlight-changes-mode)
make the mode to respect the syntax-highlighting
;; find the name of other faced using M-x ~list-faces-display~
(custom-set-faces
'(highlight-changes ((t (:background "dark green" :foreground nil)))))
(set-face-foreground 'highlight-changes nil)
(set-face-background 'highlight-changes "dark green")
I'd like to have emacs not to have a background color when I open a frame in the terminal. I'm using a terminal with a translucent background, and characters with a background color are not "see-through". TERM is set to "xterm-256color".
How do I get emacs to use the default background color (no color at all), when the frame is not graphical?
Edit:
I've got it, sort of:
(add-to-list 'custom-theme-load-path "~/.emacs.d/themes")
(load-theme 'my-awesome-theme t)
(defun on-frame-open (frame)
(if (not (display-graphic-p frame))
(set-face-background 'default "unspecified-bg" frame)))
(on-frame-open (selected-frame))
(add-hook 'after-make-frame-functions 'on-frame-open)
I put the above code in my init file, but only suppresses the background when opening an emacsclient in a terminal, and not emacs itself (i.e. only when invoked with emacsclient -t and not when invoked with emacs). Adding an extra (unless window-system (set-face-background 'default "unspecified-bg" (selected-frame))) doesn't work and only confuses graphical frames.
Any ideas on why this might happen?
(defun on-after-init ()
(unless (display-graphic-p (selected-frame))
(set-face-background 'default "unspecified-bg" (selected-frame))))
(add-hook 'window-setup-hook 'on-after-init)
Combined with the code in your edit, it worked nicely for me for both emacsterms and newly started emacsen. As for why window-setup-hook:
http://www.gnu.org/software/emacs/manual/html_node/elisp/Startup-Summary.html
(neither of the earlier hooks seemed to work except for this one.)
I tried the method that was suggested in this answer but I had no luck getting it to work. this snippet works for me though
(defun on-frame-open (&optional frame)
"If the FRAME created in terminal don't load background color."
(unless (display-graphic-p frame)
(set-face-background 'default "unspecified-bg" frame)))
(add-hook 'after-make-frame-functions 'on-frame-open)
Although it has a setback, if the terminal has a different background settings than the theme I use (dark vs. light), default theme faces are being used which may not seem good on the light or dark background. but in my case which both terminal and theme are dark it works fine.
There are already two answers to this question, one using window-setup-hook, which is called on startup, and another using after-make-frame-functions, which is called when a new frame is made, including after invoking emacsclient. To cover all possible cases, I found that I needed to do it this way:
(defun set-background-for-terminal (&optional frame)
(or frame (setq frame (selected-frame)))
"unsets the background color in terminal mode"
(unless (display-graphic-p frame)
(set-face-background 'default "unspecified-bg" frame)))
(add-hook 'after-make-frame-functions 'set-background-for-terminal)
(add-hook 'window-setup-hook 'set-background-for-terminal)
Note that I am only using selected-frame if necessary; it seems that in client mode, the hook is called before the frame is selected, so it is important to use the frame argument in that case.
For all my tasks I use URW Chancery L font in Emacs. But for some tasks,
like org-mode tables, shell or sunrise-commander, I would like to set mono-width font.
So, my question, how can I do it? All I found about it is set-default-font, which is not what I want.
Faces (i.e. the objects used to specify appearence of text such as font, color, ...) are mostly global in Emacs, although they can also be set on a frame basis, so you can do the above by creating a separate frame and change the `default' face to use in that frame.
This said, Emacs can also now also change face's appearence for specific buffers via face-remapping. E.g.
(face-remap-add-relative 'default '(:family "Monospace"))
should make the current buffer use Monospace. So adding the above to org-mode-hook might just solve your problem.
This snippet sets the "Arial" font family only in C mode:
(defun set-my-font ()
(overlay-put (make-overlay (point-min) (point-max) nil nil t)
'face '(:family "Monospace")))
(add-hook 'org-mode-hook 'set-my-font)
Replace with org-mode-hook with the desired mode(s), and it should work as well.
This solution effects creation of buffer-local font by setting the font family property of an overlay over the entire buffer. The overlay's face property only specifies the font family (Monospace), and Emacs redisplay is smart enough to merge it with other text properties, such as the colors specified by font-lock.
Did you try to customize org-table ?
You can modify it with org-menu > Customize > Customize > org-table
or use the command line
M-x set-face-font RET org-table RET -PfEd-DejaVu Sans Mono-normal-normal-normal-*-*-*-*-*-m-0-iso10646-1
Use tab to auto-complete and see other available fonts
Finaly you could also directly modify you init.el to have something like
(custom-set-faces
'(org-table ((t (:foreground "LightSkyBlue" :family "DejaVu Sans Mono")))))
I have the following code in my .emacs:
(if (null window-system)
(progn
(require 'color-theme)
(color-theme-initialize)
(color-theme-simple-1)))
When I open Emacs on the console, I can verify that the progn block runs (by a (message "Got here.")), and I see a flash that suggests that the color theme was loaded, but if it was loaded, it is overridden by something else. If, after loading, I open my .emacs file and submit the block above using C-x C-e, it works. I've tried doing:
(add-hook 'after-init-hook
(lambda ()
(progn
(require 'color-theme)
(color-theme-initialize)
(color-theme-simple-1))))
but that acts the same.
It may be relevant that I'm using Emacs 24, and that this code is not in my .emacs, but in ~/Dropbox/.emacs, which is loaded from my .emacs.
An additional note: I've tried M-x customize-themes, but none of those work acceptably on the console. They either produce a nearly unreadable light theme, or most of the text is invisible.
Emacs 24 has built-in theming, which doesn't use statements like (require 'color-theme). As Drew points out in the comments, there are differences between color themes and custom themes, and the new direction is towards the latter. Try M-x customize-themes to take a look. From .emacs, you can do things like (load-theme 'wombat t).
But...
It may still be going wrong for you. One thing that can mess it up like this is changing the face -- maybe in the custom-set-faces part of your .emacs file. Emacs's interactive customization automatically includes the color information (both background and foreground) of whatever theme you happen to be using at the time you set it, so this can definitely make trouble with color themes. If that is what's causing it, you can just set the particular attribute you care about with something like
(set-face-attribute 'default nil :height 120)
That will change the font size without changing the colors.
Emacs 24 have own theming system.
M-x customize-themes
or
(custom-set-variables
....
'(custom-enabled-themes (quote (selected-theme)))
)
I have a line in my .emacs which set a default font:
(set-default-font "Monaco-10")
It works fine for me, but I need two exceptions:
I need to change default font for a one file, for example ~/some. How can I do it?
I need to change default fonr for a gnux (M-x gnus). How can I achieve it?
Take a look at the variable `face-remapping-alist'. For example, you can have something like this:
(add-hook 'find-file-hook
(lambda ()
(if (equal "~/some" (abbreviate-file-name (buffer-file-name)))
(set (make-local-variable 'face-remapping-alist)
'((default :family "DejaVu Serif"))))))
In Emacs files are represented in buffers - you don't change the font of the file, but that of the buffer. Shift-LeftClick in a buffer and you'll get a font dialog. In it you should select:
Change Buffer Font...
You might take a look at the code this command is bound to and use it for your needs.