Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

elisp: call command on current file

Tags:

emacs

elisp

I want to set a key in emacs to perform a shell command on the file in the buffer, and revert the buffer without prompting. The shell command is: p4 edit 'currentfilename.ext'

(global-set-key [\C-E] (funcall 'revert-buffer 1 1 1)) 
;; my attempt above to call revert-buffer with a non-nil 
;; argument (ignoring the shell command for now) -- get an init error:
;; Error in init file: error: "Buffer does not seem to be associated with any file"

Completely new to elisp. From the emacs manual, here is the definition of revert-buffer:

Command: revert-buffer &optional ignore-auto noconfirm preserve-modes

Thanks!

like image 622
atp Avatar asked Jan 15 '11 00:01

atp


1 Answers

The actual error you're seeing is because you've specified the global-set-key incorrectly, namely the function call. What you want is:

(global-set-key (kbd "C-S-e") '(lambda () (revert-buffer t t t)))

You had the funcall actually evaluating when your .emacs was loading, which is what caused the error.

Then, to get the whole thing, you can create a command like:

(defun call-something-on-current-buffers-file ()
  "run a command on the current file and revert the buffer"
  (interactive)
  (shell-command 
   (format "/home/tjackson/bin/dummy.sh %s" 
       (shell-quote-argument (buffer-file-name))))
  (revert-buffer t t t))
(global-set-key (kbd "C-S-e") 'call-something-on-current-buffers-file)

Obviously customize the command, and add error checking if you want.

like image 56
Trey Jackson Avatar answered Sep 20 '22 10:09

Trey Jackson