contrib/gadict.el
changeset 1241 57382ad6d332
parent 1239 eebaa469434b
equal deleted inserted replaced
1240:39c9cb59cfea 1241:57382ad6d332
   252 (defvar-local gadict-tr nil
   252 (defvar-local gadict-tr nil
   253   "Translation markers as string separated by comma. Define own
   253   "Translation markers as string separated by comma. Define own
   254   values in .dir-local.el or as -*- gadict-tr: \"...\" -*- file prelude")
   254   values in .dir-local.el or as -*- gadict-tr: \"...\" -*- file prelude")
   255 (put 'gadict-tr 'safe-local-variable 'string-or-null-p)
   255 (put 'gadict-tr 'safe-local-variable 'string-or-null-p)
   256 
   256 
   257 (defun gadict-insert-article (headword)
   257 (defun gadict-insert-article (headword pos)
   258   "Insert new article after the current place.
   258   "Insert new article after the current place.
   259 
   259 
   260 If `gadict-espeak-enabled' is `t' pronunciation will be filled
   260 If `gadict-espeak-enabled' is `t' pronunciation will be filled
   261 with espeak `gadict-espeak-default-voice'."
   261 with espeak `gadict-espeak-default-voice'."
   262   (interactive)
       
   263   (if (re-search-forward "^__" nil t)
   262   (if (re-search-forward "^__" nil t)
   264       (beginning-of-line)
   263       (beginning-of-line)
   265     (goto-char (point-max)))
   264     (goto-char (point-max)))
   266   (while (eq (char-before) ?\n)
   265   (while (eq (char-before) ?\n)
   267     (delete-char -1))
   266     (delete-char -1))
   268   (insert-char ?\n)
   267   (insert-char ?\n)
   269   (insert-char ?_ 2)
   268   (insert-char ?_ 2)
   270   (insert-char ?\n 3)
   269   (insert-char ?\n 3)
   271   (when (stringp gadict-tr)
   270   (forward-char -1)
   272     (mapc (lambda (tr)
       
   273             (insert-char ?\n)
       
   274             (insert tr)
       
   275             (insert ": "))
       
   276           (split-string gadict-tr ","))
       
   277     (insert-char ?\n)
       
   278     (backward-char)
       
   279     (re-search-backward "^$"))
       
   280   (backward-char)
       
   281   (insert headword)
   271   (insert headword)
   282   (insert "\n  [")
   272   (insert "\n  [")
   283   (if gadict-espeak-enabled
   273   (when gadict-espeak-enabled
   284       (progn
   274     (insert (gadict-espeak-ipa headword gadict-espeak-default-voice)))
   285         (insert (gadict-espeak-ipa headword gadict-espeak-default-voice))
   275   (insert "]")
   286         (insert "]")
   276   (insert-char ?\n 2)
   287         (when gadict-tr
   277   (insert pos)
   288           (search-forward ": ")))
   278   (gadict--insert-tr))
   289     (insert "]")
       
   290     (backward-char)))
       
   291 
   279 
   292 (defun gadict-search-floor (headword)
   280 (defun gadict-search-floor (headword)
   293   "Move to HEADWORD definition or place on posiiton for new corresponding
   281   "Move to HEADWORD definition or place on posiiton for new corresponding
   294 definition. Check for headwords ordering during search.
   282 definition. Check for headwords ordering during search.
   295 
   283 
   327 definition. Check for headwords ordering during search."
   315 definition. Check for headwords ordering during search."
   328   (interactive (list (read-string "Headword: ")))
   316   (interactive (list (read-string "Headword: ")))
   329   (gadict-search-floor headword)
   317   (gadict-search-floor headword)
   330   (recenter))
   318   (recenter))
   331 
   319 
   332 (defun gadict-insert-article-in-order (headword)
   320 (defun gadict-insert-article-in-order ()
   333   "Insert new article template with respect of headword order."
   321   "Insert new article template with respect of headword order."
   334   (interactive (list (read-string "Headword: ")))
   322   (interactive)
   335   (unless (gadict-search-floor headword)
   323   (let (headword pos)
   336     (gadict-insert-article headword))
   324     (setq headword (read-string "Headword: "))
   337   (activate-input-method nil)
   325     (unless (gadict-search-floor headword)
   338   (recenter))
   326       (setq pos (completing-read "POS: " gadict--pos nil t))
       
   327       (gadict-insert-article headword pos)
       
   328       (recenter))))
   339 
   329 
   340 (defun gadict--find-headword-end ()
   330 (defun gadict--find-headword-end ()
   341   (save-excursion
   331   (save-excursion
   342     (end-of-line)
   332     (end-of-line)
   343     (re-search-backward "^__$")
   333     (re-search-backward "^__$")
   352   (let ( (headword-end (gadict--find-headword-end)) )
   342   (let ( (headword-end (gadict--find-headword-end)) )
   353     (if (< (point) headword-end)
   343     (if (< (point) headword-end)
   354         (goto-char headword-end)
   344         (goto-char headword-end)
   355       (re-search-forward "^\\(?:\\|__\\)$")
   345       (re-search-forward "^\\(?:\\|__\\)$")
   356       (when (eq (char-before) ?_)
   346       (when (eq (char-before) ?_)
   357         (beginning-of-line)
   347         (beginning-of-line)))
   358         ;; (newline)
   348     (insert-char ?\n 2)
   359         ;; (backward-char)
   349     (forward-char -1)
   360         ))
       
   361     (insert-char ?\n)
       
   362     (insert pos)
   350     (insert pos)
   363     (insert-char ?\n)
   351     (gadict--insert-tr) ))
       
   352 
       
   353 (defun gadict--insert-tr ()
       
   354   "Insert `gadict-tr' as multiline template on next line. Place point on the end of the first new line."
       
   355   (when (stringp gadict-tr)
   364     (save-excursion
   356     (save-excursion
       
   357       (end-of-line)
   365       (mapc (lambda (lang)
   358       (mapc (lambda (lang)
       
   359               (insert-char ?\n)
   366               (insert lang)
   360               (insert lang)
   367               (insert ": \n"))
   361               (insert ": "))
   368             (split-string gadict-tr ",")))
   362             (split-string gadict-tr ",")))
   369     (end-of-line) ))
   363     (end-of-line 2)))
   370 
   364 
   371 (defun gadict-nearest-headword ()
   365 (defun gadict-nearest-headword ()
   372   "Return nearest headword looking upward."
   366   "Return nearest headword looking upward."
   373   (save-excursion
   367   (save-excursion
   374     (let ( (orig (point)) limit )
   368     (let ( (orig (point)) limit )