I have an Emacs macro(named/saved) in a mymacro.el file
I would like to execute N times the macro in batch mode
like this:
emacs --batch -l ~/mymacro.el test.txt -f MyFoo
Question how to add N times in the lisp mymacro.el code ?
:-)
ErgoEmacs has a good page on this. What you want to do is reference the argv variable after calling emacs with the --script option. (elt argv 0) will give you the value of the first argument you specify. (elt argv 1) will give you the second argument, etc.
For example, let's say you call the following command:
emacs --script ~/mymacro.el ~/test.txt 22
Emacs will call the script ~/mymacro.el.
(elt argv 0) will give you "~/test.txt".
(elt argv 1) will give you "22".
(elt argv 3) will give you nil.
You can also use the variable command-line-args, outlined here.
To specifically batch-execute a macro a number of times, borrowing from this page on the wiki, we can create a script to run mymacro on a file, in the file run-mymacro.el:
;; Insert the macro here with `insert-kbd-macro`
(fset 'mymacro [...])
(defun : ()
(let ((file-name (elt argv 0))
(repeats (string-to-int (elt argv 1))))
(find-file file-name)
(condition-case nil
(execute-kbd-macro (symbol-function 'mymacro) repeats)
(error nil) )
(save-buffer) )
We can then call this script as so:
emacs --script ~/run-mymacro.el ~/test.txt 22
This will open the file ~/text.txt, run your macro 22 times and save the result. The original file should be saved in a backup with ~ at the end of the filename.
This is all untested, so it may need some tweaking before it works 100%.
As far as I understand your question, is it the case that you want to run the command emacs --batch -l ~/mymacro.el test.txt -f MyFoo N times?
If so, and if you are using Linux, it is easier to just use the for-loop of Linux shell.
#!/bin/bash
for i in {1..N} # replace N by your wanted number
do
emacs --batch -l ~/mymacro.el test.txt -f MyFoo
done
Related
In emacs you can do M-x cd to change the default directory.
I usually have 5 splits/windows, so the cd I do in the first split does't affect the others. What If I want the cd to affect all my splits/buffers.
Is there an alternative command I can use?
There's nothing built-in but it's not too hard to write the function by hand:
(defun cd-all-windows (dir)
(interactive "Ddirectory: ")
(dolist (window (window-list))
(with-current-buffer (window-buffer window)
(cd dir))))
Put that in your .emacs and you should be able to run M-x cd-all-windows to get the desired effect.
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))))) ))
I would like to use Emacs in batch mode to export a number of org files to HTML from the command-line. And I would like to get the same result than interactively using C-cC-eh, in particular:
honor file-local variables (such as org-export-publishing-directory)
honor all options specified through #+KEYWORD: headlines
Starting from the example given in org-export-as-html-batch, I got to this point:
emacs --batch \
--visit=/tmp/foo.org \
--eval "(defun safe-local-variable-p (sym val) t)" \
--funcall hack-local-variables \
--eval "(setq org-export-headline-levels 4)" \
--funcall org-export-as-html-batch
However, some problems remain:
I need to explicitly specify the headline level and I fail to see why all other #+OPTIONS are honored (like toc:nil) but not this one
I had to manually trigger file-local variables parsing using hack-local-variables (I guess it is not automatically done in batch mode) but more importantly I had to resort to a hack to mark all local variables as safe (I'm sure there is much space for improvement here).
NB:
In case it matters, I'm using emacs 23.2.1 (Debian Squeeze flavour)
Here is a sample org file on which I tested this:
#+TITLE: Foo
#+OPTIONS: H:4 toc:nil author:nil
* 1
** 2
*** 3
**** 4
# Local Variables:
# org-export-publishing-directory: "/some/where";
# End:
I eventually got the following script, which seems to fulfill all my requirements:
#!/bin/sh
":"; exec emacs --script "$0" -- "$#" # -*-emacs-lisp-*-
;;
;; Usage:
;; org2html FILE1 [FILE2 ...]
;; Mark org-related variables as safe local variables,
;; regardless of their value.
(defun my/always-safe-local-variable (val) t)
(dolist (sym '(org-export-publishing-directory
org-export-html-preamble
org-export-html-postamble))
(put sym 'safe-local-variable 'my/always-safe-local-variable))
(defun my/org-export-as-html (filename)
"Export FILENAME as html, as if `org-export-to-html' had been called
interactively.
This ensures that `org-export-headline-levels' is correctly read from
the #+OPTIONS: headline."
(save-excursion
(find-file filename)
(message "Exporting file `%s' to HTML" filename)
(call-interactively 'org-export-as-html)))
(mapcar 'my/org-export-as-html
(cdr argv)) ;; "--" is the first element of argv
A few notes on this script:
The executable emacs-lisp script trick comes from this question.
The only way I found to use the org-export-headline-levels value from the #+OPTIONS: headline is to call org-export-as-html interactively, instead of org-export-as-html-batch.
hack-local-variables does not need to be explicitly called, provided that local variables are marked as safe before the file is opened.
I think it is better to only mark org-related variables as safe, using the safe-local-variable symbol property.
I'm trying to start using eshell in place of bash within emacs, but I rely heavily on bash functions that I have written over the years. I'd like to configure eshell to invoke bash whenever a "command not found" condition occurs, in case the command in question is implemented as a bash function.
There is a variable tantalizingly named eshell-alternate-command-hook that sounds like it is made to order, but my lack of elisp skill is interfering with my success I think.
This is my best effort:
(add-hook 'eshell-alternate-command-hook 'invoke-bash t t)
(defun invoke-bash (command args)
(throw 'eshell-replace-command
(list "bash -c" command args)))
But when I test it, it doesn't work:
c:/temp $ lsd
Wrong number of arguments: (lambda (command args) (throw (quote eshell-replace-command) (list "bash -c" command args))), 1
c:/temp $
This is what I eventually came up with:
(defun invoke-bash (command)
(progn
(setq invoke-bash-cmd (concat "bash -c \"" command " " (mapconcat 'identity eshell-last-arguments " ") "\""))
(message invoke-bash-cmd)
(throw 'eshell-replace-command
(eshell-parse-command invoke-bash-cmd))))
I'm not eshell guru, but in the place where this hook is used, I see that it receives only one argument - command, that you trying to execute, so your code could look like
(add-hook 'eshell-alternate-command-hook 'invoke-bash)
(defun invoke-bash (command)
(throw 'eshell-replace-command
(list "bash -c" command)))
but it doesn't work, because you need to return elisp function, not name of command (according to documentation). If you want to run bash, then you need to return string with full path to it, but I hadn't found how to pass additional arguments to bash. Maybe you can find more in corresponding section on Emacs Wiki?
I'm trying to create a "system" command for clisp that works like this
(setq result (system "pwd"))
;;now result is equal to /my/path/here
I have something like this:
(defun system (cmd)
(ext:run-program :output :stream))
But, I am not sure how to transform a stream into a string. I've reviewed the hyperspec and google more than a few times.
edit: working with Ranier's command and using with-output-to-stream,
(defun system (cmd)
(with-output-to-string (stream)
(ext:run-program cmd :output stream)))
And then trying to run grep, which is in my path...
[11]> (system "grep")
*** - STRING: argument #<OUTPUT STRING-OUTPUT-STREAM> should be a string, a
symbol or a character
The following restarts are available:
USE-VALUE :R1 Input a value to be used instead.
ABORT :R2 Abort main loop
Break 1 [12]> :r2
Something like this?
Version 2:
(defun copy-stream (in out)
(loop for line = (read-line in nil nil)
while line
do (write-line line out)))
(defun system (cmd)
(with-open-stream (s1 (ext:run-program cmd :output :stream))
(with-output-to-string (out)
(copy-stream s1 out))))
[6]> (system "ls")
"#.emacs#
Applications
..."
Per the CLISP documentation on run-program, the :output argument should be one of
:terminal - writes to the terminal
:stream - creates and returns an input stream from which you can read
a pathname designator - writes to the designated file
nil - ignores the output
If you're looking to collect the output into a string, you'll have to use a read-write copying loop to transfer the data from the returned stream to a string. You already have with-output-to-string in play, per Rainer's suggestion, but instead of providing that output stream to run-program, you'll need to write to it yourself, copying the data from the input stream returned by run-program.
You are asking specifically about clisp. I'll add here that
if you are using Clozure CL then you can also easily run os subprocesses.
Some examples:
;;; Capture the output of the "uname" program in a lisp string-stream
;;; and return the generated string (which will contain a trailing
;;; newline.)
? (with-output-to-string (stream)
(run-program "uname" '("-r") :output stream))
;;; Write a string to *STANDARD-OUTPUT*, the hard way.
? (run-program "cat" () :input (make-string-input-stream "hello") :output t)
;;; Find out that "ls" doesn't expand wildcards.
? (run-program "ls" '("*.lisp") :output t)
;;; Let the shell expand wildcards.
? (run-program "sh" '("-c" "ls *.lisp") :output t)
Do a search for run-program in the CCL docs located here: http://ccl.clozure.com/ccl-documentation.html
There are a couple nice Lisp ways of doing this in this stackoverflow answer: Making a system call that returns the stdout output as a string Once again, Rainer to the rescue. Thanks Ranier.
This is a shorter one
(defun system(cmd)
(ext:shell (string cmd)))
> (system '"cd ..; ls -lrt; pwd")