contrib/gadict.el
changeset 398 54ef571cf72a
parent 395 32309860fe92
child 420 bc0245c315a1
--- a/contrib/gadict.el	Sun Mar 27 22:35:41 2016 +0300
+++ b/contrib/gadict.el	Sun Mar 27 22:36:19 2016 +0300
@@ -113,8 +113,8 @@
   values in .dir-local.el or as -*- gadict-tr: \"...\" -*- file prelude")
 (put 'gadict-tr 'safe-local-variable 'string-or-null-p)
 
-(defun gadict-new-entry ()
-  "Insert new article template."
+(defun gadict-insert-template (&optional headword)
+  "Insert new article template after the current place."
   (interactive)
   (if (re-search-forward "^__" nil t)
       (beginning-of-line)
@@ -132,11 +132,52 @@
           (split-string gadict-tr ","))
     (insert-char ?\n)
     (re-search-backward "^$"))
-  (backward-char))
+  (backward-char)
+  (when headword (insert headword)))
+
+(defun gadict-search-floor (headword)
+  "Move to HEADWORD definition or place before definition should
+be placed. Check for headwords ordering during search.
+
+Return `t' if definition found, `nil' if no such headword."
+  (interactive (list (read-string "Headword: ")))
+  (let ( prev curr )
+    (catch 'exit
+      (goto-char (point-min))
+      (unless (re-search-forward "^__$" nil t)
+        (throw 'exit nil))
+      (forward-line 2)
+      (setq prev (buffer-substring-no-properties (point) (line-end-position)))
+      (when (string= headword prev)
+        (throw 'exit t))
+      (when (string< headword prev)
+        (throw 'exit nil))
+      (while t
+        (unless (re-search-forward "^__$" nil t)
+          (throw 'exit nil))
+        (forward-line 2)
+        (setq curr (buffer-substring-no-properties (point) (line-end-position)))
+        (unless (string< prev curr)
+          (error (format "%s < %s" curr prev)))
+        (when (string= headword curr)
+          (throw 'exit t))
+        (when (string< headword curr)
+          (forward-line -2)
+          (re-search-backward "^__$")
+          (forward-line 2)
+          (throw 'exit nil))
+        (setq prev curr)) )))
+
+(defun gadict-insert-template-in-order (headword)
+  "Insert new article template with respect of headword order."
+  (interactive (list (read-string "Headword: ")))
+  (unless (gadict-search-floor headword)
+    (gadict-insert-template headword)))
 
 (defun gadict-setup-keymap ()
   "Setup gadict keymap."
-  (define-key (current-local-map) [C-return] 'gadict-new-entry))
+  (define-key (current-local-map) [S-return] 'gadict-search-floor)
+  (define-key (current-local-map) [C-return] 'gadict-insert-template-in-order))
 
 ;;;###autoload
 (define-derived-mode gadict-mode fundamental-mode "gadict"