How to automatically evaluate certain lisp code after starting an emacsclient?
Asked Answered
S

5

15

When starting Emacs, init.el (or .emacs.el) is evaluated. However, when starting emacsclient, no similar lisp code is evaluated.

How can I get a lisp file to be evaluated every time I open a new emacsclient?

(This would be handy for frame specific customizations.)

I assume the answer is to use some hook, but I can't seem to find the correct hook to use.

I look forward to your answers.

Steam answered 24/6, 2009 at 18:37 Comment(0)
E
16

You can add a function to the hook 'server-visit-hook, which is run every time the server is called (every time you call emacsclient).

Enchondroma answered 24/6, 2009 at 18:46 Comment(1)
Apparently this hook is only called if emacsclient is passed a file list. If it is called without specifying any file, then the hook isn't run - which is annoying.Webbing
A
7

I use the following code to automatically change the behavior of server buffers. I use it especially with the Firefox extension It's All Text. In that extension, buffers are named according to the domain name, so you can figure out which rule to apply by using string-match to match the name of the file.

(defun server-edit-presets ()
  (cond
   ;; When editing mail, set the goal-column to 72.
   ((string-match "mail\\.google\\.com\\.[0-9a-z]+\\.txt" (buffer-name))
    (longlines-mode-off)
    (auto-fill-mode 1)
    (set-fill-column 72)
    (save-excursion
      ;; Don't know if this is necessary, but it seems to help.
      (set-buffer (buffer-name))
      (goto-char (point-min))
      ;; Replace non-breaking strange space characters
      (while (search-forward (char-to-string 160) nil t)
        (replace-match " "))))))

(add-hook 'server-visit-hook 'server-edit-presets)
(add-hook 'server-visit-hook '(lambda () (longlines-mode 1)))
Atal answered 25/6, 2009 at 0:15 Comment(0)
P
5

If you really want new frame customizations, there's create-frame-hook which takes one arg (the new frame)...

If you mean gnuclient, you can use the command-line option "-eval" to evaluate something (and then just make an alias to always eval your customizations).

Parrish answered 24/6, 2009 at 18:43 Comment(0)
S
1

@LSW:

Try 'window-setup-hook. This addresses the annoyance since it is called even if emacsclient is not passed a file.

Statesmanship answered 1/7, 2015 at 19:23 Comment(0)
D
1

It seems that those hooks are no more, so here's the new version.

(add-hook 'server-after-make-frame-hook 'consult-recent-file)
Directorate answered 30/4, 2022 at 12:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.