Calling backup-buffer in after-save-hooks deletes original file - emacs

I am trying to create a backup for file after-save and before-save with the help of save hooks. Here is a code from .emacs
(defun force-backup-of-buffer ()
(interactive)
(setq buffer-backed-up nil)
(backup-buffer)
)
(add-hook 'before-save-hook 'force-backup-of-buffer)
(add-hook 'after-save-hook 'force-backup-of-buffer)
But this leads to deletion of original file. Can someone tell me why this happens?
My goal is to create two backup file everytime I save. I want to use same version control numbers as used by emacs. Hence I am using backup-buffer and not written my own hook to write file.

Check the documentation for backup-buffer, which points you to the make-backup-files variable: C-hv make-backup-files RET
By default, Emacs backs up by renaming the original file to the backup filename before saving the buffer to the original filename; hence your file disappearing.
I'm not sure that backup-buffer is really intended to be called elsewhere, however if you set it to backup by copying, you'll probably be okay.

Related

avoiding duplicating file when editing with emacs

I always notice this file that duplicates the file I'm editing whenever I use Emacs:
duplicated file
What code should I put in my ~/.emacs file to avoid this?
Those are backups, if you want to disable them add
(setq make-backup-files nil)
Alternatively, you can keep backups but store them all in a specific location by customizing backup-directory-alist, eg
(setq backup-directory-alist '(("" . "your/backup/directory")))

How to configure Emacs to save backup for files under temp directory?

I use emacsclient to edit temp files in /tmp a lot and would like to create backup copies of my files automatically like we do with other files. I'm sure there is a way to do it - but how? :)
(I searched the Emacs manual, emacswiki and SO but couldn't find anything useful)
Look at the normal-backup-enable-predicate function, which is the default value for the backup-enable-predicate variable.
As the sole purpose of the default function is to inhibit backups for files in various temporary directories, you may just want to set a replacement which returns t unconditionally.
(setq backup-enable-predicate (lambda (name) t))
The usage in files.el suggests to me that you could also just set this variable to nil. That's not stated in the documentation, so it might not be reliable, but the variable isn't referenced by any other library in Emacs, so it's probably fine (but I'd still recommend using the lambda, because it's more obvious what that's doing).
See also C-hig (elisp) Making Backups RET
n.b. I'm not actually familiar with small-temporary-file-directory (see the docstring for that variable), but the temporary-file-directory value would typically be /tmp/, so those two cases are usually the same.
If you did want to retain the default behaviour for some temporary directories but not others, you should define a modified copy of the original function: (defun my-backup-enable-predicate ...) and then (setq backup-enable-predicate 'my-backup-enable-predicate)
Stick this in yer .emacs file:
;; create an invisible backup directory so our directories
;; look a bit cleaner
;; thanks to #emacs in irc.freenode.org, Ryan Barrett of snarfed.org
;; and freethegnu.wordpress.com
(defun make-backup-file-name (filename)
(defvar backups-dir "/tmp/")
(make-directory backups-dir t)
(expand-file-name
(concat backups-dir (file-name-nondirectory filename) "~")
(file-name-directory filename)))

emacs: open all .txt files in a specific directory in a specific major mode

EDIT: It turns out that the second edit to my .emacs file actually works. (See the comments below this entry.)
I tried a couple of addition to the .emacs to make all txt files opened in emacs use orgmode. They did not work. How can I make it happen?
;;SET EMACS AS DEFAULT MAJOR MODE TO FOR ALL FILES WITH AN UNSPECIFIED MODE
(setq default-major-mode 'org-mode)
;;OPEN ALL TXT FILES IN ORGMODE
(add-to-list 'auto-mode-alist '("\\.txt$" . org-mode))
Additionally:
It would be even better to open only txt files in a certain directory orgmode. Any hint as to how that could be done would also be appreciated.
Another way to do this is using directory-local variables. This is nice because you can put a file in any directory where you want this behavior to engage, and it works recursively in any subdirectories.
Create a file called .dir-locals.el in the desired directory.
Here are the contents:
((nil (eval . (if (string-match ".txt$" (buffer-file-name))(org-mode)))))
Read this like so: for any major-mode (nil), evaluate the following form:
(if .... (org-mode))
The regex in auto-mode-alist could be something more complex, like "^/path/to/.*\\.txt$"
You can implement a hook which verifies the file directory and modifies the buffer mode:
(add-hook 'find-file-hooks
(lambda ()
(let ((file (buffer-file-name)))
(when (and file (equal (file-name-directory file) "c:/temp/"))
(org-mode)))))
As an alternative you can add the mode line in the beginning of your text file. In this case emacs will set the specified mode.
; -*- mode: org;-*-
* header 1
** header 2
I glued together some code from Oleg Pavliv's answer here, and from yibe's at elisp - File extension hook in Emacs - Stack Overflow
(defun use-org-mode-for-dot-txt-files-in-owncloud ()
(when (and (string-match owncloud buffer-file-name)
(string-match "\\.txt\\'" buffer-file-name))
(org-mode)))
(add-hook 'find-file-hook 'use-org-mode-for-dot-txt-files-in-owncloud)
This way, though ownCloud Web and phone apps are currently friendly only with .txt files, from my PC I can use Emacs' Org-mode for them.
(If I set all .txt files to use Org-mode, it breaks todotxt-mode.)
(Note that owncloud is a string variable equal to my ownCloud path.)

Emacs - intercept file before it is open with external program and modify it, then open modified verison

I need to process a file of certain types with external command line program accepting single argument (filename) and then use file modified by this program either open modified file or accept output of command line program as data source for file.
Any way to do this?
Where I used to work there were some binary files that I wanted to view in emacs. The way I did this was to add to jka-compr-compression-info-list like the following for editing applescripts:
(add-to-list 'jka-compr-compression-info-list
["\\.scpt\\'"
"Compiling" "osacompile-helper.sh" nil
"Decompiling" "osacompile-helper.sh" ("-d")
nil nil "Fasd"])
(jka-compr-update)
Here osacompile-helper.sh is just a little shell wrapper around osacompile and osadecompile that reads from stdin and writes to stdout (which is required). You also need to turn on auto-compression-mode, although I think that's the default. If you use the customize interface to change jka-compr-compression-info-list, instead of setting it directly, then you don't have to call jka-compr-update.
If you just want this to work when you open the file with C-x C-f, then you can probably just attach your behaviour to find-file, but deeper down I believe insert-file-contents is what eventually reads files in.
A cursory look doesn't seem to show any appropriate hook, so you could look at doing this with before advice.
(defadvice insert-file-contents
(before my-before-insert-file-contents-advice)
"Process files externally before reading them."
(let ((filename (expand-file-name (ad-get-arg 0))))
(message "About to read file %s" filename)
;; your code here.
;; ;; stupid unsafe example:
;; (let ((file (shell-quote-argument filename))
;; (tempfile (shell-quote-argument (make-temp-file "some-prefix-"))))
;; (shell-command (format "sort %s >%s" file tempfile))
;; (shell-command (format "mv %s %s" tempfile file)))
))
(ad-activate 'insert-file-contents)
You might like to elaborate on your requirements, in case you don't actually need to clobber the original file? (which I think is a horrendous idea, frankly; I certainly wouldn't use code like this!)
For example, you could read in the original file, process it within the buffer (maybe using shell-command-on-region with the replace flag), and set the buffer as unmodified. That way you are only likely to save the changes made by the shell command if you make other edits to the file, and the mere act of loading the file into an editor hasn't actually modified it.
In any case, I trust you'll implement sensible backup processes into your code, and will be plenty paranoid when testing!
You can call the external program with shell-command, with the output directed to a new buffer. A minimal working example is:
(defun my-find-and-process-file ()
(interactive)
(let* ((file (read-file-name "File name: "))
(buf (pop-to-buffer file)))
(shell-command (format "cat %s" file) buf)))
Replace cat with the name of your program. This will create a buffer and fill it with the output of your program. If a buffer with the name of your file already exists, it will over-write it. If that's a possibility, you will want to change the buffer name to something safe by adding a suffix or something. This code also doesn't trigger any of the find-file hooks, so you'll have to manually select the mode, or modify the code to do that for you.

What's the best way to handle multiple like-named files in emacs?

One problem that I have with emacs is that it doesn't seem to handle like-named files in different directories very well. For example, if I'm not careful, I'll end up with 20 __init__.py buffers open. What I've been doing is using M-x rename-buffer and renaming it to indicate what package it's within. However, doing this manually is somewhat tedious.
Does anyone have any strategies for attacking this problem?
I like uniquify, which comes with Emacs:
(require 'uniquify)
(setq uniquify-buffer-name-style 'reverse)
(setq uniquify-separator "/")
(setq uniquify-after-kill-buffer-p t) ; rename after killing uniquified
(setq uniquify-ignore-buffers-re "^\\*") ; don't muck with special buffers (or Gnus mail buffers)
With those settings, the directory gets added to the buffer name, giving you an indication of where the file is. For example, loading the files /some/path/to/Makefile and /some/path/to/different/Makefile would result in the following buffer names:
Makefile/to (which is /some/path/to/Makefile)
and
Makefile/different (which is /some/path/to/different/Makefile)
uniquify also handles updating the buffer names when buffers are deleted, so when one of the two Makefile buffers is deleted, the other gets renamed to simply Makefile.
If you want full control you can redefine create-file-buffer.
If you want the full filename it could be as simple as
(defun create-file-buffer (filename)
"Create a suitably named buffer for visiting FILENAME, and return it."
(generate-new-buffer filename))
See files.el for reference.