Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emacs startup problem in daemon mode when nlinum/linum configured to be enabled globally #4

Closed
joe9 opened this issue Apr 12, 2015 · 11 comments

Comments

@joe9
Copy link

joe9 commented Apr 12, 2015

Hello,

I am sorry to raise this here.

I am trying to use the fix recommended by you here: http://debbugs.gnu.org/cgi/bugreport.cgi?msg=43;att=1;bug=18616

But, for some reason, nothing happens when I start emacs in daemon mode with that code snippet in my init.el. I am using GNU Emacs 24.4.1

Just want to check if the relevant code snippet is still valid.

Thanks again and Sorry for the bother,

This is the code snippet of what I tried:

(if (daemonp)
    (add-hook 'emacs-startup-hook
              (lambda ()
                (message ">> Daemon mode")
                ;; It is mandatory to load linum AFTER the frames are set up
                ;; Else, I get "*ERROR*: Invalid face: linum"
                (require 'nlinum)
                (global-nlinum-mode)
                ;;                 (nlinum-mode 1)
                ;;                 (add-hook 'find-file-hook 'nlinum-mode)
                ))
  (progn
    (message ">> Non daemon mode")
    (require 'nlinum)
    (global-nlinum-mode)
    ))

@kaushalmodi
Copy link
Owner

I have seen issues with global-nlinum-mode and emacs daemon mode which I haven't yet got chance to debug.

But the below works as of today:

I have this in my init.el:

If you want, you can use stuff from my setup-linum.el. The linum setup looks complicated because I wanted a clean way to switch between linum packages: nlinum, linum and linum-relative. But the crucial piece of code in there that might be useful to you is how to set the linum/nlinum mode only for your selected major modes.

(defconst modi/linum-mode-hooks '(verilog-mode-hook
                                  emacs-lisp-mode-hook
                                  cperl-mode-hook
                                  c-mode-hook
                                  python-mode-hook
                                  matlab-mode-hook
                                  sh-mode-hook
                                  web-mode-hook
                                  html-mode-hook
                                  css-mode-hook
                                  makefile-gmake-mode-hook
                                  tcl-mode-hook)
  "List of hooks of major modes in which a linum mode should be enabled.")

Here's the relevant piece that enable nlinum only for those modes:

(when global-linum-mode
      (global-nlinum-mode -1))
(dolist (hook modi/linum-mode-hooks)
      (add-hook hook #'nlinum-mode))

As I mentioned, I need to look into why global enabling of nlinum/linum doesn't work in daemon mode.

@kaushalmodi kaushalmodi changed the title Bug 18616 fix Emacs startup problem in daemon mode when nlinum/linum configured to be enabled globally Apr 13, 2015
@kaushalmodi
Copy link
Owner

I'll keep this issue open till we find a solution to be able to successfully start emacs in daemon mode with nlinum enabled globally.

But in the meanwhile, let me know if the daemon startup issue is resolved for you if you enable nlinum only for selected major modes using their hooks.

@kaushalmodi
Copy link
Owner

@joe9 Feel free to test out this method of linum activation. It now works fine for me on both emacs and emacsclient.

Fix

https://github.com/kaushalmodi/.emacs.d/tree/a96103dcdea5c1b144a2e4edb2794aa9f42379b8/init.el#L278-L284

Explanation

Delay the loading of linum to 1 second after emacs becomes idle (after setting up the frame, etc). So it does not matter if you are starting emacs or emacsclient.

Load the desktop only after linum has loaded. The problem earlier was that if your saved desktop had references to file buffers that had linum on at the time of saving the desktop, emacs would try to enable linum mode for those buffers even before linum was enabled or loaded. This solution makes sure that linum is loaded before the desktop is loaded.

Summary

  • Startup emacs as normal or using emacsclient
  • Wait for a second and then load linum
  • Load desktop after linum is loaded.

@kaushalmodi
Copy link
Owner

@joe9 I am closing this issue as I have tested global linum mode to work fine in an emacs daemon session now. Feel free to reopen it if you still see this problem.

@joe9
Copy link
Author

joe9 commented Jun 15, 2015

Thanks for fixing this.

@the9000
Copy link

the9000 commented Dec 18, 2015

Sounds great.
Does it work for splitting out a new frame (C-x 5 2)? My current setup (not derived from this code) allows for normal startup, but opening a new frame fails. I wonder if you've overcome this issue.

@kaushalmodi
Copy link
Owner

I use a single frame workflow. So I never needed to do C-x 5 2. But the good news is that this delaying trick works for that too :)

Here's how I quickly tested the code by modifying my code at this line:

;; Do desktop setup after linum setup so that the desktop loaded files will show
;; linum if enabled for that major mode or if enabled globally
(with-eval-after-load 'setup-linum
  (require 'setup-desktop)
  (make-frame-command))

@the9000
Copy link

the9000 commented Dec 18, 2015

Nice to know! Thanks for the code.

@kaushalmodi
Copy link
Owner

Just FYI, the same code might not work for you if you haven't delayed loading setup-linum as I have.

@zw963
Copy link

zw963 commented Jun 25, 2016

following piece code worked for me.

(defun initialize-nlinum (&optional frame)
  (require 'nlinum)
  (add-hook 'prog-mode-hook 'nlinum-mode))
(when (daemonp)
  (add-hook 'window-setup-hook 'initialize-nlinum)
  (defadvice make-frame (around toggle-nlinum-mode compile activate)
  (nlinum-mode -1) ad-do-it (nlinum-mode 1)))

Those code make my emacs 24.5.1 daemon mode worked with nlinum-mode
very well. No matter what open a frame from terminal or create a new frame in emacs.

@kaushalmodi
Copy link
Owner

Thanks for sharing that. Since I posted the last time in this thread, I have made a big improvement in how to deal with daemon and linum/font-checking/etc.

Solution: Start linum/nlinum in after-make-frame-functions when (daemonp).
Code:

;; Set/unset linum
(if (daemonp)
;; Need to delay linum activation till the frame and fonts are loaded, only
;; for emacsclient launches. For non-daemon, regular emacs launches, the
;; frame is loaded *before* the emacs config is read. Not doing so results
;; in the below error in emacs 24.5:
;; *ERROR*: Invalid face: linum
(add-hook 'after-make-frame-functions #'modi/toggle-linum)
;; Even when running in non-daemon mode, run `modi/toggle-linum' only after the
;; init has loaded, so that the last modified value of `modi/linum-fn-default'
;; if any in setup-personal.el is the one effective, not its standard value
;; in its defvar form above.
(add-hook 'after-init-hook #'modi/toggle-linum))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants