From c3c944ec936b1980abc0f8adc2615d4e458da8f3 Mon Sep 17 00:00:00 2001 From: defanor Date: Tue, 24 Apr 2018 00:56:19 +0300 Subject: Initial commit --- html-wysiwyg.el | 107 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 html-wysiwyg.el (limited to 'html-wysiwyg.el') diff --git a/html-wysiwyg.el b/html-wysiwyg.el new file mode 100644 index 0000000..1a069fe --- /dev/null +++ b/html-wysiwyg.el @@ -0,0 +1,107 @@ +;;; html-wysiwyg.el --- WYSIWYG elements for HTML editing + +;; Copyright (C) 2018 defanor + +;; Author: defanor +;; Maintainer: defanor +;; Created: 2018-04-24 +;; Keywords: WYSIWYG, HTML +;; Homepage: https://github.com/defanor/html-wysiwyg +;; Version: 0.1.0 + +;; This file is not part of GNU Emacs. + +;; This program is free software: you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation, either version 3 of the License, or +;; (at your option) any later version. + +;; This program is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY 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 . + +;;; Commentary: + +;; Inspired by org-mode's link handling, but parsing HTML with regexps +;; like there's no tomorrow. + +;;; Code: + +(require 'thingatpt) + +(define-minor-mode html-wysiwyg-mode + "A minor mode for partial WYSIWYG editing of HTML, akin to + org." + :after-hook (html-wysiwyg-initialise) + :keymap `((,(kbd "C-c , e") . html-wysiwyg-edit-link-at-point) + (,(kbd "C-c , h") . html-wysiwyg-hide-links) + (,(kbd "C-c , s") . html-wysiwyg-show-links))) + +(defun html-wysiwyg-initialise () + "Initialises html-wysiwyg-mode." + (interactive) + (add-to-list 'font-lock-extra-managed-props 'invisible) + (add-to-list 'font-lock-extra-managed-props 'help-echo) + (html-wysiwyg-hide-links)) + +(defvar html-wysiwyg-link-match + (rx (group ""))) "href=" (syntax string-quote)) + (group (*? (not (syntax string-quote)))) + (group (syntax string-quote) (*? (not (any ">"))) ">") + (group (+? (not (any "<" ">")))) + (group "")) + "A regular expression used to match elements.") + +(defvar html-wysiwyg-font-lock-keywords + `((,html-wysiwyg-link-match + 0 + (progn (add-text-properties (match-beginning 0) + (match-end 0) '(invisible t)) + 'default) + t) + (,html-wysiwyg-link-match + 4 + (progn + (add-text-properties + (match-beginning 4) (match-end 4) + `(invisible + nil + help-echo + ,(buffer-substring-no-properties + (match-beginning 2) (match-end 2)))) + 'link) + t)) + "Keywords for font lock mode.") + +(defun html-wysiwyg-hide-links () + "Hide tags." + (interactive) + (font-lock-add-keywords nil html-wysiwyg-font-lock-keywords) + (font-lock-fontify-buffer)) + +(defun html-wysiwyg-show-links () + "Make tags visible again." + (interactive) + (font-lock-remove-keywords nil html-wysiwyg-font-lock-keywords) + (font-lock-fontify-buffer)) + +(defun html-wysiwyg-edit-link-at-point () + "Edit an element's @href attribute." + (interactive) + (save-excursion + (if (thing-at-point-looking-at html-wysiwyg-link-match) + (let* ((start (match-beginning 2)) + (end (match-end 2)) + (href (buffer-substring-no-properties start end)) + (new-href (read-from-minibuffer "href: " href))) + (delete-region start end) + (goto-char start) + (insert new-href)) + (message "No link at point.")))) + +(provide 'html-wysiwyg) +;;; html-wysiwyg.el ends here -- cgit v1.2.3