How to open an org-mode file automatically in Emacs - emacs

How to open files automatically when starting emacs? does not work either under Windows or under Linux.
After adding the find-file command I received a message
so I disabled the auto-save, but the file does not load anyway.
(add-to-list 'load-path "~/emacs/org-8.0.3")
(setq auto-save-default nil)
(find-file "/home/uwe/Dropbox/orgmode.org")

You probably want to set the initial-buffer-choice variable so that it switches to your org file after running your init.el.
(setq
org-default-notes-file "/home/uwe/Dropbox/orgmode.org"
initial-buffer-choice org-default-notes-file)

The message you see proves that the file is indeed loaded just fine. All it tells you is that there's some auto-save file left over, indicating that some edits were not saved last time. You can ignore the message (which is not an error message), or you can use M-x recover-this-file RET to recover the unsaved changes from the auto-save file.
I strongly recommend you don't disable auto-saving.
IOW what you think doesn't work (automatically loading orgmode.org) actually does work. The only thing that doesn't work the way you want is that this file is not displayed and instead the *scratch* buffer is displayed. The reason for this depends on how you started Emacs. And the fix for it depends on all the different ways you might start Emacs (e.g. if you only ever start Emacs in the exact same way, it's easier).

Don't disable the auto-save, it could save ours files.
Anyway, delete #orgmode.org, if the diff between the two file don't interest you.

Related

How to disable Emacs Lisp auto-reformatting?

I absolutely despise that Emacs automatically reformats my init.el file. Every time I use the GUI to change a setting, when I look back at my init.el file, Emacs has warped all of the formatting I've done on the file (e.g. changing the indentation, moving things onto their own lines, etc.).
How do I disable this functionality?
Your choice is to either avoid the customize GUI altogether (I am not
even sure that is possible because, e.g.,
packages
use them), or put something like this in your .emacs:
(custom-set-variables
`(custom-file ,(expand-file-name "custom.el" user-emacs-directory)))
(when (file-readable-p custom-file)
(message "ignoring and deleting `%s'!" custom-file)
(delete-file custom-file))
This will make Emacs write all its custom forms to a file that you
never load and delete whenever you start Emacs.
This also means that you will have to manually move all your
customizations from custom-file to your .emacs yourself.

undo-tree doesn't auto load history

I was trying to make persistent undo history work in emacs.
I have (setq undo-tree-auto-save history t). The history is indeed saved when I save file. But when I open file, the undo history is not loaded until I use undo.
So if I open a file and make some changes, then run M-x undo-tree-visualize, the previous undo tree is gone, only the recent changes are there. But if I run M-x undo-tree-visualize first, then I can see the old undo tree. Or if I just use undo before making any changes, the old undo history is loaded and the undo works as expected.
Edit: My configuration looks like this: https://gitlab.com/snippets/22693
Edit2: This problem still happens with the most minimal configure file:
;;; init.el --- user init file -*- no-byte-compile: t -*-
(load-file "~/.emacs.d/undo-tree.el")
(global-undo-tree-mode 1)
(setq undo-tree-auto-save-history t)
How come no one has ever noticed this? There's a bug in undo-tree.
The function undo-list-transfer-to-tree failed to append 'undo-tree-canary to buffer-undo-list, which cause it to discard the content of buffer-undo-tree.
I'm still looking into it to see if I can find a solution. Simply append 'undo-tree-canary to buffer-undo-list causing it to discard to content in buffer-undo-list instead
Edit: The solution is indeed to put canary at the end of buffer-undo-list
(when (not (eq (last buffer-undo-list) 'undo-tree-canary))
(setq buffer-undo-list (append buffer-undo-list '(nil undo-tree-canary))))
When undo-tree-auto-save-history is enabled, undo history is loaded from file by the undo-tree-load-history-hook function, which gets added to find-file-hook. So undo history will be loaded automatically when you open a file with undo-tree-mode enabled.
#djangoliv is right: this will only work if global-undo-tree-mode is enabled, because undo-tree-mode needs to be enabled before find-file-hook is called. Perhaps the undo-tree-mode minor-mode function ought to attempt to load history if it detects the buffer hasn't been modified since the last undo history save. But at the moment, it doesn't.
However, your config does enable global-undo-tree-mode, so this can't be the issue.
History loading works just fine for me with global-undo-tree-mode enabled. So unless you give a complete MWE that reproduces your problem -- i.e. step-by-step instructions starting from "emacs -Q" and including Emacs and undo-tree version numbers -- you're unlikely to get much more help here. (Also, note that stackexchange is not a bug tracker. Bug reports should be sent by email to the address listed at the top of the package.)
Have you tinkered with the order you are loading your plugins as well as files you open automatically via init.el or .emacs? That did the trick for me. Evil requires Undo-Tree. So everything having to do with undo-tree should fire before you load Evil. Especially if you use ":e" instead of "find-file" for loading files. A least on my system that solved this issue.
I just want to chime in that I experience a similar thing.
if I emacs file-x from the terminal, earlier undo tree history for file-x is not is not available to me before I undo-tree-load-history. However, when I open emacs and then :edit file-x, the history is loaded just fine.
I'm using spacemacs 0.105.19, with emacs-24.5.1.
My .spacemacs is https://pastebin.com/VNWtB0F1
Reason for this behavior is that when you start up emacs and open up file in buffer even with undo-tree-auto-save-history enabled so that undo-tree-load-history is called as it should be, buffer-undo-list is initially set to nil. If you make an edit before invoking any functionality of undo-tree, buffer-undo-list will have value that doesn't end with an undo-tree-canary.
When undo-list-transfer-to-tree sees buffer-undo-list not ending with undo-tree-canary, it will discard current buffer-undo-tree and construct new one with the value of buffer-undo-list which only have undo history of your initial edit. Consequently, buffer-undo-tree previously built by undo-tree-load-history is deleted, and you are left with fresh, minimal buffer-undo-tree only with single undo history.
So what I did to prevent this from happening was to initialize buffer-undo-list with undo-tree-canary by advising undo-tree-load-history-hook which should call undo-tree-load-history with undo-tree-auto-save-history set to t, and it is also called for opening file as part of find-file-hook.
That is, I added this line of code to my init file:
(advice-add 'undo-tree-load-history-hook :after (lambda () (setq buffer-undo-list '(nil undo-tree-canary))))
Put the (global-undo-tree-mode) in the :init will auto load history if you're using use-package
Example:
(use-package undo-tree
:init
(global-undo-tree-mode)
:custom
(undo-tree-auto-save-history t)
(undo-tree-history-directory-alist '(("." . "~/.config/emacs/undo-tree-history")))
(undo-tree-visualizer-diff t)
(undo-tree-visualizer-timestamps t))

How to remove the temporary files starting and ending with '#' created by Emacs when it closes?

When Emacs is closed with modified buffers present, it creates a file with the format '#file_name#' for recovery purpose in its parent directory for each modified buffer (except for scratch).
When recover-file command is used when Emacs opens the next time, the previously modified buffer of the file is recovered. Even so the file #file_name# is not deleted automatically.
This would not occur if you manually kill all buffers before closing. This is a bit tedious as you would have to use the command kill-some-buffer or kill-matching buffer and say 'yes' to each of the prompts one by one.
Is there a simpler method to overcome this? It would be nice to have solutions for one or more of the following.
Prevent Emacs from creating a recovery file for modified buffer while closing
A simple command to force kill all buffers without prompt to save
Setting to re-route the recovery files to a different location (like ~/.emacs.d/)
(Versions: Emacs-24 on Ubuntu-12.04 / OS-X-10.9)
By adding the following line on your init.el file you'll disable the auto-save feature and no more of these files will be generated
(setq auto-save-default nil)
But you may want those files so you can use the code above which I borrowed from emacswiki
(setq backup-directory-alist
`((".*" . ,temporary-file-directory)))
(setq auto-save-file-name-transforms
`((".*" ,temporary-file-directory t)))
This will cause that all backup files will go to the temporary folder on your operating system. Check the emacswiki link to know more about what you can do with this feature
More on this
Emacs manual entry on auto-save

Notify out-of-date buffers in the mode-line

Sometimes I use an alternative method (usually sed) to edit a file
that's already being edited by Emacs. Later, if I try to edit the file
in Emacs without reverting the changes first, I get an error message
and a prompt asking me what to do.
That's all fine. The problem is that I tend to forget very often when
I've made some parallel changes, so I'd like Emacs to
remind me by showing a red "M" in the mode-line.
I know how to customize the mode-line (by adding strings to the
mode-line-format variable), but I have no idea how to check if a
file has been modified outside of Emacs.
Is there a function to check whether an Emacs buffer is up to date
with the file it corresponds?
Try
(verify-visited-file-modtime (current-buffer))
See Section 27.6 Buffer Modification Time.
Not really a direct answer to the question, but you can avoid this problem by turning on auto-revert globally in emacs with (global-auto-revert-mode t).

Emacs: How can I use a save list to restore buffers from crash?

If you enable the desktop feature, you can return to your previously open set of files when you exit and reenter Emacs. This doesn't seem to be a crash recovery feature however.
If Emacs crashes, there is a save list called "saves-PID-machine" that has a list of the files that had buffers. The list has the full path to both the file itself and the corresponding ~ backup file.
How do I use this save list to get back to the set of visited files in buffers that I had before the crash? None of the files had edits pending so recover-session and recover-file don't do anything.
You can download and install https://github.com/tripleee/recover-buffers which visits all the files in the auto-save file, and offers to recover any for which unsaved auto-save data exists.
;;; recover-buffers.el --- revisit all buffers from an auto-save file
;;
;;; Commentary:
;;
;; Works like `recover-session', but attempts to really recover all state
;; back to the way it was when Emacs quit or crashed. Concretely, it
;; revisits all buffers which were open, however skipping any which match
;; an ignore list.
There is an open Emacs bug about this, too; http://debbugs.gnu.org/889
I am the author of this code, and would appreciate any feedback (though not via this public forum).
Tried M-x recover-session ?
(recover-session)
Recover auto save files from a previous Emacs session.
This command first displays a Dired buffer showing you the
previous sessions that you could recover from.
To choose one, move point to the proper line and then type C-c C-c.
Then you'll be asked about a number of files to recover.