Skip to content

Commit

Permalink
Add Markdown plugin (#890)
Browse files Browse the repository at this point in the history
  • Loading branch information
igordsm authored Dec 15, 2020
1 parent ad4711c commit 1868f07
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 0 deletions.
4 changes: 4 additions & 0 deletions data/io.elementary.code.appdata.xml.in
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@
<releases>
<release version="3.4.2" date="2020-06-09">
<description>
<p>New features:</p>
<ul>
<li>New Markdown plugin for WYSIWYG-like editing.</li>
</ul>
<p>Minor updates:</p>
<ul>
<li>Allow Spell Checker extension in Markdown files</li>
Expand Down
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
143 changes: 143 additions & 0 deletions plugins/markdown-actions/markdown-actions.vala
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));
}
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

0 comments on commit 1868f07

Please sign in to comment.