简体   繁体   中英

Emacs, convert “M-x command <ret> <tab>” to single command

I've been spoiled by my recent dip into Vim. It is easy to make a macro there that will combine key strokes that result in a prompt. For instance the following, with ,c , I get a prompt for color schemes, as well as the list generated by typing Tab .

" colorscheme switcher
nnoremap <leader>c :colorscheme <C-z><S-Tab>

Now I realize, I'm not sure how to do it Emacs. Here is a naive attempt at how the code would go,

(defun load-theme-prompt ()
       (interactive)
       (load-theme)
       (kbd "<tab>"))

Of course that won't work, and I did try recording a macro and inserting (even tried kmacro ), but it complained about ' not being a theme, but I didn't see any quotes in the macro output.

That is, I tried to record Mx load-theme <RET> <TAB> , and insert to buffer, but the function does not work, even if I trim parts off.

Here is the recorded macro that does not work,

(fset 'load-theme-prompt
   [?\M-x ?l ?o ?a ?d tab ?t ?h tab return tab])

Trying Mx load-theme-prompt gives me the following error,

load-theme: Invalid theme name `'

负载主题提示

With lawlist's comments, I've taken another shot, but now I get wrong number of arguments:

(defun load-theme-prompt ()
  (interactive 
   (list
    (intern (completing-read "Load custom theme: "
                             (mapcar 'symbol-name
                                     (custom-available-themes))))
   nil nil))
  (load-theme "%s")
  )

I've tried moving the nil nil into (load-theme "%s" nil nil) but still not working.

Just looking at your code, you should probably change it to something like this:

(defun load-theme-prompt (name arg2 arg3)
  (interactive 
   (list
    (intern (completing-read "Load custom theme: "
                             (mapcar 'symbol-name
                                     (custom-available-themes))))
   nil nil))
  (load-theme name arg2 arg3))

(interactive ...) passes arguments to the function, but your function takes no arguments. Also, you don't really want to pass a literal "%s" to load-theme.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM