bind key to negative-argument + command in emacs - emacs

I am trying to bind to M-b a command preceded by a negative argument. I post my code.
working
(global-unset-key (kbd "M-f"))
(global-set-key (kbd "M-f") 'forward-whitespace)
not working
(global-unset-key (kbd "M-b"))
(global-set-key (kbd "M-b") (lambda () (interactive) (negative-argument(forward-whitespace))))
How can I make it working?

The specific issue you have is solved easily:
(global-set-key (kbd "M-b") (lambda () (interactive) (forward-whitespace -1)))
You might wonder if you can write a macro that would call an arbitrary command interactively with negative argument.
It is not really hard:
(defmacro call-with-negative-argument (command)
`(lambda ()
(interactive)
(prefix-command-preserve-state)
(setq prefix-arg '-)
(universal-argument--mode)
(call-interactively ,command)))
(global-set-key (kbd "...") (call-with-negative-argument forward-whitespace))
but, really, any compliant function would also work fine like this:
(defmacro call-with-negative-argument (command)
`(lambda ()
(interactive)
(,command -1)))

Related

Emacs bindings documentation

I am writing some bindings to lambdas in my ~/.emacs and would like to have a description of what the function does appear when I do (for example) C-c ?. I tried to put a string immediately after lambda () but that still does nothing. How do I get something relevant to appear in the binding column?
Example that still functionally works but documentation doesn't:
(global-set-key (kbd "M-p") (lambda ()
"Moves the current line up by one"
(interactive)
(let ((col (current-column)))
(transpose-lines 1)
(forward-line -2)
(forward-char col))))
You should use defun to define your interactive function and bind to that.
(defun my-func ()
"Moves the current line up by one"
(interactive)
(let ((col (current-column)))
(transpose-lines 1)
(forward-line -2)
(forward-char col)))
(global-set-key (kbd "M-p") 'my-func)

wrong type argument commandp with a custom function in emacs

I know this type of question have been asked by many people,
but I have read many similar posts and still have no idea
what to do. So here is the elisp code in .emacs:
;; send line to python console
(require 'python-mode)
(defun py-execute-line-down ()
"execute python line and move cursor down"
(progn
(py-execute-line)
(evil-next-line)))
(add-hook 'python-mode-hook
(lambda () (define-key python-mode-map (kbd "C-c C-j") 'py-execute-line-down)))
I also tried to add (interactive) into the function, it didn't work.
Just to keep the record here, this seemed to do the trick, not sure if it's optimal though:
;; send line to python console
(require 'python-mode)
(defun py-execute-line-down ()
"execute python line and move cursor down"
(interactive)
(py-execute-line)
(evil-next-line 1))
(defun kaiyin-pykeys ()
"python mode custome keys"
(local-set-key (kbd "C-c j") 'py-execute-line-down)
)
(add-hook 'python-mode-hook 'kaiyin-pykeys)
Taking Dan's advice, I changed the above into:
;; send line to python console
(require 'python-mode)
(defun py-execute-line-down ()
"execute python line and move cursor down"
(interactive)
(py-execute-line)
(forward-line 1))
(define-key python-mode-map (kbd "C-c j") 'py-execute-line-down)

Emacs: how to get the global shortcut value

I have the following global keyboard shortcut in Emacs:
(global-set-key (kbd "C-<right>") 'forward-word)
For the org-mode I decided to redefine this shortcut. If the cursor stands on a link, then go to the link location. Otherwise - use forward-word function.
(defun is-link-p ()
(if (org-in-regexp org-bracket-link-regexp)
t))
(defun follow-link-or-next-word ()
(interactive)
(if (is-link-p)
(org-open-at-point)
(forward-word)))
(add-hook 'org-mode-hook (lambda ()
(define-key org-mode-map (kbd "C-<right>") 'follow-link-or-next-word)))
Is it possible to change org-mode shortcut in the following manner: instead of calling (forward-word), find what function is globally bound to "C-<right>" and call it instead.
Thus I won't need to change (forward-word) twice in case I decide to change the global shortcut.
I think you're looking for the function (lookup-key keymap key &optional accept-defaults)
This function returns the definition of key in keymap. All the other
functions described in this chapter that look up keys use lookup-key.
Here are examples:
(lookup-key (current-global-map) "\C-x\C-f")
⇒ find-file
(lookup-key (current-global-map) (kbd "C-x C-f"))
⇒ find-file
You could extend your functions:
(defun is-link-p ()
(if (org-in-regexp org-bracket-link-regexp)
t))
(defun follow-link-or-default-action()
(interactive)
(let ((global-default (lookup-key (current-global-map) (kbd "C-<right>"))))
(if (is-link-p)
(org-open-at-point)
(funcall global-default))))
(add-hook 'org-mode-hook (lambda ()
(define-key org-mode-map (kbd "C-<right>") 'follow-link-or-default-action)))

How do I configure emacs to bind something to meta-up?

This binds an operation to Control-p
(global-set-key (kbd "C-p") (λ () (interactive) (previous-line 5)))
I would like to instead bind it to Meta-UpArrow.
Thanks!
(global-set-key (kbd "M-<up>") (λ () (interactive) (previous-line 5)))
I figured this out by typing "C-h k" and then pressing meta + uparrow.
If use global set key, that mapping will be shadowed by a mode-specific mapping. So, while your mapping works in markdown-mode where there is no mode specific mapping for "M-<up>", it won't work in org-mode where <M-up> maps to org-metaup.
So, if you really want to map "M-<up>" even if it may conflict with some modes that you use, you can do the following:
(global-set-key (kbd "M-<up>") (lambda () (interactive) (previous-line 5)))
(require 'org)
(define-key org-mode-map (kbd "M-<up>") (lambda () (interactive) (previous-line 5)))
However, you need to do this (define-key) for every mode that you use that already has a mapping for <M-up>.

emacs lisp call function with prefix argument programmatically

I want to call a function from some elisp code as if I had called it interactively with a prefix argument. Specifically, I want to call grep with a prefix.
The closest I've gotten to making it work is using execute-extended-command, but that still requires that I type in the command I want to call with a prefix...
;; calls command with a prefix, but I have to type the command to be called...
(global-set-key (kbd "C-c m g")
(lambda () (interactive)
(execute-extended-command t)))
The documentation says that execute-extended-command uses command-execute to execute the command read from the minibuffer, but I haven't been able to make it work:
;; doesn't call with prefix...
(global-set-key (kbd "C-c m g")
(lambda () (interactive)
(command-execute 'grep t [t] t)))
Is there any way to call a function with a prefix yet non-interactively?
If I'm understanding you right, you're trying to make a keybinding that will act like you typed C-u M-x grep <ENTER>. Try this:
(global-set-key (kbd "C-c m g")
(lambda () (interactive)
(setq current-prefix-arg '(4)) ; C-u
(call-interactively 'grep)))
Although I would probably make a named function for this:
(defun grep-with-prefix-arg ()
(interactive)
(setq current-prefix-arg '(4)) ; C-u
(call-interactively 'grep))
(global-set-key (kbd "C-c m g") 'grep-with-prefix-arg)
Or you could just use a keyboard macro
(global-set-key (kbd "s-l") (kbd "C-u C-SPC"))
In this example, the key combination "s-l" (s ("super") is the "windows logo" key on a PC keyboard) will go up the mark ring, just like you if typed "C-u C-SPC".