I am trying to use One-Key Macros (as was written here)
(defun toggle-kbd-macro-recording-on ()
"One-key keyboard macros: turn recording on."
(interactive)
(define-key
global-map
(events-to-keys (this-command-keys) t)
'toggle-kbd-macro-recording-off)
(start-kbd-macro nil))
(defun toggle-kbd-macro-recording-off ()
"One-key keyboard macros: turn recording off."
(interactive)
(define-key
global-map
(events-to-keys (this-command-keys) t)
'toggle-kbd-macro-recording-on)
(end-kbd-macro))
(global-set-key '[(f1)] 'call-last-kbd-macro)
(global-set-key '[(shift f1)] 'toggle-kbd-macro-recording-on)
But when I press Shift-F1 I get error:
define-key: Symbol's function
definition is void: events-to-keys
How can I fix it?
The events-to-keys function seems to be something obsolete. From what I can tell, it should work by just writing:
(defun toggle-kbd-macro-recording-on ()
"One-key keyboard macros: turn recording on."
(interactive)
(define-key
global-map
(this-command-keys)
'toggle-kbd-macro-recording-off)
(start-kbd-macro nil))
(defun toggle-kbd-macro-recording-off ()
"One-key keyboard macros: turn recording off."
(interactive)
(define-key
global-map
(this-command-keys)
'toggle-kbd-macro-recording-on)
(end-kbd-macro))
(global-set-key '[(f1)] 'call-last-kbd-macro)
(global-set-key '[(shift f1)] 'toggle-kbd-macro-recording-on)
Related
I have this weird issue where whenever I type down :w or any other kind of command which can be used in vim's command mode (I am using evil-mode in emacs so that I can vim emulation) I always get this weird white cursor at the bottom of the minibuffer. Could you please tell me a way to get rid of this because it is really bugging me.
I thought that it may be useful if I included my init.el. If it is needed this is it:
(defun ali/initial-setup ()
"Basic Settings to make emacs usable"
(set-default-coding-systems 'utf-8)
(set-face-attribute 'default nil :font "Consolas" :height 120)
(setq scroll-margin 6)
(setq visible-bell 1)
(global-set-key (kbd "<escape>") 'keyboard-escape-quit)
(setq inhibit-startup-message t)
(tool-bar-mode -1)
(menu-bar-mode -1)
(scroll-bar-mode -1)
(setq mouse-wheel-scroll-amount '(1 ((shift) . 1)))
(setq mouse-wheel-progressive-speed nil)
(setq mouse-wheel-follow-mouse 't)
(setq scroll-step 3)
(setq-default indent-tabs-mode nil)
"Setting up fringes and truncated lines"
(global-visual-line-mode t)
(setq truncate-lines t)
(add-to-list 'default-frame-alist '(internal-border-width . 1))
(setq-default left-fringe-width 6)
"Making the line numbers wider"
(add-hook 'display-line-numbers-mode-hook '(lambda() (setq display-line-numbers-width 3)))
"Getting rid of autosave files and other stuff"
(setq make-lockfiles nil
make-backup-files nil)
(auto-save-mode -1)
"Making splits always be vertical by default"
(setq split-height-threshold nil
split-width-threshold 0)
(setq-default tab-width 4)
(setq-default indent-tabs-mode nil)
(setq-default c-basic-offset 4
c-default-style "linux"))
(defun ali/brackets()
"Automatically completing brackets and pairs"
(show-paren-mode 1)
(electric-pair-mode 1)
(setq electric-pair-pairs
'((?\" . ?\")
(?\{ . ?\}))))
;; Setting up packages
(require 'package)
(setq package-archives '(("melpa" . "https://melpa.org/packages/")
("gnu" . "https://elpa.gnu.org/packages/")))
(require 'package)
(package-initialize)
(unless package-archive-contents
(package-refresh-contents))
(defvar packages-we-need '(org modus-themes evil ido))
(dolist (package packages-we-need)
(unless (package-installed-p 'package)
(package-install 'package)))
(defun ali/ido()
"Setting up ido mode"
(require 'ido)
(setq ido-create-new-buffer 'always)
"Making ido-mode work in M-x"
(global-set-key
"\M-x"
(lambda ()
(interactive)
(call-interactively
(intern
(ido-completing-read
"M-x "
(all-completions "" obarray 'commandp))))))
(ido-everywhere t)
(ido-mode 1))
(require 'modus-themes)
(load-theme 'modus-vivendi t)
(defun ali/evil()
(require 'evil)
"Making cursor always be a box"
(setq evil-insert-state-cursor 'box)
(evil-make-overriding-map ali-keymap 'normal 'motion)
(evil-mode 1))
(let ((ali-keymap (make-sparse-keymap)))
(define-key ali-keymap (kbd "C-h") 'evil-window-left)
(define-key ali-keymap (kbd "C-j") 'evil-window-down)
(define-key ali-keymap (kbd "C-k") 'evil-window-up)
(define-key ali-keymap (kbd "C-l") 'evil-window-right)
(define-key ali-keymap (kbd "M-d") '(lambda() (interactive) (execute-kbd-macro (read-kbd-macro "C-x d RET"))))
(define-key ali-keymap (kbd "C-p") 'ido-switch-buffer)
(define-key ali-keymap (kbd "C-f") 'ido-find-file)
(define-key ali-keymap (kbd "M-=") 'enlarge-window-horizontally)
(define-key ali-keymap (kbd "M--") 'shrink-window-horizontally)
(define-key ali-keymap (kbd "C-=") 'text-scale-increase)
(define-key ali-keymap (kbd "C--") 'text-scale-decrease)
(define-key ali-keymap (kbd "C-/") 'comment-line)
(define-key ali-keymap (kbd "C-S-i") 'display-line-numbers-mode)
(define-key ali-keymap (kbd "M-v") 'evil-window-vsplit)
(define-key ali-keymap (kbd "M-h") 'evil-window-split)
(define-key ali-keymap (kbd "C-S-h") 'help)
(define-key ali-keymap (kbd "C-d") 'kill-buffer)
(define-key ali-keymap (kbd "M-1") '(lambda() (interactive) (execute-kbd-macro (read-kbd-macro "M-x make-directory RET"))))
(defvar ali-keymap ali-keymap
"These are my keybindings"))
(define-minor-mode ali-keybindings-mode
nil
:global t
:lighter " keys"
:keymap ali-keymap)
(ali-keybindings-mode 1)
(ali/initial-setup)
(ali/evil)
(ali/brackets)
(ali/ido)
(custom-set-variables
;; custom-set-variables was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
'(custom-safe-themes
'("27a1dd6378f3782a593cc83e108a35c2b93e5ecc3bd9057313e1d88462701fcd" "0feb7052df6cfc1733c1087d3876c26c66410e5f1337b039be44cb406b6187c6" "57e3f215bef8784157991c4957965aa31bac935aca011b29d7d8e113a652b693" "0f7fa4835d02a927d7d738a0d2d464c38be079913f9d4aba9c97f054e67b8db9" "ddff22007104a1317014e48ff3d4911a83771a4ccf57185ccebf7f91339dbfb8" default))
'(package-selected-packages '(geiser-stklos modus-themes evil)))
(custom-set-faces
;; custom-set-faces was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
)
I solved removing where I wrote (setq-default left-fringe-width 6) and instead I added (add-to-list 'default-frame-alist '(left-fringe . 5))
Here's a minimal snippet to get things going:
(define-prefix-command 'foo)
(define-key foo "a" 'bar)
(define-key foo "b" 'baz)
(global-set-key (kbd "C-M-r") 'foo)
Now I can "call" the foo keymap when I press C-M-r.
But I wonder how I can do this from code, e.g. something like:
(funcall (lambda-from-keymap 'foo))
After this call, I expect the focus to be in the minibuffer, expecting either
a, or b or C-h to be entered.
Is something like this possible?
You can use read-key-sequence and lookup-key to implement this:
(defun call-keymap (map &optional prompt)
"Read a key sequence and call the command it's bound to in MAP."
;; Note: MAP must be a symbol so we can trick `describe-bindings' into giving
;; us a nice help text.
(let* ((overriding-local-map `(keymap (,map . ,map)))
(help-form `(describe-bindings ,(vector map)))
(key (read-key-sequence prompt))
(cmd (lookup-key map key t)))
(if (functionp cmd) (call-interactively cmd)
(user-error "%s is undefined" key))))
If you hit C-h read-key-sequence still waits for you to complete the sequence. I think you could simulate Emacs' normal behaviour by looping with read-key instead, it's a bit more involved though.
Use it like this:
(defun bar () (interactive) (message "you called bar"))
(defun baz () (interactive) (message "you called baz"))
(define-prefix-command 'foo)
(define-key foo "a" 'bar)
(define-key foo "b" 'baz)
(global-set-key (kbd "C-M-r") 'foo)
(defun call-foo ()
(interactive)
;; Note: pass the symbol form of the keymap so we can give nice help
(call-keymap 'foo "enter a foo command: "))
(global-set-key (kbd "C-c f") 'call-foo)
If the keymap is bound to a key sequence, you can invoke it by emulating the exact key sequence by setting unread-command-events:
(setq unread-command-events
(mapcar (lambda (e) `(t . ,e))
(listify-key-sequence (kbd "C-M-r"))
You need to have foo interactive. I did so using:
(global-set-key (kbd "C-M-r") (lambda () (interactive) ( foo)))
This should fix your problem.
I added the following code to my .emacs file.
(defun delete-right-window ()
(interactive)
(windmove-right)
(delete-window))
(defun delete-left-window ()
(interactive)
(windmove-left)
(delete-window))
(defun delete-below-window ()
(interactive)
(windmove-down)
(delete-window))
(defun delete-above-window ()
(interactive)
(windmove-up)
(delete-window))
(global-set-key (kbd "C-s-<right>") 'delete-right-window)
(global-set-key (kbd "C-s-<left>") 'delete-left-window)
(global-set-key (kbd "C-s-<down>") 'delete-below-window)
(global-set-key (kbd "C-s-<up>") 'delete-above-window)
As you can see, most of the codes are repetitive.
I read How do I pass a function as a parameter to in elisp? and tried to refactor the code to passing windmove-* function as follow:
(defun delete-other-window (callback)
(interactive)
(funcall callback)
(delete-window))
...
(defun delete-right ()
(delete-other-window 'windmove-right))
And I bound keystroke like this:
(global-set-key (kbd "C-s-<right>") 'delete-right)
But when I hit C-s-<right> it doesn't work, only display Wrong type argument: commandp, delete-right in the mini buffer.
What am I missing or what should I do to work the code correctly?
Here's a fix:
(defun delete-after (fn)
`(lambda () (interactive)
(,fn)
(delete-window)))
(global-set-key (kbd "C-s-<right>") (delete-after 'windmove-right))
(global-set-key (kbd "C-s-<left>") (delete-after 'windmove-left))
(global-set-key (kbd "C-s-<down>") (delete-after 'windmove-down))
(global-set-key (kbd "C-s-<up>") (delete-after 'windmove-up))
To make a function into a command, you need to add the special form interactive to it:
(defun delete-right ()
(interactive)
(delete-other-window 'windmove-right))
The problem with your code is that it's the function bind to the key that should be interactive. Not one of the function it call:
(defun delete-other-window (callback)
(funcall callback)
(delete-window))
(defun delete-right ()
(interactive)
(delete-other-window 'windmove-right))
You could also use a macro:
(defmacro defun-delete-other-window (direction)
`(defun ,(intern (concat "delete-" direction)) ()
(interactive)
(,(intern (concat "windmove-" direction)))
(delete-window)))
(defun-delete-other-window "right")
I suppose my problem is pretty obvious, but I don't know Lisp and can't figure it out. Every time I try to write a capital "M" in a python file it won't work (It seems to think that's a start of a shortcut). My guess is that's somewhere in a module or my .emacs file it tries to bind something to Alt + something. But instead it binds it to capital M something. Here's my .emacs file (of which I copied most of it from the net):
; load-path setting is only needed if the directory you put
; weblogger.el in isn't already in your load-path
(add-to-list 'load-path "~/.emacs.d/")
; Remap Ctrl-tab to M-Tab
(define-key function-key-map [(control tab)] [?\M-\t])
(require 'ipython)
(define-key py-mode-map (kbd "M-") 'anything-ipython-complete)
(define-key py-shell-map (kbd "M-") 'anything-ipython-complete)
(define-key py-mode-map (kbd "C-c M") 'anything-ipython-import-modules-from-buffer)
(require 'python-mode)
(add-to-list 'auto-mode-alist '("\\.py\\'" . python-mode))
(require 'python-pep8)
(require 'python-pylint)
(require 'lambda-mode)
(add-hook 'python-mode-hook #'lambda-mode 1)
(require 'comint)
(define-key comint-mode-map (kbd "M-") 'comint-next-input)
(define-key comint-mode-map (kbd "M-") 'comint-previous-input)
(define-key comint-mode-map [down] 'comint-next-matching-input-from-input)
(define-key comint-mode-map [up] 'comint-previous-matching-input-from-input)
(autoload 'pylookup-lookup "pylookup")
(autoload 'pylookup-update "pylookup")
(setq pylookup-program "~/.emacs.d/pylookup/pylookup.py")
(setq pylookup-db-file "~/.emacs.d/pylookup/pylookup.db")
(global-set-key "\C-ch" 'pylookup-lookup)
(autoload 'autopair-global-mode "autopair" nil t)
(autopair-global-mode)
(add-hook 'lisp-mode-hook
#'(lambda () (setq autopair-dont-activate t)))
(add-hook 'python-mode-hook
#'(lambda ()
(push '(?' . ?')
(getf autopair-extra-pairs :code))
(setq autopair-handle-action-fns
(list #'autopair-default-handle-action
#'autopair-python-triple-quote-action))))
(require 'python-pep8)
(require 'python-pylint)
(add-hook 'before-save-hook 'delete-trailing-whitespace)
(autoload 'pylookup-lookup "pylookup")
(autoload 'pylookup-update "pylookup")
(setq pylookup-program "~/.emacs.d/pylookup/pylookup.py")
(setq pylookup-db-file "~/.emacs.d/pylookup/pylookup.db")
(global-set-key "\C-ch" 'pylookup-lookup)
;; Initialize Rope
(pymacs-load "ropemacs" "rope-")
(setq ropemacs-enable-autoimport t)
(require 'auto-complete)
(global-auto-complete-mode t)
;(when (require 'auto-complete nil t)
; (require 'auto-complete-yasnippet)
; (require 'auto-complete-python)
; (require 'auto-complete-css)
; (require 'auto-complete-cpp)
; (require 'auto-complete-emacs-lisp)
; (require 'auto-complete-semantic)
; (require 'auto-complete-gtags))
; (global-auto-complete-mode t)
; (setq ac-auto-start 3)
; (setq ac-dwim t)
; (set-default 'ac-sources '(ac-source-yasnippet ac-source-abbrev ac-source-words-in-buffer ac-source-files-in-current-dir ac-source-symbols))
; (load-library "init_python")
Any idea where I should start looking for the problem? A simple search or any other way to debug the files in the .emacs-directory?
This looks wrong to me:
(define-key py-mode-map (kbd "M-") 'anything-ipython-complete)
(define-key py-shell-map (kbd "M-") 'anything-ipython-complete)
(define-key comint-mode-map (kbd "M-") 'comint-next-input)
(define-key comint-mode-map (kbd "M-") 'comint-previous-input)
kbd expect a full binding, but you're trying to bind M-
to some functions. I suspect a letter is missing.
i.e.
(define-key py-mode-map (kbd "M-TAB") 'anything-ipython-complete)
Here, M stands for META which is usually bound to ALT
or ESC.
By the way, you can do XC-h to see
minor mode bindings starting with X.
I would like the "delete to end of word" command to delete the word, regardless of cursor position.
(defun my-kill-word ()
(interactive)
(backward-word)
(kill-word 1))
(global-set-key (kbd "M-d") 'my-kill-word)
A better code could be:
(defun my-kill-word ()
(interactive)
(unless (looking-at "\\<")
(backward-word))
(kill-word 1))
(global-set-key (kbd "M-d") 'my-kill-word)
So we move backward only if we are not at the beginning of the word
yet.