Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

recompile

3 views
Skip to first unread message

lode leroy

unread,
Mar 13, 2013, 11:17:37 AM3/13/13
to help-gn...@gnu.org
I want to define a keyboard macro that does the following:
copy the current line, do a few search-and-replace operations and run it in a shell...
I haven't done anything non-trivial in elisp, so I need some help...

something along the lines of:

(defun recompile-current-line
  (beginning-of-line)
  (set-mark-command)
  (end-of-line)
  (copy-region-as-kill)
  (shell-command
   (replace-regexp "^" "cd ~/build && "
    current-kill)))

can someone give some advice on how to implement this?

Stephen Berman

unread,
Mar 14, 2013, 5:23:56 AM3/14/13
to help-gn...@gnu.org
Although you asked for a keyboard macro, your code example is more like
a Lisp function definition, so perhaps you'll be satisfied with an Emacs
Lisp command. In that case you can just tack a copy of the current line
(which is a substring of the buffer) onto the beginning of your shell
command string. Does the following do what you want (call it by typing
`M-x recompile-current-line')?

(defun recompile-current-line ()
"Execute current line as shell command after cd'ing to ~/build."
(interactive)
(let ((curline (buffer-substring-no-properties
(line-beginning-position) (line-end-position))))
(shell-command (concat "cd ~/build && " curline))))

Steve Berman


Doug Lewan

unread,
Mar 14, 2013, 9:16:08 AM3/14/13
to lode leroy, help-gn...@gnu.org
lode leroy,

The following might do roughly what you want.

(defun recompile-current-line ()
"Rerun the command on the current line in the ~/build directory."
(interactive)
(let* ((line-text (buffer-substring-no-properties
(line-beginning-position)
(line-end-position)))
(compile-command (concat "cd ~/build && " line-text)))
(shell-command compile-command)))

There are probably a couple things worth doing if you expect to have more tasks that need automation.

1. Learn emacs lisp. (You've already hinted at this. The next steps should help you with this.)
2. Name, save, study and edit your keyboard macros. (More below.)
3. Read the introduction to emacs lisp, `info "Emacs Lisp Intro"' brings it up. The table of contents alone gives a good idea of what is possible.
4. Use steps 2 & 3 to learn emacs lisp.

Regarding keyboard macros: `C-x C-k e' brings up special buffer for editing keyboard macros. It also contains hints (sometimes wrong unfortunately) about the corresponding lisp functions involved. Here's one that inserts "foo" with some corrections.

Macro:

f ;; self-insert-command
ooo ;; self-insert-command * 3
C-b ;; backward-char
C-k ;; kill-line
C-n ;; next-line

The corresponding elisp functions are on the right. They suggest the following function definition:

(defun insert-foo-extravagantly ()
"Insert `foo' extravagantly with some mistakes."
(interactive)
(insert "fooo")
(backward-char)
(kill-line)
(next-line))

I hope this helps.


,Douglas
Douglas Lewan
Shubert Ticketing
(201) 489-8600 ext 224

Journalism is printing what someone else does not want printed. Everything else is public relations. - George Orwell
0 new messages