EMACS: Auto run a function when open a new buffer - emacs

How can I auto run a function when open a new buffer, say text-scale-adjust.

A slightly more detailed version of abo-abo's answer:
(add-hook 'find-file-hook
'(lambda () (text-scale-adjust 1)))
This will run for all files you visit. You can look at mode-specific hooks if you want to tailor which types of files/buffers get this treatment.

You could take a look at these hooks, as well:
after-change-major-mode-hook
change-major-mode-hook
change-majory-mode-after-body-hook
first-change-hook
window-configuration-change-hook
But can you clarify what the aim really is?
If you just want to make all buffers display with larger or smaller text, then it is enough to do one of the following:
Choose from the menu bar: Options > Set Default Font....
Put this in your init file: (set-frame-font "THE FONT YOU WANT, WITH THE PROPER SIZE" nil t)
Set the default font size for all frames to what you want, at the outset, simply by customizing default-frame-alist wrt frame parameter font.
IOW, set the font size for your frames, rather than scaling the text of all buffers.

Related

emacs - startup window size changed from default to maximum

I use below code to setup emacs using fullscreen:
(setq initial-frame-alist '( (fullscreen . maximized)))
(setq default-frame-alist '( (fullscreen . fullheight)))
I wish it startup with maximized window, the behavior is almost as expected but the window size firstly with default size, then I can see it changed to maximum size obviously.
How can emacs startup to fullscreen in one step?
Look at the documentation for initial-frame-alist (C-h v initial-frame-alist). In particular, Emacs creates the first frame before reading init.el, so you need to specify these things somewhere other than init.el. Two common ways are to give the --maximized command line option, or to set the X11 resource Emacs.Fullscreen: maximized; either of these are applied before init.el is read and evaluated.
It looks like you're on OS X, which I don't have, and it may be different there.
Update for Emacs 27:
You can set these things in early-init.el, which loads before the GUI starts.

Altering the font size for the Emacs minibuffer separately from default emacs?

I've been attempting to alter the font / face for the emacs minibuffer separately from emacs default fonts, but without much luck.
Specifically, I'm interested in making the minibuffer font size larger for use with the emacs MULE as, with my current font setting or if I'm using emacs on a "netbook" screen, sometimes the character selection options in the MULE are a bit small.
Options easily accessed within emacs are the minibuffer-prompt & minibuffer-prompt-properties, but these are only for command prompts and not the regular minibuffer text.
There seem to be a number of minibuffer variables listed in emacs for creating minibuffer frames, or getting contents from minibuffer windows, etc.. but these do not pertain to altering the minibuffer face. Is it even possible to alter the minibuffer face separately from the default emacs?
An interesting option is the oneonone emacs http://www.emacswiki.org/emacs/OneOnOneEmacs project. But could the dedicated minibuffer frame be altered? Also before I alter my current emacs set-up that drastically, I'd hope to be able to just alter fonts first or create my own alterable minibuffer frame, etc...
Any help and/or creative ideas would be greatly appreciated.
You can add customization to the minibuffer through the minibuffer-setup-hook. In there, you can do some face remapping like so:
(add-hook 'minibuffer-setup-hook 'my-minibuffer-setup)
(defun my-minibuffer-setup ()
(set (make-local-variable 'face-remapping-alist)
'((default :height 2.0))))
Change the body of the my-minibuffer-setup as desired. The above doubles the height of the default face.
Yes, you can easily customize the properties of a standalone minibuffer frame, including its default face and font.
You can customize the OneOnOneEmacs user option 1on1-minibuffer-frame-alist. (Or you can customize the standard option minibuffer-frame-alist -- its frame parameter values are used as defaults by 1on1-minibuffer-frame-alist.)
The font frame parameter is the one that controls the font (duh). So you would customize option 1on1-minibuffer-frame-alist, changing its setting for the font.
Alternatively, you can just set 1on1-minibuffer-frame-font to the font you want -- it is used as the default value for the font setting by 1on1-minibuffer-frame-alist whenever there is no explicit font setting in minibuffer-frame-alist. For example:
(setq 1on1-minibuffer-frame-font
"-*-Lucida Console-normal-r-*-*-14-112-96-96-c-*-iso8859-1")
If you do not want to use a standalone minibuffer frame then see Trey's answer.

Cant apply color theme to one frame in Emacs?

My .emacs file is here. I want the theme to change when I am in shell-mode. But what happens is that the theme gets applied to all the windows. I set the variable color-theme-is-global to nil, but the problem still persists.
(add-hook 'shell-mode-hook 'color-theme-monokai-terminal)
(set-variable 'color-theme-is-global nil)
These are the corresponding lines in my .emacs file. What should I do to make it work?
I usually start Emacs as a daemon and then open frames as needed. I use different color themes for X frames and terminal frames like so:
(require 'color-theme)
(color-theme-initialize)
(defun apply-color-theme (frame)
"Apply color theme to a frame based on whether its a 'real'
window or a console window."
(select-frame frame)
(if (window-system frame)
(color-theme-tango)
(color-theme-tango-black)))
(setq color-theme-is-global nil)
(add-hook 'after-make-frame-functions 'apply-color-theme)
You can replace the (if window-system ...) part with your check for shell-script-mode and the color-theme-X parts with your favorite themes.
There is one downside to this: if you don't start Emacs as a deamon, the customization will only kick in after you create a second frame, the first one that pops up will have the standard theme.
I think your terminology is off: in emacs-speak frame means what people normally mean by window in a graphical environment. (That is, the thing that has the close, minimize and maximize buttons and a titlebar, etc, is the "frame".) Whereas the things that show up when you do a C-x 3 (split-window) are called windows, and when you do something like M-x shell-mode you get a new buffer, which may or may not be in a new window.
Color themes are always frame-global (as far as I know, and it's certainly what the documentation suggests) the variable color-theme-is-global determines whether a single theme propagates across frames.
I'm thinking that the closest thing to what you want is something like (completely untested, probably broken):
(defun shell-mode-in-new-frame ()
(interactive)
(select-frame (make-frame))
(color-theme-monokai-terminal)
(shell-mode))
Although this does create a new frame, which isn't what you want.

custom-theme-set-faces compatible with tty

I have created an emacs-23 custom theme using customize-create-theme. It works fine under X (Linux gnome desktop). However, when running under a tty (within gnome-terminal) some of the colors are wrong.
It is not the accuracy of the colors which are a problem (although it would be nice to match them under both situations) but the fact that some are so off as to be unworkable. For example, function names which appear green under X are invisible under the tty, although keywords which appear gold under X also appear gold (or at least some kind of yellow) under the tty.
Perhaps under the tty colors can't be matched exactly and so something similar is being substituted? If so, this doesn't seem to work all the time.
How can I fix this? Is it possible to specify, either in the 'customize' GUI or in the ~/.emacs.d/my-theme.el file, that certain faces only apply to frames displayed on X and others are only for the tty, or something similar?
(I'm interested in getting this, the built-in emacs theming system working rather than using some external color theme system.)
If a color is unavailable on a frame, emacs should try and pick something "close", but that's often very wrong on limited color displays. You should ask emacs how many colors it thinks it has in gnome-terminal either using M-x list-colors-display (to actually view the colors) or run (display-color-cells) in the scratch buffer. If it says you only have 8, you might want to consider changing your TERM environment variable to something like xterm-256color before you start emacs (though I'm not sure how well this actually works in gnome-terminal; I use xterm).
So that might help emacs be able to find a color that's closer, but if it's still wrong, you'll want to do something more drastic, like set the colors based on the window system.
If you're not using daemon mode, you can use something like
(if window-system (set-face-foreground 'font-lock-function-name-face "LightSkyBlue"))
If you use M-x describe-face, it will ask which face you want to describe, defaulting to the one currently at point. You can get the name (and usually the color) from there.
If you are using daemon mode, then you'll want different colors for each frame, in which case you'll need to set the color for the frame in the new frame hook, something more like:
(defun set-new-frame-colors (frame)
"Set colors based on frame type."
(if (window-system frame)
(set-face-forgeground 'font-lock-function-name-face "LightSkyBlue" frame)
(set-face-forgeground 'font-lock-function-name-face "blue" frame)))
(add-hook 'after-make-frame-functions 'set-new-frame-colors)
Alternatively, instead of checking (window-system frame), you could check (length (defined-colors frame)) and base it on how many colors are supported by the system, so that you can have different colors for 8-color vs. 256-color terminals.
You can tell whether or not the current frame is associated with a graphical window by examining the variable window-system. The link has the documentation, but it looks like:
window-system is a variable defined in `C source code'.
Its value is nil
Documentation:
Name of window system through which the selected frame is displayed.
The value is a symbol--for instance, `x' for X windows.
The value is nil if the selected frame is on a text-only-terminal.
So, you can wrap the current theme inside an
(if window-system
;; current theme configuration
)
and then when in an xterm, create a new one that you like, and put that in the else (or another if statement, or unless and when)

emacs 23 and font size

I am using emacs 23 on my Ubuntu netbook edition. Every app, automatically goes to fullscreen (so does my emacs). But depending on the font size (:height), I get a smaller working window.
If I go to :height normal I get the full area but the fonts are HUGE!
any ideas?
Your window manager is broken. emacs resizes itself when you change the font size (this happens during startup). Your window manager should tell emacs that emacs was resized by the window manager, at which point everything will work normally.
Anyway, start emacs as "emacs --daemon" and connect with "emacsclient -c" and you should not notice this problem.
OK, so actually I added this to my initfile:
(add-hook 'after-make-frame-functions
(lambda (frame)
(progn
(add-to-list 'default-frame-alist
(cons 'height
(/ (x-display-pixel-height)
(frame-char-height)))
(add-to-list 'default-frame-alist
(cons 'width
(/ (x-display-pixel-width)
(frame-char-width))))))))
and now the window is the same size as the full screen. If you are setting your fonts inside of the after-make-frame-functions hook then it is important that this comes first in your initfile, (I think because hooks are run in reverse order) but if you're just setting the fonts then this should work OK anywhere.
Of course for maximum safety you could put this and your 'set fonts' stuff into the same defun, with this coming after the fonts have been set.
EDIT:
This is a slightly more forceful way to do it, in case that doesn't work.
This gives me some issues though, really you would probably want to subtract the height of the top panel from the height you're setting it to.
(add-hook 'after-make-frame-functions
(lambda (frame)
(progn
(set-frame-height frame
(/ (x-display-pixel-height)
(frame-char-height)))
(set-frame-width frame
(/ (x-display-pixel-width)
(frame-char-width))))))
The font size issue can be fixed simply by selecting a different size font as the default font (Options->Set Default Font), then saving the options (Options->Save Options). Emacs seems to have issues with font point sizes matching system sizes (there's discussion about X standard DPIs versus GTK standard DPIs), but if you select one that works it will stay the same.
To get the window to come up maximized correctly, I've found there's an issue with the frame alist not accounting for the minibuffer correctly as well as the different font sizes not triggering the frame to resize correctly. If you set the initial-frame-alist to include (fullscreen . fullwidth) and (minibuffer-lines . 1) it accounts for the minibuffer size correctly and comes up with the correct width, forcing an effect as if you resized the window by hand to the maximum visible area (not quite the same as maximizing). You can set these via Options->Customize Emacs->Settings Matching Regexp... Then typing initial-frame-alist. Set two new parameters and values, "minibuffer-lines" to "1" and "fullscreen" to "fullwidth".
Removing the "minibuffer-lines" parameter will give you a full screen width window that's the wrong height, and removing the "fullscreen" parameter means nothing resizes correctly. Attempting to set "fullscreen" to "fullscreen" gives the same issue as setting nothing, and "fullscreen" to "fullheight" gives blank space only across the height and not the width when a smaller font size is used.