Updated Emacs gaphrase to support new format with unique ids.
authorOleksandr Gavenko <gavenkoa@gmail.com>
Sat, 20 Apr 2019 17:41:33 +0300
changeset 1145 79b55cca9f44
parent 1144 b121f6a0217e
child 1146 50007cd95972
Updated Emacs gaphrase to support new format with unique ids. Id is necessary to keep progress in Anki on deck subsequent import.
contrib/gaphrase.el
--- a/contrib/gaphrase.el	Sat Apr 20 16:54:18 2019 +0300
+++ b/contrib/gaphrase.el	Sat Apr 20 17:41:33 2019 +0300
@@ -23,51 +23,44 @@
 ;;; Code:
 
 (defvar gaphrase-font-lock-keywords
-  '(("^\\(__\\)\n\n" (1 font-lock-type-face))
-    ("^\\(__\n?[^\n]+\\)" (1 highlight))
+  '(("^# [1-9][0-9]*" . font-lock-type-face)
+    ("^## [1-9][0-9]*" . font-lock-warning-face)
     ("^- " . font-lock-keyword-face)))
 
-(defvar font-lock-beg)
-(defvar font-lock-end)
-(defun gaphrase-font-lock-extend-region ()
-  ;; (gaphrase-font-lock-extend-region--debug)
+(defun gaphrase-next-num ()
   (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))))
+    (goto-char (point-min))
+    (let (beg end num)
+      (catch 'return
+        (when (looking-at "## \\([1-9][0-9]*\\)")
+          (setq beg (match-beginning 1)
+                end (match-end 1))
+          (setq num (string-to-int (buffer-substring beg end)))
+          (delete-region beg end)
+          (goto-char beg)
+          (setq num (1+ num))
+          (insert (int-to-string num))
+          (throw 'return num))
+        (insert "## 1\n")
+        1))))
 
 (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)
+  (when (looking-at "# ")
+    (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")
+  (insert "\n# ")
+  (insert (int-to-string (gaphrase-next-num)))
+  (insert "\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)