it is said in the manual that if you use kill-region sequentially, the texts you kill will be concatenated into one in the kill-ring.
I'm just confused how this works. So I tried to eval this in the scratch buffer:
(progn
(kill-region 1 5) ; this kills ";; T"
(kill-region 1 5)); this kills "his "
what I expect is that, since I use kill-region 2 times, the killed texts should be concatenated as one in the kill-ring.
but when I use C-y, I get only "his ".
So I have 2 questions here:
in lisp, how to invoke kill-region several times so that the killed texts are concatenated?
using keyboard C-w, how to invoke kill-region several times so that the killed texts are concatenated? since the typical workflow is kill-region(C-w), then move-cursor, then kill-region again.
here is the doc string of kill region. isn't the 2nd paragraph and the last paragraph contradictory?
"Kill (\"cut\") text between point and mark.
This deletes the text from the buffer and saves it in the kill ring.
The command \\[yank] can retrieve it from there.
\(If you want to save the region without killing it, use \\[kill-ring-save].)
If you want to append the killed region to the last killed text,
use \\[append-next-kill] before \\[kill-region].
If the buffer is read-only, Emacs will beep and refrain from deleting
the text, but put the text in the kill ring anyway. This means that
you can use the killing commands to copy text from a read-only buffer.
Lisp programs should use this function for killing text.
(To delete text, use `delete-region'.)
Supply two arguments, character positions indicating the stretch of text
to be killed.
Any command that calls this function is a \"kill command\".
If the previous command was also a kill command,
the text killed this time appends to the text killed last time
to make one entry in the kill ring."
The documentation refers to commands, not functions. A command is a function
that initiates the command loop.
Any command that calls this function is a \"kill command\".
If the previous command was also a kill command,
the text killed this time appends to the text killed last time
to make one entry in the kill ring.
This does not mean kill-region per se. It's saying any command that calls the
kill-region function becomes a "kill command" (including kill-region
itself). E.g. kill-line kill-word, etc
in lisp, how to invoke kill-region several times so that the killed texts
are concatenated?
Use kill-append.
(progn
(kill-region 1 5) ; this kills ";; T"
(kill-region 1 5)); this kills "his "
what I expect is that, since I use kill-region 2 times, the killed texts
should be concatenated as one in the kill-ring.
You called kill-region twice but not as commands. Both these calls happen
within the same command loop run.
Related
I have this code:
(defun do-repeated-work (args)
"some work that need executed repeatedly"
(message nil)
(message "doing some repeated work, arg = %s, current time = %s" args (format-time-string "%H:%M:%S")))
(setq timer (run-with-idle-timer 3 t 'do-repeated-work (list "arg1" "arg2" "arg3")))
The purpose of the code above is: print a line of message in minibuffer repeatedly every three seconds. But I found that, when the function do-repeated-work works again, the old message in emacs minibuffer cannot be cleared, so the new message cannot be displayed. I have already tried the way mentioned in this question: how to empty or clear the emacs minibuffer?, but it doesn't work.
My Emacs version is 25.3
How to cope with this problem?
You've made an incorrect assumption, and consequently your problem isn't what you think it is.
The purpose of the code above is: print a line of message in minibuffer repeatedly every three seconds.
That's not what it does.
You've used run-with-idle-timer which will run one time once Emacs has been idle for 3 (in this case) seconds, and will not repeat until some non-idle activity has taken place -- after which it will again run once Emacs has become idle for 3 seconds.
See C-hf run-with-idle-timer
If you want something which repeats at a consistent interval, use run-with-timer.
Sometime the content of the minibuffer shows the output of a command (emacs 24). This is not too much of an inconvenience when the output is just one line. It's more annoying when the command is multiple lines long and the minibuffer uses many lines of display that could be used for something else.
Is there a way to clear the content of the minibuffer ?
Note: When I M-! echo usage: foo ; echo the minibuffer content changes to usage: foo.
Note: I'm not in recursive edit, the minibuffer is not active, using C-g, M-x C-g , (message nil), M-x delete-minibuffer-contents, M-: (kill-buffer " Echo Area 0") does not clear the minibuffer
Normally, C-g works just fine in those cases. It'll print "Quit" in the minibufer, which is just one line and unobtrusive enough.
If you need to clear the minibuffer programmatically, call (message nil).
If, for some reason, C-g does not work for you, make a new command and a keybinding for clearing the minibuffer
(defun my-clear-message ()
(interactive)
(message nil))
(global-set-key (kbd "C-c c") 'my-clear-message)
My guess, from your description ("the minibuffer is not active") and your replies to other answers, is that it is not the minibuffer that needs clearing - it is the echo area.
This is the same physical space, but the echo area is for output (e.g. message), whereas the minibuffer is primarily for input.
To clear the echo area, use (message nil), as suggested. Or use the minibuffer, followed by C-g - e.g., M-x C-g. That usually takes care of the job (but see below, about killing the echo-area buffer, if you really need to clear it).
If it really is the minibuffer input that you want to clear, then:
C-g (repeated, if necessary) quits the minibuffer.
You can use any text-clearing keys to clear the input without exiting. E.g., C-x DEL will clear quite a bit (it is backward-kill-sentence). I bind M-k (normally kill-sentence) to a command that deletes all of the minibuffer input (this is the case in Icicles, for instance). Command delete-minibuffer-contents wipes it all out.
(You can also kill the echo-area buffer, if it should ever get polluted with some text you want to get rid of. The buffer will be re-created automatically. With vanilla Emacs it is a bit problematic to do this interactively, but you can at least do it using M-: (kill-buffer " *Echo Area 0*") (note the SPC char prefix).)
Although I do not understand why following clears the minibuffer: M-! echo ; echo
This output is normally cleared as soon as you do anything else in Emacs. But sometimes Emacs gets confused and the message keeps reappearing in the echo area.
Deleting the buffer named *Shell Command Output* would solve the problem, but when this happens to me Emacs refuses to delete this buffer. Deleting the contents of this buffer solves the problem, but output reappears in the buffer the next time you do a shell command.
You can prevent that problem by renaming the buffer. One way to do that is to make that buffer current (e.g., with C-x b or M-x switch-to-buffer) and do M-x rename-uniquely. If you do that and delete the contents of the buffer, the problem is avoided.
I have no idea why sometimes Emacs refuses to kill this buffer, but fortunately it allows renaming it so it will no longer be reused for command output.
Try pressing C-g, this will clear the echo area.
Well. When I type some first keys of the key series, emacs write those keys in minibuffer after some interval of time. Like that: Typing C-x 4 will make C-x 4- visible in minibuffer.
The question is: can this be modified? I was thinking about making something like combining part of key-help (generated by C-h when type some keys) with this string.
Can interval for waiting this message be shorten too?
Is it subroutine?
Edited, new question
There is a message when I quit emacs with C-x C-c and have modified buffers, that ask me if I want to save them. How can I know that this message is here? I tried to look in (minibuffer-prompt) (minibuffer-contents) (buffer-substring (point-min) (point-max)), selecting (select-window (minibuffer-window)). Nothing gives me results.
Yes, the user option echo-keystrokes controls how much time elapses before the prefix key is shown in the minibuffer. From (emacs) Echo Area Customization:
User Option: echo-keystrokes
This variable determines how much time should elapse before command
characters echo. Its value must be an integer or floating point
number, which specifies the number of seconds to wait before
echoing. If the user types a prefix key (such as `C-x') and then
delays this many seconds before continuing, the prefix key is
echoed in the echo area. (Once echoing begins in a key sequence,
all subsequent characters in the same key sequence are echoed
immediately.)
If the value is zero, then command input is not echoed.
You can control the timing of this help message by setting suggest-key-bindings to a larger/smaller number.
(setq suggest-key-bindings 5) ; wait 5 seconds
There is no easy way to customize the behavior, you'd have to edit the C code for execute-extended-command, or use a replacement for it which also provides the help. One possibility for a replacement is the anything-complete library which has a replacement for execute-extended-command (note: I haven't tried it). It builds on top of the package anything, which is a different experience than the standard Emacs.
I wrote working version of what I wanted to implement.
To use, (require 'keylist), copy one or two last lines to .emacs and uncomment them.
As you can see through this code, I used this
(not cursor-in-echo-area)
(not (minibufferp))
(not (= 13 (aref (this-command-keys-vector) 0)))
to find out, if my minibuffer, or echo area is in use.
The difference between them is that minibuffer is used to read, and echo area is used to message something.
When you type C-x C-c cursor is placed in echo area, and value of cursor-in-echo-area is changed.
The last string (= 13 (aref (this-command-keys-vector) 0)) is the most funny. It is used to catch things like query-replace. When making raplacements, (this-command-keys-vector) shows that RET is the first key pressed, then keys of your choise(y,n). As far as I don't have key-sequences starting with RET, i am okay with this.
Something I do often in Emacs is to cut a bit of text, and then replace another bit with the cut text. So, say I've got the text I want to yank as the last item in my kill-ring. I yank it into the new place, then kill the text that was already there. But now the killed text is the latest item in the kill-ring. So next time I want to yank the first item, I have to do C-y M-y. Then the next time there are two more recent items in the kill-ring, so I have to do C-y M-y M-y, and so on.
I'm guessing there's a better way to do this. Can someone enlighten me please?
Several alternatives:
Turn on delete-selection-mode, and use C-d or delete to delete region without touching the kill-ring.
Use C-x r s i to save text to register i, and later, C-x r i i to insert the saved text.
If the pattern of texts to be replaced can be captured in a regular expression, use query-replace-regexp (C-M-%).
You should use delete-region instead of kill-region.
delete-region deletes the region without putting it in the kill ring. It is bind to <menu-bar> <edit> <clear> by default.
If you only want to use default bindings without using the menu, you could use delete-rectangle with C-x r d but it works on rectangle. It could be fine to use it on a single line like delete-region.
One of the oldest and best kept secrets in Emacs -- dunno why: Emacs has a secondary selection.
And this is exactly what it is good for. It saves another selection of text for you to use, over and over.
Select some text, then yank the secondary in to replace it. Repeat elsewhere. Often this is more convenient, flexible, and precise than something like query-replace.
Please take a look, for your own good -- maybe it will stop being such a little-known feature...
http://www.emacswiki.org/emacs/SecondarySelection
I wrote this function to pop the newest item off the kill-ring:
(defun my-kill-ring-pop ()
"Pop the last kill off the ring."
(interactive)
(when kill-ring
(setq kill-ring (cdr kill-ring)))
(when kill-ring-yank-pointer
(setq kill-ring-yank-pointer kill-ring))
(message "Last kill popped off kill-ring."))
So after I kill something I don't want to keep, I hit a key that calls this.
Related to this is M-x browse-kill-ring. If you use M-x anything, you can also use M-x anything-show-kill-ring.
This is scenario that I ran into a few times:
I copy some text from other program. Go to Emacs and did some editing before I paste/yank the text in. C-y to yank and voila ... not the text I intended to paste in. Then I realize that while I am moving things around, I used commands like kill-line and backward-kill-word, and those killed lines and words now occupied the kill-ring. But typing M-y does not bring the original copied text back, so I need to go back to my original program to copy the text again. And even worst if the original program is closed, then I lost the copied text completely.
Kill-line, etc. are such basic commands (like hitting the delete key, almost), and while I don't mind that the kill-ring gets a bit cluttered by using those command, I expect that my original text stays somewhere in the kill-ring so that I can eventually find it by typing M-y a few times. How can I make Emacs to automatically preserve the current clipboard content into the kill-ring before overriding the clipboard content?
This code should automatically put the selection (from outside Emacs) onto the kill-ring any time you do a kill in Emacs. It's been tested on Linux, but shouldn't be restricted to Linux.
(defadvice kill-new (before kill-new-push-xselection-on-kill-ring activate)
"Before putting new kill onto the kill-ring, add the clipboard/external selection to the kill ring"
(let ((have-paste (and interprogram-paste-function
(funcall interprogram-paste-function))))
(when have-paste (push have-paste kill-ring))))
If you find yourself doing this a lot, it may be useful to take a look at the package browse-kill-ring, which gives you a nice view of the kill ring (as opposed to repeatedly typing M-y).
Note that latest Emacs CVS version have the variable save-interprogram-paste-before-kill which does exactly this cf: from the etc/NEWS file :
** When save-interprogram-paste-before-kill'
is non-nil, emacs will not clobber the
the interprogram paste when something
is killed in it by saving the former
in thekill-ring' before the latter.
I think the problem is that when you yank from the clipboard outside emacs, you're not saving to the kill ring.
What you'd need is to use the function clipboard-yank to insert the region, then somehow select it and save it to the kill ring, like the function kill-ring-save does.
Or even better write a function clipboard-save-to-kill-ring-and-yank which saves the clipboad to the kill ring and then yanks it.
edit: Tracking through the code a bit, this does what you want; you could hook it up to a key. It saves the windows clipboard contents to the kill ring.
(defun clipboard-to-kill-ring()
"save the external clipboard contents to the kill ring"
(interactive)
(let ((clip (funcall interprogram-paste-function)))
(when clip
(kill-new clip)))
(defadvice yank (before maybe-copy-windows-clipboard (arg))
(clipboard-to-kill-ring))
(ad-activate 'yank)
I'd guess you could hack the various kill commands to not put text into the clipboard, and then have clipboard-yank bound to a different key, dunno if that would work.
I sort of work around this by just yanking the text then re-killing it, when I pop into emacs after having done a copy or cut in another windows app.
A better approach would be to hack emacs so that when you kill something, it compares the existing clipboard to the topmost entry in the kill ring, and if different, it pushes the clipboard contents to the kill ring, before doing the kill you explicitly requested.