contrib/gadialog.el
changeset 1191 a3a7c8342b1c
parent 1168 24da81e9a023
child 1217 451296ba92c9
equal deleted inserted replaced
1190:f92cd37ad600 1191:a3a7c8342b1c
       
     1 ;;; gadialog.el --- major mode for editing gadialog 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.17
       
    11 ;; Keywords: dict, dictionary
       
    12 
       
    13 ;;; Commentary:
       
    14 ;;
       
    15 ;; Mode can be installed by:
       
    16 ;;
       
    17 ;;   (autoload 'gadialog-mode "gadialog")
       
    18 ;;
       
    19 ;; File association can be registered by:
       
    20 ;;
       
    21 ;;   (add-to-list 'auto-mode-alist (cons "\\.gadialog$" 'gadialog-mode))
       
    22 
       
    23 ;;; Code:
       
    24 
       
    25 (defvar gadialog-font-lock-keywords
       
    26   '(("^# [1-9][0-9]*" . font-lock-type-face)
       
    27     ("^## [1-9][0-9]*" . font-lock-warning-face)
       
    28     ("^- " . font-lock-keyword-face)))
       
    29 
       
    30 (defvar gadialog-syntax-table
       
    31   (let ((table (make-syntax-table text-mode-syntax-table)))
       
    32     (modify-syntax-entry ?' "w" table)
       
    33     table))
       
    34 
       
    35 (defun gadialog-next-num ()
       
    36   (save-excursion
       
    37     (goto-char (point-min))
       
    38     (let (beg end num)
       
    39       (catch 'return
       
    40         (when (looking-at "## \\([1-9][0-9]*\\)")
       
    41           (setq beg (match-beginning 1)
       
    42                 end (match-end 1))
       
    43           (setq num (string-to-number (buffer-substring beg end)))
       
    44           (delete-region beg end)
       
    45           (goto-char beg)
       
    46           (setq num (1+ num))
       
    47           (insert (int-to-string num))
       
    48           (throw 'return num))
       
    49         (insert "## 1\n")
       
    50         1))))
       
    51 
       
    52 (defun gadialog-insert-template ()
       
    53   (interactive)
       
    54   (forward-line 0)
       
    55   (when (looking-at "# ")
       
    56     (forward-line 1))
       
    57   (unless (re-search-forward "^# " (+ (point) 10240) t)
       
    58     (goto-char (point-max)))
       
    59   (forward-line 0)
       
    60   (while (memq (char-before) '(?\  ?\n ?\t))
       
    61     (delete-char -1))
       
    62   (insert "\n# ")
       
    63   (insert (int-to-string (gadialog-next-num)))
       
    64   (insert "\n- \n")
       
    65   (backward-char))
       
    66 
       
    67 (defun gadialog-insert-sentence ()
       
    68   (interactive)
       
    69   (forward-line 1)
       
    70   (while (not (or (eobp)
       
    71                   (memq (char-after) (list ?- ?#))))
       
    72     (forward-line 1))
       
    73   (insert ?\n)
       
    74   (backward-char)
       
    75   (insert "- "))
       
    76 
       
    77 
       
    78 (define-derived-mode gadialog-mode fundamental-mode "gadialog"
       
    79   (setq font-lock-defaults '(gadialog-font-lock-keywords))
       
    80   (define-key (current-local-map) [C-return] 'gadialog-insert-template)
       
    81   (define-key (current-local-map) [S-return] 'gadialog-insert-sentence)
       
    82   (set-syntax-table gadialog-syntax-table))
       
    83 
       
    84 (provide 'gadialog)
       
    85 
       
    86 ;;; gadialog.el ends here