# HG changeset patch # User Oleksandr Gavenko # Date 1555716502 -10800 # Node ID 879c410290eaa8653f0e966c247db2b9e4a86b96 # Parent 4bd7c6066b4fa8fb97f89733d0a9e46b8ae93874 Added Emacs major mode for editing gaphrase dialog files. diff -r 4bd7c6066b4f -r 879c410290ea contrib/gaphrase.el --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/contrib/gaphrase.el Sat Apr 20 02:28:22 2019 +0300 @@ -0,0 +1,75 @@ +;;; gaphrase.el --- major mode for editing gaphrase dialog files -*- lexical-binding: t -*- + +;; Copyright (C) 2019 by Oleksandr Gavenko + +;; You can do anything with this file without any warranty. + +;; Author: Oleksandr Gavenko +;; Maintainer: Oleksandr Gavenko +;; Created: 2019 +;; Version: 0.1 +;; Keywords: dict, dictionary + +;;; Commentary: +;; +;; Mode can be installed by: +;; +;; (autoload 'gaphrase-mode "gaphrase") +;; +;; File association can be registered by: +;; +;; (add-to-list 'auto-mode-alist (cons "\\.gaphrase$" 'gaphrase-mode)) + +;;; Code: + +(defvar gaphrase-font-lock-keywords + '(("^\\(__\\)\n\n" (1 font-lock-type-face)) + ("^\\(__\n?[^\n]+\\)" (1 highlight)) + ("^- " . font-lock-keyword-face))) + +(defvar font-lock-beg) +(defvar font-lock-end) +(defun gaphrase-font-lock-extend-region () + ;; (gaphrase-font-lock-extend-region--debug) + (save-excursion + (goto-char font-lock-beg) + (forward-line 0) + (unless (looking-at "__") + (re-search-backward "^__" (- (point) 10240) t)) + (if (= (point) font-lock-beg) + nil + (setq font-lock-beg (point))))) + +(defun gaphrase-font-lock-extend-region--debug () + (save-excursion + (let ( beg end ) + (goto-char font-lock-beg) + (beginning-of-line) + (setq beg (buffer-substring-no-properties (point) font-lock-beg)) + (goto-char font-lock-end) + (end-of-line) + (setq end (buffer-substring-no-properties font-lock-end (point))) + (message "lines: %d, %s ## %s" (count-lines font-lock-beg font-lock-end) beg end)))) + +(defun gaphrase-insert-template () + (interactive) + (forward-line 0) + (unless (looking-at "__") + (re-search-backward "^__" (- (point) 10240) t)) + (forward-line 1) + (unless (re-search-forward "^__" (+ (point) 10240) t) + (goto-char (point-max))) + (forward-line 0) + (while (memq (char-before) '(?\ ?\n ?\t)) + (delete-char -1)) + (insert "\n__\n\n- \n") + (backward-char)) + +(define-derived-mode gaphrase-mode fundamental-mode "gaphrase" + (setq font-lock-defaults '(gaphrase-font-lock-keywords)) + (setq font-lock-extend-region-functions (list #'gaphrase-font-lock-extend-region)) + (define-key (current-local-map) [C-return] 'gaphrase-insert-template)) + +(provide 'gaphrase) + +;;; gaphrase.el ends here