Session management in emacs using Desktop library - emacs

For session management with emacs, I appended the following lines to my .emacs file
(load "desktop")
(desktop-load-default)
(desktop-read)
I was able to save the session using M-x desktop-save. But after exiting emacs, I am unable to recover the earlier saved session. I did start emacs in the same directory as that during "desktop-save"
Please let me know if I am missing anything or not correct in my efforts
Thanks
-- Harish

Chapter 51 'Saving Emacs Sessions' of my Emacs manual (using a Emacs 23 snapshot) has this
You can save the desktop manually
with the command M-x desktop-save'.
You can also enable automatic saving
of the desktop when you exit Emacs,
and automatic restoration of the last
saved desktop when Emacs starts: use
the Customization buffer (*note Easy
Customization::) to set
desktop-save-mode' to t' for future
sessions, or add this line in your
~/.emacs' file:
(desktop-save-mode 1)
which is different from what you tried.

The following worked for me (emacs 21.3.1):
(load "desktop")
(setq desktop-save-mode 1)
(desktop-load-default)
(desktop-read)
The desktop-save-mode line is only needed if you want to save desktop automatically on exit (i.e. without bothering to type M-x deskstop-save).
The only difference is that I put those lines at the beginning of the .emacs file but I doubt that's a problem.

Related

Set up emacs server so that it dies when the last frame is closed

I have run into issues using the emacs server started when I log in and emacsclient. Specifically, I use the same emacs server for different R projects things get ugly and commands from different projects end up going to the same R session.
My work around is to invoke a second server with /usr/bin/emacs --daemon=Rmd-1 when I log in. I have a bash script written such that the first .Rmd file I work with attaches to this daemon and then the command /usr/bin/emacs --daemon=Rmd-2 is issued. If I end up working with a second .Rmd file, then this server is used and a third server is started with /usr/bin/emacs --daemon=Rmd-3 and ready to work with another .Rmd file if needed.
This works pretty well except for one thing. Because after many years I am hard wired to end my emacs session using C-x C-c by the end of the day I have many emacs servers running. I'm looking for a way to trigger save-buffers-kill-emacs when the C-x C-c command is given in an emacsclient running on a daemon matching "Rmd-[0-9]+". I can't see any emacsclient options that would do that and I'm at a loss as to how to edit my custom.el file.
Can some one help me?
(note I'm running GNU Emacs 27.1 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.24.30) on Ubuntu 22.04)
Drawing on McNisse's suggestion, adding the following to my personal.el file seems to give me the desired behavior.
;; Remap C-c C-x if daemon name matches "Rmd-[0-9]+"
;;
(cond
((string-prefix-p "Rmd-" (daemonp))
(global-set-key (kbd "C-x C-c") 'save-buffers-kill-emacs)
)
)

Is there any way to transfer the content of an external editor in postgresql to query buffer without quitting the editor?

My environment:
OS: Linux CentOS 7 (x86_64)
PostgreSQL version: 10.5
Emacs 26.1
I use Emacs as the external editor in postgresql (set in my EDITOR environment variable). So whenever I type in psql shell, \e it opens Emacs where I can write/modify queries, views, functions, etc.
If I understand correctly, once Emacs is open, when I enter C-x C-s, that is, I save and then I quit C-x C-c, the content edited in Emacs is transferred to the query buffer to be parsed and executed (assuming it contains semicolon at the end). So basically each time I have to run \e then edit, then save and quit the editor to get the job done.
Now, given that I use Emacs for multiple programming languages, I've rather a big init file. As a result, it takes several seconds to start Emacs (both in -nw and GUI mode). Obviously this is quite annoying given the number of times that I have to open and quit the editor while I'm using \e in psql.
So my question is: Is there any way to let the external editor remain open and continue working with the same editor for further queries and somehow decide to transfer the result to query buffer without needing to quit the editor?
Run emacsclient instead of emacs. I have a script at ~/bin/editor:
#!/bin/sh
exec emacsclient -c -a '' "$#"
And then I set export EDITOR=$HOME/bin/editor in my ~/.bashrc.
The upshot is you only start Emacs once, and every time you run $EDITOR, it just attaches to the same Emacs session.
Also, I do
(global-set-key (kbd "C-x C-c") #'delete-frame)
(global-set-key (kbd "C-x C-S-c") #'save-buffers-kill-emacs)
so C-x C-c just deletes the frame instead of killing Emacs.
Just save the query to a temporary file, for example /tmp/q.sql and run \i /tmp/q.sql from psql multiple times from a second terminal.

ansi-term won't find file under current directory? [duplicate]

This question already has answers here:
How can I have term.el (ansi-term) track directories if using anyhting other than bash
(2 answers)
Closed 8 years ago.
It's a very strange problem. I think it must caused my incorrect configuration of ansi-term, but i still can't find out where it is.
The issue is: when i in ansi-term and press M-x find-file, the prompt isn't current directory but the path i entered in my previous find file action. So when i change directory, it still display the same directory. So i have to enter the current directory every time. But it works very well in M-x shell and M-x eshell
Does the same thing happen when you start Emacs without your init file, i.e., emacs -Q? If so, that's the designed behavior or (especially if you use a development snapshot) perhaps an Emacs bug.
If not, then bisect your init file recursively to find out which part of it causes this behavior. To do that, use, e.g., command comment-region (see prefix arg in doc) to comment and uncomment a block of text. Comment out 1/2 of your init file, then 3/4, then 7/8,...,
each time testing whether the uncommented portion causes or removes the problematic behavior. You will very quickly identify what causes the behavior.
Because the path of emacs is different from that of term, it can only be changed by use the emacs command "cd".
So to solve this problem, I add the following code to my emacs configure file. The method is
find the pid of current term
find current working directory(cwd) of this pid.
I use multi-term, I think the method will be similar on ansi-term.
(defadvice term-send-input (after update-cwd)
(let* ((pid (process-id (get-buffer-process (current-buffer))))
(cwd (shell-command-to-string
(format "lsof -p %d -Fn | awk 'NR==2{print}' | sed \"s/n\\//\\//\" | tr -d '\n'" pid))))
(cd cwd)
(message (concat "change emacs path to: " cwd))))
(ad-activate 'term-send-input)
Then you can bound the key of term-send-input to <enter>. When you press <enter> in term, the emacs will change to the same path with the current path of term.
BTW, I use Mac Os. If you are on Linux, you can use the following code to find cwd.
(cwd (file-truename (format "/proc/%d/cwd" pid)))

emacs won't close because of buffer "ido.last"

When I try to close emacs I get the message
"buffer ido.last modified; kill anyway?"
and whatever I answer, emacs stays open. But I can't open this buffer and the ido.last file doesn't exist. How can I get my emacs closed?
If you've used emacs as another user, as root for instance, it's possible that the .ido.last file is owned by root and hence you aren't allowed to remove or save it.
When you get the "buffer .ido.last modified"-question just remove the .ido.last file before you answer yes (if you're using emacs within a shell, just C-z, remove file and then resume with %). Then the .ido.last file will be written to disk with you as the owner.
IDO tries to save a file called ido.last into ~/.emacs.d directory. But, in your case IDO seems to be unable to do so. Maybe your ~/.emacs.d directory is read-only for a particular reason, or your disk is full, etc. So IDO raise an error that prevent your emacs to close.
If you don't use IDO, try to remove this kind of lines from your .emacs :
(require 'ido)
(ido-mode t)
Add this code (eg. to your .emacs.el, init.el, etc.) to
fix the issue with ido preventing emacs from exiting
when the ido.last file is not writable:
(defun ido-kill-emacs-hook ()
(ignore-errors (ido-save-history)))
Like Jérôme Radix said, IDO gives that error when unable to save the ido.last file, and the error prevents emacs from closing.
The default location for IDO to save the file is into ~/.emacs.d directory, but this can be overridden by setting the variable ido-save-directory-list-file. This means you should check the value of this variable (e.g., M-x describe-variable) to see where IDO is trying to save a file, and figure out why it can't save the file.
Possible reasons include:
The directory or file is read-only.
The directory does not exist. IDO will not create the directory for you.
The directory or file is owned by another user.
The disk is full.

Find-file with a hint?

Is there a way to give the 'find-file' function a hint?
I'm working with files in the same directory on a remote server, and I'm getting tired of typing in the machine name, and directory structure all the time. It would sure be great if I could write a function that would bring up the find-file prompt with the machine name and directory already filled in.
(Note: I use Emacs 23.1)
Thanks for your help in advance.
If you're starting the 'find-file command from buffers associated with files on the remote server, the staring point should already include the directory/remote server filled in.
One way to skin this cat is to do
M-x cd /ssh:user#machine:/starting/path
note: I use tramp, and that's how find-file starts. I'd not noticed the /ssh: before today, but I don't use tramp very much any longer.
To answer your question directly, this command calls find-file with the "hint":
(defun my-remote-find-file ()
"call 'find-file with a starting directory"
(interactive)
(let ((default-directory "/ssh:user#machine:/starting/path/"))
(call-interactively 'find-file)))
Obviously customize the starting point.
One last way I can think of solving this is to do M-x dired on the remote server, and do your file finding from that point.
Set up recentf, and when you want to open a file on the server, go through the recentf menu; if the exact file you want is not there, just open something else in the same directory, then type C-x C-f and the directory should be filled in for you.
Use a bookmark to get to the remote directory. Then, as noted by Trey above, once you're in a file or directory that is remote, the default-directory will be set to what you want.
You can bookmark a remote file or directory. With Bookmark+, when you bookmark a Dired buffer you can save also all its file markings, omissions etc. -- IOW, the Dired state. When you later jump to the bookmark that saved state is restored. Use bookmarks to organize code projects etc.
bookmarksbookmark