Skip to content

Instantly share code, notes, and snippets.

@takaxp takaxp/dict-app.el
Last active Aug 29, 2015

Embed
What would you like to do?
Displaying content through Dictionary.app
;;; dict-app.el --- Displaying content through Dictionary.app
;;
;; Copyright (C) 2014 Takaaki ISHIKAWA
;;
;; Author: Takaaki ISHIKAWA <takaxp at ieee dot org>
;; Maintainer: Takaaki ISHIKAWA <takaxp at ieee dot org>
;; Twitter: @takaxp
;; Repository:
;; Keywords: dictionary, dictionary.app
;;
;; Committers:
;;
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3 of the License, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;
;;; Requirement:
;;
;;; Usage:
;; 1. Put this elisp into your load-path
;; 2. Add (require 'dict-app) in your .emacs
;; 3. Call dict-app-search on a query word
;;
;;; Recommended minimum settings:
;;
;;; Note:
;; - this elisp is based on a lisp shared in http://sakito.jp/mac/dictionary.html
;; -
(defconst dict-app "1.0.1"
"The version number of the dict-app.el")
(defgroup dict-app nil
"User variables for dict-app."
:group 'dict-app)
(defvar dict-app-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "q") 'dict-app-quit)
map)
"The default key bindings for dict-app.")
(defvar dict-app-mode nil)
(defvar display-dict-app-string nil)
(defvar dict-app-buffer-name "dict-app-result")
(defvar dict-bin-name "dict.py"
"dict.py is CLI for connecting with Dictionary.app.
This command should be placed in exec-path.
see also http://sakito.jp/mac/dictionary.html.")
;;;###autoload
(define-minor-mode dict-app-mode
"Displaying content through Dictionary.app.
Usage:
"
:lighter " D"
:keymap dict-app-mode-map
:group 'dict-app
)
;;;###autoload
(defun dict-app-search ()
"Lookup a word through Dictionary.app."
(interactive)
(cond
(;; on the dict-app-result buffer
(equal (buffer-name) dict-app-buffer-name)
(setq buffer-read-only nil)
(set-buffer-modified-p t))
;; not on the dict-app-buffer-name, but dict-app-buffer-name exists
((get-buffer dict-app-buffer-name)
(kill-buffer dict-app-buffer-name))
(t nil))
(let ((editable (not buffer-read-only))
(pt (save-excursion (mouse-set-point last-nonmenu-event)))
beg end)
(if (and mark-active
(<= (region-beginning) pt) (<= pt (region-end)) )
(setq beg (region-beginning)
end (region-end))
(save-excursion
(goto-char pt)
(setq end (progn (forward-word) (point)))
(setq beg (progn (backward-word) (point)))
))
(let ((word (buffer-substring-no-properties beg end))
(tmpbuf dict-app-buffer-name))
(pop-to-buffer tmpbuf)
(erase-buffer)
(insert "Query: " word "\n\n")
(start-process "dict-process" tmpbuf dict-bin-name word)
(goto-char 0)
(dict-app-mode t)
(setq buffer-read-only t)
(set-buffer-modified-p nil))))
;;;###autoload
(defun dict-app-quit ()
"Delete the window and quit dict-app."
(interactive)
(when (equal (buffer-name) dict-app-buffer-name)
(delete-window)
(kill-buffer dict-app-buffer-name))
(da-abort)
(message "Quit"))
;;; Internal functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun da-setup ()
;; do nothing
)
(defun da-abort ()
;; do nothing
)
(provide 'dict-app)
;;; dict-app.el ends here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.