How do I configure Emacs html-mode to behave like TextMate's default HTML bundle? - html

A friend of mine is considering switching to Emacs from TextMate. He is used to TextMate's default HTML editing mode which has 4-space tab stops and inserts tab characters (i.e. it does no auto-indenting by default). It also allows completion of open HTML tags with "Cmd-Shift->". Any ideas?

I think these settings should do the trick:
(defun my-html-mode-hook ()
(setq tab-width 4)
(setq indent-tabs-mode t)
(define-key html-mode-map (kbd "<tab>") 'my-insert-tab)
(define-key html-mode-map (kbd "C->") 'sgml-close-tag))
(defun my-insert-tab (&optional arg)
(interactive "P")
(insert-tab arg))
(add-hook 'html-mode-hook 'my-html-mode-hook)
An explanation of the settings in 'my-html-mode-hook is as follows:
set the tab width to 4
force tabs to be inserted (as opposed to spaces)
force the TAB key to insert a tab (by default it is bound to do indentation, not just insertion of tabs
'sgml-close-tag is the command that inserts a close tag for you, and this setting gets you the keybinding you want
I'm having a bit of a brain freeze and couldn't figure out the simple way to have the TAB key insert a TAB character, so I wrote my own. I don't know why a binding to 'self-insert-command didn't work (that's what normal keys are bound to).
The last line just adds the setup function to the 'html-mode-hook. The key bindings really only need to be run once (as opposed to every time html-mode is enabled), but this is a little easier to read than using 'eval-after-load. It's use is left as an exercise to the reader.

I don't know about emacs's HTML modes specifically, but I can answer about general editing:
by default, Emacs doesn't autoindent, so nothing to do here.
Emacs preserves tab characters, unless you explicitely ask them changed (check out tabify and untabify). Their width is determined by the buffer-local tab-width variable. M-x set-variable, (setq...), customize at will.
you should be able to get the behavior you want with the tab key by setting indent-line-function to tab-to-tab-stop, setting tab-stop-list to (4 8 12 16...) and indent-tabs-mode to t.
Setting indent-tabs-mode allows Emacs to insert tab characters when indenting. The tab-to-tab-stop is a form of indentation that only goes to specific positions in the line, which we set to match the expected behavior of the tab characters by setting tab-stop-list to the multiples of 4.
About completion, the only thing my muscle memory tells me is "C-c C-e", but I don't remember for sure which major mode it's supposed to go with. The closest I see in the list is sgml-close-tag, bound to C-c /
A bit of politics: don't use tab characters, especially if you use widths not equal to 8. It only results in unpredictable results

Related

PhpStorm shortcut to wrap text with function call

I'm in PhpStorm and I need to select some text, press some shortcut and have that text wrapped in a function call (that I would have defined somewhere in the settings beforehand).
For example:
"Hello World" would become input("Hello World").
$_GET["foo"] would become input($_GET["foo"]).
I don't know if this is even possible, but it could help me save so much time if so.
Applying regex to solve this problem is unfortunately not possible. Manually selecting what I need to wrap isn't an issue.
Will the function name be the same every time or different?
In any case: it can be done this way:
Make a Live Template of "surround template" type with the following content:
$FUN$($SELECTION$)$END$
The $SELECTION$ variable here tells that it's a surround template.
Apply correct Context (where this template can be used)
Give it an abbreviation (name used to locate & invoke it) and brief description.
Here is mine:
NOTE: replace $FUN$ by a fixed function name if the function will always be the same. You can have additional templates with different abbreviations (that will have different hardcoded function names).
To use it:
Make a selection and invoke Code | Surround with... action where you select the right template. On Windows keymap it's Ctrl + Alt + T
In action (NOTE: it's without hardcoding the function name hence me typing the myFunc part):
(HINT: you can select the desired entry in a few keypresses if the name is unique -- just start typing the name in the popup -- the standard Speed Search work here)
P.S. Code | Surround with... can have other (possibly irrelevant for you in this case) entries. To list Live Templates only, use the shortcut for Surround with Live Template... action (Ctrl + Alt + J here on my Windows keymap). You can check the shortcut or change it in the Settings/Preferences | Keymap:
This way the popup menu will be a bit shorter:
Less keypresses:
You may be able to use Macros functionality to record the invoking the popup and selecting the right entry. You can then assign a custom shortcut to that Macros: select the text, hit the shortcut and it will playback the recorded sequence.
Sadly I cannot 100% guarantee that Macros will always work nicely (sometimes/on some setups it can "eat" keypresses).
P.S. It would be much easier if the IDE would support assigning keyboard shortcuts to specific Live Templates .. but it's in the backlog and no ideas on when this might be implemented. Anyway: https://youtrack.jetbrains.com/issue/IDEA-67811 -- watch this ticket (star/vote/comment) to get notified on any progress.
P.S. You can also try Postfix completion. It's good for writing the code and not really suitable for your case (editing small parts of it), but who knows. You will have to make a custom postfix for this -- should not be an issue though.
https://www.jetbrains.com/help/phpstorm/2021.3/auto-completing-code.html#postfix_completion

Turn off automatic space insertion on macOS

When you copy then paste the string “word”, macOS sometimes inserts “word” (unchanged), “ word”, “word ”, or “ word ”, depending on what you’re pasting it next to, whether it thinks you copied it as a word or as a range, and whether you’re pasting into a writing input (like a note in the Notes app) or a string input (like Safari’s URL bar). Click-and-dragging to select results in a range copy, double clicking results in a word copy.
On macOS, Safari and Chrome perform automatic space insertion in all inputs, while Firefox performs it in none. Firefox also does not follow the native behavior for double clicking a word, right clicking a word, or copying a word.
You can play with the behavior with the following demo. Try double clicking a word then pasting it multiple times, then try selecting a word by dragging then pasting it multiple times.
<div>one two three</div>
<input>
Automatic space insertion is probably fine and intuitive for macOS users, but is sometimes inappropriate for the same reason it’s inappropriate in Safari’s URL bar: some contexts are not in English or any other written language. In these contexts, automatic space insertion leads to surprising results. For example, I ran into this issue when entering input in a micro-language of the form [field.{id}], then pasting an ID, which was copied as a word, then getting [field. {id}] and the error that it caused.
Ideally, I want to instruct the browser not treat an input’s value as writing. Minimally, I want to turn off automatic space insertion an inputs. How can I do this?
Things I’ve tried that didn’t work:
Setting lang="" or lang="none" on both the input and the copied text
Setting lang="zh" on both the input and the copied text (Chinese languages do not use spaces between words)
Setting spellcheck="false" on the input
Setting autocorrect="off" on the input (non-standard, Safari only)
One solution is to cancel then mimic the paste event in JS.
<input id="example">
document.querySelector('#example').addEventListener('paste', e => {
e.preventDefault();
const pastedText = e.clipboardData.getData('text/plain');
document.execCommand('insertText', false, pastedText);
});
This solution has the following limitations:
Requires JS
Does not also disable other writing-specific niceties, if any exist
Uses the deprecated document.execCommand API

Return key is not handled in cell editor

I'm studying mxGraph examples and have the following problem. When I double-click a cell and edit in-place its content, I assume that pressing Return key should end editing and set the cell's content to the newly typed. But instead pressing the Return key works as usually in editors: it moves the caret to the new line.
Why I assume that Return should stop editing? Because comments say so (e.g. in userobject.html):
// Stops editing on enter key, handles escape
new mxKeyHandler(graph);
And also because there would be no convenient way to end cell editing apart from clicking with the mouse somewhere outside the cell. (By the way, Escape key is handled OK: it also ends editing, but without updating the cell content.)
This behaviour is observed in all browsers I tried: Firefox 60 on Linux, Firefox 52 and IE 11 on Windows.
Why this problem exist and how can it be solved?
Have you tried graph.enterStopsCellEditing = true? See https://jgraph.github.io/mxgraph/docs/js-api/files/view/mxGraph-js.html#mxGraph.enterStopsCellEditing

When I create a new class="" the cursor starts outside the quotes in PhpStorm. How can I get it to go between the quotes so I can continue typing?

When I type in class="" it autofills and puts the cursor after the closing quote.
This means I need to delete a quote to enter the class name or click between them. Both of which ruin my workflow.
Is there a way to put the cursor inbetween the quotes in settings? And if there is, is there a way to jump out of the quotes and keep adding to my markup?
In Sublime Text it puts the cursor in the class and then tab takes you outside of it. I'm sure PhpStorm can do the same.
Instead of typing the whole class="" thing by yourself -- just let IDE autocompletion to do the job for you.
Just two characters (cl) is enough to make class entry first in the list (unless you have used some other similarly named attributes/properties recently that would temporarily bring them higher than class):
Completing with Enter will have the text inserted into current position with caret located in the right place class="[CARET_HERE]".
Completing with Tab does the same but replaces the text that is currently under caret (useful when changing class/image name/function/etc completely).
If standard code completion is not good enough for some reason or you do not like automatic completion popop (and prefer invoking it manually only when needed) -- you will be interested in Live Templates functionality that allows creating some abbreviation and expanding it into the final snippet with minimal key presses (e.g. cl[Tab] into class="|")
IDE also has options to:
insert quotes ("" or '') after typing = in XML/HTML attributes.
Settings/Preferences | Editor | General | Smart Keys --> Add quotes for attribute value on typing '=' and attribute completion
insert pair quotes (closing one) when entering opening one -- works in different contexts/languages. It will "eat" the closing quote preventing you from typing too many.
Settings/Preferences | Editor | General | Smart Keys --> Insert pair quote
In Sublime Text it puts the cursor in the class and then tab takes you outside of it. I'm sure PhpStorm can do the same.
If I understood you correctly (sorry, never used Sublime myself) -- No... and may not have it for quite some time (devs say that the way how IDE works somehow conflicts with proposed Tab or Esc behaviour).
Better explanation/arguments from both sides can be found in actual ticket: https://youtrack.jetbrains.com/issue/IDEABKL-6984

Insert spaces instead of TAB in Emacs viper-mode

I am long time-vim user and recently discovered that emacs has viper-mode, offering best of both worlds (at least for me). However one thing is really bothering me since I am mostly coding in python and mixing of tabs and spaces is a big deal.
When in insert mode I would like to insert viper-shift-width spaces instead of TAB when I press TAB. How can I do this? I suppose some simple lisp function/setting is the solution.
I didn't find anything in viper-mode settings that could do this.
Edit:
I have (setq-default indent-tabs-mode nil) in my .emacs but this doesn't work when I am in insert mode (in vim meaing of insert mode) in viper-mode
First, you should ensure the default value of 'indent-tabs-mode is nil, like so:
(setq-default indent-tabs-mode nil)
Then, in viper-mode, it also depends on your viper-expert-level. At level 1 or 2, TAB appears to be bound to 'self-insert-command via the mode map viper-insert-diehard-minor-mode (which is enabled when the expert level is either 1 or 2). I guess that it is trying to provide maximal vi compatibility, which means you sacrifice some Emacs features, including the use of some pretty basic customizations.
So... you can up your expert level to 3 or higher:
(setq viper-expert-level 5) ; really, why use anything less?
If you really want level 1 or 2, but want TAB to not be a self inserting command, then add this to your .viper file:
(define-key viper-insert-diehard-map (kbd "TAB") 'viper-insert-tab)
That does the trick for me, even on level 1.
indent-tabs-mode perhaps?
What happens if you set it to nil, or unset it?
After you're in viper mode try doing M-x apropos and then search for space or tab or indent.