How can I display the value and/or type of an fstar expression? - fstar

I'm going through the fstar tutorial using the emacs fstar-mode. Is there a way to evaluate an expression or its type?
What I'm looking for is an equivalent to Lean's #check or #eval.

In fstar-mode.el, the emacs mode for F*, you can do
C-c C-s C-e: to evaluate an expression
C-c C-s C-d: to show the type and docs of an identifier
These and other utilities can be found from the F* menu in the menu bar of emacs when running fstar-mode.el

Related

Disable default value in minibuffer in Emacs

I'm using query-replace-regexp very often and there are very long expressions from time to time.
My problem is the following:
Query replace regexp (default foo -> bar):
Is there a possibility to hide the default expression?
A simple advice does the job:
(defadvice query-replace-read-from (before no-default activate)
(setq query-replace-defaults nil))
If you use minibuffer-electric-default-mode, the default will disappear as soon as you type something into the minibuffer.

Case conversion of file name (regexp) in Emacs file browser

I have a similar problem to that addressed in
Emacs: regular expression replacing to change case
I want to use Emacs' file browser mode Dired to rename a file from 08 - hey you.mp3 to 08 - Hey you.mp3.
I type % R to replace ^\([0-9]* - \)\([a-z]\)\(.*\)$ by \1\,(upcase \2)\3 and I get an error
Invalid use of `\' in replacement text
When I query-replace-regexp a similar regexp \([0-9]* - \)\([a-z]\)\(.*\) by \1\,(upcase 2)\3 in a normal text buffer I don't have problems. The only input difference are the enclosing ^ and $, that are required as explained in http://www.gnu.org/software/emacs/manual/html_node/emacs/Transforming-File-Names.html#Transforming-File-Names, however the source of error seems to be the \,(upcase ), which seems not usable in Dired, since I don't have this error when I use for example \1\3\2as replace regexp.
I am using Emacs 23.2.1
% R in dired calls dired-do-rename-regexp which ends up calling replace-match from dired-string-replace-match, and replace-match does not support elisp replacements.
What you can do is take advantage of wdired which allows you to edit the directory listing as plain text.
C-xC-q (i.e. the usual toggle-read-only binding) will toggle between dired and wdired. Once in wdired, you can use a normal search and replace, including the elisp replacement pattern.

Emacs-style Regex in Info-reader?

I am a Vim-user lost in the Emacs-style Regex of Info-reader. I want to match:
$ info find
?How-in-Info-reader? :%s#\(\\;.*\\+\)\|\(\\+.*\\;\)#WORKS!#g
INFO: "C-X n" to go through the matches
I am looking for the Emacs-counterpart for the Vim-command marked with "?How-in-Info-reader?".
How can you find the matches in Info-reader?
For the standalone info reader, your choices are more limited than when using Emacs proper for browsing *info* pages.
I'm not familiar with the details of ?How-in-Info-reader, but there are two ways (I can see to search in the standalone info browser.
M-x index-apropos SOMESTRING
will give you a list of all the index nodes which contain SOMESTRING.
And the other searches C-s (for interactive search) and / or s (non-interactive search) for a particular string in the current view (they don't drop down into the nodes).
I think you're trying to replace either backslash-semi-anystring-backslashes or backslashes-anystring-backslash-semi with "WORKS!" everywhere in the file. It doesn't look like info is an editor. it doesn't even look like it has regex searching. In emacs, I'd type esc-control-s (to get incremental regular expression search, which means you can try out expressions and see how they work).
Once you're in emacs, the search string you presented should work just fine if I've understood your question. You can also type Esc-r, and then type the first string ("\(\\;.*\\+\)\|\(\\+.*\\;\)"), a RETURN, and the replacement string ("#WORKS!").

class & function names highlighting in Vim

I just recently set up my Vim environment from Textmate, after becoming addicted to its modal input.
However, syntax highlighting seems to be not so beautiful in Vim. I code in C++ and since the function call and class names can't be highlighted, the code is more difficult to read. I played with color scheme for a bit, but couldn't find any field that corresponded to "class name" or "function name".
In the picture below, notice how DroughtLayer:: and *.size() is not highlighted on the right in MacVim.
(source: ivzhao.com)
Any ideas how to solve this? It really annoys me as I am so much a visual-sensitive guy.
I had this very same problem when I started using vim. The solution is simple, you just have to edit the c syntax file used by vim, here's how to do it:
When you start editing a C or C++ file, vim reads the default c syntax file located in
$VIMRUNTIME/syntax/c.vim
(Where $VIMRUNTIME is where you have vim installed. You can find out it's default value by opening vim and using the command ":echo $VIMRUNTIME").
You can simply overwrite that file, or you can create your custom C syntax file (which will be loaded by vim instead of the default one) in this location:
$HOME/.vim/syntax/c.vim (for UNIX)
$HOME/vimfiles/syntax/c.vim (for PC or OS/2)
(I have never used a Mac so I don't know which one will work for you. You can find out more in the vim help, ":help vimfiles")
Now the fun part. Copy the default "$VIMRUNTIME/syntax/c.vim" file to your vimfiles directory ("$HOME/.vim/syntax/c.vim" for UNIX), and edit it by adding these lines:
" Highlight Class and Function names
syn match cCustomParen "(" contains=cParen,cCppParen
syn match cCustomFunc "\w\+\s*(" contains=cCustomParen
syn match cCustomScope "::"
syn match cCustomClass "\w\+\s*::" contains=cCustomScope
hi def link cCustomFunc Function
hi def link cCustomClass Function
That's it! Now functions and class names will be highlighted with the color defined in the "Function" highlight (":hi Function"). If you want to customize colors, you can change the last two lines above to something like this:
hi def cCustomFunc gui=bold guifg=yellowgreen
hi def cCustomClass gui=reverse guifg=#00FF00
or you can leave the C syntax file alone and define colors in your vimrc file (":help vimrc"):
hi cCustomFunc gui=bold guifg=yellowgreen
hi cCustomClass gui=reverse guifg=#00FF00
(Note the absence of the "def" keyword, go to ":help highlight-default" for details). For the available parameters to the ":hi" command see ":help :highlight".
You can find the complete c.vim file for Vim 7.2 on this link (Note: only use this if you have a non-modified Vim, version 7.2):
http://pastebin.com/f33aeab77
And the obligatory screenshot:
this is my first post here and i didn't know how to make an observation, the answer of Eduardo makes "(" and "{" look unmached and bugs syntax foldind, I changed it a little to fix this.
syn match cCustomParen "?=(" contains=cParen,cCppParen
syn match cCustomFunc "\w\+\s*(\#=" contains=cCustomParen
syn match cCustomScope "::"
syn match cCustomClass "\w\+\s*::" contains=cCustomScope
hi def cCustomFunc gui=bold guifg=yellowgreen
hi def link cCustomClass Function
Interestingly, the syntax highlighters in VIM don't support applying a syntax to identifiers or function names - at least not the syntax highlighters for C and C++. So, even if you do:
:hi Function guifg=red
or
:hi Identifier guifg=red
it doesn't give these a color. I just seems to be not much more than keywords and constants for these languages.
Here, someone has started extending the cpp syntax file to support method names. It's a start I guess.
http://vim.wikia.com/wiki/Highlighting_of_method_names_in_the_definition
The one solution is to use built ctags database. So create one with the ctags utility. Then set the 'tags' variable and put the following to the
~/.vim/after/syntax/c.vim
function! s:highlight()
let list = taglist('.*')
for item in list
let kind = item.kind
if kind == 'f' || kind == 'c'
let name = item.name
exec 'syntax keyword Identifier '.name
endif
endfor
endfunction
call s:highlight()
I must warn you that this can work very slow on the very big ctags database.
Also there is one solution on the vim.org but I didn't try this one. Let me know if it works for you.
EDIT: color_coded may be too heavy for you. try octol/vim-cpp-enhanced-highlight. It supports C++11/14 and integrates what #Eduardo answers.
Semantic based highlighter:
I would recommend jeaye/color_coded,
A vim plugin for libclang-based highlighting
So sorry that i'm new to stackoverflow which means I've not enough reputation to post images. Go see its effects if you wanna give it a shot. :)
Pros:
Easy installation
Semantic highlighting
Clighter mentioned as above, need vim compiled with python2.7.
However, color_coded is written in C++ and provides lua binding ->
C++.
Cons:
It delays unless you make some vim events to acitve it.
Customization is bit harder; you need to edit syntax/color_coded.vim
yourself. But customization has been placed on its roadmap.
Although it's still under development, it's increasingly gaining attention.
Sergey, changing the first line from
syn match cCustomParen "(" contains=cParen,cCppParen
to
syn match cCustomParen "(" contains=cParen contains=cCppParen
seems to fix it for me.
Try using this plugin http://www.vim.org/scripts/script.php?script_id=2646
Its does all ctags highlighting very efficiently for you
Use a plug-in for vim like Taglist or set up ctags or cscope integration with vim (here's a tutorial for the vim/cscope.)
I really recommend you the taghighlight plugin, click here for it's website.
The Clighter plugin can also be considered, which is a
plugin for c-family semantic source code highlighting, based on Clang
However, requires fairly recent versions and software: vim 7.4.330 +python2 and libclang.
To match C functions definitions only, this works for me:
syn match cCustomFuncDef display /\(\w\+\(\s\|*\)\+\)\#<=\w\+\s*(\#=/
hi def cCustomFuncDef ctermfg=lightblue

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.