Applying a keyboard macro to an active region in Emacs - emacs

I am trying to learn to use keyboard macros more but wasn't sure how to approach this.
I need to select a region, then run two query-replacements on that region. The region will be different each time, but the query replacements will be the same.
The problem comes because the first query replace (or replace-string) removes the active region. If I use C-u C-SPC, it appears the beginning mark is saved, but the point is not saved, so the active region is not available for the second replace operation.
How can I apply two operations, which remove the active region as a side effect, to the same region in a keyboard macro?

As I don't perfom tasks like the one described that often I'm not sure I can advice you the quickest solution but here at least two approaches that will work.
Option 1
This option involves using narrowing to reduce the buffer to the active region prior to performing query-replace, or whatever you want to do with the region. The workflow would be as follows:
... start recording your keyboard macro with region active
M-x narrow-to-region
... perform the operations you want to perform happily jumping back to the start via M-< (beginning-of-buffer) and the like
M-x widen
... stop recording your keyboard macro
You should be able to happily apply it to any highlighted region.
BTW: narrow-to-region and widen have a default bindings of C-x n n (narrow-to-region) and C-x n w (widen) so you could use them either in case you haven't changed your keybindings
Option 2
This options involves using registers to store the locations of the region prior to "destroying" it. The actual workflow would look like
... start recording your keyboard macro with region active
C-x r SPC 1 (point-to-register)
C-x C-x (exchange-point-and-mark)
C-x r SPC 2 (point-to-register)
... perform the operation that "destroys" the region
C-x r j 2 (jump-to-register)
C-SPC
C-x r j 1 (jump-to-register)
... now your region should be back again, so happily apply any other operations not "destroying" the region
... stop recording your keyboard macro

What you're missing is exchange-point-and-mark C-x C-x. This has
the effect of re-activating the mark.

Related

Emacs cursor jumps before period on Proof General

I only encounter this problem when running Proof General. I'm assuming this is some random minor mode that is started by Proof General, but can't figure out which one! I include a list of minor modes bellow, in case you can recognise the name.
If I place a period in Emacs, the cursor will jump before it, like so:
Writing something|
Writing something.|
Writing something|.
Where | represents cursor and the last two lined happen immediately one after the other.
the same happens if I click at the end of a line with a period. The cursor will appear after the period and immediately jump before the period.
Some sentence. (click here)
Some sentence. |
Some sentence|.
Where the last two lined happen immediately one after the other.
Here is the list of minor modes, in case you can spot the name:
Aquamacs-Autoface Auto-Composition
Auto-Compression Auto-Encryption Blink-Cursor Column-Number Cua
Delete-Selection Electric-Indent File-Name-Shadow Font-Lock
Global-Font-Lock Holes Line-Number Menu-Bar Mouse-Wheel Osx-Key
Recentf Savehist Show-Paren Smart-Frame-Positioning Tabbar
Tabbar-Mwheel Tool-Bar Tooltip Transient-Mark
key binding
--- -------
^C Prefix Command
ESC Prefix Command
. proof-electric-terminator
<C-M-down> pg-move-region-down
<C-M-mouse-3> proof-mouse-goto-point
<C-M-up> pg-move-region-up
<C-S-mouse-1> pg-identifier-under-mouse-query
<C-return> proof-script-complete
(that binding is currently shadowed by another mode)
<M-down> proof-forward-command
<M-up> proof-backward-command
<remap> Prefix Command
... then a bunch of Proof specific minor modes...
Proof General is incompatible with show-paren-mode. You can turn it off with M-x show-paren-mode or by putting (add-hook 'proof-ready-for-assistant-hook (lambda () (show-paren-mode 0))) in the appropriate place in your .emacs.
See http://proofgeneral.inf.ed.ac.uk/trac/ticket/496.

In emacs how can I kill multiple buffers without opening each individually

I have a lot (~30) of buffers whose names start with the same substring. Is there a way I can pass a regular expression to the kill buffer command so I can kill all of them in one go?
The command I was looking for M-x kill-matching-buffers.
Run M-x ibuffer (built-in command).
On each line with a buffer you want to delete press 'd'.
When finished press 'x' to commit changes.
A more interactive and versatile approach is with helm-buffers-list:
Type what you want to match.
Select all matches with M-m (helm-toggle-all-marks).
Optionally, refine your selection with C-SPC (helm-toggle-visible-marks),
using C-n/C-p to navigate.
Press TAB, select "kill buffers" from the available options.
Other options are query-replace / occur / grep / revert, and a bunch of others.
C-x C-b to list all buffers
k to mark buffers for kill (D appears next to buffer)
x to execute kill
Using helm:
Open helm using helm-mini. In my case I have it mapped to C-2
Scroll through the list and mark the ones you want to delete with C-Space
Now just press M-S-d / M-D
This deletes all selected buffers and closes helm-mini.

Emacs, How can I display only current task and hide others in org-mode?

Org-mode.
I have a big tasks tree and I want to select only 1 in buffer (write notes just for it) and hide others while editing.
How can I do this?
task-1
subtask-1.1
subtask-1.2
task-2
subtask2.1
...
...
For example I want to display for me only:
subtask-1.2
(notes for this subtask)
Use narrow-to-defun or org-narrow-to-subtree command with point in task. (widen to move back to whole buffer content).
Default shortcuts:
C-x n s # org-narrow-to-subtree (bound in org-mode)
C-x n d # narrow-to-defun
C-x n w # widen
Manual
You also have org-tree-to-indirect-buffer, or C-c C-x b, which will create a new buffer with only this subtree, and reflect any change in one buffer into the other.

Emacs docview: saving/jumping to locations with

When using doc-view in Emacs, I'd like to jump to different locations (e.g., move to the bibliography of a paper and back to where I was). I've tried using C-u C-SPC and registers to jump to different locations, but it doesn't work when I'm in doc-view mode. So, do you know if there is any way I could save (and jump to) different locations while in the doc-view mode?
Thanks!
Normal marks don't work, indeed, and registers won't work either, sadly. But bookmarks will work (e.g. C-x r m and C-x r b). I'd welcome patches that try to make registers use the same infrastructure as bookmarks so that they also work under doc-view-mode and friends.

Save the shape of a rectangular selection in an Emacs register

I need to repeatedly copy text from a fixed size rectangular region and I'd like to be able to save the shape of that rectangular region in a register so I don't have to keep re-creating the same size.
cua-set-rectangle-mark (<C-return>)
Move point to create a region 8x16 (note: this is the step I want to remove)
piture-clear-rectangle (C-c C-k)
Move point to new location.
picture-yank-rectangle (C-c C-y)
I'd like to replace steps 1 and 2 with a single 'paste rectangular region from register' command. Is this possible?
Wouldn't it be easier to simply use a keyboard macro for this?
E.g.:-
C-x C-( [start recording kbd macro]
steps 1-2
C-x C-) [end recording kbd macro]
Then
C-x e [execute kbd macro]
You probably want to use copy-rectangle-to-register and insert-register:
C-x r r runs the command copy-rectangle-to-register, which is an
interactive compiled Lisp function in `register.el'.
It is bound to C-x r r.
(copy-rectangle-to-register REGISTER START END &optional DELETE-FLAG)
Copy rectangular region into register REGISTER.
With prefix arg, delete as well.
To insert this register in the buffer, use C-x r g.
Called from a program, takes four args: REGISTER, START, END and DELETE-FLAG.
START and END are buffer positions giving two corners of rectangle.
insert-register:
C-x r g runs the command insert-register, which is an interactive
compiled Lisp function in `register.el'.
It is bound to C-x r g, C-x r i.
(insert-register REGISTER &optional ARG)
Insert contents of register REGISTER. (REGISTER is a character.)
Normally puts point before and mark after the inserted text.
If optional second arg is non-nil, puts mark before and point after.
Interactively, second arg is non-nil if prefix arg is supplied.
See also:
C-xrc: clear-rectangle
C-xrd: delete-rectangle
C-xrk: kill-rectangle
C-xro: open-rectangle
C-xrr: copy-rectangle-to-register
C-xrt: string-rectangle
C-xry: yank-rectangle
EDIT:
Right, I completely misunderstood the question.
If you still particularly want to use registers, you could save and re-execute point movements with something like this:
(set-register ?a [right right right down down])
(command-execute (get-register ?a))