As I known, "C-u M-x shell" can be used to run multiple shells.
But how to define a function to do the same thing as "C-u M-x shell" do ?
(defun my-named-shell ()
"Equivalent to C-u M-x shell RET"
(interactive)
(shell (get-buffer
(read-buffer
"Shell buffer: "
(generate-new-buffer-name "*shell*")))))
I used describe-function and find-function to examine the behaviour of shell, and its interactive declaration in particular, and then I just copied the necessary code to turn that into an argument for a non-interactive call to the shell function (but wrapping it in get-buffer so as to provide a buffer argument).
I've actually left out some code which dealt with remote files, because the comments in that code seemed a bit confused. If you weren't in the habit of using C-u M-x shell in buffers accessing remote files via Tramp, that omission won't affect you.
That all said, an even simpler (and more complete) approach is simply:
(defun my-named-shell ()
"Equivalent to C-u M-x shell RET"
(interactive)
(let ((current-prefix-arg '(4)))
(call-interactively 'shell)))
For more information, refer to https://stackoverflow.com/a/9388058/324105
In this instance current-prefix-arg could be any non-nil value, but I think it's a good habit to use a value that C-u actually generates.
Related
I'm looking for a command to put an S-expression to the kill ring, without removing it.
The following scenario would do the thing, however the expression would be removed, when using M-x kill-sexp:
(foo (bar bam))
^
point here
There is no single chord, but you can do two:
both C-M-SPC and C-M-# run the command mark-sexp
M-w runs the command kill-ring-save
Alternatively, you can do
C-M-k runs the command kill-sexp
C-/ runs the command undo
If your buffer is read-only, the first command will fail, but the
S-expression will still be copied into the kill-ring.
There are many ways to do this (e.g. with the built-in thing-at-point, or just calling kill-sexp via call-interactively and restoring the original buffer contents after).
It's pretty straightforward to implement as a slightly modified kill-sexp though. This is what I use:
(defun copy-sexp-as-kill (&optional arg)
"Save the sexp following point to the kill ring.
ARG has the same meaning as for `kill-sexp'."
(interactive "p")
(save-excursion
(let ((orig-point (point)))
(forward-sexp (or arg 1))
(kill-ring-save orig-point (point)))))
(global-set-key (kbd "M-K") #'copy-sexp-as-kill)
I recommend installing smartparens then simply using sp-copy-sexp.
I've defined a major mode for my language.
I'm trying to redefine C-x C-e so that when I'm in my major mode, it'd evaluate the expression using "my" custom interpreter.
Suppose my interpreter is just a command-line program that could be invoked like this:
$my-interpreter <some expression>
I imagine, all I need to do is to do a system call, passing the expression "before point" as argument and print the return value in the echo area.
How hard could it be, right?
Problem: I have no idea where to start!
Any hint?
Thanks.
You can take a look at shell-command and its relatives, along with thing-at-point. Here's a really simple example that uses an "interpreter" (just the shell echo command) to echo the word at point:
(defun my-interpreter ()
(interactive)
(let ((arg (thing-at-point 'word)))
(when arg
(shell-command (concat "echo " arg)))))
(Edit in response to comment.)
If you have defined a keymap for your major mode, you can bind C-x C-e in you major mode with a call to define-key. Otherwise, you can just bind it locally with (local-set-key (kbd "C-x C-e") 'my-interpreter).
It occurred to me that you might be interested in building in interactive functionality like a REPL. To do so, you might consider comint-mode; see the EmacsWiki and this post from Mastering Emacs to get inspired.
In Emacs - how do I kill buffers matching regexp?
Edit:
How do I answer y automatically (kill-matching-buffers asks if I should kill a modified buffer)?
Something like this?
(defun bk-kill-buffers (bfrRgxp)
(interactive)
(kill-matching-buffers bfrRgxp)
[return])
How do I answer y automatically (kill-matching-buffers asks if I should kill a modified buffer)?
kill-matching-buffers calls kill-buffer-ask which calls yes-or-no-p. You could temporarily redefine the latter, but for safety reasons I am inclined not to do that -- killing a given buffer could trigger other functionality which needs to ask a yes-or-no question.
Redefining kill-buffer-ask seems a safer bet (or simply copying and modifying the kill-matching-buffers function itself).
(require 'cl)
(defun bk-kill-buffers (regexp)
"Kill buffers matching REGEXP without asking for confirmation."
(interactive "sKill buffers matching this regular expression: ")
(flet ((kill-buffer-ask (buffer) (kill-buffer buffer)))
(kill-matching-buffers regexp)))
For Emacs version >=24, the kmb.el library from ELPA repository
does exactly that with the command kmb-kill-matching-buffers-no-ask.
It also provides the command kmb-delete-process-and-kill-buffer-no-ask,
which kills the current buffer (without confirmation).
I bind the latter command as follows:
(global-set-key (kbd "H-M-<delete>") 'kmb-delete-process-and-kill-buffer-no-ask)
so that i don't call it accidentaly, just when i need it.
You can use kill-matching-buffers. Below code effectively behaves as if kill-buffer (which does not ask before killing) was called instead of kill-buffer-ask:
(defun kill-matching-buffers-just-do-it ()
"Kill buffers whose names match REGEXP, without asking."
(interactive)
(cl-letf (((symbol-function 'kill-buffer-ask) #'kill-buffer))
(call-interactively #'kill-matching-buffers)))
M-x kill-matching-buffers
This will prompt for a regex, check the documentation for details.
[I apologize for the poor title, but couldn't come up with a better one.]
bin chen asked on Google+:
How to input relative path of (buffer-file-name) in minibuffer after M-! in #emacs?
I thought if the buffer-file-name is saved in a register, it should be accessible by invoking insert-register (C-x r i) while at the shell-command prompt.
(defun save-buffer-file-name-in-register ()
(set-register ?F (buffer-file-name))
(set-register ?D (file-name-directory buffer-file-name)))
(defadvice shell-command (before save-buffer-file-name)
"Save buffer-file-name to register F before running shell-command"
(save-buffer-file-name-in-register))
(ad-activate 'shell-command)
When I invoke shell-command (M-!) followed by insert-register (C-x r i), I get the error message: Register does not contain any text.
But when I run list-registers I do see that the registers F and D are set with the appropriate values. If I run the shell-command again, I can access the values from the registers previously saved.
Is it possible that the registers are being set too late for the first time? How can I fix the code to do what I want?
Edit: Changed around to before (Thanks to #phils)
n.b. You have defined around advice, not before advice.
Around advice acts as a wrapper, and must include the token ad-do-it to execute the code of the function it is wrapping.
You have effectively replaced the body of the shell-command function with a call to save-buffer-file-name-in-register
As to your main question, I'd need to check the documentation, but I suspect that because the arguments to the advised function are available to advice, the original function's interactive declaration probably executes before the advice does, which would explain why your register values are not visible at the interactive shell-command prompt.
(If the around in the above code is indeed what you were using, the fact that you were still being prompted for a shell command would seem to verify this sequence.)
When the interactive form runs, your advice hasn't executed yet. See: this question
You need to specify an interactive form in your advice that redefines the original if you want to stick with this approach. However, this approach is a little fancy-pants for the sake of fancy-pants-ness.
Just define your own interactive function which does what you want without registers.
(defun insert-cur-dir ()
(interactive)
(let ((dir-name (file-name-directory (buffer-file-name (window-buffer (minibuffer-selected-window))))))
(insert (or dir-name ""))))
(define-key minibuffer-local-map (kbd "C-c i") 'insert-cur-dir)
An alternative, but way awesomer approach is to use yasnippet.
I'm using emacs and I have written a script which uses "current-buffer". However the emacs system doesn't recognise "current-buffer". When I try "M - x current-buffer" i get the response:
no match
: Any idea what I'm doing wrong?
current-buffer is not an interactive function. That is, can't be invoked interactively via M-x as you've tried to do. You can execute non-interactive lisp-code directly by using eval-expression as follows:
M-: (current-buffer) RET
Notice that you have to enter a proper lisp expression. If you want to capture the value in a variable, something like this
M-: (setq xyzzy (current-buffer)) RET
will store the current buffer into the variable xyzzy.
Do I interpret you correct that you have created a function named current-buffer that you want to be available with M-x current-buffer?
To enable functions to be called by M-x function-name the function needs to be marked as interactive.
A sample from the emacs manual:
(defun multiply-by-seven (number) ; Interactive version.
"Multiply NUMBER by seven."
(interactive "p")
(message "The result is %d" (* 7 number)))
The (interactive "p") part makes the function callable from the minibuffer (through M-x).
I sounds like you would (also) like to know how to get the name of the current buffer interactively. Use M-: (buffer-name).