wrong type argument commandp with a custom function in emacs - 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)

Related

bind key to negative-argument + command in 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)))

emacs ow can I helm-find with default directory pre-specified?

I use emacs for notes mainly. All my notes are in:
~/Dropbox/Uni/Notes
I want to tie a keyboard shortcut (e.g C-f12) to do a helm-find that always starts in the above dir irrelevant of the source buffer.
I have tried:
(global-set-key (kbd "C-<f2>") (lambda () (interactive) (helm-find "~/Dropbox/Uni/Notes/")))
But when I run it, it still prompts me for 'DefaultDirectory' which is usually the same as the current buffer.
?
[edit]
I made a hack-around:
(global-set-key (kbd "<C-f2>")
(lambda ()
(interactive)
(find-file "~/Dropbox/Uni/Notes/leo.org")
(helm-find nil)))
That opens a file and then when I do a helm-find, it's relative to leo.org's location. But a better solution would be preferred.
[edit]
Below solution works perfectly.
Here you go:
(defmacro helm-find-note (dir)
`(defun ,(intern (format "helm-find-note-%s" dir)) ()
(interactive)
(let ((default-directory ,dir))
(helm-find nil))))
(global-set-key (kbd "C-M-3") (helm-find-note "~/Downloads"))

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)))

Emacs mode specific keybinding - Symbol's value as variable is void error

I have tried to set rst-mode specific keybinding such as:
(eval-after-load "rst-mode"
(define-key rst-mode-map (kbd "<f5>")
(lambda ()
(interactive)
(shell-command "make html"))))
But I get:
Symbol's value as variable is void: rst-mode-map
Edited: thanks to the help, I've got the following working version:
(eval-after-load "rst"
'(progn
(define-key rst-mode-map (kbd "<f5>")
(lambda ()
(interactive)
(shell-command (concat "make " "html"))))))
eval-after-load requires the form to be quoted:
(eval-after-load "rst-mode"
'(define-key ...))
(otherwise you are evaluating the form immediately instead of after rst-mode has loaded.)

Emacs dired use the same buffer

I use the following code in .emacs:
(require 'dired+)
(toggle-diredp-find-file-reuse-dir 1)
So it won't create a buffer for every dir I visit. Then I decided to add some ergonomics:
(add-hook 'dired-mode-hook
(lambda ()
(define-key dired-mode-map (kbd "C-<up>") 'dired-up-directory)))
So when I click Ctrl-<up> it will move to the parent directory. But it opens the parent dir in a new buffer.
How to make it open in the same buffer?
The solution can be found there:
(add-hook 'dired-mode-hook
(lambda ()
(define-key dired-mode-map (kbd "C-<up>")
(lambda () (interactive) (find-alternate-file "..")))
; was dired-up-directory
))