Skip to content

Instantly share code, notes, and snippets.

@daimatz
Created September 20, 2011 01:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save daimatz/1228110 to your computer and use it in GitHub Desktop.
Save daimatz/1228110 to your computer and use it in GitHub Desktop.
Emacs Lisp Tips
;; Mac の Dictionary.app を、 Emacs の popwin.el から使う
;; dict.py is from http://sakito.jp/mac/dictionary.html
(defun dictionary ()
"dictionary.app"
(interactive)
(let ((word (if (and transient-mark-mode mark-active)
(buffer-substring-no-properties (region-beginning) (region-end))
(read-string "Dictionary: ")))
(cur-buffer (current-buffer))
(tmpbuf " * dict-process *"))
(set-buffer (get-buffer-create tmpbuf))
(erase-buffer)
(insert word "\n")
(let ((coding-system-for-read 'utf-8-mac)
(coding-system-for-write 'utf-8-mac))
(call-process "~/scripts/dict.py" nil tmpbuf nil word) ;; specify full pass of dict.py
(let ((str (buffer-substring (point-min) (- (point-max) 2))))
(set-buffer cur-buffer)
(popup-tip str :scroll-bar t))
)))
(global-set-key (kbd "C-M-d") 'dictionary)
(add-hook 'server-visit-hook
(lambda ()
(let ((file (buffer-file-name)))
(elscreen-create)
(find-file file))))
;; M-: で評価した結果を kill-ring に追加
(defun eval-kill ()
(interactive)
(let* ((val (call-interactively 'eval-expression))
(str (format "%s" val)))
(setq kill-ring (cons str kill-ring))
(setq kill-ring-yank-pointer (cons str kill-ring-yank-pointer))
val))
;; find-file したディレクトリが存在しなければ作成する
(defadvice find-file (before find-file-mkdir (file &optional WILDCARDS) activate)
(let ((dirname (file-name-directory file)))
(if (and dirname (not (file-directory-p dirname)) (not (string-match "/ssh:" dirname)))
(if (not (= 0 (call-process "mkdir" nil nil nil "-p" (expand-file-name dirname))))
(error "mkdir %s failed." dirname)))))
;; C-w で backward-kill-word もできるようにする
(defun kill-region-or-backward-kill-word ()
(interactive)
(call-interactively
(if (and transient-mark-mode mark-active)
'kill-region
'backward-kill-word)))
(global-set-key (kbd "C-w") 'kill-region-or-backward-kill-word)
;; rsync after-save
(defun after-save-rsync-callback (process event)
(princ (format "%s: `%s'" process (replace-regexp-in-string "\n" "" event))))
(defun rsync-setup (local-dir remote-dir)
"setup rsync"
(interactive "DLocal directory: \nsRemote directory (may includes `ssh://`): ")
(setq after-save-rsync-local-dir (expand-file-name local-dir))
(setq after-save-rsync-remote-dir remote-dir)
(setq after-save-rsync-process-name "rsync-process")
(defun after-save-rsync ()
(when (string-match (concat "^" after-save-rsync-local-dir) (buffer-file-name))
(if (member (process-status after-save-rsync-process-name) '(run stop open closed))
(kill-process (get-process after-save-rsync-process-name)))
(start-process-shell-command after-save-rsync-process-name
"*rsync-process*"
"rsync"
"-acvz"
; "--delete"
; "--exclude=.git/"
; (concat "--exclude-from=" (expand-file-name "~/.gitignore"))
after-save-rsync-local-dir
after-save-rsync-remote-dir)
(set-process-sentinel (get-process after-save-rsync-process-name) 'after-save-rsync-callback)
))
(if (y-or-n-p "rsync command will be executed every after you save file. Is that OK? ")
(progn
(add-hook 'after-save-hook 'after-save-rsync)
(message "rsync is setuped. when you quit rsync, execute 'M-x rsync-quit'."))
(message "rsync is not setuped")))
(defun rsync-quit ()
(interactive)
(if (y-or-n-p "quit rsync? ")
(progn
(remove-hook 'after-save-hook 'after-save-rsync)
(message "quited rsync."))
(message "rsync is not quited.")))
;; 現在のウィンドウと次のウィンドウの位置を入れ替える
(defun swap-window (n)
(let* ((cpt (point))
(cwin (selected-window))
(cbuf (current-buffer))
(npt (progn (other-window n) (point)))
(nwin (selected-window))
(nbuf (current-buffer)))
(switch-to-buffer cbuf)
(goto-char cpt)
(other-window (- n))
(switch-to-buffer nbuf)
(goto-char npt)
(other-window n)
))
(defun swap-next-window ()
(interactive) (swap-window 1))
(defun swap-previous-window ()
(interactive) (swap-window -1))
(global-set-key (kbd "C-x n") 'swap-next-window) ;; C-x n is narrow-to-region. Who use this?
(global-set-key (kbd "C-x p") 'swap-previous-window)
@ngn999
Copy link

ngn999 commented Aug 11, 2013

where is ~/scripts/dict.py?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment