emacs suggests to recover-file, but I missed it: how to make it prompt? - emacs

When emacs notices a crash, on next open of the file it "suggests" M-x recover file. But that only flashes up briefly, so I missed it this morning :( I went on editing, and lost last evening's work.
Is there a way to make that suggestion a prompt that must be responded to before it continues?

The warning message comes from the function after-find-file. I don't find an option to control this, but you can define a function to do something similar:
(defvar already-in-prompt-for-auto-save nil)
(defun prompt-for-auto-save-recovery ()
(if (and (not buffer-read-only)
(not already-in-prompt-for-auto-save)
(file-newer-than-file-p (or buffer-auto-save-file-name
(make-auto-save-file-name))
buffer-file-name)
(y-or-n-p (format "%s has auto save data: do you want to recover it? "
(file-name-nondirectory buffer-file-name))))
(let ((already-in-prompt-for-auto-save t))
(recover-this-file))))
and then install it as a hook.
(add-hook 'find-file-hook 'prompt-for-auto-save-recovery)
This is lightly tested code--I extracted what looked like the relevant parts of after-find-file--but maybe it will get you started in the right direction.

Related

Open Org Capture buffer in specific window?

I've been an Emacs user for about a year or so. I routinely have the same window set up each session (four windows).
I've set up capture templates and can capture what I want, but: instead of capture mode temporarily jerking me out of my window setup, I'd like the chosen capture template to open in a new (fifth) window, preserving my existing layout. I typically want the capture template open for a while, so it's disruptive.
This seems like it would be an obvious option, but I can't figure it out. Thanks in advance to all the Emacs heads out there.
I came up with a easier-to-use version of Dan's answer to the linked question:
(defun my-org-capture-place-template-dont-delete-windows (oldfun &rest args)
(cl-letf (((symbol-function 'delete-other-windows) 'ignore))
(apply oldfun args)))
(with-eval-after-load "org-capture"
(advice-add 'org-capture-place-template :around 'my-org-capture-place-template-dont-delete-windows))
That is, instead of having to modify Org-mode code and remove the call to delete-other-windows, this piece of code temporarily redefines delete-other-windows to ignore while org-capture-place-template is being called.
It doesn't do quite what you want: it picks one of the existing windows and puts the capture buffer there. At least it's better than the default behaviour of removing all previous windows but one.
There's probably a way to do what you want by customising the variable display-buffer-alist, but I couldn't figure it out...
You could also use https://github.com/raxod502/el-patch and patch org-capture after loading (look for the (el-patch-remove (delete-other-windows))):
(el-patch-feature org-capture)
(with-eval-after-load 'org-capture
(el-patch-defun org-capture-place-template (&optional inhibit-wconf-store)
"Insert the template at the target location, and display the buffer.
When `inhibit-wconf-store', don't store the window configuration, as it
may have been stored before."
(unless inhibit-wconf-store
(org-capture-put :return-to-wconf (current-window-configuration)))
(el-patch-remove (delete-other-windows))
(org-switch-to-buffer-other-window
(org-capture-get-indirect-buffer (org-capture-get :buffer) "CAPTURE"))
(widen)
(org-show-all)
(goto-char (org-capture-get :pos))
(setq-local outline-level 'org-outline-level)
(pcase (org-capture-get :type)
((or `nil `entry) (org-capture-place-entry))
(`table-line (org-capture-place-table-line))
(`plain (org-capture-place-plain-text))
(`item (org-capture-place-item))
(`checkitem (org-capture-place-item)))
(org-capture-mode 1)
(setq-local org-capture-current-plist org-capture-plist)) )
For some reason, the #legoscia approach fails for me in emacs 28.
So here is the el-patch snippet as suggested previously:
(el-patch-feature org-capture)
(with-eval-after-load 'org-capture
(el-patch-define-and-eval-template
(defun org-capture-place-template)
(el-patch-remove (delete-other-windows))))

Emacs bibtex-mode unable to parse un-visited file

I'm not sure the best way to phrase this question, but hopefully my examples will make clear what's going on.
I have some code where I want to insert the contents of a bibtex file in a temporary buffer and move through the entries one at a time, grabbing the entry using bibtex-parse-entry for later use. However, whenever I run the code on a bibtex file that I haven't visited during this emacs session, bibtex-parse-entry returns a (wrong-type-argument stringp nil) error.
Once I visit the file, even if I then close the buffer, the code runs without any issues. And if I remove the bibtex-parse-entry call, bibtex-kill-entry has the same issue.
Here's the elisp code I'm using:
(with-temp-buffer
(insert-file-contents "~/test.bib")
(goto-char (point-min))
(bibtex-mode)
(while (not (eobp))
(let* ((entry (bibtex-parse-entry t)))
(message "i'm here"))
(bibtex-kill-entry)
(bibtex-beginning-of-entry)
)
)
and a dummy .bib file:
#Article{test,
author = {joe shmo},
title = {lorem ipsum},
journal = {something},
year = {1990},
}
With these you should be able to reproduce my error.
I have no idea what's going on, so I'd greatly appreciate any help!
I am not really an expert at this. I just debugged the situation a bit (try M-x toggle-debug-on-error in cases like this) and found a call to looking-at with a nil value. The stack-trace tells us that the problem is in the bibtex function bibtex-valid-entry. There, I found the variable bibtex-entry-maybe-empty-head which --according to its docstring-- is set by bibtex-set-dialect.
Thus, adding a call to bibtex-set-dialect to your function after calling bibtex-mode seems to fix the issue. As I do not really know, what you want to achieve in the end, I am not sure it actually fixes your problem. At least the function does raise an error anymore.
Hope, that makes sense and helps.
(with-temp-buffer
(insert-file-contents "~/test.bib")
(goto-char (point-min))
(bibtex-mode)
(bibtex-set-dialect) ;; <-- add this
(while (not (eobp))
(let* ((entry (bibtex-parse-entry t)))
(message "i'm here"))
(bibtex-kill-entry)
(bibtex-beginning-of-entry)))

how do I run this Emacs M-x command "wg-update-workgroup" automatically before emacs closes?

I would like this to happen when I right click the launcher...icon..and select quit..and choose file exit. I would like this command to be run right before emacs exits....so when I right click the launcher icon and select quit then this command "wg-update-workgroup" is run then Emacs exits..I've tried learning about hooks but don't get how to add one....If someone can give me exact code I put into my initialization file I would be very grateful....tried binding a function to a key but get weird command p error.
Here's the code:
(add-hook 'kill-emacs-hook
(lambda ()
(wg-update-workgroup)))
UPD
It seems that the code isn't working for you since wg-update-workgroup needs an argument.
You have to test this yourself, since I don't really want to get familiar with the package.
Solution 1:
(add-hook 'kill-emacs-hook
(lambda ()
(wg-update-all-workgroups)))
Solution 2:
(add-hook 'kill-emacs-hook
(lambda ()
(call-interactively 'wg-update-workgroup)))
UPD: disregard everything from above:)
I'm pretty sure this is what you want:
(setq wg-query-for-save-on-emacs-exit nil)
(push (lambda()(or (ignore-errors
(wg-update-all-workgroups-and-save)) t))
kill-emacs-query-functions)
The first statement removes the extremely annoying y/n query about saving
workgroups on exit. The second statement saves everything unconditionally on exit.
Just to list my full configuration:
(require 'workgroups)
(workgroups-mode 1)
(setq wg-query-for-save-on-emacs-exit nil)
(wg-load "~/wg")
(push (lambda()(or (ignore-errors
(wg-update-all-workgroups-and-save)) t))
kill-emacs-query-functions)
You're right to be baffled. The issue is this: when Emacs begins exiting, workgroups.el cleans itself up earlier than 'kill-emacs-hook. What you want is this:
(add-hook 'kill-emacs-query-functions 'wg-update-all-workgroups)
The hook variable should then look something like this:
(wg-update-all-workgroups wg-emacs-exit-query)
I recommend using 'wg-update-all-workgroups rather than 'wg-update-workgroup if, like me, you have more than one workgroup saved in a given workgroups file.
IMO workgroups.el is the best Emacs session manager. Somebody new has just taken it over. I'm excited that a new version with even more features may be forthcoming:
https://github.com/pashinin/workgroups2/

is there an emacs tool to show a directory structure as a tree?

I am aware of Speedbar (I prefer the structure in the same frame as the rest of my work), and dired shows too much information. I'm after something like the svn-status tree representation. Is there anything like that?
Thank you.
EDIT: Here's what I found the most intuitive:
I am using Speedbar with the following hack from here. I had to use the commented "try this" part, FYI.
(require 'speedbar)
(defconst my-speedbar-buffer-name "SPEEDBAR")
; (defconst my-speedbar-buffer-name " SPEEDBAR") ; try this if you get "Wrong type argument: stringp, nil"
(defun my-speedbar-no-separate-frame ()
(interactive)
(when (not (buffer-live-p speedbar-buffer))
(setq speedbar-buffer (get-buffer-create my-speedbar-buffer-name)
speedbar-frame (selected-frame)
dframe-attached-frame (selected-frame)
speedbar-select-frame-method 'attached
speedbar-verbosity-level 0
speedbar-last-selected-file nil)
(set-buffer speedbar-buffer)
(speedbar-mode)
(speedbar-reconfigure-keymaps)
(speedbar-update-contents)
(speedbar-set-timer 1)
(make-local-hook 'kill-buffer-hook)
(add-hook 'kill-buffer-hook
(lambda () (when (eq (current-buffer) speedbar-buffer)
(setq speedbar-frame nil
dframe-attached-frame nil
speedbar-buffer nil)
(speedbar-set-timer nil)))))
(set-window-buffer (selected-window)
(get-buffer my-speedbar-buffer-name)))
So I wanted to just print a tree quickly and all Emacs based solutions seemed either rather old or massive like (ECB/CEDET) which I don't know enough about, so for quick fix I did
sudo apt-get install tree
then from dired buffer
M-! tree --dirsfirst RET
voilĂ ! directory tree without any specific libs, as an added bonus using tree -f outputs tree with full paths, making it obvious to jump to file with C-x C-f directly from the tree output buffer.
There is a tree extension for emacs mode Sunrise Commander. Sunrise Commander is like Midnight Commander.
There's the Emacs Code Browser. Last I heard using it with a current emacs may be tricky or difficult because it makes use of some tools that were recently integrated into Emacs via CEDET.
I'd recommend NeoTree. It's easy to install and easy to use.

How to get equivalent of Vim's :Texplore in Emacs?

I know about M-x dire, but would like to customize it. I would like to hit one key (for example F2) and get dire buffer open. When I navigate across the directory hierarchy it shouldn't open new buffers.
And when I finally open the file it also shouldn't open new buffer for it (not strictly necessary, but strongly preferred).
Of course this behavior can be global, i.e. for all dire buffers/invocations.
Check out dired-single, which pretty much does what you want (except that last bit, where it reuses the dired buffer for the newly visted file).
Caveat Lector: I wrote it, so I'm biased towards its usefulness.
Some alternatives - EmacsWiki: DiredReuseDirectoryBuffer, and this short snippet from an awkwardly-formatted blog-entry.
caveat: haven't tried them, myself.
I know this is very old but All you have to do is press 'a' on a dir or file to get this functionality. It's already there.
Here's what I finally used:
(require 'dired)
(global-set-key [(f2)] 'my-dired)
(defun my-dired ()
(interactive)
(dired (file-name-directory (buffer-file-name))))
(defadvice dired-advertised-find-file (around dired-subst-directory activate)
"Replace current buffer if file is a directory."
(interactive)
(let ((orig (current-buffer)) (filename (dired-get-filename :no-error-if-not-filep t)))
ad-do-it
(when (not (eq (current-buffer) orig)) (kill-buffer orig))))