Vnoremap function equivalent in Spacemacs - spacemacs

Is there a way to get something like
vnoremap <F3> :call Surround("prfx_", "_psfx")<Enter>
function! Surround(prefix, postfix)
" get the selection
let selection = #*
" remove selected text
normal gv"xx
" inserting text with prefix and postfix
execute "normal i" . a:prefix . selection . a:postfix
endfunction
In Spacemacs? I have this in my .vimrc and it would be incredibly handy to have the same functionality while working with .org files. Is this something I will need to learn to implement in Elisp?

Yes, there is a way. And yes it is something you will have to implement in elisp. I'm not 100% sure of what exactly your function does.
But you'll probably have to use:
(evil-define-key '(visual) 'org-mode-map "<f3>" 'your-surround-function)
Also look into packages like this: https://github.com/emacs-evil/evil-surround

Related

InteliJ - How to create shortcuts for code samples

I am using InteliJ and really love using it. One of the questions I have is this:
Is there a way to create code short cuts?
For instance, while bug testing, I am forever writing:
<?php die(var_dump($var)); ?>
and figured it would be great to have a shortcut key to automate this. i.e.
"Cmd Option D"
or something similar to dump the pre-defined statement into my code...
Any thoughts on this?
You can use Live Templates:
To define a template go to Settings/Live templates, then select group or create new group of templates and hit the green plus button and select Live Template.
In the abbreviation field type for example vd which will be the trigger for your snippet, define context, which represents the languages this template will be available for and put this in the Template Text field:
<?php die(var_dump($SELECTION$)); ?>
The $SELECTION$ part is a variable which represents current selection.
Now when you are in editor, you can just type vd and hit Tab. That will expand your snippet and put your cursor inside var_dump().
You can event type the variable name you want to dump, select it, hit CTRL+ALT+T, which will show you a Surround with dialog, where you can choose your template. After you select it your variable name will be surrounded with the var_dump snippet.
Another way to invoke a live template is to hit CTRL+J which will show you autocomplete popup with the available templates.

How to Use GuiDropFiles with GUI controls?

How can I use GuiDropFiles with GUI controls?
I have several edit fields in my form, and I want to be able to drop files onto them separately and work with them.
This is what I came up with:
First, my controls are set up like this:
WS_EX_ACCEPTFILES=0x10
Gui, add, edit, vedit1, %file_1%
WinSet,ExStyle, +WS_EX_ACCEPTFILES, edit1
And my drag-drop routine is as such:
GuiDropFiles: ; Support drag & drop.
Loop, parse, A_GuiControlEvent, `n
{
thisfile := a_loopfield ; Get the first file only (in case there's more than one).
thiscontrol := a_guicontrol
break
}
alert(thisfile . "`r" . thiscontrol)
if(thiscontrol = edit1)
guicontrol,,%edit1%, %thisfile%
if(thiscontrol = edit2)
guicontrol,,%edit2%, %thisfile%
if(thiscontrol = edit3)
guicontrol,,%edit3%, %thisfile%
return
I am using the basic example from the autohotkey documentation. I also tried the example from here, but it keep saying, "not dropped on an edit box".
Any clue would be great.
Figured it out (after a few lost hours).
First, I didn't need this: WinSet,ExStyle, +WS_EX_ACCEPTFILES, edit1
and I didn't need to set any styles on the edit controls.
All I needed was this, which works since my edit controls have the variable-name starting with "UI_file":
GuiDropFiles: ; Support drag & drop.
Loop, parse, A_GuiEvent, `n
{
thisfile := A_LoopField ; Get the first file only (in case there's more than one).
thiscontrol := a_guicontrol
break
}
;alert(thisfile . "`r" . thiscontrol)
If InStr(A_GuiControl, "UI_file")
guicontrol,,%A_GuiControl%, %thisfile%
return

How could I escape a & in Haml so that it compiles to & instead of &? (Haml noob)

I am trying to use the Icomoon icon font with Haml and can't seem to find a way to escape the & so that it stays just an & instead of &.
The Icomoon font allows you to use HTML entities with a data-icon="" attribute. Works smooth as butter in HTML and even in a Haml file if I just do a straight HTML link.
However, since I'm learning Haml I thought I'd see if anyone on here would like to recommend the best way to approach this.
Here's a sample of what happens.
This is the original Haml:
%a(href='/posts' data-icon="&#x0026" aria-hidden='true')
This is how it compiles:
<a aria-hidden='true' data-icon='&#x0026' href='/posts'>
This is how it needs to compile for the icon font to work:
<a aria-hidden='true' data-icon='&#x0026' href='/posts'>
and here is a codepen where you can see how the icon renders due to the amp; addition: http://codepen.io/dandenney/pen/3/6
I didn't like the top poster's way of completing this question. So far the best way I've found is to do:
- foo = "&#x0026".html_safe
%a(href='/posts' data-icon=foo aria-hidden='true')
I'm not fully happy with this, but think it's better for rails apps rather than turning off HTML escaping everywhere.
You can use the :escape_attrs option to control whether HTML sensitive characters in attributes are escaped:
require 'haml'
haml = "%a(href='/posts' data-icon=\"&#x0026\" aria-hidden='true')"
puts Haml::Engine.new(haml, :escape_attrs => false).to_html
Output:
<a aria-hidden='true' data-icon='&#x0026' href='/posts'></a>
Note that this will apply to all attributes in your Haml template.
In my opinion, I don't like the idea to disable the feature to escape the characters generally. Maybe you use relay at some point in your application on it.
For me the best way to do it is:
%a{ href: '/', 'data-icon' => "✐".html_safe }

How to simulate a selection from a SELECT control in sfTestBrowser

In a functional test in symfony, sfTestBrowser provides methods
click() "Simulates a click on a link or button."
select() "Simulates selecting a checkbox or radiobutton."
and unselect().
But I have not found a way to simulate making a selection from a <select> element.
Does anybody know a way to do this?
This has troubled me too. I'm assuming you just want to set the value for form submission? If you know the value, you can of course just do
$browser->click('Save', array(
'theselectfield' => 'desired_value'
));
But usually I don't know the value I want posted, because it's from a database-driven select box. So my solution is
$theOption = $browser->getResponseDomCssSelector()->matchAll('select[name*=name_of_select_field] option:contains(TheOptionTextYouWant)')->getNode();
$browser->setField('theselectfield', $theOption->getAttribute('value'));
... or use $browser->click() instead ...
Frustrating because you have to break out of the $browser call chain, in order to use getResponseDomCssSelector(), but I haven't found an easier way.

Drupal: How to get deeplink to comment?

I need to build and print a deeplink to any given comment. So that the user can directly access the specific comment with just clicking a link. I could not find a native drupal function to get this so i build it my own.
My solution
<?php
global $base_url;
$base = drupal_lookup_path('alias',"node/".$node->nid);
$path = $base_url.'/'.$base.'#comment-'.$comment->cid;
$link_options = array('html'=> $html);
$commentlink = l($date, $path, $link_options);
?>
To print the link you only have to call <?php print $commentlink;?>. But i'm pretty sure there is better and much more drupal like way to solve the problem.
The Better Way
Mikeker did it :) As he suggested here are the solution.
<?php
$commentlink = l(
$date,
"node/$comment->nid",
array("fragment" => "comment-$comment->cid"));
?>
Note the little difference bettween Mikeker and my version. array("fragment" => "comment-$comment->cid")); and array("query" => "comment-$comment->cid"));
The query param will add an ? to the url. So your path looks like
//…query
http://example.com/path/to/node?comment-2
In opposite to my solution (fragment):
//…fragment
http://example.com/path/to/node#comment-2
Note:
Do not include the leading '#' character to a fragment identifier. It will be added by drupal.
That's basically the way to do it. Comment permalinks are in the form of:
node/<nid>#comment-<cid>
Where <nid> and <cid> are the node and comment IDs, respectively. You can save yourself a step by not doing calling drupal_lookup_path() -- l() or url() do it for you. The shortened routine would look like:
$commentlink = l(
$date, // Text of the link
"node/$node->nid", // path to node, l() handles aliases
array('query' => "comment/$comment->cid"), // fragment to specific comment
);
In case anyone was wondering, the Drupal 7 way (at least, appears to be) this:
<a href='http://YOURSITE.com/comment/CID#comment-CID'>link text</a>
For example:
print "<a href='/comment/$comment->cid#comment-$comment->cid'>text here</a>";
And this would be placed in, perhaps, a comment.tpl.php file.