0

This is probably a newbie question.

I'm trying to bind a keyboard macro to a key and have it available each time I load a file. I'm using lispbox.

This is the code I'm trying (the macro used here is just a placeholder):

(fset 'macro1
      (lambda (&optional arg) 
    "Keyboard macro." 
    (interactive "p") 
    (kmacro-exec-ring-item (quote ("uuu" 0 "%d")) arg)))
(global-set-key "[f5]" 'macro1)

But when evaluating, fset and global-set-key are undefined. I think I managed to avoid using fset by doing:

(setf (symbol-function 'macro1)...

But I can´t work my way around global-set-key. Calling the variable global-map shows the same message. I'm guessing, after searching a lot, that this isn't meant to work en CL but in Elisp. How should I proceed?

ad absurdum
  • 19,498
  • 5
  • 37
  • 60
  • `fset` and `global-set-key` are elisp functions. You should put your definitions in a `.emacs` or `init.el` file – ad absurdum Nov 22 '22 at 03:38
  • I never used Lispbox, but I use Slime. For custom keybindings I define functions in my `.emacs.d/init.el` file and then do something like `(add-hook 'lisp-mode-hook (lambda () (local-set-key "" 'my-function)))` to bind the key only when I open a lisp file. – ad absurdum Nov 22 '22 at 04:18
  • Your conclusion is correct -- you can't (or at least shouldn't) evaluate Emacs Lisp programs in a Common Lisp process. You need to tell Emacs to evaluate elisp programs. – phils Nov 22 '22 at 04:55
  • Check `C-h v user-init-file` in Emacs, and add your code to that file. See also `C-h i g (emacs)Init File`. – phils Nov 22 '22 at 04:58
  • Thank you very much for your answers. Yes, I was running the code in the Common Lisp repl. My understanding is very limited yet. I checked `C-h v user-init-file` and it reads "user-init-file is a variable defined in `C source code'. Its value is nil". Does this mean that there is currently no init file? – Mathiuston Nov 22 '22 at 05:18
  • 1
    @Mathiuston -- probably. You can create a new init file as `~/.emacs` or better `~/.emacs.d/init.el`. But, it seems that Lispbox does not load the emacs init file by default. [Here is an SO Q&A about the issue that might help.](https://stackoverflow.com/questions/5839829/how-to-load-emacs-configuration-file-in-lispbox) In any case, Lispbox is pretty outdated and AFAIK is unsupported. It is pretty straightforward to just install emacs using your package manager in Linux, then install Slime, then install SBCL. Or maybe try [Portacle](https://portacle.github.io/). – ad absurdum Nov 22 '22 at 06:03
  • OK. I'll try your suggestions with time tomorrow. You've helped me understand the problem better. Thanks. – Mathiuston Nov 22 '22 at 06:20
  • "user-init-file ... is nil" is highly unexpected. Even when the file does not *exist*, that variable *should* indicate a default path -- unless `emacs` was run with `--no-init-file` or equivalent, but in that case putting lisp in such a file will have no effect. Possibly `lispbox` (with which I am unfamiliar) is doing something unhelpful (or at least non-standard) here. You may need to look at its documentation. (Edit: Oh, ad absurdum has covered this in their comment.) – phils Nov 22 '22 at 07:49
  • See also https://emacs.stackexchange.com for any question related to Emacs (which Lispbox configures a certain way) – coredump Nov 22 '22 at 09:55
  • 1
    Following your suggestions, I tried using Portacle and could find the init file with `C-h v user-init-file`. It was "user.el". The macro worked fine after copying it there. I will later try to setup SBCL as I read that it is a more mantained implementation. Lot to learn yet. Thank you all for your valuable advice. – Mathiuston Nov 23 '22 at 01:39
  • @Mathiuston Portacle ships the SBCL implementation. Portacle is a portable combination of: Emacs (editor), SBCL (CL implementation), Slime (Emacs package for CL), Git and a few other Emacs packages. – Ehvince Nov 23 '22 at 09:59

0 Answers0