1

I have this function in config

(defun launch-separate-emacs-under-x ()
  (interactive)
  (call-process "sh" nil nil nil "-c" "emacs &"))

(defun restart-emacs ()
  (interactive)
  (add-hook 'kill-emacs-hook
            (if (display-graphic-p)
                #'launch-separate-emacs-under-x
              #'launch-separate-emacs-in-terminal)
            t)
  (kill-emacs))

Update:

In general if i execute any command say package-install it will be added to command history. But when I execute above function using M-x, it is not getting added into commands history. How to make emacs to add this command to history?

  • If you execute that command via helm-M-x, I guess setting helm-M-x-always-save-history to t can save that history. – xuchunyang Jul 30 '15 at 9:54
  • Looks like it is not working. – ChillarAnand Jul 30 '15 at 9:58
  • I can't try this command since I don't know launch-separate-emacs-*. And you didn't say where you invoke this command from, I should just assume the built-in M-x. Does other commands works as you expected? AFAIK , extended-command-history is usually used to keep command history and the savehist package can save various minibuffer input history across Emacs session. – xuchunyang Jul 30 '15 at 10:31
  • @Name Sorry. I want emacs to remember that I have executed the that function. Update question – ChillarAnand Jul 30 '15 at 16:53
  • My guess is that it only gets added to history after it's done running but since emacs is getting killed that never happens. Instead of calling kill-emacs directly. Run kill-emacs on a 0 second timer so that the stack can clear and the command might then be added to the history. – Jordon Biondo Jul 30 '15 at 17:02
3

The command won't be placed into your command history until after it is run, and since the command kills emacs, that never happens.

You can counter this by killing emacs only after the command has ran and the call stack clears by running kill-emacs on a 0 second timer.

The call to (kill-emacs) would then be replaced with this:

(run-with-timer 0 nil (lambda () (kill-emacs)))
1

An alternate solution is to move kill-emacs to seperate function.

(defun stop-emacs ()
  (kill-emacs))

(defun restart-emacs ()
  (interactive)
  (add-hook 'kill-emacs-hook
            (if (display-graphic-p)
                #'launch-separate-emacs-under-x
              #'launch-separate-emacs-in-terminal)
            t)
  (stop-emacs))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.