Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Markdown plugin #890

Merged
merged 21 commits into from
Dec 15, 2020
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions plugins/markdown-actions/markdown-actions.plugin
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
124 changes: 124 additions & 0 deletions plugins/markdown-actions/markdown-actions.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
// -*- 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);
igordsm marked this conversation as resolved.
Show resolved Hide resolved
}

current_source = doc.source_view;
configure_shortcuts ();

current_source.notify["language"].connect (configure_shortcuts);
jeremypw marked this conversation as resolved.
Show resolved Hide resolved
});
}

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;
if (control && !shift && evt.keyval == Gdk.Key.b) {
igordsm marked this conversation as resolved.
Show resolved Hide resolved
igordsm marked this conversation as resolved.
Show resolved Hide resolved
add_markdown_tag ("**");
return true;
} else if (control && shift && evt.keyval == Gdk.Key.I) {
add_markdown_tag ("_");
return true;
} else if (control && evt.keyval == Gdk.Key.k) {
insert_link ();
}
return false;
}

private void insert_link () {
var current_buffer = current_source.buffer;
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);
}
}

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;
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);
}
go_back_n_chars (tag.length);
}

public void deactivate () {
if (current_source != null) {
current_source.key_press_event.disconnect (shortcut_handler);
}
}
}

[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));
}
32 changes: 32 additions & 0 deletions plugins/markdown-actions/meson.build
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),
)
1 change: 1 addition & 0 deletions plugins/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ subdir('brackets-completion')
subdir('detect-indent')
subdir('editorconfig')
subdir('highlight-word-selection')
subdir('markdown-actions')
subdir('outline')
subdir('pastebin')
subdir('preserve-indent')
Expand Down