help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Trying to bind a new key in C mode


From: August
Subject: Re: Trying to bind a new key in C mode
Date: Sat, 12 Feb 2005 03:41:49 +0100

On fre, 2005-02-11 at 18:09 -0600, Jim Hunter wrote:
> I would like to bind C-c c to run the 'compile' function in C mode 
> (and, ultimately, C-c r to run 'recompile'), but cannot get it to
> work.  Unfortunately, I know nothing about Lisp, so I simply copied
> the example from the Emacs info page "Local Keymaps" and modified it
> to this (in my .emacs file):
> 
> (add-hook 'c-mode-hook
>               '(lambda ()
>                  (define-key c-mode-map "\C-cc" 'compile)))
> 

Try

(add-hook 'c-mode-hook
          '(lambda () (local-set-key "\C-cc" 'compile)))

Since compiling and running a program are such common operations I
instead have the following definitions in my `.emacs'. The global key
settings apply to all buffers in all modes.

(global-set-key [f11] 'compile)
(global-set-key [f12] 'my-shell-command)

(defun my-shell-command ()
  "Similar to `shell-command', but shows previous command as initial
contents or (if there is no previous command) guesses the name of the
program you want to run based on the name of the current buffer and
inserts that as initial contents."
  (interactive)
  (shell-command 
   (read-from-minibuffer "Shell command: " 
                         (let ((f (buffer-file-name)))
                           (when f
                             (let ((bin (file-name-sans-extension 
                                         (file-name-nondirectory f))))
                               (cond (shell-command-history 
                                      (car shell-command-history))
                                     (t
                                      (concat "./" bin " &"))))))
                         nil nil 'shell-command-history)))

-- 
August




reply via email to

[Prev in Thread] Current Thread [Next in Thread]