问题
I know Emacs automatically opens compressed files like .tar.gz
. I'm trying to figure how to achieve this with my own compression script rather than the standard ones.
Following this link, I added the following to my Emacs init file
(if (fboundp 'auto-compression-mode)
(auto-compression-mode 0)
(require 'jka-compr))
(add-to-list 'jka-compr-compression-info-list
["\\.customcom\\'"
"custom compressing" "customcom" (-c)
"custom decompressing" "customcom" (-d)
nil t])
(auto-compression-mode 1)
Ideally, i want to run the command customcom -d foo.customcom
when the file is opened but with the above addition, its running customcom -d < foo.cusotmcom
and giving an error. How can I modify the above to nullify the input redirection so that it takes the filename only rather than the contents of the file or is there a different way of approaching this problem?
回答1:
Maybe, the following code is helpful. By the way, it is interesting that the author of jka-compr
did not take the evaluation of the program arguments into consideration and did not provide filename
as one of the possible evaluable arguments.
I do not know your compression/uncompression program. Therefore, I just used cat
for files ending with .cat
instead for testing.
(defadvice jka-compr-info-compress-args (around eval-args activate)
"Evaluate program arguments"
(setq ad-return-value (mapcar 'eval (aref info 3))))
(defadvice jka-compr-info-uncompress-args (around eval-args activate)
"Evaluate program arguments"
(setq ad-return-value (mapcar 'eval (aref info 6))))
(add-to-list 'jka-compr-compression-info-list ["\\.cat\\'" "cat" "cat" ("-")
"cat uncompress" "cat" (filename) nil t ""])
(add-to-list 'auto-mode-alist '("\\.cat\\'" nil jka-compr))
(add-to-list 'file-name-handler-alist '("\\.cat\\'" . jka-compr-handler))
Corresponding enhancement-request:
http://debbugs.gnu.org/cgi/bugreport.cgi?msg=5;att=1;bug=16454
回答2:
It's hard to test this without knowing what your custom compression and decompression tools are, but using xargs
seems to work for me on Linux:
(if (fboundp 'auto-compression-mode)
(auto-compression-mode 0)
(require 'jka-compr))
(add-to-list 'jka-compr-compression-info-list
["\\.customcomm\\'"
"custom compressing" "xargs" ("customcom" "-c")
"custom decompressing" "xargs" ("customcom" "-d")
nil t])
(auto-compression-mode 1)
Note that you may still have problems if the output of this command is not to STDOUT
.
回答3:
You might like to try and use ("-d" "-")
as argument, in case customcom
follows the custom of interpreting -
to mean "use stdin". Or depending on your OS, you could try ("-d" "/dev/stdin")
: it should work under GNU/Linux at least.
来源:https://stackoverflow.com/questions/21118197/how-to-automatically-decompress-a-custom-compressed-file-when-opened-in-emacs