Align selected region in emacs - emacs

For example I write code
if Foo do
# do something
end
And then, I paste in my code many lines.
if Foo do
# do something
# do something
# do something
# do something
# do something
end
How I can fast align added lines?

Do you want to align automatically on insert, or you want to align it later? For later you can use indent-region function (bound to C-M-\). For automatic align on insert you can use following recipe (I don't remember where I took it, so I'll refer to my config) - see lines 45-66. You will need to add more modes to yank-indent-modes, but concrete mode should provide working indent function.

Related

Combining two commands in a new command in Latex

I am trying to create a new command that will replace text with a blank line to write on. Essentially, the lovechild of, \underline and \phantom if it can be toggled on and off that would be even better but I seem to have a misunderstanding of how macros work in latex.
My crappy code is below
\newcommand{\rmv{}}{\Underline{\phantom{}}}
You're macro needs to know what text you want in the gap, so it needs an argument. The syntax to pass argument to the new macro is
\newcommand\macroname[number of arguments]{use the argument with #1 etc.}
\documentclass{article}
\newif\ifgaps
\gapstrue % comment/uncomment to toggle
\newcommand{\rmv}[1]{%
\ifgaps%
\underline{\phantom{#1}}%
\else%
#1%
\fi%
}
\begin{document}
test \rmv{test} test
\end{document}
(to automatically create two versions of your pdf with and without the gaps, you could use something similar to https://topanswers.xyz/tex?q=583 )

How to add custom indentation rules to align multiple lines of code when creating a language extension?

I'm authoring an extension for Racket (lisp dialect) and I'd like to customize the indentation rules further than just "when line ends with X, indentation +1, else indentation -1". However, as far as I understand it, that exactly is the point of increaseIndentPattern and decreaseIndentPattern in language-configuration.json.
In particular, I'd like to specify some rule that goes like this:
(call-foo first-arg
......... second-arg
......... (third-arg-function foo
..............................bar))
Where the dots denote the auto-indentation (spaces) automatically inserted by VS Code upon pressing return on the preceeding lines. Is it possible to do something like this?

Why does the first hash mark disappear in a vim fold?

I am using manual folding to write perl programs. Here is a typical fold:
sub do_something # does something --{{{
{
# perl code here
} # --}}}
When folded, these four lines are displayed thus:
+-- 4 lines: sub do_something does something ----------------------------
In the folded version, the hash mark before the word "does" has disappeared. Why? If this is a feature, how can I disable it? As a workaround, I am writing 'sub do_something ## does something --{{{', but is there a clean way to get vim to just display what I have typed? (Maybe this has something to do with perl.vim?)
What gets displayed instead of the folded lines is controlled by the 'foldtext' option. By default, the internal foldtext() function is used. :help foldtext() explains:
The returned string looks like this:
+-- 45 lines: abcdef
The number of dashes depends on the foldlevel. The "45" is
the number of lines in the fold. "abcdef" is the text in the
first non-blank line of the fold. Leading white space, "//"
or "/*" and the text from the 'foldmarker' and 'commentstring'
options is removed.
As you see, this is a heuristic of Vim to reduce clutter. To turn that off:
You can clear 'commentstring'; it is only used to add manual fold markers (and some commenting plugins may rely on it as a fallback), by putting :setlocal commentstring= into ~/.vim/after/ftplugin/perl.vim.
You can write your own fold function (example and instructions at :help fold-foldtext), and install that, either globally or (like above alternative) only for the Perl filetype.

Problem writing a snippet containing Emacs Lisp code

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$>
}$>

How to enforce maximum line length in Emacs?

In Emacs, how can I enforce a maximum line length of, say, 80 characters? I want it to insert proper line breaks in my code, much like fill-paragraph for text, if possible, with the correct insertion of the second part of the line.
Here a little example:
LongNameType<PrettyLong, AlsoLong> doSomethingWithLongFunctionName(int a, int b);
foo();
If I actually do fill-paragraph it becomes:
LongNameType<PrettyLong, AlsoLong>
doSomethingWithLongFunctionName(int a, int b); foo();
whereas I'd prefer this:
LongNameType<PrettyLong, AlsoLong>
doSomethingWithLongFunctionName(int a, int b);
foo();
There are a number of packages which warn you of line length limits. Personally, I use wide-column, which changes the cursor color depending on its current column.
fill-paragraph and auto-fill-mode deliberately don't wrap code. There are just too many ways to do it and it'd probably get it wrong. They will wrap comments, but that doesn't help you here.
The only way I've ever done it to to explicitly put the where I want the text to break. Then the auto-indent should put the broken line in the right place.
Are you trying to reflow a large body of existing code? Or trying to have auto-fill work on code you are writing now?
Not really an emacser, but what happens if you turn on auto-fill-mode while in c++-mode?
C++ mode should give you auto-indent, and auto-fill-mode gives you line-wrapping....
I use modeline-posn package. It highlights column number in the modeline if it's greater than specified value.
You should check out one of the many "vertical line" libraries for Emacs. Some keep a vertical highlight line over the entire buffer at point at all times (not really what you want) but other libraries put the vertical highlight on a fix column at all times, which is not really what you want, but you can immediately see when you ought to be wrapping lines.
Try
'(c-max-one-liner-length 80)
'(fill-column 80)
'(c-ignore-auto-fill (quote (string cpp)))
Hope it helps.
You could use the more advanced clang-format package.
You have to install clang-fromat along with it's emacs package.
Add this to your .emacs (setq clang-format-style "file") or add to custom-set-variables '(clang-format-style "file").
Generate your template style by clang-format -style=gnu -dump-config > .clang-format then place into your project's root.
Customize .clang-format as you like and change ColumnLimit: 120 to 80 or whatever value you want.
That would force column limit using clang-format tool.
Reference:
ClangFormatStyleOptions.
Related question