Why is ido-mode trying to use this variable? - emacs

I have emacs auto-save and backup to a single directory off in my home directory. For some reason, when I try to exit the variable name where I set the save directory is getting passed to ido-mode and it won't let me exit out of emacs. I have tried deleting old versions of my ido.last file (and the symbolic link .#ido.last), but this doesn't seem to consistently take care of the problem. I have full permissions and ownership of the directory the files are stored in and the files themselves. This happens on several systems that I work on covering emacs major versions 21, 22 and 24.
Here are the relevant parts of my emacs configuration:
(defvar home (concat (getenv "HOME") "/"))
(defvar emacs-dir (concat home ".emacs.d/"))
(defvar savedir (concat home ".saves/"))
(setq backup-directory-alist `((".*" . savedir)))
(setq auto-save-file-name-transforms `((".*" ,savedir t)))
(setq backup-by-copying t)
(setq delete-old-versions t
kept-new-versions 10
kept-old-versions 6
version-control t)
(setq ido-save-directory-list-file (concat emacs-dir "cache/ido.last"))
(ido-mode t)
(setq ido-enable-flex-matching t
ido-everywhere t)
This is what the debugger output looks like when the problem happens.
Debugger entered--Lisp error: (wrong-type-argument stringp savedir)
expand-file-name(savedir "/home/pinyaka/.emacs.d/cache/")
make-backup-file-name-1("/home/pinyaka/.emacs.d/cache/ido.last")
make-backup-file-name("/home/pinyaka/.emacs.d/cache/ido.last")
find-backup-file-name("/home/pinyaka/.emacs.d/cache/ido.last")
backup-buffer()
basic-save-buffer-2()
basic-save-buffer-1()
basic-save-buffer()
save-buffer()
write-file("/home/pinyaka/.emacs.d/cache/ido.last" nil)
ido-save-history()
ido-kill-emacs-hook()
run-hooks(kill-emacs-hook)
kill-emacs()
save-buffers-kill-emacs(nil)
call-interactively(save-buffers-kill-emacs)
You can see that for some reason ido has gotten a hold of savedir even though I have never used that variable in connection with ido-mode (I have included everywhere that that variable is used as well as all the ido calls that I make). Why does ido do anything with savedir?

I think the problem is:
(setq backup-directory-alist `((".*" . savedir)))
Should be
(setq backup-directory-alist `((".*" . ,savedir)))
Explanation: When Emacs exits, Ido is trying ta save its history; the standard backup procedure of Emacs kicks in and try to backup that file. However you forget to unquote savedir in the configuration for backup-directory-alist, so the cons cell is a pair of string and symbol instead of a pair of strings as expected.

Related

Viewing / Reloading emacs backup files

I am successfully using the code below to cause emacs to save many versions of each file. But I cannot figure out what commands you use in emacs to actually load those files into a buffer.
I am expecting some kind of a history viewer command!!! I can find nothing.
(setq backup-directory-alist '(("." . "~/auto-saves")))
(setq version-control t
kept-old-versions 2 kept-new-versions 200
delete-old-versions t backup-by-copying t)
You can just open the files in which ever directory you're saving them in (~/autosaves). But the backup-walker package is way better.
Update: I highly recommend using the no-littering package to keep your ~/.emacs.d (and $HOME) clean. Here's my backup config (assuming you have use-package and melpa set up):
(use-package no-littering)
(setq make-backup-files t
vc-make-backup-files t
version-control t
kept-new-versions 128
kept-old-versions 0
delete-old-versions t
backup-by-copying t)
(defun force-backup-of-buffer ()
(setq buffer-backed-up nil))
(add-hook 'before-save-hook #'force-backup-of-buffer)
(use-package backup-walker)
(let ((dir (no-littering-expand-var-file-name "auto-save/")))
(make-directory dir t)
(add-to-list 'auto-save-file-name-transforms `(".*" ,dir t) 'append))
If you don't want to use no-littering, set backup-directory-alist, tramp-persistency-file-name, tramp-backup-directory-alist, and tramp-auto-save-directory.
(setq emacs-persistence-directory
(expand-file-name "var" user-emacs-directory))
(let ((dir (expand-file-name "backup" emacs-persistence-directory)))
(unless (file-directory-p dir)
(make-directory dir t))
(setq backup-directory-alist `(("." . ,dir))))
(let ((backup-dir (concat emacs-persistence-directory "tramp-backup/")))
(setq tramp-persistency-file-name (concat emacs-persistence-directory
"tramp")
tramp-backup-directory-alist `(("." . ,backup-dir))
tramp-auto-save-directory (concat emacs-persistence-directory
"tramp-auto-save/"))
(dolist (d (list tramp-auto-save-directory backup-dir))
(unless (file-exists-p d)
(make-directory d t))))
Side note: auto-save is a different feature than backups. Backups save a copy the first time you save a buffer (C-x C-s). Above, I have the function force-backup-of-buffer on before-save-hook to backup on every save. Autosave saves a copy every time you make a certain number of edits. For a given file, there can be many backups, but there's only one autosave.
I am leaving jpkotta's answer as the selected one, since backup-walker seems to work for others. For my OSX box, I could not get Backup-Modes to work, and backup walker is more focused on DIFFS rather than just providing access to backup files.
Here is my hacked solution it is a kinda gross, but it works for me.
You will need to edit path names for your environment.
(see https://www.emacswiki.org/emacs/ForceBackups for the original)
I tried, Backup-Modes, and Backup-Directory https://www.emacswiki.org/emacs/BackupDirectory, but this is the first things that kinda worked.)
What this does:
- It fixes emacs so it always does an auto-save (surprisingly this is NOT the default)
- it adds the command ``M-x history'' to open the backups directory.
Crude, but it works. So sad to see Emacs die!
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; FORCE-BACKUP-OF-BUFFER
;;; (See https://www.emacswiki.org/emacs/ForceBackups)
(defun dao-setup-force-backup-of-buffer()
(setq vc-make-backup-files t) ; Do backups even for version controlled files!
(setq version-control t ; Use version numbers for backups.
kept-new-versions 10 ; Number of newest versions to keep.
kept-old-versions 0 ; Number of oldest versions to keep.
delete-old-versions t ; Don't ask to delete excess backup versions.
backup-by-copying t) ; Copy all files, don't rename them.
(add-hook 'before-save-hook 'force-backup-of-buffer)
)
(defun force-backup-of-buffer ()
;; Make a special "per session" backup at the first save of each
;; emacs session.
(when (not buffer-backed-up)
;; Override the default parameters for per-session backups.
'(let ((backup-directory-alist '(("." . "~/emacs-backups")))
(kept-new-versions 3))
(backup-buffer)))
(backup-buffer)
;; Make a "per save" backup on each save. The first save results in
;; both a per-session and a per-save backup, to keep the numbering
;; of per-save backups consistent.
(let ((buffer-backed-up nil))
(backup-buffer)))
(defun history ()
(interactive)
(dired-find-file "/User/oblinger/emacs-backups")
)

If desktop previously saved, then switch from *scratch* to last viewed file

Could someone please give me a hand switching to the last viewed file from the desktop save when opening Emacs, taking into consideration that my *scratch* buffer is also an existing file?
The *scratch* buffer always trumps the desktop save in terms of the initial buffer choice.
(setq initial-scratch-message nil)
;; (setq initial-buffer-choice "~/.0.data/.0.emacs/*scratch*")
(defun kill-default-scratch ()
"Avoid having a buffer named `*scratch*<2>` when Emacs loads
my preferred *scratch* file from a specified location -- however,
yield to the last viewed file if it exists from desktop-save."
(kill-buffer "*scratch*")
(find-file "~/.0.data/.0.emacs/*scratch*") )
(add-hook 'after-init-hook 'kill-default-scratch)
(require 'saveplace)
(setq-default save-place t)
(setq save-place-file "~/.0.data/.0.emacs/.saved-places")
(desktop-save-mode 1)
(setq desktop-dirname "~/.0.data/.0.emacs/"
desktop-base-file-name ".emacs.desktop"
desktop-base-lock-name ".lock"
desktop-path (list desktop-dirname)
desktop-save t
desktop-files-not-to-save "[*]bbdb[*]\\|[*]BBDB[*]\\|[*]TODO[*]" ;; "^$" reload tramp paths
desktop-load-locked-desktop nil )
(setq desktop-buffers-not-to-save
(concat "\\("
"^nn\\.a[0-9]+\\|\\.log\\|(ftp)\\|^tags\\|^TAGS"
"\\|\\.emacs.*\\|\\.diary\\|\\.newsrc-dribble"
"\\)$"))
(add-to-list 'desktop-modes-not-to-save 'dired-mode)
(add-to-list 'desktop-modes-not-to-save 'Info-mode)
(add-to-list 'desktop-modes-not-to-save 'info-lookup-mode)
(add-to-list 'desktop-modes-not-to-save 'fundamental-mode)
EDIT: Based upon the answer provided by #juanleon, I decided to just let nature run its course and deal with it using the emacs-startup-hook (which loads subsequent to the after-init-hook). The scratch buffer wants to trump desktop.el, so let it and then bury it. To deal with the situation when there is no last viewed file saved by desktop.el (i.e., because they were all closed before exiting), I bury the *Messages* buffer (if it has focus) so the result is that the custom *scratch* buffer has focus again. To remove the custom *scratch* file from the desktop.el save feature, I added [*]scratch[*] to the list of desktop-files-not-to-save. I am using auto-save-buffers-enhanced to automatically save my custom *scratch* file (set to occur 1 second after every modification) and that utility lets me exclude user-defined file types with (setq auto-save-buffers-enhanced-exclude-regexps . . .): https://github.com/kentaro/auto-save-buffers-enhanced/blob/master/auto-save-buffers-enhanced.el [NOTE: The reference to flet therein would need to be changed to cl-flet if using a recent version of Emacs.]
(setq initial-scratch-message nil)
(setq initial-buffer-choice t)
(require 'auto-save-buffers-enhanced)
(auto-save-buffers-enhanced t)
(setq auto-save-buffers-enhanced-save-scratch-buffer-to-file-p 1)
(setq auto-save-buffers-enhanced-exclude-regexps '("^not-save-file" "\\.ignore$" "\\.txt" "[*]TODO[*]" "\\.yasnippet" "\\.tex" "\\user_pref" "\\.org_archive" "\\.org" "\\.ppet"))
(require 'saveplace)
(setq-default save-place t)
(setq save-place-file "~/.0.data/.0.emacs/.saved-places")
(desktop-save-mode 1) ;; uses the after-init-hook
(setq desktop-dirname "~/.0.data/.0.emacs/"
desktop-base-file-name ".emacs.desktop"
desktop-base-lock-name ".lock"
desktop-path (list desktop-dirname)
desktop-save t
desktop-files-not-to-save "[*]scratch[*]\\|[*]bbdb[*]\\|[*]BBDB[*]\\|[*]TODO[*]" ;; "^$" reload tramp paths
desktop-load-locked-desktop nil )
(setq desktop-buffers-not-to-save
(concat "\\("
"^nn\\.a[0-9]+\\|\\.log\\|(ftp)\\|^tags\\|^TAGS"
"\\|\\.emacs.*\\|\\.diary\\|\\.newsrc-dribble"
"\\)$"))
(add-to-list 'desktop-modes-not-to-save 'dired-mode)
(add-to-list 'desktop-modes-not-to-save 'Info-mode)
(add-to-list 'desktop-modes-not-to-save 'info-lookup-mode)
(add-to-list 'desktop-modes-not-to-save 'fundamental-mode)
(defun custom-scratch-setup ()
"Avoid having a buffer named `*scratch*<2>` when Emacs loads
my preferred *scratch* file from a specified location -- however,
yield to the last viewed file if it exists from desktop-save."
(kill-buffer "*scratch*")
(message "The default `*scratch*` buffer has been killed.")
(find-file "~/.0.data/.0.emacs/*scratch*")
(message "Finished loading the custom `*scratch*` file.")
(bury-buffer)
(message "The buffer *scratch* has been buried.")
(if (eq (current-buffer)
(get-buffer "*Messages*") )
(progn (bury-buffer)
(message "The buffer *Messages* has been buried."))) )
(add-hook 'emacs-startup-hook 'custom-scratch-setup)
My guess would be that after-init-hook runs after desktop has finished loading its stuff (desktop uses that hook)
So, the find-file will run at the end of everything, and find-file happens to "unbury" the buffer of a file, if already loaded. You may add a (bury-buffer) at the end of your kill-default-scratch if you don't want it to taking over initial buffer choice, nor being the initial buffer choice.
Another option would be to swap the order the hooks in after-init-hook are run. You can do that by playing with the moment desktop.el is "required" and/or using the arg APPEND of add-hook. If your function is run before desktop's function, (desktop-read) will take precedence over your find-file

Persistent Undos in Emacs [duplicate]

Is there any way to have EMACS save your undo history between sessions?
I'm aware of the savehist lib, the saveplace lib, the desktop lib, and the windows lib, these all provide some session control but none seem to save the undo history.
From version 0.4 onwards, undo-tree supports persistent storage of undo-tree data between sessions "out of the box". (Note that there are significant bug-fixes related to this feature in more recent versions; the latest version at the time of writing is 0.6.3.)
Simply enable the undo-tree-auto-save-history customization option to automatically save and load undo history in undo-tree buffers. Or use the undo-tree-save/load-history commands to save and load undo history manually.
You need at least Emacs version 24.3 for this to work reliably, but with a recent enough Emacs it works very well.
Add the following to your .emacs file :
(global-undo-tree-mode)
(setq undo-tree-auto-save-history t)
(setq undo-tree-history-directory-alist '(("." . "~/.emacs.d/undo")))
Explanation
(global-undo-tree-mode) enables undo tree.
(setq undo-tree-auto-save-history t) enables auto save of undo history.
(setq undo-tree-history-directory-alist '(("." . "~/.emacs.d/undo"))) so that your project does not get littered with undo-history savefiles.
Here's some code I wrote which seems to do the trick. It isn't bullet-proof, as in, it doesn't handle all the file handling intricacies that Emacs does (e.g. overriding where auto-save files are put, symlink handling, etc.). But, it seemed to do the trick for some simple text files I manipulated.
(defun save-undo-filename (orig-name)
"given a filename return the file name in which to save the undo list"
(concat (file-name-directory orig-name)
"."
(file-name-nondirectory orig-name)
".undo"))
(defun save-undo-list ()
"Save the undo list to a file"
(save-excursion
(ignore-errors
(let ((undo-to-save `(setq buffer-undo-list ',buffer-undo-list))
(undo-file-name (save-undo-filename (buffer-file-name))))
(find-file undo-file-name)
(erase-buffer)
(let (print-level
print-length)
(print undo-to-save (current-buffer)))
(let ((write-file-hooks (remove 'save-undo-list write-file-hooks)))
(save-buffer))
(kill-buffer))))
nil)
(defvar handling-undo-saving nil)
(defun load-undo-list ()
"load the undo list if appropriate"
(ignore-errors
(when (and
(not handling-undo-saving)
(null buffer-undo-list)
(file-exists-p (save-undo-filename (buffer-file-name))))
(let* ((handling-undo-saving t)
(undo-buffer-to-eval (find-file-noselect (save-undo-filename (buffer-file-name)))))
(eval (read undo-buffer-to-eval))))))
(add-hook 'write-file-hooks 'save-undo-list)
(add-hook 'find-file-hook 'load-undo-list)
desktop-save-mode does not save buffer-undo-list by default. You just have to tell him!
(add-to-list 'desktop-locals-to-save 'buffer-undo-list)
Emacs Session appears to support this:
(add-to-list 'session-locals-include 'buffer-undo-list)
I have managed to get the undo history working by using the information provided here: http://emacs.stackexchange.com/q/3725/2287
Instead of patching the original file desktop.el.gz I created an advice that temporarily overrides (buffer-local-variables) then I use it together with the function that gathers information about the buffer.
(defun +append-buffer-undo-list-to-buffer-local-variables-advice (orig-fn &rest args)
"Override `buffer-local-variables' and call ORIG-FN with ARGS.
There is a bug in Emacs where the `buffer-undo-list' data is
missing from the output of `buffer-local-variables'. This
advice temporarily overrides the function and appends the
missing data."
(let ((orig-buffer-local-variables-fn (symbol-function 'buffer-local-variables)))
(cl-letf (((symbol-function 'buffer-local-variables)
#'(lambda () (append (funcall orig-buffer-local-variables-fn)
`(,(cons 'buffer-undo-list buffer-undo-list))))))
(apply orig-fn args))))
(advice-add #'desktop-buffer-info :around #'+append-buffer-undo-list-to-buffer-local-variables-advice)
(push 'buffer-undo-list desktop-locals-to-save)
(desktop-save-mode 1)
I hope this helps someone else.

Getting vc-diff to use ediff in Emacs 23.2

Had this working well in Emacs 23.1.x but it appears to have broke in the move to Emacs 23.2
I want to use ediff when comparing working copy of a file with SVN HEAD.
Normally I press C-x v = and ediff runs because of the following configuration in my .emacs
;; Use ediff and not diff
(setq diff-command "ediff")
But, alas I still get the normal vc-diff buffer appearing and no ediff session...
Has anyone else encountered this and know what might be the problem?
Am a bit skeptical that the above setting did what you say it did.
That said, this will bind '=' to use 'ediff-revision:
(eval-after-load "vc-hooks"
'(define-key vc-prefix-map "=" 'ediff-revision))
I use the command vc-ediff to skip entering the file names: just compare the current modified copy with the base version (HEAD).
(eval-after-load "vc-hooks"
'(define-key vc-prefix-map "=" 'vc-ediff))
Then C-x v = will bring up the Ediff session.
Found out I could just rebind C-x v = to the following:
(defun ediff-current-buffer-revision ()
"Run Ediff to diff current buffer's file against VC depot.
Uses `vc.el' or `rcs.el' depending on `ediff-version-control-package'."
(interactive)
(let ((file (or (buffer-file-name)
(error "Current buffer is not visiting a file"))))
(if (and (buffer-modified-p)
(y-or-n-p (message "Buffer %s is modified. Save buffer? "
(buffer-name))))
(save-buffer (current-buffer)))
(ediff-load-version-control)
(funcall
(intern (format "ediff-%S-internal" ediff-version-control-package))
"" "" nil)))
This approach means you avoid having to specify the versions to compare as it defaults to comparing HEAD and the current file state.
Source: http://www.groupsrv.com/computers/about152826.html

How can I hide the backup files that emacs creates?

I just started using emacs after having used vi for a long time. :)
One thing which is annoying me is that whenever I modify a file, save it and exit emacs, I see a backup file created in the same directory named filename~ (if the file I edited was filename).
Is there any way I can get rid of this? Or hide these files? It is very annoying to see tons of backup files when I do ls of the directory.
You can either move them to their own folder with the following code:
;; Don't clutter up directories with files~
(setq backup-directory-alist `(("." . ,(expand-file-name
(concat dotfiles-dir "backups")))))
;; Don't clutter with #files either
(setq auto-save-file-name-transforms
`((".*" ,(expand-file-name (concat dotfiles-dir "backups")))))
Or you can remove them completely, like so:
(setq make-backup-files nil)
(setq auto-save-default nil)
Personally I would be wary of removing them as they can come in useful. Further discussion is here:
http://www.emacswiki.org/emacs/BackupDirectory
http://www.emacswiki.org/emacs/AutoSave
I would recommend checking out the emacs-starter-kit it sorts out a load of issues that people have when coming to emacs, and is pretty heavily used.
http://github.com/technomancy/emacs-starter-kit/blob/master/starter-kit-misc.el
Update:
There seems to be much confusion over how to use the functions. I'm going to have a little play around later but here is some more information. Note that auto-save-file-name-transforms:
lets you specify a series of regular expressions and replacements to transform the auto save file name
[emacs-manual]
so it's not just as simple as adding in a folder name. That said it seems from a quick google search the following might just do what you all want:
;;; backup/autosave
(defvar backup-dir (expand-file-name "~/.emacs.d/backup/"))
(defvar autosave-dir (expand-file-name "~/.emacs.d/autosave/"))
(setq backup-directory-alist (list (cons ".*" backup-dir)))
(setq auto-save-list-file-prefix autosave-dir)
(setq auto-save-file-name-transforms `((".*" ,autosave-dir t)))
http://www.google.com/codesearch?hl=en&lr=&q=auto-save-file-name-transforms&sbtn=Search
The following lines in ~/.emacs will put all of the auto-save and backup files in /tmp:
(setq backup-directory-alist
`((".*" . ,temporary-file-directory)))
(setq auto-save-file-name-transforms
`((".*" ,temporary-file-directory t)))
In your .emacs:
(setq make-backup-files nil)
Edit:
If you're unfamiliar with the .emacs file, it's a file named .emacs that resides in your user $HOME directory. If you don't have one already, you can just create it and emacs will load it on startup.
Here is a link to the same question answered on SuperUser and my response. And a StackOverflow question entitled Emacs: Don’t create #these# files when not saving modified buffer
And for completeness, as stated by others; to stop the backup files being created put this in your .emacs
(setq make-backup-files nil)