Emacs tries to be clever with its comment functions so that lines which are blank don't get a comment prefix. Is there an easy built in way to make sure that all lines (blank or not) get commented when I call something like comment-region? So, if I have this code:
Comment comment comment
More comments more comments
Suppose the comment prefix is //, I want to be able to select that region, and have the comments look like:
// Comment comment comment
//
// More comments more comments
Yes, I could write a custom function to do this, but it seems like basic enough behavior that it might somehow be built in.
comment-empty-lines is a variable defined in newcomment.el.
Its value is nil
Documentation:
If nil, comment-region does not comment out empty lines.
If t, it always comments out empty lines.
If eol it only comments out empty lines if comments are
terminated by the end of line (i.e. comment-end is empty).
You can customize this variable.
Related
I'm interested in only searching comments in VSCode. Is this possible? Specifically, I would like to search for instances of () in comments only.
Supposed it's not a block comment, you could try matching it with a regex.
# line comment
or
// line comment
/*
block comment
*/
Searching using regex can be easily achieved by clicking the icon with two stars on the right of the search bar.
If # is your comment character, you could use # *yourInstanceHere.
I have a very long script in MATLAB (1500 lines) and want to test two different settings. To do so, I need to comment out some codes in a specific interval (e.g. form line 234 to line 255).
Is there a function in MATLAB that takes the intervals and comments/uncomments them automatically?
You can just highlight your code and click on the comment button. Highlight and click on uncomment to remove comment.In windows you may also use shortcut keys cntrl+r and cntrl+t. But yeah, the if else is a better idea but takes more time in the beginning to code in the if else.
I would agree with others that putting your code into a block surrounded by if-else would probably be a better solution than what you originally asked for.
But if you want to do it, you can use the following function:
function commentout(fromline, toline)
currentDoc = matlab.desktop.editor.getActive;
currentDoc.insertTextAtPositionInLine(sprintf('%%{\n'), fromline, 0)
currentDoc.insertTextAtPositionInLine(sprintf('%%}\n'), toline+1, 0)
This will work in most recent versions of MATLAB.
To uncomment, I think you'll need to do something a bit more complex, like getting the entire text from the active document, removing the specified comment lines, then setting the entire text back again (get, modify and set the Text property of the document).
I'm using emacs (24.3.1) along with cc-mode and hideshow for programming c++. I am working on a project, where the coding styles requires that any keywords present in the header file must be repeated in the source file. In case that this is not allowed by the standard, the keyword must be placed in comments. Let me give you and example:
/* virtual */ void MyAwesomeFunction( int arg, int optarg /* = 0 */ ){
// stuff
}
Obviously there is a comment starting the line. It seems that hideshow can't cope with this sort of formatting. When I call hs-hide-all all code blocks are folded correctly, the same is true if I call hs-hide-block from anywhere within the function. However, if I call hs-hide-block whith point beeing somewhere in the opening line of the function (the first line of my codesnippet) now folding occurs and the error message:
(not enough comment lines to hide)
is printed. The only explicit configuration of hideshow I have done so far is:
(setq hs-hide-comments nil)
However, removing this line makes it even worse: Afterwards not even calling hs-hide-all works properly: all inner blocks are folded, but folding at the function level does not occurs for functions with a leading comment.
Anyone knows how to fix this?
It might help to explicitly hide comments first, either just within the region or throughout the buffer. You can use library hide-comnt.el to do that. A description is here.
I've been trying to make use of a cool feature of YASnippet: write snippets containing embedded Emacs Lisp code. There is a snippet for rst-mode that surrounds the entered text with "=" that is as long as the text such as in
====
Text
====
Based on this snippet, I decided to slightly modify it (with Elisp) so that it comments out these three lines depending on the major mode you are in (I thought that such a snippet would be useful to organize the source code). So I wrote this:
${1:`(insert comment-start)`} ${2:$(make-string (string-width text) ?\-)}
$1 ${2:Text}
$1 ${2:$(make-string (string-width text) ?\-)}
$0
This code works relatively well except for one problem: the indentation of these three lines gets mixed up, depending on the major mode I'm in (e.g., in emacs-lisp-mode, the second and the third lines move more to the right than the first line).
I think the source of the problem might have something to do with what comes after the string ${1: on the first line. If I add a character, I have no problem (i.e., all three lines are correctly aligned at the end of the snippet expansion). If I add a single space after this string, the misalignment problem still continues though.
So my question is: do you know of any way of rewriting this snippet so that this misalignment does not arise? Do you know what's the source of this behaviour?
Cheers,
From Writing snippets:
yas/indent-line
The variable yas/indent-line controls the indenting. It is bound to 'auto by default, which causes your snippet to be indented according to the mode of the buffer it was inserted in.
Another variable yas/also-auto-indent-first-line, when non-nil does exactly that :-).
To use the hard-coded indentation in your snippet template, set this variable to fixed.
To control indentation on a per-snippet basis, see also the directive # expand-env: in Writing Snippets.
For backward compatibility with earlier versions of YASnippet, you can also place a $> in your snippet, an (indent-according-to-mode) will be executed there to indent the line. This only takes effect when yas/indent-line is set to something other than 'auto.
for (${int i = 0}; ${i < 10}; ${++i})
{$>
$0$>
}$>
I'm writing an Emacs major mode for an APL dialect I use at work. I've gotten
basic font locking to work, and after setting comment-start and
comment-start-skip, comment/uncomment region and fill paragraph also
work.
However, comment blocks often contain javadoc style comments and i
would like fill-paragraph to avoid glueing together lines starting
with such commands.
If I have this (\ instead of javadoc #):
# This is a comment that is long and should be wrapped.
# \arg Description of argument
# \ret Description of return value
M-q gives me:
# This is a comment that is long and
# should be wrapped. \arg Description
# of argument \ret Description of
# return value
But I want:
# This is a comment that is long and
# should be wrapped.
# \arg Description of argument
# \ret Description of return value
I've tried setting up paragraph-start and paragraph-separate to
appropriate values, but fill-paragraph still doesn't work inside a
comment block. If I remove the comment markers, M-q works as I want
to, so the regexp I use for paragraph-start seems to work.
Do I have to write a custom fill-paragraph for my major
mode? cc-mode has one that handles cases like this, but it's really
complex, I'd like to avoid it if possible.
The problem was that the paragraph-start regexp has to match the entire line to work, including the actual comment character. The following elisp works for the example I gave:
(setq paragraph-start "^\\s-*\\#\\s-*\\\\\\(arg\\|ret\\).*$")
Here a page that has an example regexp for php-mode that does this:
http://barelyenough.org/blog/2006/10/nicer-phpdoc-comments/
There's other modes that have less complex functions used for fill-paragraph-function. Browsing through my install, it looks like the ones in ada-mode and make-mode are good examples.
What I do in these cases is open a blank line between the paragraph lines and the argument lines, then use M-q to wrap the paragraph lines, then kill the blank line between them. Not ideal, but it works and is easy enough to record in a macro if you need to repeat it.