How do I uncompress/unzip within Emacs - emacs

I would like to run unzip (or even zip) within dired or a dired-like buffer. Is there anything like this? I would like something similar as in the Nautilus file manager: i.e., selecting files and then pressing a keystroke to get these files into a new archive file.
Thank you

You've got options...
To uncompress a .zip file, you just need to add to the variable 'dired-compress-file-suffixes
(eval-after-load "dired-aux"
'(add-to-list 'dired-compress-file-suffixes
'("\\.zip\\'" ".zip" "unzip")))
Now the Z key in dired will recognize the .zip extension and uncompress a .zip archive. Already supported are gunzip, bunzip2, uncompress and dictunzip.
If you want to mark files and add them to a .zip archive you can use the following to make z bound to zip the set of marked files:
(eval-after-load "dired"
'(define-key dired-mode-map "z" 'dired-zip-files))
(defun dired-zip-files (zip-file)
"Create an archive containing the marked files."
(interactive "sEnter name of zip file: ")
;; create the zip file
(let ((zip-file (if (string-match ".zip$" zip-file) zip-file (concat zip-file ".zip"))))
(shell-command
(concat "zip "
zip-file
" "
(concat-string-list
(mapcar
'(lambda (filename)
(file-name-nondirectory filename))
(dired-get-marked-files))))))
(revert-buffer)
;; remove the mark on all the files "*" to " "
;; (dired-change-marks 42 ?\040)
;; mark zip file
;; (dired-mark-files-regexp (filename-to-regexp zip-file))
)
(defun concat-string-list (list)
"Return a string which is a concatenation of all elements of the list separated by spaces"
(mapconcat '(lambda (obj) (format "%s" obj)) list " "))

To zip files, open the directory in dired. Mark the files you want to zip with m. Then type
! zip foo.zip * <RET>
To extract an entire archive from dired you can mark a file and run & unzip, just as you would in a shell.
zip-archive mode will allow you to browse zip files in a dired-like fashion. It should come with recent versions of GNU emacs and will be used by default when you visit a file with the .zip extension. From this mode you can extract individual files into a buffer, and from there save them with C-x C-s.

Related

Emacs - I cannot save buffer into file because of backup

I am using C-x C-s to save a change in a buffer to a file. I am getting the following in my minibuffer:
symbol's variable as value is void: “/home/alex/\.emacs_backups/”
I added .emacs_backups/ a couple of days ago and I altered my .emacs file to:
;; create a backup file directory
(defun make-backup-file-name (file)
(concat “/home/alex/.emacs_backups/” (file-name-nondirectory file) “~”))
This does not happen in every directory. In some directories I can save a buffer change to a file no problem.
You have "smart quotes" in your .emacs. Elisp uses ASCII doublequotes as string delimiters:
(defun make-backup-file-name (file)
(concat "/home/alex/.emacs_backups/" (file-name-nondirectory file) "~"))
I replaced “ and ” with "
After you edit your .emacs, you should evaluate this new function definition with C-x C-e, so that you'll be able to save it without getting an error.

Emacs: extending dired-do-compress to directories

If I press 'Z' in dired (or x-dired) mode in emacs, the file under the cursor is compressed or uncompressed.
I'd like to extend this behavior to directories. That is, if the cursor is at a directory "stuff", I'd like 'Z' to run
tar -zcf stuff.tgz stuff
(on the assumption that 'tar' is provided by the OS).
Side note:
The converse (expanding stuff.tgz into the full directory tree) can already be done by '!', which suggests guesses for expansion. The asymmetry ('Z' to compress and '!' to uncompress) does not bother me.
Cool idea. Here's a start that works for me: just need to change a few lines in dired-compress-file. I've highlighted the changes with BEGIN EDIT and END EDIT comments (sorry if there's a better way to highlight a diff). I'm sure this isn't robust, but maybe it can point you in the right direction.
EDIT: I should say that the below works for me in GNU Emacs 24.3.1.
(defun dired-compress-file (file)
;; Compress or uncompress FILE.
;; Return the name of the compressed or uncompressed file.
;; Return nil if no change in files.
(let ((handler (find-file-name-handler file 'dired-compress-file))
suffix newname
(suffixes dired-compress-file-suffixes))
;; See if any suffix rule matches this file name.
(while suffixes
(let (case-fold-search)
(if (string-match (car (car suffixes)) file)
(setq suffix (car suffixes) suffixes nil))
(setq suffixes (cdr suffixes))))
;; If so, compute desired new name.
(if suffix
(setq newname (concat (substring file 0 (match-beginning 0))
(nth 1 suffix))))
(cond (handler
(funcall handler 'dired-compress-file file))
((file-symlink-p file)
nil)
((and suffix (nth 2 suffix))
;; We found an uncompression rule.
(if (not (dired-check-process (concat "Uncompressing " file)
(nth 2 suffix) file))
newname))
(t
;;; We don't recognize the file as compressed, so compress it.
;;; Try gzip; if we don't have that, use compress.
(condition-case nil
;; BEGIN EDIT - choose the correct name if looking at a directory
(let ((out-name (if (file-directory-p file) (concat file ".tar.gz") (concat file ".gz"))))
;; END EDIT
(and (or (not (file-exists-p out-name))
(y-or-n-p
(format "File %s already exists. Really compress? "
out-name)))
;; BEGIN EDIT: create a tarball if we're looking at a directory
(not (if (file-directory-p file)
(dired-check-process (concat "Compressing " file)
"tar" "-zcf" out-name file)
(dired-check-process (concat "Compressing " file)
"gzip" "-f" file)))
;; END EDIT
(or (file-exists-p out-name)
(setq out-name (concat file ".z")))
;; Rename the compressed file to NEWNAME
;; if it hasn't got that name already.
(if (and newname (not (equal newname out-name)))
(progn
(rename-file out-name newname t)
newname)
out-name)))
(file-error
(if (not (dired-check-process (concat "Compressing " file)
"compress" "-f" file))
;; Don't use NEWNAME with `compress'.
(concat file ".Z"))))))))
The following is a link to the library dired-tar -- it tars and compresses directories and files. I have verified it works on OSX with a recent version of Emacs Trunk built on March 19, 2014.
http://www.emacswiki.org/emacs/DiredTar
This functionality is now installed in master:
press Z on a directory to run tar -czf dirname.tar.gz dirname
press Z on a *.tar.gz file to run tar -zxvf dirname
The latter can be customized via dired-compress-file-suffixes, if you ever need to use something other than -zxvf.

Emacs auto-save: Why are files not stored in the correct folder?

I looked in various places and finally came up with the following setup for 'auto-save' mode in Emacs:
(defvar my-auto-save-folder (concat "~/.emacs.d/auto-save")); folder for auto-saves
(setq auto-save-list-file-prefix "~/.emacs.d/auto-save/.saves-"); set prefix for auto-saves
(setq auto-save-file-name-transforms `((".*", my-auto-save-folder t))); location for all auto-save files
(setq tramp-auto-save-directory my-auto-save-folder); auto-save tramp files in local directory
After having this setup for some weeks, I visited ~/.emacs.d and found that the folder ~/.emacs.d/auto-save is empty, while ~/.emacs.d contained two auto-save files of the form #!home!<myusername>!<myfolder>!<myfile>. Why are the auto-save files not stored in ~/.emacs.d/auto-save? [the folder auto-save has rights 775, .emacs.d 700]
Your error is in:
(defvar my-auto-save-folder (concat "~/.emacs.d/auto-save")); folder for auto-saves
(the call to concat with a single argument is pointless, incidentally).
If the optional element UNIQUIFY is non-nil, the auto-save file name is
constructed by taking the directory part of the replaced file-name,
concatenated with the buffer file name with all directory separators
changed to `!' to prevent clashes.
Emacs identifies directory names by a trailing /, which means that "the directory part" of the path you've used is "~/.emacs.d/".
You want:
(defvar my-auto-save-folder "~/.emacs.d/auto-save/"); folder for auto-saves
The positioning of the comma in the following is also strange (although apparently it still works):
`((".*", my-auto-save-folder t)))
That should really be:
`((".*" ,my-auto-save-folder t)))
This is what i have in my .emacs, which works well for me:
(add-to-list 'auto-save-file-name-transforms
(list "\\(.+/\\)*\\(.*?\\)" (expand-file-name "\\2" my-auto-save-folder))
t)

How to [tar and] compress marked files in Emacs

In dired+ in Emacs 23.2.1 on a Debian Squeeze variant I selected four files with * and then pressed Z to compress them. I answered y to the prompt and saw some status updates in the mini-buffer. Where do I find the compressed file? I tested on one file (C-u Z) and Emacs ran gzip on the one file and made it a .gz file. How do I [tar and] compress marked files in Emacs?
(To preempt any philosophical or methodological discussions about tar, gzip, other formats and archives in general, all I want is the four files to be stored in one file as compressed data. If that can be achieved via tar and gzip or compressing each directly into an archive doesn't matter.)
If dired+ is anything like dired, you can mark the files with m and then hit ! (to run a shell command on the marked files) and specify the command as tar -czf foo.tar.gz * (the * is a special marker that is replaced by the names of the marked files).
You can also archive files just by marking and copying them to an archive file.
For example, mark several files in dired, and select m-x dired-do-copy.
When prompted for destination, type test.zip. The files will be added to the zip archive automatically.
You can also uncompress files by selecting them in dired and running the command dired-do-extract
To set this up, look at the following variables:
dired-to-archive-copy-alist
dired-extract-alist
Here's my setup, which has served me for many years...
;; dired-a provides support functions, including archiving, for dired
(load "dired-a")
;; Alist with information how to add files to an archive (from dired-a)
;; Each element has the form (REGEXP ADD-CMD NEW-CMD). If REGEXP matches
;; the file name of a target, that target is an archive and ADD-CMD is a command
;; that adds to an existing archive and NEW-CMD is a command that makes a new
;; archive (overwriting an old one if it exists). ADD-CMD and NEW-CMD are:
;; 1. Nil (meaning we cannot do this for this type of archive) (one of
;; ADD-CMD and NEW-CMD must be non-nil).
;; 2. A symbol that must be a function e.g. dired-do-archive-op.
;; 3. A format string with two arguments, the source files concatenated into
;; a space separated string and the target archive.
;; 4. A list of strings, the command and its flags, to which the target and
;; the source-files are concatenated."
(setq dired-to-archive-copy-alist
'(("\\.sh\\(ar\\|[0-9]\\)*$" nil "shar %s > %s")
("\\.jar$" ("jar" "uvf") ("jar" "cvf"))
("\\.tar$" ("tar" "-uf") ("tar" "-cf"))
("\\.tgz$\\|\\.tar\\.g?[zZ]$" ("tar" "-uf %s" "|" "gzip > %s") ("tar" "-czvf"))
("\\.ear$" ("zip" "-qr") ("zip" "-qr"))
; ("\\.rar$" ("rar" "a") ("rar" "a"))
("\\.war$" ("zip" "-qr") ("zip" "-qr"))
("\\.zip$" ("zip" "-qr") ("zip" "-qr"))
("\\.wmz$" ("zip" "-qr") ("zip" "-qr")) ;; for media player skins
("\\.arc$" ("arc" "a") nil)
("\\.zoo$" ("zoo" "aP") nil)
))
;; use pkzip with manipulating zip files (t) from within dired (use zip
;; and unzip otherwise)
(setq archive-zip-use-pkzip nil)
;; add these file types to archive mode to allow viewing and changing
;; their contents
(add-to-list 'auto-mode-alist '("\\.[ejrw]ar$\\'" . archive-mode))
;; modify the dired-extract switches to use the directory
;; ~/download/tryout as the default extract directory for zip files
(defconst MY_TRYOUT_DIR "~/downloads/tryout"
"Directory for extracting files")
(setq dired-extract-alist
`(
("\\.u\\(ue\\|aa\\)$" . dired-uud)
("\\.jar$" . "jar -xvf %s")
("\\.tar$" . ,(concat "tar -xf %s -C " MY_TRYOUT_DIR))
("\\.tgz$\\|\\.tar\\.g?[zZ]$" . ,(concat "tar -xzf %s -C " MY_TRYOUT_DIR))
("\\.arc$" . "arc x %s ")
("\\.bz2$" . ,(concat "bunzip2 -q %s"))
("\\.rar$" . ,(concat "unrar x %s " MY_TRYOUT_DIR "\\"))
("\\.zip$" . ,(concat "unzip -qq -Ux %s -d " MY_TRYOUT_DIR))
("\\.ear$" . ,(concat "unzip -qq -Ux %s -d " MY_TRYOUT_DIR))
("\\.war$" . ,(concat "unzip -qq -Ux %s -d " MY_TRYOUT_DIR))
("\\.zoo$" . "zoo x. %s ")
("\\.lzh$" . "lha x %s ")
("\\.7z$" . "7z e %s ")
("\\.g?[zZ]$" . "gzip -d %s") ; There is only one file
))
Platform: Ubuntu
Requirements
System:
sudo apt-get install atool
Emacs:
M-x package-list-packages
C-s dired-atool RET
i
x
Solution
1) Mark the files in dired buffer.
2) M-x dired-atool-do-pack
NOTE: Make sure that shell-file-name is set to "/bin/bash".
In answer to the latter question, another tool which can be used to generate a compressed tar file from dired is pack. Mark the desired files and execute pack-dired-do-pack, specifying whatever.tar.gz as the output file name.

Erlang emacs mode - setting outdir

Does anyone know how to configure Erlang emacs mode so that compiling a buffer [C-c C-k] writes the beam file to the ebin directory rather than the current directory ?
Thanks!
You might want to have a look to this thread on the Erlang Questions Mailing List:
http://www.erlang.org/pipermail/erlang-questions/2007-August/028367.html
Moreover, you should be able to compile your file in debug mode:
C-u C-c C-k
The erlang-compile command should support prefix arguments. You might want to have a look to:
http://www.gnu.org/software/emacs/manual/html_node/elisp/Prefix-Command-Arguments.html
If you set up your directory structure like so:
/
/src/
/ebin/
and place your module (e.g. "my_file.erl") in the "/src/" directory then compile it (C-c C-k) then Emacs should automatically put the beam into the "/ebin/" directory.
However, if your module isn't in a directory named "/src/" (or if the "ebin" directory is missing) the beam will be dropped alongside the source file.
To see exactly how this works take a peek at $ERL_TOP/lib/tools/emacs/erlang.el and search for "ebin". Here's what you'll find:
(defun inferior-erlang-compile-outdir ()
"Return the directory to compile the current buffer into."
(let* ((buffer-dir (directory-file-name
(file-name-directory (buffer-file-name))))
(parent-dir (directory-file-name
(file-name-directory buffer-dir)))
(ebin-dir (concat (file-name-as-directory parent-dir) "ebin"))
(buffer-dir-base-name (file-name-nondirectory
(expand-file-name
(concat (file-name-as-directory buffer-dir)
".")))))
(if (and (string= buffer-dir-base-name "src")
(file-directory-p ebin-dir))
(file-name-as-directory ebin-dir)
(file-name-as-directory buffer-dir))))
Not sure when this goody was added, but it was in OTP_R13B03 and it works for me in R14B03.