-
-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
190 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[Plugin] | ||
Module=markdown-actions | ||
Loader=C | ||
IAge=2 | ||
Name=Markdown Actions | ||
Description=Adds keyboard shortcuts for quick editing Markdown files | ||
Icon=format-text-bold | ||
Authors=Igor Montagner <[email protected]> | ||
Copyright=Copyright © 2020 Igor Montagner | ||
Hidden=false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
// -*- Mode: vala; indent-tabs-mode: nil; tab-width: 4 -*- | ||
/*** | ||
BEGIN LICENSE | ||
Copyright (C) 2020 Igor Montagner <[email protected]> | ||
This program is free software: you can redistribute it and/or modify it | ||
under the terms of the GNU Lesser General Public License version 3, as published | ||
by the Free Software Foundation. | ||
This program is distributed in the hope that it will be useful, but | ||
WITHOUT ANY WARRANTY; without even the implied warranties of | ||
MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR | ||
PURPOSE. See the GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License along | ||
with this program. If not, see <http://www.gnu.org/licenses/> | ||
END LICENSE | ||
***/ | ||
|
||
public class Code.Plugins.MarkdownActions : Peas.ExtensionBase, Peas.Activatable { | ||
Scratch.Widgets.SourceView current_source; | ||
Scratch.Services.Interface plugins; | ||
|
||
public Object object { owned get; construct; } | ||
|
||
public void update_state () {} | ||
|
||
public void activate () { | ||
plugins = (Scratch.Services.Interface) object; | ||
plugins.hook_document.connect ((doc) => { | ||
if (current_source != null) { | ||
current_source.key_press_event.disconnect (shortcut_handler); | ||
current_source.notify["language"].disconnect (configure_shortcuts); | ||
} | ||
|
||
current_source = doc.source_view; | ||
configure_shortcuts (); | ||
|
||
current_source.notify["language"].connect (configure_shortcuts); | ||
}); | ||
} | ||
|
||
private void configure_shortcuts () { | ||
var lang = current_source.language; | ||
if (lang != null && lang.id == "markdown") { | ||
current_source.key_press_event.connect (shortcut_handler); | ||
} else { | ||
current_source.key_press_event.disconnect (shortcut_handler); | ||
} | ||
} | ||
|
||
private bool shortcut_handler (Gdk.EventKey evt) { | ||
var control = (evt.state & Gdk.ModifierType.CONTROL_MASK) != 0; | ||
var shift = (evt.state & Gdk.ModifierType.SHIFT_MASK) != 0; | ||
var other_mods = (evt.state & Gtk.accelerator_get_default_mod_mask () & | ||
~Gdk.ModifierType.SHIFT_MASK & | ||
~Gdk.ModifierType.CONTROL_MASK) != 0; | ||
|
||
if (evt.is_modifier == 1 || other_mods == true) { | ||
return false; | ||
} | ||
|
||
if (control && shift) { | ||
switch (evt.keyval) { | ||
case Gdk.Key.B: | ||
add_markdown_tag ("**"); | ||
return true; | ||
case Gdk.Key.I: | ||
add_markdown_tag ("_"); | ||
return true; | ||
case Gdk.Key.K: | ||
insert_link (); | ||
break; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
private void insert_link () { | ||
var current_buffer = current_source.buffer; | ||
current_buffer.begin_user_action (); | ||
if (current_buffer.has_selection) { | ||
insert_around_selection ("[", "]"); | ||
current_buffer.insert_at_cursor ("()", 2); | ||
go_back_n_chars (1); | ||
} else { | ||
current_buffer.insert_at_cursor ("[]", 2); | ||
current_buffer.insert_at_cursor ("()", 2); | ||
go_back_n_chars (3); | ||
} | ||
current_buffer.end_user_action (); | ||
} | ||
|
||
private void go_back_n_chars (int back_chars) { | ||
Gtk.TextIter insert_position; | ||
var current_buffer = current_source.buffer; | ||
current_buffer.get_iter_at_offset (out insert_position, current_buffer.cursor_position - back_chars); | ||
current_buffer.place_cursor (insert_position); | ||
} | ||
|
||
private void insert_around_selection (string before, string after) { | ||
Gtk.TextIter start, end; | ||
var current_buffer = current_source.buffer; | ||
current_buffer.get_selection_bounds (out start, out end); | ||
var mark_end = new Gtk.TextMark (null); | ||
current_buffer.add_mark (mark_end, end); | ||
current_buffer.place_cursor (start); | ||
current_buffer.insert_at_cursor (before, before.length); | ||
|
||
current_buffer.get_iter_at_mark (out end, mark_end); | ||
current_buffer.place_cursor (end); | ||
current_buffer.insert_at_cursor (after, after.length); | ||
} | ||
|
||
public void add_markdown_tag (string tag) { | ||
var current_buffer = current_source.buffer; | ||
current_buffer.begin_user_action (); | ||
if (current_buffer.has_selection) { | ||
insert_around_selection (tag, tag); | ||
} else { | ||
current_buffer.insert_at_cursor (tag, tag.length); | ||
current_buffer.insert_at_cursor (tag, tag.length); | ||
} | ||
current_buffer.end_user_action (); | ||
go_back_n_chars (tag.length); | ||
} | ||
|
||
public void deactivate () { | ||
if (current_source != null) { | ||
current_source.key_press_event.disconnect (shortcut_handler); | ||
current_source.notify["language"].disconnect (configure_shortcuts); | ||
} | ||
} | ||
} | ||
|
||
[ModuleInit] | ||
public void peas_register_types (TypeModule module) { | ||
var objmodule = module as Peas.ObjectModule; | ||
objmodule.register_extension_type (typeof (Peas.Activatable), | ||
typeof (Code.Plugins.MarkdownActions)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
module_name = 'markdown-actions' | ||
|
||
module_files = [ | ||
'markdown-actions.vala', | ||
] | ||
|
||
module_deps = [ | ||
codecore_dep, | ||
] | ||
|
||
shared_module( | ||
module_name, | ||
module_files, | ||
dependencies: module_deps, | ||
install: true, | ||
install_dir: join_paths(pluginsdir, module_name), | ||
) | ||
|
||
custom_target(module_name + '.plugin_merge', | ||
input: module_name + '.plugin', | ||
output: module_name + '.plugin', | ||
command : [msgfmt, | ||
'--desktop', | ||
'--keyword=Description', | ||
'--keyword=Name', | ||
'-d' + join_paths(meson.source_root (), 'po', 'plugins'), | ||
'--template=@INPUT@', | ||
'-o@OUTPUT@', | ||
], | ||
install : true, | ||
install_dir: join_paths(pluginsdir, module_name), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters