using the current buffer's file name in M-x compile - emacs

I would like emacs to use the current buffer's file name as part of the command passed to M-x compile. For example, if I am editing ~/foo.rb, I would like M-x compile to execute ruby ~/foo.rb
I tried setting compilation-command to (list "ruby" buffer-file-name), but apparently you can't pass an s-expression here.

1. Read the documentation of the function
C-hfcompileRET
compile is an interactive autoloaded compiled Lisp function in
`compile.el'.
[snip]
Interactively, prompts for the command if `compilation-read-command' is
non-nil; otherwise uses `compile-command'. With prefix arg, always prompts.
Additionally, with universal prefix arg, compilation buffer will be in
comint mode, i.e. interactive.
Now that we've found what we were looking for, let's …
2. … read the documentation of the variable …
C-hvcompile-commandRET
compile-command is a variable defined in `compile.el'.
Its value is "make -k "
[snip]
Sometimes it is useful for files to supply local values for this variable.
You might also use mode hooks to specify it in certain modes, like this:
(add-hook 'c-mode-hook
(lambda ()
(unless (or (file-exists-p "makefile")
(file-exists-p "Makefile"))
(set (make-local-variable 'compile-command)
(concat "make -k "
(file-name-sans-extension buffer-file-name))))))
3. … and finally adapt the example to your needs
(add-hook 'ruby-mode-hook
(lambda ()
(set (make-local-variable 'compile-command)
(concat "ruby " buffer-file-name))))
Of course you can easily customize it to use rake if there is a
Rakefile.

Related

elisp - Get path to file relative to script

Say I am writing an emacs lisp function that interfaces with a file located relative to the file in which the function is defined.
- bin/executable
- foo.el
foo.el:
(defun foo ()
(shell-command-to-string
(format "echo '%s' | ./bin/executable"
(buffer-substring-no-properties
(point-min)
(point-max)))))
If I run this from foo.el then it works great. If I invoke the function while editing any other file it doesn't work because the path ain't right.
How can I reliably reference ./bin/executable from within foo.el no matter where the function is invoked?
Use load-file-name variable.
(defconst directory-of-foo (file-name-directory load-file-name))
(defun foo ()
(shell-command-to-string
(format "echo '%s' | %s"
(buffer-substring-no-properties
(point-min)
(point-max))
(expand-file-name "./bin/executable" directory-of-foo))))
You can use a combination of load-file-name and default-directory. If you only check the former, the file will work if you explicitly load it, but it will not work if you eval it in a buffer.
For example:
(defvar my-directory (if load-file-name
;; File is being loaded.
(file-name-directory load-file-name)
;; File is being evaluated using, for example, `eval-buffer'.
default-directory))
In addition, it might be a good idea to convert the path to an absolute path using expand-file-name.

Emacs and CWB under Windows

I'm trying to use the Edinburgh Concurrency Workbench (http://homepages.inf.ed.ac.uk/perdita/cwb/) with Emacs under Windows. I have placed the file cwb.el under C:\emacs\emacs-22.3\emacs-stuff. My .emacs file is located at C:\emacs and has the following content:
(setq load-path ; Look in my own library first.
(cons (expand-file-name "C:\emacs\emacs-22.3\emacsstuff")
load-path))
(autoload 'cwb "cwb" "Run a CWB process." t)
(autoload 'cwb-file-mode "cwb" "Major mode for editing CWB source." t)
(add-hook 'cwb-load-hook
(function
(lambda ()
(setq cwb-program-name "cwb7")))) ;; only necessary if your v7 isn't
;; called cwb
Yet, when I enter "M-x cwb", I get "Cannot open load file: cwb".
I tried to follow the instruction here: http://homepages.inf.ed.ac.uk/perdita/cwb/doc/emacs.html.
Thanks
In Emacs Lisp strings, backslash is an escape character, similar to C, so "C:\emacs\emacs-22.3\emacsstuff" ends up being "C:^[macs^[macs-22.3^[macsstuff". (You can try it with either M-: or M-x ielm.)
You can either write the path with forward slashes instead ("C:/emacs/emacs-22.3/emacsstuff") or use double backslashes ("C:\\emacs\\emacs-22.3\\emacsstuff").

One-shot command to knit and latexmk under Emacs + AUCtex

I want to knit AND latexmk Knitr documents using one AUCtex command.
I don't know how to code in lisp, and web search didn't turn up anything like this.
I have something close. The extension of the file needs to be changed for latexmk.
Any help will be appreciated.
Following line is for my .emacs file.
(add-hook 'LaTeX-mode-hook (lambda () (push '
("KnitrLaTeX" "Rscript -e \"library(knitr)\; knit('%s')\" && latexmk -pdf %s"
TeX-run-TeX nil t :help "Run knitr and latexmk on file")
TeX-command-list)))
When I run C-c C-c (KnitrLaTeX), emacs runs the following command:
Running `KnitrLaTeX' on `slides.Rnw' with ``Rscript -e "library(knitr); knit('slides.Rnw')" && latexmk -pdf slides.Rnw''
Which is wrong. It should read "... && latexmk -pdf slides.tex"
Thanks in advance.
It appears that you are having trouble with how the second usage of %s is being interpreted at the tail end of your compile command -- i.e., you want the second usage of %s to mean slides.tex instead of slides.Rnw.
Although I am not familiar with knit, I am familiar with creating custom variables for use with AUCTeX. Set forth below are some examples of how to create custom variables and add them to the TeX-expand-list.
Rather than of using %s for a second time (i.e., at the tail end of your compilation command), perhaps consider using %(tex-file-name) instead. This assumes that your *.tex file is open in the buffer with focus when you begin your compilation command -- i.e., the full file name will be inserted into your compilation command.
If you have a file with a different extension that is open in the buffer with focus when you run your compilation command, and if you want the base name to be the same (but with a different extension), then you would do something similar to the example of %(pdf-file-name) -- i.e., remove whatever extension is there and replace it with the new one.
(eval-after-load "tex" '(progn
(add-to-list 'TeX-expand-list '("%(tex-file-name)" (lambda ()
(concat "\"" (buffer-file-name) "\""))))
(add-to-list 'TeX-expand-list '("%(pdf-file-name)" (lambda ()
(concat
"\"" (car (split-string (buffer-file-name) "\\.tex"))
".pdf" "\""))))
(add-to-list 'TeX-expand-list '("%(line-number)" (lambda ()
(format "%d" (line-number-at-pos))))) ))

elisp: call command on current file

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!
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.
Maybe using the minor mode "auto-revert-mode" is an option.
Just enable it on the current buffer:
M-x "auto-revert-mode"
and always ensure the buffer is saved, before executing an external command.

P4CONFIG with emacs

I would like to see examples of how to setup perforce, using the config file functionality where emacs is used as the diff and merge programs (P4DIFF and P4MERGE settings). Even better if this is on Windows.
I'm also struggling with getting the P4EDITOR to work correctly when using emacsclientw, specifically specifying the alternate-editor functionality.
Any tips, suggestions, example configs are very welcome.
Here's a different trick I used to use. It adds a few command line options to emacs so that you can do diffs and merges in a new emacs instance (again using ediff).
;; -diff
(defun command-line-diff (switch)
(let ((file1 (pop command-line-args-left))
(file2 (pop command-line-args-left)))
(ediff file1 file2)))
(add-to-list 'command-switch-alist '("-diff" . command-line-diff))
;; -merge
(defun command-line-merge (switch)
(let ((base (pop command-line-args-left))
(sccs (pop command-line-args-left))
(mine (pop command-line-args-left))
(merg (pop command-line-args-left)))
(ediff-merge-with-ancestor sccs mine base () merg)))
(add-to-list 'command-switch-alist '("-merge" . command-line-merge))
Just put that in your .emacs file. Then you can set your P4DIFF program to be emacs -diff and your P4MERGE program to be emacs -merge.
I'm assuming you're already using p4.el.
Here's a function that will allow you to set your p4-client-config easily:
(defun p4-go (config)
(interactive
(list (read-file-name "P4 Config file: "
(concat (getenv "HOME") "/etc/perforce/")
""
t)))
(p4-set-client-config (expand-file-name config))
t)
Then I just run M-x p4-go <RET> conf <RET>.
My ~/etc/perforce/conf file looks like:
P4CLIENT=ewarmenhoven-ppd
P4PORT=perforce.netflix.com:1666
P4USER=ewarmenhoven
P4EDITOR=emacsclient
P4DIFF=diff -dupU8
P4MERGE=~/bin/emerge
The emerge merge program is just a short little shell script that calls emacsclient appropriately:
#!/bin/bash
base=$1
sccs=$2
mine=$3
merg=$4
emacsclient -e "(ediff-merge-files-with-ancestor \"$base\" \"$sccs\" \"$mine\" () \"$merg\")"
emacsclient "$merg"
If you're using cygwin it should work just fine.
For doing diffs, if it's running from the shell then I want the output in the shell, hence just using normal diff. If it's not, I use p4-ediff, which is bound to C-x p - by default.
The awesome answer by Eric doesn't work properly in latest emacs because of welcome screen. In order to hide the welcome screen (so that you may get the diff properly) please refer Unable to hide welcome screen in Emacs.
Another nifty setting which opens the diff in regular vertical mode is setting the below config variable
(custom-set-variables
;; custom-set-variables was added by Custom -- don't edit or cut/paste it!
;; Your init file should contain only one such instance.
'(ediff-split-window-function (quote split-window-horizontally)))