contrib/gaphrase.el
changeset 1141 879c410290ea
child 1145 79b55cca9f44
equal deleted inserted replaced
1140:4bd7c6066b4f 1141:879c410290ea
       
     1 ;;; gaphrase.el --- major mode for editing gaphrase dialog files -*- lexical-binding: t -*-
       
     2 
       
     3 ;; Copyright (C) 2019 by Oleksandr Gavenko <gavenkoa@gmail.com>
       
     4 
       
     5 ;; You can do anything with this file without any warranty.
       
     6 
       
     7 ;; Author: Oleksandr Gavenko <gavenkoa@gmail.com>
       
     8 ;; Maintainer: Oleksandr Gavenko <gavenkoa@gmail.com>
       
     9 ;; Created: 2019
       
    10 ;; Version: 0.1
       
    11 ;; Keywords: dict, dictionary
       
    12 
       
    13 ;;; Commentary:
       
    14 ;;
       
    15 ;; Mode can be installed by:
       
    16 ;;
       
    17 ;;   (autoload 'gaphrase-mode "gaphrase")
       
    18 ;;
       
    19 ;; File association can be registered by:
       
    20 ;;
       
    21 ;;   (add-to-list 'auto-mode-alist (cons "\\.gaphrase$" 'gaphrase-mode))
       
    22 
       
    23 ;;; Code:
       
    24 
       
    25 (defvar gaphrase-font-lock-keywords
       
    26   '(("^\\(__\\)\n\n" (1 font-lock-type-face))
       
    27     ("^\\(__\n?[^\n]+\\)" (1 highlight))
       
    28     ("^- " . font-lock-keyword-face)))
       
    29 
       
    30 (defvar font-lock-beg)
       
    31 (defvar font-lock-end)
       
    32 (defun gaphrase-font-lock-extend-region ()
       
    33   ;; (gaphrase-font-lock-extend-region--debug)
       
    34   (save-excursion
       
    35     (goto-char font-lock-beg)
       
    36     (forward-line 0)
       
    37     (unless (looking-at "__")
       
    38       (re-search-backward "^__" (- (point) 10240) t))
       
    39     (if (= (point) font-lock-beg)
       
    40         nil
       
    41       (setq font-lock-beg (point)))))
       
    42 
       
    43 (defun gaphrase-font-lock-extend-region--debug ()
       
    44   (save-excursion
       
    45     (let ( beg end )
       
    46       (goto-char font-lock-beg)
       
    47       (beginning-of-line)
       
    48       (setq beg (buffer-substring-no-properties (point) font-lock-beg))
       
    49       (goto-char font-lock-end)
       
    50       (end-of-line)
       
    51       (setq end (buffer-substring-no-properties font-lock-end (point)))
       
    52       (message "lines: %d, %s ## %s" (count-lines font-lock-beg font-lock-end) beg end))))
       
    53 
       
    54 (defun gaphrase-insert-template ()
       
    55   (interactive)
       
    56   (forward-line 0)
       
    57   (unless (looking-at "__")
       
    58     (re-search-backward "^__" (- (point) 10240) t))
       
    59   (forward-line 1)
       
    60   (unless (re-search-forward "^__" (+ (point) 10240) t)
       
    61     (goto-char (point-max)))
       
    62   (forward-line 0)
       
    63   (while (memq (char-before) '(?\  ?\n ?\t))
       
    64     (delete-char -1))
       
    65   (insert "\n__\n\n- \n")
       
    66   (backward-char))
       
    67 
       
    68 (define-derived-mode gaphrase-mode fundamental-mode "gaphrase"
       
    69   (setq font-lock-defaults '(gaphrase-font-lock-keywords))
       
    70   (setq font-lock-extend-region-functions (list #'gaphrase-font-lock-extend-region))
       
    71   (define-key (current-local-map) [C-return] 'gaphrase-insert-template))
       
    72 
       
    73 (provide 'gaphrase)
       
    74 
       
    75 ;;; gaphrase.el ends here