Is there a region change hook in emacs lisp? - emacs

I'm trying to get the content of current selected region in buffer. I'm aware of idle timer, but a hook should be more efficient/cleaner...

Not sure what you mean by "region change". If you mean "the text in the region is modified", then you'll need to use after-change-functions. If you mean that the selected text is modified by changing its bounds, then you'll probably want post-command-hook or maybe an idle timer (which is not less efficient than a hook, the main difference is that you get less guarantees about when it gets run; e.g. it won't be run between two commands if there's no idle time between the two, as is the case when running a keyboard macro).

A way to go seems to advice handle-shift-selection. AFAICT this function is called with every region-change by keyboard. Resp. advice mouse-drag-region.

Related

why helm-projectile-recentf set current buffer as default

I'm trying move from ido-mode to helm and projectile and are not used to, something will be overcome, but when I tried helm-projectile-recentf, I wish it can switch to last buffer I visited quickly, but the default buffer is my current buffer, I don't want to switch to my current buffer, I have to move down first. switch buffer is a very frequent operation, I think move down everytime is very annoying!
I have studied helm-projectile-recentf function, it seems it is not easy to customize it to suit my normal requirement.
Could somebody have good suggestion, thanks in advance!

How can I make Emacs isearch faster?

I use isearch all the time in Emacs but I've noticed on large org mode files it can take some time to find the first match. Is there any way to speed up isearch in a buffer?
EDIT: The most noticeable speed improvement came from setting isearch-lazy-highlight to nil. I was experiencing a noticeable delay when calling isearch to jump to text and this delay has now disappeared.
One thing to keep in mind is that the longer your search pattern the quicker the search. So quickly typing a long search pattern (or even entering one by using M-e) can help if the buffer is humongous.
Another thing you can do is to turn off option isearch-lazy-highlight, so you don't make Isearch find and highlight all of the matches in the currently visible text. See also options lazy-highlight-initial-delay and lazy-highlight-interval.
More generally, consult the Isearch doc.
If you were to Narrow the buffer to only the section(s) of interest before searching, then the search would be faster.
Whether this represents an overall time-saving or time-loss no doubt depends upon your use-cases.

Emacs - how to use colors to visually accentuate the function the cursor is in?

Inspired by ia Writer's focus mode, I'm interested in using font + background colors in emacs to accentuate the function the cursor is in and visually cue the rest of the code as the background (I use C++, but it would be nice if this worked regardless of the programming language).
Ideally the font color of code outside the function would be dimmed (this is how focus mode works). A simpler solution probably be to change the background color slightly for the function that the cursor is currently in. How can this be done?
Nothing like this exists AFAIK. If you want it to write it yourself, here is a sketch:
Write a routine that determines the boundaries of the current function. The easiest way to do this is with (bounds-of-thing-at-point 'defun).
Write a routine that, when given the bounds of a region, gets the background face property of the region of the region, darkens it, and applies the new face to the region.
Override font-lock-fontify-region-function (see here) with a routine that calls the original value of this variable, differences the region given with the region of the current defun (using #1), and then applies routine #2 to the remaining region.
I would prefer overriding font lock to, say, using jit-lock-register because you need to control the order of fontification.
HTH!
Which-function mode is used to highlight the current function. Try it to see if it helps you, and see if this post helps you:
Emacs Setting which-function-mode

Matrix-like Idle Animation for Emacs

As is, my emacs is set up to show green text on a black background. On seeing it, a friend remarked that I just took it because of the Matrix-like appearance it gives. So, now what I want to do is implement an idle animation for it where, like in the matrix, changing text falls down the screen. Like in the zone out functions, it should run after emacs has been idle for a while. How would this be done?
You might want to check out the package zone: M-x zone
The 'zone-pgm-drip is like the Matrix drip, only one character at a time. I'm sure it could be enhanced to be more flood like. Also, the 'zone-pgm-jitter has text flooding down, but it's just the text currently on the screen (so it's horizontal extent is limited to what was already showing). You can just run M-x zone over and over until you find what you like.
If you want to limit the choices zone uses, you can restrict the array that zone uses:
(setq zone-programs [zone-pgm-jitter])
The choices for zone-programs are:
zone-pgm-jitter
zone-pgm-putz-with-case
zone-pgm-dissolve
zone-pgm-explode
zone-pgm-whack-chars
zone-pgm-rotate
zone-pgm-rotate-LR-lockstep
zone-pgm-rotate-RL-lockstep
zone-pgm-rotate-LR-variable
zone-pgm-rotate-RL-variable
zone-pgm-drip
zone-pgm-drip-fretfully
zone-pgm-five-oclock-swan-dive
zone-pgm-martini-swan-dive
zone-pgm-rat-race
zone-pgm-paragraph-spaz
zone-pgm-stress
zone-pgm-stress-destress
zone-pgm-random-life
You could install zone-matrix from Marmalade. "M-x package-install zone-matrix"
Looks like someone just made one. It worked for me after a small amount of tweaking:
https://github.com/emacsmirror/zone-matrix
You could just use the xmatrix screensaver, which could probably be modified to run in a window other than the X root if you really want it to run it within emacs. Bonus points if you can modify it to use glyphs based on the text in the current window. By strange coincidence, Jamie Zawinski both wrote xmatrix and large chunks of Lucid Emacs, which was subsequently released under an open-source licence as Xemacs.

Emacs reselect region, as Vim shortcut 'gv' does

In vim, visual block can be recall by 'gv' command so that multiple commands can be applied easily. (such as, comment out, then indent, then do_something_fun).
In Emacs, how can this be achieved?
[C-xC-x] only works when current cursor position stays where previous block ended.
If previous block was changed, the closest is to go through 'point-to-register' and 'jump-to-register'.
Just I am curious if there is an Emacs built-in command making this in one shot.
If Transient Mark mode is off, the region is always active. If it's on (which it sounds like is your situation), you can set mark-even-if-inactive to non-nil to allow region commands to work while the region isn't highlighted.
However, note you also can cycle back through previous mark positions using C-u C-SPC -- this will pop the mark ring. Once you're back to where you want to be, C-x C-x will rehighlight the region you want. (It may take a little bit of playing with this feature to get a feel for it, but it's why I can't switch away from Emacs now.)
If I understand correctly what you are asking for, then you don't need to do anything. When you select a region in emacs, it stays selected until you select a new one. So you could select the region and then perform as many actions as you want.
Sounds like you're looking for the secondary selection, which stays put even as the region might change. (It stays put until you move it.)
See:
the Emacs manual, node Secondary Selection
Emacs wiki page Secondary Selection
library second-sel.el:
Also narrow-to-region (CTRL-x n n ) applies every command from then on just to that region- you can't hurt the rest of the buffer, it doesn't even show. After done editing , widen (CTRL-x n w )to get back the whole buffer.
CMM
If you use evil-mode, just press gv like in vim.
Since the answers here and for other similar SO questions didn't help for me (CUA-mode, Emacs 24, not only indent-rigidly), I continued searching and finally found a reselect-last-region defined in this collection of custom function (starting line 670). That worked like a charm for me - and hopefully does for others still arriving here.