How can I set styling and positioning of a newly created emacsclient? - emacs

I've recently switched to emacsclient for most text editing. I am trying to migrate some of my settings to the new (and slightly different) environment.
In particular, in my .emacs file I have a function that sets the window size, and prepares some themes. However code in the .emacs file is not executed on each invocation of emacsclient, so the settings do not apply to these windows. Based on the question here, I added a hook to 'server-visit-hook that called a function which executed my settings. However, the settings are not being applied when I restart the server and invoke emacsclient.
How can set the styling and positioning of new emacsclient windows? Relevant portions of my .emacs are included below:
(defun gui-mode()
(set-face-attribute 'default nil :font "Monospace-8")
(require 'theme-wombat)
(set-frame-size-according-to-resolution))
(add-hook 'server-visit-hook 'gui-mode)
(when window-system
(gui-mode))

Start emacsclient with the -e option, and use that to tell it to load your configs:
emacsclient -c -e '(load "~/.emacsclient")'
where ~/.emacsclient contains your configuration code. You probably want to define an alias or menu option so that you don't actually type that in every time you call emacsclient.

(add-to-list 'default-frame-alist '(fullscreen . fullboth))
in .emacs does the job.

Related

Add hook to default mode when using emacs -q -l

I have been loading emacs with emacs -q -l "init.el" quite a bit and was trying to enable auto-complete in my scratch buffer. I was struggling to figure out why it wasn't working but realized it must have to do with the order of operations when emacs is loaded like this - a quick test with the following init file:
(package-initialize)
(require 'auto-complete)
(ac-config-default)
(add-hook 'lisp-interaction-mode-hook
'(lambda ()
(auto-complete-mode t)))
shows completion working as I would like when calling emacs as normal from the command line. But if I call it as emacs -q -l init.el there is no dropdown completion.
Question: How can I get this hook to run?
I've tried variations on after-init-hook but none seem to work.
The following analysis is based on startup.el of the master branch: https://github.com/emacs-mirror/emacs/blob/master/lisp/startup.el
As I understand the question, it seeks an answer as to when the command line option -l aka --load FILE is run as compared to when the *scratch* buffer is initialized with the initial-major-mode that by default is lisp-interaction-mode.
Based on the sequence of events defined within startup.el, the -l or --load options are taken into consideration at line 2381 of the function command-line-1.
The function command-line-1 runs at line 1366 of startup.el, which is subsequent to the after-init-hook at line 1344 and subsequent to the *scratch* buffer being initialized with the initial-major-mode at line 1350.
To the extent that the original poster would like to rely upon loading a file manually using the -l or --load option, then functions being assigned to the lisp-interaction-mode-hook will not be seen at line 1350 because they do not exist until command-line-1 runs at line 1366. One option the original poster may wish to consider would be the following: (with-current-buffer "*scratch*" (lisp-interaction-mode)) after the auto-complete-mode has been added to the lisp-interaction-mode-hook.
If starting Emacs from the command line, you could invoke the function to run near the end of the loading sequence, as follows:
emacs -f lisp-interaction-mode
See Action Arguments in the manual:
‘-f function’
‘--funcall=function’
Call Lisp function function. If it is an interactive function (a command), it reads the arguments interactively just as if you had called the same function with a key sequence. Otherwise, it calls the function with no arguments.

Enabling whitespace mode on emacs

I'm trying to enable whitespace mode but it doesn't seem to be working for me.
I've added the whitespace.el file to my .emacs.d/ directory.
I added the following lines to me .emacs file:
(add-hook 'before-save-hook 'whitespace-cleanup)
(add-hook 'before-save-hook (lambda() (delete-trailing-whitespace)))
(require 'whitespace)
I did the following in the whitespace.el file:
M-x byte-compile-file <path to whitespace.el>
I tried executing the following command from any random text in emacs (e.g.: test.c):
M-x whitespace-toggle-options RET
But I just get a message saying [No match]
What am I missing?
Also, will I have to type in a command to enable the whitespace-mode every time I use emacs?
whitespace.el has been included in Emacs for quite a while. Unless you have a very old version, you shouldn't need to manually put it anywhere, or do anything particularly special to use it.
whitespace-toggle-options probably isn't the function you want to use. Instead, try whitespace-mode:
Toggle whitespace visualization (Whitespace mode).
With a prefix argument ARG, enable Whitespace mode if ARG is
positive, and disable it otherwise. If called from Lisp, enable
the mode if ARG is omitted or nil.
If you want to enable it by default, add
(global-whitespace-mode)
to your init file.

py-python-command ignored

I'm using python-mode 6.0.1 on OS X, emacs 23.3 (http://emacsformacosx.com/ version).
I'm trying to get C-c C-c to default to python 3.
I have the following in my .emacs:
(setq py-python-command "/usr/local/bin/python3")
And when I run C-h b py-python-command, it tells me the value is that (correctly).
However, running C-c C-c still opens 2.7.2.
I also tried adding:
(setq py-which-shell "/usr/local/bin/python3")
as suggested here: Both Python 2 and 3 in Emacs, but that doesn't change anything (py-which-shell does get changed, but it still launches 2.7.2).
Any ideas?
Try adding the following code to your Emacs init file:
(add-hook 'python-mode-hook
(lambda ()
(setq py-python-command "python3")
(setq py-default-interpreter "python3")))
py-default-interpreter for now is an alias only, delivered for backward compatibility
You might have encountered a bug.
Please file a report giving some example code at
https://bugs.launchpad.net/python-mode
Should the buffer code contain a shebang specifying pythonVERSION , than this takes precedence over default setting.
You may enforce executing buffer through specific pythonVERSION by calling
a command of class py-execute-buffer-pythonVERSION
See menu PyExec, entry Execute buffer ...

P4CONFIG with emacs

I would like to see examples of how to setup perforce, using the config file functionality where emacs is used as the diff and merge programs (P4DIFF and P4MERGE settings). Even better if this is on Windows.
I'm also struggling with getting the P4EDITOR to work correctly when using emacsclientw, specifically specifying the alternate-editor functionality.
Any tips, suggestions, example configs are very welcome.
Here's a different trick I used to use. It adds a few command line options to emacs so that you can do diffs and merges in a new emacs instance (again using ediff).
;; -diff
(defun command-line-diff (switch)
(let ((file1 (pop command-line-args-left))
(file2 (pop command-line-args-left)))
(ediff file1 file2)))
(add-to-list 'command-switch-alist '("-diff" . command-line-diff))
;; -merge
(defun command-line-merge (switch)
(let ((base (pop command-line-args-left))
(sccs (pop command-line-args-left))
(mine (pop command-line-args-left))
(merg (pop command-line-args-left)))
(ediff-merge-with-ancestor sccs mine base () merg)))
(add-to-list 'command-switch-alist '("-merge" . command-line-merge))
Just put that in your .emacs file. Then you can set your P4DIFF program to be emacs -diff and your P4MERGE program to be emacs -merge.
I'm assuming you're already using p4.el.
Here's a function that will allow you to set your p4-client-config easily:
(defun p4-go (config)
(interactive
(list (read-file-name "P4 Config file: "
(concat (getenv "HOME") "/etc/perforce/")
""
t)))
(p4-set-client-config (expand-file-name config))
t)
Then I just run M-x p4-go <RET> conf <RET>.
My ~/etc/perforce/conf file looks like:
P4CLIENT=ewarmenhoven-ppd
P4PORT=perforce.netflix.com:1666
P4USER=ewarmenhoven
P4EDITOR=emacsclient
P4DIFF=diff -dupU8
P4MERGE=~/bin/emerge
The emerge merge program is just a short little shell script that calls emacsclient appropriately:
#!/bin/bash
base=$1
sccs=$2
mine=$3
merg=$4
emacsclient -e "(ediff-merge-files-with-ancestor \"$base\" \"$sccs\" \"$mine\" () \"$merg\")"
emacsclient "$merg"
If you're using cygwin it should work just fine.
For doing diffs, if it's running from the shell then I want the output in the shell, hence just using normal diff. If it's not, I use p4-ediff, which is bound to C-x p - by default.
The awesome answer by Eric doesn't work properly in latest emacs because of welcome screen. In order to hide the welcome screen (so that you may get the diff properly) please refer Unable to hide welcome screen in Emacs.
Another nifty setting which opens the diff in regular vertical mode is setting the below config variable
(custom-set-variables
;; custom-set-variables was added by Custom -- don't edit or cut/paste it!
;; Your init file should contain only one such instance.
'(ediff-split-window-function (quote split-window-horizontally)))

How do I get Emacs shell mode to either render (or ignore) my colors instead of printing ASCII codes?

The symptom of the problem looks like "[0m[27m[24m[J[34;1" which on a terminal translates into the color blue.
-A
I've got the following in my .emacs
(add-hook 'shell-mode-hook 'ansi-color-for-comint-mode-on)
The solution that is currently giving me some success is to redefine the shell function as an ansi term:
;; shell-mode
(defun sh ()
(interactive)
(ansi-term "/bin/zsh"))
For the "ignore" alternative, put something like "alias ls=ls" or "unset LS_COLORS" in your ~/.emacs_{bash,tsch,whatever-your-shell-is-called} file. This file is executed in all subordinate shells created by emacs.
Emacs sends the new shell the contents of the file ~/.emacs_shellname as input, if it exists, where shellname is the name of the file that the shell was loaded from. For example, if you use bash, the file sent to it is ~/.emacs_bash. If this file is not found, Emacs tries to fallback on ~/.emacs.d/init_shellname.sh.
The following should work in your .bash_profile or .bashrc
case $TERM in
xterm-color)
export PS1='\[\e]0;\W\007\]\[\e[34;1m\]\W\[\e[0m\]\$ '
;;
*)
export PS1='\W\$ '
;;
esac