Code folding of round brackets in RStudio - rstudio

Is there a way to add code folding arrows next to round brackets in RStudio? For example, in the picture below a can collapse using the little arrow next to it, but no arrow shows up to collapse b. Is it possible to make those round brackets collapse without having to select the region that needs to be collapsed?
I'm especially interested in collapsing sub-brackets, which is already possible with the braced (as shown in c and d).
I have found that I can fold anything by selecting it and typing ALT+L, however, that can be rather tedious when needing to fold in a document with many brackets. Therefore, I am looking for an option to make the () brackets collapsable like the {} brackets.

Related

sublime text: view off-screen multiple selections

When I do a multiple selection from, say, the find dialog (Alt-Enter), I sometimes find that many of the selected bits are off screen, but I'd like to check the context of all of them before I start doing a multiple edit.
Is there a way to, say, cycle through them in the view (without disrupting the selections)?
Probably the easiest way is to not do a "Find All", but instead highlight (or do a single Find) what you're looking for, then use ⌘D on OS X/CtrlD on Win/Lin to sequentially add the next matches. If you come to a match you don't want to select, just use ⌘K,⌘D (or CtrlK,CtrlD) to skip the current instance and go on to the next one. ⌘U (CtrlU) is "soft undo", it moves back to the previous selection in case you made a mistake.
Alternatively, I use a color scheme with a very bright selection color (it's bright blue surrounded by bright green) so that selected regions are very easy to identify. This way I can just glance at the minimap and see where selections are, and scroll through my file quickly to ensure everything looks good. If you find a selection you don't want, you can deselect it with Alt+middle mouse button on Win/Lin, or by ⌘Shift+click on OS X. You may need to set "drag_text": false in your Preferences for it to work, though (I'm not on my Mac at the moment to test).
I've found a working solution, but it's not ideal and only works if you're ok with losing the selections (eg: with multiple selections, hit Right Arrow - you've still got multiple cursors, but the selections are gone).
It goes like this:
Make sure there is no code folding - it appears to interfere with this sort of manipulation of multiple selections.
Use altEnter in the search dialogue to select all occurrences of your regex.
Scroll through the selections until you find one you don't want (MattDMo's Neon plugin is helpful here).
Use a cursor movement to lose the selections (multiple cursors remain).
Use Alt+Left Click on the undesired cursor.
Repeat 5. as required. Making another (multiple) selection can be helpful here to locate the other cursors (eg: Control/CommandShiftLeft Arrow)
If you remove a cursor by mistake, or need to add one, use Control/Command+Left Click

Xcode Matching Pairs of Braces, Parentheses, and Brackets

If you are a notepad++ user you will understand what I want Xcode to do for me , it's very simple I want when i place the cursor before or after a Braces, Parentheses, and Brackets , it highlight for me its pair to know the matching ones juste try to place the cursor in notepad++ and tell me how to do that in Xcode i know already the trick of the right arrow of the keyboard and the double click but the trick of notepad++ is faster , thanks in advance
The only thing I can add (and I have looked a fair bit) is to train your eyes on the vertical grey bar on the left edge of the editor, called the "code folding ribbon" in preferences. It will visually hint at matching braces (not parenthesis) at all nested levels, and mousing over it hints quite strongly and gives the interface for folding.
See here for a snapshot and brief description from Apple.

Mathematica editor: Removing the right matching ']' automatically when the left '[' is removed?

In Mathematica editor (i.e. notebook), one of the hardest things I always found is when I want to edit some long expression, and go and remove the left end "[" of some expression then before I get the chance to remove the right "]", the code in the cell will get all messed up, since it is no longer balanced, making it really hard to go chase the, now hanging, right end "]" since it is no longer in its original place!
Here is a simple example, suppose we have this
Text[Row[{PaddedForm[currentTime, {6, 3}, NumberSigns -> {"", ""}, NumberPadding -> {"0", "0"}]}]]
now say I wanted to remove the outside Text[] call. So I delete "Text[" and before I get a chance to delete the other side "]", the notebook will now juggle things all over the place, making it hard to find the right "]". For long cell (which is the case for demonestrations work), the code can shift by large amount, and the right "]" can go out of view as well, has to scroll down and up looking for it.
Is there a way to set it, via an option or such, so that when I delete the left "[", Mathematica will automatically delete the matching right "]"? This applies for "(" and ")" as well.
This is really a big problem with the use of the notebook editor for me. Can't tell you how much time I spend just looking the hanging "]".
thanks
--Nasser
I shall think about an automatic method, but I currently handle this with:
place the cursor on the first token inside the function you want to delete (in this case Row)
press Ctrl+. until everything inside is selected (twice, in this case)
Copy
press Ctrl+. once to include the function to delete in the selection
Paste
It is really quite fast once you do it a few times.
Andrew Moylan suggests this mouse variation:
Here is the variant I use for this common operation:
Triple-click "Row", Ctrl+C, Triple-click "Text", Ctrl+V, done
I just registered here and I do not have MMA with me now, so this is just an outline of the proposed solution.
This is my first answer here, so bear with me please.
By the way...nice to see many mathgroupers here too. :-)
Nasser's question/problem is very common.
I personally use the same technique described by Andrew.
It is not difficult however solve this problem more elegantly with a button.
Create a small button palette with an action button.
It would work like this:
you double-click the outermost
function (the one you want to
delete). Since you double-click it
you will select everything up to the
rightmost ].
you click on the action button. The
code in the button will simply get
the selection and extract the
argument within the [] pair, then it
will paste the argument onto the
selection in the notebook. Without
evaluating the argument of course.
That's it. Couple of lines of code.
Since you are at it, you might add a few more buttons to the palette.
For example:
a button to comment/uncommnent a
selection
a button to wrap {} or () to a
selection
a button to wrap fun[] to a
selection (fun can be left selected
so it can later be overwritten with
an appropriate function)
So you end up creating your own programming palette.
If you are deleting a function with only one argument, you can
delete the function name
use (Ctrl + ., Ctrl + .) on the inside function to select its extents
go to the end of that extent and delete the ]
This website also has more information about balancing brackets in Mathematica:
http://reference.wolfram.com/mathematica/howto/BalanceBracketsAndBraces.html
(If you are deleting a function with more than one argument, auto balancing probably doesn't help anyway since you still have to chase the extra arguments.)
I don't know how to do it automatically, but here's a suggestion that can help you keep track of the brackets/parentheses visually and aid you in deleting them manually.
Break up your code into multiple lines, so that each function block opens and closes on a separate line (kinda like C, C++). For e.g., your above code will look like
Text[
Row[{
PaddedForm[
currentTime, {6, 3},
NumberSigns -> {"", ""},
NumberPadding -> {"0", "0"}
]
}]
]
I use this in blocks of code that are longer than a line or so, and especially in plotting, where it's real easy to keep track of the options you supply.
I am answering my own question here.
Using CODE as cell type instead of INPUT solved this issue for me.
closing.
The following will add the keyboard shortcut Shift+Backspace to remove the closest enclosing brackets of the current cursor position. It also adds a menu item to the edit menu.
This function takes the current clipboard content and removes the first and last "word".
cutClipboardBrackets:=Module[{nb},
nb=CreateDocument[{},Visible->False,WindowSelected->False];
NotebookWrite[nb,NotebookGet#ClipboardNotebook[]]
SelectionMove[nb,All,CellContents];
FrontEndExecute[FrontEndToken[nb,"MoveCellBeginning"]]
SelectionMove[nb,All,Word,1];
NotebookDelete[nb];
FrontEndExecute[FrontEndToken[nb,"MoveCellEnd"]];
SelectionMove[nb,All,Word,1];
NotebookDelete[nb];
SelectionMove[nb,All,CellContents];
FrontEndExecute[FrontEndToken[nb,"Copy"]];
];
This can be used to remove brackets, since they are the first and last word when copying FrontEndExecute[FrontEndToken[nb,"Balance"]]. The function that selects, cuts, removes the additional brackets and pastes is:
RemoveBrackets[nb_]:= (
FrontEndExecute[FrontEndToken[nb,"Balance"]];
FrontEndExecute[FrontEndToken[nb,"Cut"]];
cutClipboardBrackets;
FrontEndExecute[FrontEndToken[nb,"Paste"]];
);
Finally we can protect the functions and add a keyboard shortcut (like here):
Protect[cutClipboardBrackets,ClipboardBrackets];
FrontEndExecute[
FrontEnd`AddMenuCommands[
"SelectAll",{Delimiter,MenuItem["Delete Outer Brackets",
FrontEnd`KernelExecute[nb=CreateDocument[Null,Visible->False,WindowSelected->True];
NotebookWrite[nb,Cell[BoxData[RowBox[{"RemoveBrackets[SelectedNotebook[]]"}]],"Input"]];
SelectionMove[nb,Previous,Cell];
SelectionEvaluate[nb];
NotebookClose[nb]],
MenuKey["Backspace",Modifiers->{"Shift"}],System`MenuEvaluator->Automatic]}]]

xcode: select expressions shortcut

With Eclipse/Java one of the shortcuts I used a lot was selecting expressions. alt+shift+up would increase selection to the next parent/enclosing expression, alt+shift+down would bring it back down.
Is there anything like this for XCode/Objective-C. What about when using vi?
I don't know about any keyboard shortcuts, but in XCode double-clicking a parentheses, curly bracket, square bracket, or quote will select the corresponding expression/block.
Doesn't seem to be any way to do this, even in XC4. Sad.

Conventions for the behavior of double or triple "click to select text" features?

Almost any mature program that involves text implements "double click to select the word" and, in some cases, "triple click to select additional stuff like an entire line" as a feature. I find these features useful but they are often inconsistent between programs.
Example - some programs' double clicks do not select the ending space after a word, but most do. Some recognize the - character as the end of a word, others do not. SO likes to select the entire paragraph as I write this post when I triple click it, VS web developer 2005 has no triple click support, and ultra-edit 32 will select one line upon triple clicking. We could come up with innumerable inconsistencies about how double and triple click pattern matching is implemented across programs.
I am concerned about how to implement this behavior in my program if nobody else has achieved a convention about how the pattern matching should work.
My question is, does a convention (conventions? maybe an MS or Linux convention?) exist that dictates how these features are supposed to behave to the end user? What, if any, are they?
I don’t believe there is a standard to the level of specification you want, and there probably shouldn’t be. Apple Human Interface Guidelines are the most complete. With respect to selecting content (as opposed to controls or discrete data objects), they say:
Double-clicking is most commonly used as a shortcut for other actions, such as… to select a word. Triple-clicking selects the next logical unit, as defined by the application. In a word-processing document, triple-clicking in a word selects the paragraph containing the word…. Double-clicking within a word selects the word. The selection should provide “smart” behavior; if the user deletes the selected word, for example, the space after the word should also be deleted… In some contexts—in a programming language, for example—it may be appropriate to allow users to select both the left and right parentheses (or braces or brackets) in a pair, as well as all the characters between them, by double-clicking either one of them.” (p115-116)
Apple is quite specific about what characters are and aren’t included in a word.
Microsoft’s Windows User Interaction Experience Guidelines say:
For some types of selectable objects, each click expands the effect of the click. For example, single-clicking in a text box sets the input location, double-clicking selects a word, and triple-clicking selects a sentence or paragraph. (p430)
Java Swing Look and Feel Design Guidelines say:
Double-clicking (clicking a mouse button twice in rapid succession without moving the mouse) is used to select larger units (for example, to select a word in a text field)…. Triple-clicking (clicking a mouse button three times in rapid succession without moving the mouse) is used to select even larger units (for instance, to select an entire line in a text field)…. A triple click in a line of text deselects any existing selection and selects the line.
The Gnome Human Interface Guidelines don’t say much about what double- and triple-clicking should do.
This gives you the freedom to choose whatever is best for your users. Double and tripling clicking are expert shortcuts, so their behavior should aim to maximize efficiency. Consider why the user is selecting something and design to make that easiest and fastest.
For example, apparently the rationale behind including the trailing space when double-clicking a word is that users usually select a word in order to copy or paste it in another position in the text. This implies you automatically include the trailing space in order keep the user from having to manually delete a remaining extra space at the source and add a word-separating space at the destination.
Likewise if users are selecting a line of code or paragraph to copy or move it somewhere else, then you probably want to include the newline characters so the user isn’t left with an empty line at the source and force to manually add a newline at the destination (assuming they didn’t want to take the line/paragraph and combine it with another line/paragraph.
If selection is for something other than copying and moving text in sentences, then none of this may apply and you don’t necessarily want to include trailing spaces or newlines. That’s why there shouldn’t be a standard.
An alternative is to do what Apple calls Intelligent Cut and Paste (see the Human Interface Guidelines), or Microsoft Word’s Smart Cut and Paste, where spaces, newlines and other adjustment are algorithmically figured out when cutting, copying, pasting, and deleting, not when selecting.
In my perfect world I would have it work like this.
Double click on a word selects the word only (a word according to the grammar rules of the locale), no trailing space (this is for easier copying between programs so that I would not need to remove any spaces when pasting)
If I remove the selected word my text editor is aware of my content and removes any additional spaces left over
A triple click selects a line with no trailing newlines. (A paragraph is a long line that has been wrapped)
In Windows, Linux and OS X double-click selects the word under cursor triple-click selects the entire line of text (single line only, i.e., wrapped line)
Finding answers and come up with a alternative solution:
I like to write code or command in text, and copy them to shell prompt without the ending \n
1. use notepad
2. surround each line with ()
3. use ctrl + double click.
Fine...

Resources