Emacs equivalent of Vim's foldmethod = indent - vim

Question: Does Emacs have a canonical equivalent of Vim's Folding with Foldmethod=indent?
I am particularly interested in something that can work alongside any Emacs major mode and any file. The Emacs searches have not turned up a definitive answer.

Seems like it can, though I don't use folding myself, so I've not tried it. Not surprisingly, the Python folks are all about this feature. See the following:
http://mail.python.org/pipermail/tutor/2002-September/017482.html
http://www.nabble.com/Code-folder-with-Emacs-td16189193.html
http://groups.google.ca/group/comp.lang.python/msg/956f1c2d37f93995

maybe selective-display? I have the following function bound to [f2]
;; http://emacs.wordpress.com/2007/01/16/quick-and-dirty-code-folding/
(defun jao-toggle-selective-display (column)
(interactive "P")
(set-selective-display
(if selective-display nil (or column 1))))
That's pretty bare-bones, though, and you'd really want it to be Pythony-indentation sensitive....
UPDATE: I was staring at this last night, and realized that I was tired of C-u entering the column I was on (plus 1).... so I coded it up:
(defun toggle-selective-display-column ()
"set selective display fold everything greater than the current column, or toggle off if active"
(interactive)
(set-selective-display
(if selective-display nil (or (+ (current-column) 1) 1))))
Further elaboration should combine the two functions.
See also: How to achieve code folding effects in emacs

I tried all of the suggestions by Joe Casadonte and Michael Paulukonis, but none works as nicely as the vim's one. So it seems that the more accurate answer to the OP's question may be NO at the moment.

Related

Search visual selction in emacs evil

I want to emulate a behavior I've had in vim, but in emacs evil-mode. I want * to search for the current visually selected text. The code below sort of works, but pressing n or N does not retain the search string
(define-key evil-visual-state-map (kbd "*")
(lambda () (interactive)
(let ((search-string (buffer-substring
(evil-range-beginning (evil-visual-range))
(evil-range-end (evil-visual-range)))))
(evil-normal-state)
(evil-search search-string t))))
I'm new to emacs/elisp. Any ideas on what the "right way" to do this is? In the evil-search.el source I see a function evil-ex-search-update-pattern that may be useful, but I'm not sure how to put it together.
Thanks!
Bailey Ling (a former Vim user) has made a plugin that accomplishes the same thing you're after: https://github.com/bling/evil-visualstar.
You can either install it, or peek through the source code (which may help solve your issue).

Code navigation in vim and emacs

The IDE feature that I miss the most in emacs and vim are code navigation and find usages. Both of these editor have the following similar features for them:
Tags - identifiers from specified files are indexed and when you press shortcut on a word which is an identifier, you will be navigated there
CScope - it allows you to navigate to "usages" of an identifier
As far as I understand, both of these systems are very imprecise. If we have similar identifiers with the same name both tags and scope might mix them up. Are there any better alternatives and how precise they really are?
I use cscope and semantic in Emacs. It is just enough for me.
In cscope the two function I heavily used are cscope-find-global-definition and cscope-find-this-symbol. The previous function is quite precise.
C-c s s Find symbol.
C-c s d Find global definition.
As for semantic(dynamic index, do not need to generate TAGS).
(global-set-key [f8] 'semantic-ia-fast-jump) ;; jump to definition.
(global-set-key [S-f8] ;; jump back
(lambda ()
(interactive)
(if (ring-empty-p (oref semantic-mru-bookmark-ring ring))
(error "Semantic Bookmark ring is currently empty"))
(let* ((ring (oref semantic-mru-bookmark-ring ring))
(alist (semantic-mrub-ring-to-assoc-list ring))
(first (cdr (car alist))))
(if (semantic-equivalent-tag-p (oref first tag)
(semantic-current-tag))
(setq first (cdr (car (cdr alist)))))
(semantic-mrub-switch-tags first))))
GNU Global, for example, allows duplicate identifiers, and you'll be able to select needed.
There are many programs that output ctags-compatible tags files. They are often language-specific because of some low level limitations in ctags but neither they nor ctags help you when you have multiple methods with the same name because these tools only do the indexing part of the job. Searching through the index is actually Vim's job and, because it is a freaking text editor and not an IDE it has zero mean to decide which method declaration is the right one.
Fortunately, Vim shows an actionable list when there are multiple hits. But that's how far you can go.
Cscope is a little smarter than ctags and, when used from Vim, does the searching as well as the indexing. But, like ctags, cscope is still a code indexer.
It's actually possible to use both at the same time with set cscopetags but it won't help with your naming issue.
You could try GNU Global. But it doesn't support JavaScript so I've never really used it long enough to make an opinion.
IDEs usually do their magic through language-specific parsers/static analysis tools that run in the background against your code. Vim has at least one limitation and one feature that make it hard to even imagine it ever reaching the level of code awareness that you seem to be looking for:
Vim is not multithreaded. So it can't run a static analysis tool in the background.
Vim supports hundreds of languages. Providing that kind of feature for that many languages would be impossible for any organization of any size.
But none of this is a problem for me, because Vim is not an IDE.
And because I don't have multiple methods with the same name in my projects.
<C-]>, <C-w>}, :tag /foo<Tab> and :cs f c bar are enough for my humble needs.
See eclim which has a very usable emacs interface which supports the same level of code navigation for java as Eclipse.
Eclim also supoprts C/C++ among others, though support for this is not yet implemented in emacs-eclim, but if you know Elisp then it is pretty trivial to implement, because for java all the necessary infrastructure is already there, so you only need to add the implementation for the c++ calls.

slimv.vim:: Invalid auto insert parentheses

I'm using clojure in MacVim with slimv.vim.
But when I start to write some clojure code my indent does not look good. It looks like this:
(def a
(println "hello")
)
It makes me a little confused. I want to invalid auto insert parentheses. Do you have any idea?
Thanks in advance.
Arthur already described ,(. Besides that you can turn off paredit globally in your .vimrc (let g:paredit_mode=0).
But if what you don't like is 'electric return' (i.e. there's an extra newline inserted before the closing paren), then you can disable it via let g:paredit_electric_return=0. Electric returns are gathered if you press ) at the end of the line. So if you press ) after (println "hello") then the next closing paren will jump up producing: (println "hello")).
it sounds like perhaps you are asking "how do I turn off paredit mode in slimv?"
this tutorial covers how to use paredit: http://kovisoft.bitbucket.org/tutorial.html
you can disable paredit mode by typing ,(
ps: paredit is really worth learning, though it can be frustrating while getting used to it

continuously execute an emacs lisp function

Is there a way to trigger the execution of an emacs lisp function other than M-x myfun? I would like to have the function re-called every time the buffer is changed.
Background: I have a table of numbers with some mistakes. The table has column totals and other features which can be used to identify the mistakes. My elisp function highlights suspicious columns of numbers. What I would like is for the highlighting to disappear as soon as the numbers are corrected, without repeated calling of the highlight-errors function.
The analogous feature in Excel is called, I believe, "conditional formatting"
The concept you're looking for in your first paragraphs is hooks. A hook variable is a list of functions that are executed when a certain event happens. Most hook variables have a name ending in -hook. The hook after-change-functions is executed each time you type something or otherwise change the buffer. Hooks are discussed in the Emacs Lisp manual under the heading "Hooks".
However, given what you're trying to do, it would be easier to use Emacs's highlighting mechanism. The solution may be as simple as adding a regexp in the right place.
Most files containing structured text (especially programming languages) are highlighted with the font locking mechanism. This is documented in both the Emacs and Emacs Lisp manuals under "Font Lock"; see in particular the function font-lock-add-keywords, for which the Emacs manual gives an example that is pretty much what you're after. There is also some information on the Emacs wiki.
ADDED:
Font lock can go beyond regexps; unfortunately the documentation is limited to the terse explanation in the docstring of font-lock-keywords. There are a few simple examples in cperl-mode.el (though they're somewhat buried in the mass). The wiki also references ctypes.el which uses this feature. Here is an example that highlights wrong integer additions.
(defun maybe-warn-about-addition ()
(let ((x (string-to-int (match-string 1)))
(y (string-to-int (match-string 2)))
(z (string-to-int (match-string 3))))
(if (/= (+ x y) z)
font-lock-warning-face)))
(font-lock-add-keywords
nil
'(("\\s-\\([0-9]+\\)\\s-*\\+\\s-*\\([0-9]+\\)\\s-*=\\s-*\\([0-9]+\\)\\s-"
(3 (maybe-warn-about-addition) t))))
Even the regexp can be replaced by arbitrary code that looks for the bounds of what you want to highlight (a function name as MATCHER, using the vocabulary from the docstring). There is an advanced example of font lock keywords in the standard C mode (cc-fonts.el).
Add your function to the variable after-change-functions.

Which editors out of Emacs, Vim and JEdit support multiple simultaneous text insertion points?

Background: JEdit (and some other text editors as well) support a feature called Multiple simultaneous text insertion points. (at least that's what I'm calling it here).
To understand what this means, take a look at the link.
Out of all the features in use in modern text editors, initial research seems to indicate that this is one feature that both Emacs and Vim do not actually support. If correct, this would be pretty exceptional since it's quite difficult to find a text editor feature that has not made its way into at least one of these two old-school editors.
Question: Has anyone ever seen or implemented this feature in either Emacs, Vim, or both? If so, please point me to a link, script, reference or summary that explains the details.
If you know an alternate way to do the same (or similar) thing, please let me know.
The vim way to do this is the . command which repeats the last change. So, for instance, if I change a pointer to a reference and I have a bunch of
obj->func
that I want to change to
obj.func
then I search for obj->, do 2cw to change the obj-> to obj., then do n.n.n. until all the instances are changed.
Perhaps not a flexible as what you're talking about, but it works frequently and is very intuitive and fast when it does.
moccur-edit.el almost does what you want. All the locations matching the regexp are displayed, and the editing the matches makes changes in the corresponding source. However, the editing is done on a single instance of the occurrence.
I imagine it'd be straight forward to extend it to allow you to edit them all simultaneously (at least in the simple case).
There is a demo of it found here.
Turns out, the newest versions of moccur-edit don't apply changes in real-time - you must apply the changes. The changes are also now undoable (nice win).
In EMACS, you could/would do it with M-x find-grep and a macro. If you really insist that it be fully automatic, then you'd include the find-next in the macro.
But honestly, this strikes me as a sort of Microsoft-feature: yes, it adds to the feature list, but why bother? And would you remember it existed in six months, when you want to use it again?
For emacs, multiple-cursors does exactly that.
Have a look at emacsrocks episode 13, by the author of the module.
I don't think this feature has a direct analogue in either Emacs or Vim, which is not to say that everything achievable with this feature is not possible in some fashion with the two 'old-school' editors. And like most things Emacs and Vim, power-users would probably be able to achieve such a task exceedingly quickly, even if mere mortals like myself could spend five minutes figuring out the correct grep search and replace with appropriate back-references, for example.
YASnippet package for Emacs uses it. See 2:13 and 2:44 in the screencast.
Another slight similarity: In Emacs, the rectangle editing features provided by cua-selection-mode (or cua-mode) automatically gives you multiple insertion points down the left or right edge of the marked rectangle, so that you can type a common prefix or suffix to all of those lines.
e.g.:
M-x cua-selection-mode RET (enable the global minor mode, if you don't already use this or cua-mode)
C-RET down down down (marks a 1x3 character rectangle)
type prefix here
C-RET (unmark the rectangle to return to normal editing)
It should be something like this in vim:
%s/paint.\((.*),/\1.paint(/
Or something like that, I am really bad at "mock" regular expressions.
The idea is substitute the pattern:
/paint(object,/
with
/object.paint(/
So, yes, it is "supported"
It seemed simple to do a basic version of this in Emacs lisp. This is for when you just want two places to insert text in parallel:
(defun cjw-multi-insert (text)
"insert text at both point and mark"
(interactive "sText:")
(insert-before-markers text)
(save-excursion
(exchange-point-and-mark)
(insert-before-markers text)))
When you run it, it prompts for text and inserts it at both point (current position) and mark. You can set the mark with C-SPC. This could be easily extended for N different positions. A function like set-insert-point would record current position (stored as an Emacs marker) into a list and then when you run the multi-insert command, it just iterates through the list adding text at each.
I'm not sure about what would a simple way to handle a more general "multi-editing" feature.
Nope. This would be quite difficult to do with a primarily console-based UI.
That said, there is similar features in vim (and emacs, although I've not used it nearly as much) - search and replace, as people have said, and more similarly, column insert mode: http://pivotallabs.com/users/brian/blog/articles/350-column-edit-mode-in-vi

Resources