contrib/gadict.el
changeset 398 54ef571cf72a
parent 395 32309860fe92
child 420 bc0245c315a1
equal deleted inserted replaced
397:efe1c2553415 398:54ef571cf72a
   111 (defvar-local gadict-tr nil
   111 (defvar-local gadict-tr nil
   112   "Translation markers as string separated by comma. Define own
   112   "Translation markers as string separated by comma. Define own
   113   values in .dir-local.el or as -*- gadict-tr: \"...\" -*- file prelude")
   113   values in .dir-local.el or as -*- gadict-tr: \"...\" -*- file prelude")
   114 (put 'gadict-tr 'safe-local-variable 'string-or-null-p)
   114 (put 'gadict-tr 'safe-local-variable 'string-or-null-p)
   115 
   115 
   116 (defun gadict-new-entry ()
   116 (defun gadict-insert-template (&optional headword)
   117   "Insert new article template."
   117   "Insert new article template after the current place."
   118   (interactive)
   118   (interactive)
   119   (if (re-search-forward "^__" nil t)
   119   (if (re-search-forward "^__" nil t)
   120       (beginning-of-line)
   120       (beginning-of-line)
   121     (goto-char (point-max)))
   121     (goto-char (point-max)))
   122   (while (eq (char-before) ?\n)
   122   (while (eq (char-before) ?\n)
   130             (insert tr)
   130             (insert tr)
   131             (insert ": "))
   131             (insert ": "))
   132           (split-string gadict-tr ","))
   132           (split-string gadict-tr ","))
   133     (insert-char ?\n)
   133     (insert-char ?\n)
   134     (re-search-backward "^$"))
   134     (re-search-backward "^$"))
   135   (backward-char))
   135   (backward-char)
       
   136   (when headword (insert headword)))
       
   137 
       
   138 (defun gadict-search-floor (headword)
       
   139   "Move to HEADWORD definition or place before definition should
       
   140 be placed. Check for headwords ordering during search.
       
   141 
       
   142 Return `t' if definition found, `nil' if no such headword."
       
   143   (interactive (list (read-string "Headword: ")))
       
   144   (let ( prev curr )
       
   145     (catch 'exit
       
   146       (goto-char (point-min))
       
   147       (unless (re-search-forward "^__$" nil t)
       
   148         (throw 'exit nil))
       
   149       (forward-line 2)
       
   150       (setq prev (buffer-substring-no-properties (point) (line-end-position)))
       
   151       (when (string= headword prev)
       
   152         (throw 'exit t))
       
   153       (when (string< headword prev)
       
   154         (throw 'exit nil))
       
   155       (while t
       
   156         (unless (re-search-forward "^__$" nil t)
       
   157           (throw 'exit nil))
       
   158         (forward-line 2)
       
   159         (setq curr (buffer-substring-no-properties (point) (line-end-position)))
       
   160         (unless (string< prev curr)
       
   161           (error (format "%s < %s" curr prev)))
       
   162         (when (string= headword curr)
       
   163           (throw 'exit t))
       
   164         (when (string< headword curr)
       
   165           (forward-line -2)
       
   166           (re-search-backward "^__$")
       
   167           (forward-line 2)
       
   168           (throw 'exit nil))
       
   169         (setq prev curr)) )))
       
   170 
       
   171 (defun gadict-insert-template-in-order (headword)
       
   172   "Insert new article template with respect of headword order."
       
   173   (interactive (list (read-string "Headword: ")))
       
   174   (unless (gadict-search-floor headword)
       
   175     (gadict-insert-template headword)))
   136 
   176 
   137 (defun gadict-setup-keymap ()
   177 (defun gadict-setup-keymap ()
   138   "Setup gadict keymap."
   178   "Setup gadict keymap."
   139   (define-key (current-local-map) [C-return] 'gadict-new-entry))
   179   (define-key (current-local-map) [S-return] 'gadict-search-floor)
       
   180   (define-key (current-local-map) [C-return] 'gadict-insert-template-in-order))
   140 
   181 
   141 ;;;###autoload
   182 ;;;###autoload
   142 (define-derived-mode gadict-mode fundamental-mode "gadict"
   183 (define-derived-mode gadict-mode fundamental-mode "gadict"
   143   "Derived mode for editing gadict dictionary source files."
   184   "Derived mode for editing gadict dictionary source files."
   144   (gadict-setup-fontlock)
   185   (gadict-setup-fontlock)