add multiple shortcuts fot the same macro - mutt

I am using these macros for quickly changing account in mutt.
macro index,pager <f1> '<sync-mailbox><enter-command>source ~/.mutt/comptes/University<enter><change-folder>!<enter>'
macro index,pager <f2> '<sync-mailbox><enter-command>source ~/.mutt/comptes/Gmail<enter><change-folder>!<enter>'
macro index,pager <f3> '<sync-mailbox><enter-command>source ~/.mutt/comptes/Petur<enter><change-folder>!<enter>'
But I am also using keyboard without F. keys. So I would like to bind these macros to two keys f1 and cu (for pressing c then u), f2 and cg and f3 and cp.
I tried something like macro index,pager <f1>,cu ... but it is not working.

You definitely need two macro definitions when you want to have 2 bindings. But you can specify an existing key binding as macro sequence.
With this you bind your first macro definition to (1) the F1 key and (2) key combination c, u:
macro index,pager <f1> '<sync-mailbox><enter-command>source ~/.mutt/comptes/University<enter><change-folder>!<enter>'
macro index,pager cu <f1>

Related

Resharper : subtituate macro with multi-line code

Is it possible to make Resharper Substituate macro call in separate-lines mode?
Example
I have this code :-
#define TEST(T) int T=5; \
T++;
TEST(apple);
If I click
Substituate macro call and all nested calls like this :-
The line TEST(apple); will become :-
int apple=5; apple++;;
I hope there is an option to make the result be 2 separate lines :-
int apple=5;
apple++;;
Other notes
I know that macro with \ is finally interpreted as a single line,
but it would be nice if there is an option to show it as many lines for eye-candy.
(Even it may break the macro behavior, e.g. those with __LINE__ )
It would be useful for debugging for a 10+ lines macro.
It's not possible at the moment, but you can always select the resulting code after a macro substitution and invoke "Reformat Code" (Ctrl+Alt+Enter) to make it readable.

Preprocessing simple integer arithmetic with cpp

I have a following input file:
#define __SIZE_K(x) (x * 1024)
#define DT_FLASH_SIZE __SIZE_K(128)
reg = <0x08000000 DT_FLASH_SIZE>;
If I run that through a preprocessor I get this:
$ cpp -x assembler-with-cpp input.dts -E -P
reg = <0x08000000 (128 * 1024)>;
If it possible to get the macro fully evaluated? I would like to have:
reg = <0x08000000 131072>;
I would like to have devicetree source files "fully-preprocessed" and I would prefer to do this entirely in the preprocessor, but I'm not sure this is possible... The final devicetree consists of multiple files, some of which define the layout, some are headers with macros and various values depending on selected chip.
If it possible to get the macro fully evaluated?
Theoretically, yes. But it's not usable in the context you want to use it.
Preprocessor is a simple text replacement tool. It replaces one text for the other. First, you have to implement a table of all possible combinations of replacements:
#define MUL_1_1 1
#define MUL_1_2 2
#define MUL_1_3 3
// etc. for **billions** of lines
#define MUL_128_1024 the_result_here
// etc.
After those bilions of lines, you can finally write:
#define MUL_(a, b) MUL_##a##_##b
#define MUL(a, b) MUL_(a, b)
After that you can do:
#define __SIZE_K(x) MUL(x, 1024)
You can use a library - like P99_MUL or BOOST_PP_MUL - and they are basically implemented in almost the same way, with a lot of optimizations to shorten the list.
There is no point in using specifically C preprocessor in this context anyway. Use M4 or Python's Jinja2 or PHP - a full programming language, instead of some limited preprocessing C tool.

Remove pressed key from vim input buffer

I'm writing a vim auto-command for cpp files where I want an abbreviation for #include lines.
The goal is for the abbreviation to expand to #include <▐> with the pipe being the location of the cursor after expansion.
Since naturally the abbreviation is to be followed by a space I'm trying to remove the input space from the register and move on.
However even after exiting insert mode and returning, or any trick I could think of: deleting next key, keying in bs etc, the space is being entered after whatever series of commands the iabbrev includes.
Therefore I'm looking for something that will remove the space from the register AND put me still in insert mode.
Thanks.
Add this to your .vimrc:
autocmd FileType cpp iab <buffer> #i #include <><Left><C-R>=Eatchar('\s')<CR>
func Eatchar(pat)
let c = nr2char(getchar(0))
return (c =~ a:pat) ? '' : c
endfunc
In lh-cpp, I have in a C ftplugin (the place where such definitions should be)
iab <buffer> #n <C-R>=lh#map#no_context("#n ",'\<esc\>0i#include')<CR>
:Brackets < > -open=function('lh#cpp#brackets#lt') -visual=0
With lh#map#no_context() that prevents the expansion of #n within string and comment contexts -- sometimes, we want to be able to type auto s = "#n";, and lh#cpp#brackets#lt that recognizes a few contexts in which I can expand my < into <> (includes, templates, castings, etc).
As you see I don't automatically append "" nor <> here as I want to be able to choose between #include <foo> and #include "foo"
Back to abbreviations that don't eat the space characters, the answer is in the help, see Menix's answer.
To simplify my task, I've defined a command that automatically applies such function (in lh-brackets this time):
command! -narg=+ Iabbr execute "iabbr " <q-args>."<C-R>=lh#map#eat_char('\\s')<CR>"
command! -narg=+ Inoreabbr
\ execute "inoreabbr " <q-args>."<C-R>=lh#map#eat_char('\\s')<CR>"
Thus, I would define your abbreviation this way
:Inoreabbr <silent> <buffer> #n< #include <><left>
:Inoreabbr <silent> <buffer> #n" #include ""<left>
Or even better to not see these abbreviations triggerred in string and comment contexts:
:Inoreab <buffer> <silent> #n< <C-R>=lh#map#no_context("#n< ",'\<esc\>0i#include <>\<left\>')<CR>
" and so on
As you can see from #LucHermitte's answer (and the similar attempt by #Meninx before), eating that character is a bit involved.
Instead, I would recommend you upgrade to a snippets plugin (if you don't use one already). Snippets are like the built-in :abbreviate on steroids, usually with parameter insertions, mirroring, and multiple stops inside them. One of the first, very famous (and still widely used) Vim plugins is snipMate (inspired by the TextMate editor); unfortunately, it's not maintained any more; though there is a fork. A modern alternative (that requires Python though) is UltiSnips. There are more, see this list on the Vim Tips Wiki.
Depending on the snippet plugin, the expansion may need to be explicitly triggered, others may already support insertion without the trailing space. Usually, you can configure a default for the included filename (that can be overwritten easily) - this functionality cannot be had via Vim abbreviations alone.
alternative
If you don't want full snippets functionality just for this, I would recommend to define the abbreviation based on triggering it with <Enter> instead of <Space>. This should fit well for this example (the next code / #include will be on a new line, anyway), and so you don't have to artificially remove anything.

(C/C++) how to create a macro that accepts the increment operator

to make my question precise i would like to create a macro that accepts as variable ++ and pastes it with another variable. In code:
#define MYMACRO(A, OP) /* .... */
and then if i write in the source file
MYMACRO(this->var, ++)
the preprocessor should paste in the code
this->var++;
When i am trying to define the macro as A##OP , it provides the following error:
pasting "++" and "var" does not give a valid preprocessing token
Is it possible to do what i am trying?
thanks in advance for the answer
You don't need the ## token pasting operator, because you're not trying to combine the parameters into a single token. Just use:
#define MYMACRO(a, op) (a)op;
You just need to combine the tokens like this:
#define MYMACRO(a, incrdecr) (a)incrdecr

How do I bind a regular expression to a key combination in emacs?

For context, I am something of an emacs newbie. I haven't used it for very long, but have been using it more and more (I like it a lot). Also I'm comfortable with lisp, but not super familiar with elisp.
What I need to do is bind a regular expression to a keyboard combination because I use this particular regex so often.
What I've been doing:
M-C-s ^.*Table\(\(.*\n\)*?GO\)
Note, I used newline above, but I've found that for isearch-forward-regexp, you really need to replace the \n in the regular expression with the result of C-q Q-j. This inserts a literal newline (without ending the command) enabling me to put a newline into the expression and match across lines.
How can I bind this to a key combination?
I vaguely understand that I need to create an elisp function which executes isearch-forward-regexp with the expression, but I'm fuzzy on the details. I've searched google and found most documentation to be a tad confusing.
How can I bind a regular expression to a key combination in emacs?
Mike Stone had the best answer so far -- not exactly what I was looking for but it worked for what I needed
Edit - this sort of worked, but after storing the macro, when I went back to use it later, I couldn't use it with C-x e. (i.e., if I reboot emacs and then type M-x macro-name, and then C-x e, I get a message in the minibuffer like 'no last kbd macro' or something similar)
#Mike Stone - Thanks for the information. I tried creating a macro like so:
C-x( M-C-s ^.*Table\(\(.*C-q C-J\)*?GO\) C-x)
This created my macro, but when I executed my macro I didn't get the same highlighting that I ordinarily get when I use isearch-forward-regexp. Instead it just jumped to the end of the next match of the expression. So that doesn't really work for what I need. Any ideas?
Edit: It looks like I can use macros to do what I want, I just have to think outside the box of isearch-forward-regexp. I'll try what you suggested.
You can use macros, just do C-x ( then do everything for the macro, then C-x ) to end the macro, then C-x e will execute the last defined macro. Then, you can name it using M-x name-last-kbd-macro which lets you assign a name to it, which you can then invoke with M-x TESTIT, then store the definition using M-x insert-kbd-macro which will put the macro into your current buffer, and then you can store it in your .emacs file.
Example:
C-x( abc *return* C-x)
Will define a macro to type "abc" and press return.
C-xeee
Executes the above macro immediately, 3 times (first e executes it, then following 2 e's will execute it twice more).
M-x name-last-kbd-macro testit
Names the macro to "testit"
M-x testit
Executes the just named macro (prints "abc" then return).
M-x insert-kbd-macro
Puts the following in your current buffer:
(fset 'testit
[?a ?b ?c return])
Which can then be saved in your .emacs file to use the named macro over and over again after restarting emacs.
I've started with solving your problem literally,
(defun search-maker (s)
`(lambda ()
(interactive)
(let ((regexp-search-ring (cons ,s regexp-search-ring)) ;add regexp to history
(isearch-mode-map (copy-keymap isearch-mode-map)))
(define-key isearch-mode-map (vector last-command-event) 'isearch-repeat-forward) ;make last key repeat
(isearch-forward-regexp)))) ;`
(global-set-key (kbd "C-. t") (search-maker "^.*Table\\(\\(.*\\n\\)*?GO\\)"))
(global-set-key (kbd "<f6>") (search-maker "HELLO WORLD"))
The keyboard sequence from (kbd ...) starts a new blank search. To actually search for your string, you press last key again as many times as you need. So C-. t t t or <f6> <f6> <f6>. The solution is basically a hack, but I'll leave it here if you want to experiment with it.
The following is probably the closest to what you need,
(defmacro define-isearch-yank (key string)
`(define-key isearch-mode-map ,key
(lambda ()
(interactive)
(isearch-yank-string ,string)))) ;`
(define-isearch-yank (kbd "C-. t") "^.*Table\\(\\(.*\\n\\)*?GO\\)")
(define-isearch-yank (kbd "<f6>") "HELLO WORLD")
The key combos now only work in isearch mode. You start the search normally, and then press key combos to insert your predefined string.
#Justin:
When executing a macro, it's a little different... incremental searches will just happen once, and you will have to execute the macro again if you want to search again. You can do more powerful and complex things though, such as search for a keyword, jump to the beginning of the line, mark, go to end of the line, M-w (to copy), then jump to another buffer, then C-y (paste), then jump back to the other buffer and end your macro. Then, each time you execute the macro you will be copying a line to the next buffer.
The really cool thing about emacs macros is it will stop when it sees the bell... which happens when you fail to match an incremental search (among other things). So the above macro, you can do C-u 1000 C-x e which will execute the macro 1000 times... but since you did a search, it will only copy 1000 lines, OR UNTIL THE SEARCH FAILS! Which means if there are 100 matches, it will only execute the macro 100 times.
EDIT: Check out C-hf highlight-lines-matching-regexp which will show the help of a command that highlights everything matching a regex... I don't know how to undo the highlighting though... anyways you could use a stored macro to highlight all matching the regex, and then another macro to find the next one...?
FURTHER EDIT: M-x unhighlight-regexp will undo the highlighting, you have to enter the last regex though (but it defaults to the regex you used to highlight)
In general, to define a custom keybinding in Emacs, you'd write
(define-key global-map (kbd "C-c C-f") 'function-name)
define-key is, unsurprisingly, the function to define a new key. global-map is the global keymap, as opposed to individual maps for each mode. (kbd "C-c C-f") returns a string representing the key sequence C-c C-f. There are other ways of doing this, including inputting the string directly, but this is usually the most straightforward since it takes the normal written representation. 'function-name is a symbol that's the name of the function.
Now, unless your function is already defined, you'll want to define it before you use this. To do that, write
(defun function-name (args)
(interactive)
stuff
...)
defun defines a function - use C-h f defun for more specific information. The (interactive) there isn't really a function call; it tells the compiler that it's okay for the function to be called by the user using M-x function-name and via keybindings.
Now, for interactive searching in particular, this is tricky; the isearch module doesn't really seem to be set up for what you're trying to do. But you can use this to do something similar.