You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

2568 lines
87 KiB

  1. ;;; emacs.el --- 10sr emacs initialization
  2. ;;; Commentary:
  3. ;;; Code:
  4. ;; (and (file-readable-p "~/.dotfiles/emacs.el")
  5. ;; (load-file "~/.dotfiles/emacs.el"))
  6. ;; make directories
  7. (unless (file-directory-p (expand-file-name user-emacs-directory))
  8. (make-directory (expand-file-name user-emacs-directory)))
  9. (let ((d (expand-file-name (concat user-emacs-directory
  10. "lisp"))))
  11. (unless (file-directory-p d)
  12. (make-directory d))
  13. (add-to-list 'load-path d))
  14. (eval-when-compile
  15. (require 'cl nil t))
  16. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  17. ;; download library from web
  18. (defun fetch-library (url &optional byte-compile-p force-download-p)
  19. "Download a library from URL and locate it in \"~/emacs.d/lisp/\".
  20. Return nil if library unfound and failed to download,
  21. otherwise the path where the library installed.
  22. If BYTE-COMPILE-P is t byte compile the file after downloading.
  23. If FORCE-DOWNLOAD-P it t ignore exisiting library and always download."
  24. (let* ((dir (expand-file-name (concat user-emacs-directory "lisp/")))
  25. (lib (file-name-sans-extension (file-name-nondirectory url)))
  26. (lpath (concat dir lib ".el"))
  27. (locate-p (locate-library lib)))
  28. (if (or force-download-p (not locate-p))
  29. (if (progn (message "Downloading %s..."
  30. url)
  31. (download-file url
  32. lpath
  33. t))
  34. (progn (message "Downloading %s...done"
  35. url)
  36. (when (and byte-compile-p
  37. (require 'bytecomp nil t))
  38. (and (file-exists-p (byte-compile-dest-file lpath))
  39. (delete-file (byte-compile-dest-file lpath)))
  40. (byte-compile-file lpath)))
  41. (progn (and (file-writable-p lpath)
  42. (delete-file lpath))
  43. (message "Downloading %s...failed"
  44. url))))
  45. (locate-library lib)))
  46. (defun download-file (url path &optional ok-if-already-exists)
  47. "Download file from URL and output to PATH."
  48. (or
  49. (let ((curl (executable-find "curl")))
  50. (when curl
  51. (if (and (not ok-if-already-exists)
  52. (file-exists-p path))
  53. nil
  54. (and (eq 0
  55. (call-process curl
  56. nil
  57. nil
  58. nil
  59. "--output"
  60. path
  61. url
  62. ))
  63. path))))
  64. (ignore-errors
  65. (require 'url)
  66. (url-copy-file url
  67. path
  68. ok-if-already-exists)
  69. path)))
  70. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  71. ;; package
  72. (when (require 'package nil t)
  73. (add-to-list 'package-archives
  74. '("ELPA" . "http://tromey.com/elpa/"))
  75. (add-to-list 'package-archives
  76. '("melpa" . "http://melpa.milkbox.net/packages/")
  77. t)
  78. (add-to-list 'package-archives
  79. '("marmalade" . "http://marmalade-repo.org/packages/"))
  80. (package-initialize)
  81. ;; (package-refresh-contents)
  82. )
  83. (defun my-auto-install-package ()
  84. "Install packages semi-automatically."
  85. (interactive)
  86. (mapc (lambda (pkg)
  87. (or (package-installed-p pkg)
  88. (locate-library (symbol-name pkg))
  89. (package-install pkg)))
  90. '(
  91. markdown-mode
  92. yaml-mode
  93. ;; ack
  94. color-moccur
  95. )))
  96. ;; (lazy-load-eval 'sudoku)
  97. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  98. ;; autoload
  99. (defmacro lazy-load-eval (feature &optional functions &rest body)
  100. "Define autoloading FEATURE that defines FUNCTIONS.
  101. FEATURE is a symbol. FUNCTIONS is a list of symbols. If FUNCTIONS is nil,
  102. the function same as FEATURE is defined as autoloaded function. BODY is passed
  103. to `eval-after-load'.
  104. When this macro is evaluated, this returns the path to library if FEATURE
  105. found, otherwise returns nil."
  106. (let* ((libname (symbol-name (eval feature)))
  107. (libpath (locate-library libname)))
  108. (and libpath
  109. `(progn
  110. ,@(mapcar (lambda (f)
  111. (unless (fboundp f)
  112. `(progn
  113. (message "Autoloaded function `%S' defined (%s)"
  114. (quote ,f)
  115. ,libpath)
  116. (autoload (quote ,f)
  117. ,libname
  118. ,(concat "Autoloaded function defined in \""
  119. libpath
  120. "\".")
  121. t))))
  122. (or (eval functions)
  123. `(,(eval feature))))
  124. (eval-after-load ,feature
  125. (quote (progn
  126. ,@body)))
  127. ,libpath))))
  128. (put 'lazy-load-eval 'lisp-indent-function 2)
  129. (when (lazy-load-eval 'tetris nil
  130. (message "Tetris loaded!"))
  131. (message "Tetris found!"))
  132. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  133. ;; my-idle-hook
  134. (defvar my-idle-hook nil
  135. "Hook run when idle for several secs.")
  136. (defvar my-idle-hook-sec 5
  137. "Second to run `my-idle-hook'.")
  138. (run-with-idle-timer my-idle-hook-sec
  139. t
  140. (lambda ()
  141. (run-hooks 'my-idle-hook)))
  142. ;; (add-hook 'my-idle-hook
  143. ;; (lambda ()
  144. ;; (message "idle hook message")))
  145. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  146. ;; start and quit
  147. (setq inhibit-startup-message t)
  148. (setq confirm-kill-emacs 'y-or-n-p)
  149. (setq gc-cons-threshold (* 1024 1024 4))
  150. (when window-system
  151. (add-to-list 'default-frame-alist '(cursor-type . box))
  152. (add-to-list 'default-frame-alist '(background-color . "white"))
  153. (add-to-list 'default-frame-alist '(foreground-color . "gray10"))
  154. ;; (add-to-list 'default-frame-alist '(alpha . (80 100 100 100)))
  155. ;; does not work?
  156. )
  157. ;; (add-to-list 'default-frame-alist '(cursor-type . box))
  158. (if window-system (menu-bar-mode 1) (menu-bar-mode 0))
  159. (and (fboundp 'tool-bar-mode)
  160. (tool-bar-mode 0))
  161. (and (fboundp 'set-scroll-bar-mode)
  162. (set-scroll-bar-mode nil))
  163. (add-hook 'kill-emacs-hook
  164. ;; load init file when terminating emacs to ensure file is not broken
  165. 'reload-init-file)
  166. (add-hook 'after-init-hook
  167. (lambda ()
  168. (message "%s %s" invocation-name emacs-version)
  169. (message "%s was taken to initialize emacs." (emacs-init-time))
  170. (switch-to-buffer "*Messages*")
  171. ))
  172. (cd ".") ; when using windows use / instead of \ in `default-directory'
  173. ;; locale
  174. (set-language-environment "Japanese")
  175. (set-default-coding-systems 'utf-8-unix)
  176. (prefer-coding-system 'utf-8-unix)
  177. (setq system-time-locale "C")
  178. ;; my prefix map
  179. (defvar my-prefix-map nil
  180. "My prefix map.")
  181. (define-prefix-command 'my-prefix-map)
  182. (define-key ctl-x-map (kbd "C-x") 'my-prefix-map)
  183. (define-key my-prefix-map (kbd "C-q") 'quoted-insert)
  184. (define-key my-prefix-map (kbd "C-z") 'suspend-frame)
  185. ;; (comint-show-maximum-output)
  186. ;; kill scratch
  187. (add-hook 'after-init-hook
  188. (lambda ()
  189. (kill-buffer "*scratch*")))
  190. ;; modifier keys
  191. ;; (setq mac-option-modifier 'control)
  192. ;; display
  193. (setq redisplay-dont-pause t)
  194. (setq visible-bell t)
  195. (setq ring-bell-function 'ignore)
  196. (mouse-avoidance-mode 'banish)
  197. (and window-system
  198. (fetch-library
  199. "https://raw.github.com/10sr/emacs-lisp/master/save-window-size.el"
  200. t)
  201. (require 'save-window-size nil t))
  202. (defun reload-init-file ()
  203. "Reload Emacs init file."
  204. (interactive)
  205. (when (file-readable-p user-init-file)
  206. (load-file user-init-file)))
  207. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  208. ;; for windows
  209. (defun start-ckw-bash ()
  210. "Start ckw in windows."
  211. (interactive)
  212. (start-process
  213. "ckw_bash"
  214. nil
  215. "C:/Documents and Settings/sr/Application Data/dbx/apps/ckw/ckw.exe"))
  216. ;; command seems to have to be in c drive
  217. (defun my-w32-add-export-path (&rest args)
  218. "Add pathes ARGS for windows."
  219. (mapc (lambda (path)
  220. (add-to-list 'exec-path (expand-file-name path)))
  221. (reverse args))
  222. (setenv "PATH"
  223. (mapconcat 'convert-standard-filename
  224. exec-path
  225. ";")))
  226. (when (eq system-type 'windows-nt)
  227. ;; (setq scheme-program-name "\"c:/Program Files/Gauche/bin/gosh.exe\" -i")
  228. ;; (setq python-python-command "c:/Python26/python.exe")
  229. ;; (define-key my-prefix-map (kbd "C-c") 'start-ckw-bash)
  230. (my-w32-add-export-path "c:/Windows/system"
  231. "c:/Windows/System32"
  232. "c:/Program Files/Git/bin"
  233. "c:/MinGW/bin"
  234. "c:/MinGW/mingw32/bin"
  235. (expand-file-name "~/.local/bin")
  236. (expand-file-name "~/dbx/apps/bin"))
  237. (when window-system
  238. (setq w32-enable-synthesized-fonts t))
  239. (setq w32-apps-modifier 'meta)
  240. (setq file-name-coding-system 'sjis))
  241. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  242. ;; global keys
  243. (global-set-key (kbd "<up>") (lambda() (interactive) (scroll-down 1)))
  244. (global-set-key (kbd "<down>") (lambda() (interactive) (scroll-up 1)))
  245. (global-set-key (kbd "<left>") 'scroll-down)
  246. (global-set-key (kbd "<right>") 'scroll-up)
  247. ;; (define-key my-prefix-map (kbd "C-h") help-map)
  248. (global-set-key (kbd "C-\\") help-map)
  249. (define-key ctl-x-map (kbd "DEL") help-map)
  250. (define-key ctl-x-map (kbd "C-h") help-map)
  251. (define-key help-map "a" 'apropos)
  252. ;; disable annoying keys
  253. (global-set-key [prior] 'ignore)
  254. (global-set-key (kbd "<next>") 'ignore)
  255. (global-set-key [menu] 'ignore)
  256. (global-set-key [down-mouse-1] 'ignore)
  257. (global-set-key [down-mouse-2] 'ignore)
  258. (global-set-key [down-mouse-3] 'ignore)
  259. (global-set-key [mouse-1] 'ignore)
  260. (global-set-key [mouse-2] 'ignore)
  261. (global-set-key [mouse-3] 'ignore)
  262. (global-set-key (kbd "<eisu-toggle>") 'ignore)
  263. (global-set-key (kbd "C-<eisu-toggle>") 'ignore)
  264. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  265. ;; title and mode-line
  266. (when (fetch-library
  267. "https://raw.github.com/10sr/emacs-lisp/master/terminal-title.el"
  268. t)
  269. (require 'terminal-title nil t)
  270. ;; if TERM is not screen use default value
  271. (if (getenv "TMUX")
  272. ;; if use tmux locally just basename of current dir
  273. (setq terminal-title-format
  274. '((file-name-nondirectory (directory-file-name
  275. default-directory))))
  276. (when (equal (car (split-string (frame-parameter nil
  277. 'tty-type)
  278. "-"))
  279. "screen")
  280. (if (getenv "SSH_CONNECTION")
  281. ;; TMUX is not set but TERM is screen:
  282. ;; it seems that TMUX is used by locally and ssh to remote host
  283. (setq terminal-title-format
  284. `("em:"
  285. ,user-login-name
  286. "@"
  287. ,(car (split-string system-name
  288. "\\."))
  289. ":"
  290. (file-name-nondirectory (directory-file-name
  291. default-directory))))
  292. ;; this wont happen? (TMUX is not set, TERM is screen, not ssh-ed)
  293. (setq terminal-title-format
  294. '((file-name-nondirectory (directory-file-name
  295. default-directory)))))))
  296. (terminal-title-mode))
  297. (setq eol-mnemonic-dos "\\r\\n")
  298. (setq eol-mnemonic-mac "\\r")
  299. (setq eol-mnemonic-unix "\\n")
  300. (which-function-mode 0)
  301. (line-number-mode 0)
  302. (column-number-mode 0)
  303. (size-indication-mode 0)
  304. (setq mode-line-position
  305. '(:eval (format "L%%l/%d,C%%c"
  306. (count-lines (point-max)
  307. (point-min)))))
  308. ;; http://www.geocities.jp/simizu_daisuke/bunkei-meadow.html#frame-title
  309. ;; display date
  310. (add-hook 'after-init-hook
  311. (lambda ()
  312. (when display-time-mode
  313. (display-time-update))
  314. ))
  315. (when (require 'time nil t)
  316. (setq display-time-interval 29)
  317. (setq display-time-day-and-date t)
  318. (setq display-time-format "%a, %d %b %Y %T")
  319. (if window-system
  320. (display-time-mode 0)
  321. (display-time-mode 1)))
  322. ;; ;; current directory
  323. ;; (let ((ls (member 'mode-line-buffer-identification
  324. ;; mode-line-format)))
  325. ;; (setcdr ls
  326. ;; (cons '(:eval (concat " ("
  327. ;; (abbreviate-file-name default-directory)
  328. ;; ")"))
  329. ;; (cdr ls))))
  330. ;; ;; display last modified time
  331. ;; (let ((ls (member 'mode-line-buffer-identification
  332. ;; mode-line-format)))
  333. ;; (setcdr ls
  334. ;; (cons '(:eval (concat " "
  335. ;; my-buffer-file-last-modified-time))
  336. ;; (cdr ls))))
  337. (defun buffer-list-not-start-with-space ()
  338. "Return a list of buffers that not start with whitespaces."
  339. (let ((bl (buffer-list))
  340. b nbl)
  341. (while bl
  342. (setq b (pop bl))
  343. (unless (string-equal " "
  344. (substring (buffer-name b)
  345. 0
  346. 1))
  347. (add-to-list 'nbl b)))
  348. nbl))
  349. ;; http://www.masteringemacs.org/articles/2012/09/10/hiding-replacing-modeline-strings/
  350. ;; (add-to-list 'minor-mode-alist
  351. ;; '(global-whitespace-mode ""))
  352. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  353. ;; system info
  354. (defun my-message-current-info ()
  355. "Echo current login name, hostname and directory."
  356. (interactive)
  357. (message "%s@%s:%s"
  358. user-login-name
  359. system-name
  360. (abbreviate-file-name default-directory)))
  361. ;; (run-with-idle-timer 3
  362. ;; t
  363. ;; 'my-message-current-info)
  364. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  365. ;; minibuffer
  366. (setq insert-default-directory t)
  367. (setq completion-ignore-case t
  368. read-file-name-completion-ignore-case t
  369. read-buffer-completion-ignore-case t)
  370. (setq resize-mini-windows t)
  371. (temp-buffer-resize-mode 1)
  372. (savehist-mode 1)
  373. (fset 'yes-or-no-p 'y-or-n-p)
  374. ;; complete symbol when `eval'
  375. (define-key read-expression-map (kbd "TAB") 'lisp-complete-symbol)
  376. (define-key minibuffer-local-map (kbd "C-u")
  377. (lambda () (interactive) (delete-region (point-at-bol) (point))))
  378. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  379. ;; letters, font-lock mode and fonts
  380. ;; (set-face-background 'vertical-border (face-foreground 'mode-line))
  381. ;; (set-window-margins (selected-window) 1 1)
  382. (and (or (eq system-type 'Darwin)
  383. (eq system-type 'darwin))
  384. (fboundp 'mac-set-input-method-parameter)
  385. (mac-set-input-method-parameter 'japanese 'cursor-color "red")
  386. (mac-set-input-method-parameter 'roman 'cursor-color "black"))
  387. (when (and (boundp 'input-method-activate-hook) ; i dont know this is correct
  388. (boundp 'input-method-inactivate-hook))
  389. (add-hook 'input-method-activate-hook
  390. (lambda () (set-cursor-color "red")))
  391. (add-hook 'input-method-inactivate-hook
  392. (lambda () (set-cursor-color "black"))))
  393. (when (require 'paren nil t)
  394. (show-paren-mode 1)
  395. (setq show-paren-delay 0.5
  396. show-paren-style 'parenthesis) ; mixed is hard to read
  397. (set-face-background 'show-paren-match
  398. (face-foreground 'default))
  399. (set-face-inverse-video-p 'show-paren-match
  400. t))
  401. (transient-mark-mode 1)
  402. (global-font-lock-mode 1)
  403. (setq font-lock-global-modes
  404. '(not
  405. help-mode
  406. eshell-mode
  407. term-mode
  408. Man-mode))
  409. ;; (standard-display-ascii ?\n "$\n")
  410. (defvar my-eol-face
  411. '(("\n" . (0 font-lock-comment-face t nil)))
  412. )
  413. (defvar my-tab-face
  414. '(("\t" . '(0 highlight t nil))))
  415. (defvar my-jspace-face
  416. '(("\u3000" . '(0 highlight t nil))))
  417. (add-hook 'font-lock-mode-hook
  418. (lambda ()
  419. ;; (font-lock-add-keywords nil my-eol-face)
  420. (font-lock-add-keywords nil my-jspace-face)
  421. ))
  422. (when (require 'whitespace nil t)
  423. (add-to-list 'whitespace-display-mappings ; not work
  424. `(tab-mark ?\t ,(vconcat "^I\t")))
  425. (add-to-list 'whitespace-display-mappings
  426. `(newline-mark ?\n ,(vconcat "$\n")))
  427. (setq whitespace-style '(face
  428. trailing ; trailing blanks
  429. newline ; newlines
  430. newline-mark ; use display table for newline
  431. ;; tab-mark
  432. empty ; empty lines at beg or end of buffer
  433. lines-tail ; lines over 80
  434. ))
  435. ;; (setq whitespace-newline 'font-lock-comment-face)
  436. (global-whitespace-mode t)
  437. (when (eq (display-color-cells)
  438. 256)
  439. (set-face-foreground 'whitespace-newline "brightblack")))
  440. (and nil
  441. (fetch-library
  442. "http://www.emacswiki.org/emacs/download/fill-column-indicator.el"
  443. t)
  444. (require 'fill-column-indicator nil t)
  445. (setq fill-column-indicator))
  446. ;; highlight current line
  447. ;; http://wiki.riywo.com/index.php?Meadow
  448. (defface my-hl-line
  449. '((((min-colors 256)
  450. (background dark))
  451. (:background "color-234"))
  452. (((min-colors 256)
  453. (background light))
  454. (:background "color-234"))
  455. (t
  456. (:underline "black")))
  457. "*Face used by hl-line.")
  458. (setq hl-line-face 'my-hl-line) ;; (setq hl-line-face nil)
  459. (global-hl-line-mode 1) ;; (hl-line-mode 1)
  460. (setq hl-line-global-modes
  461. '(not
  462. term-mode))
  463. (set-face-foreground 'font-lock-regexp-grouping-backslash "#666")
  464. (set-face-foreground 'font-lock-regexp-grouping-construct "#f60")
  465. ;; fonts
  466. (defun my-set-ascii-and-jp-font (list)
  467. "Set font configuration List."
  468. (let ((fspec1 (if (> emacs-major-version 22)
  469. ;; font spec is available in emacs23 and later
  470. (font-spec :family (nth 2 list) :size (nth 3 list))
  471. (cons (nth 2 list) "jisx0208.*")))
  472. (fspec2 (if (> emacs-major-version 22)
  473. (font-spec :family (nth 2 list) :size (nth 3 list))
  474. (cons (nth 2 list) "jisx0201.*"))))
  475. (set-face-attribute 'default nil
  476. :family (nth 0 list)
  477. :height (nth 1 list))
  478. (set-fontset-font "fontset-default"
  479. 'japanese-jisx0208
  480. fspec1)
  481. (set-fontset-font "fontset-default"
  482. 'katakana-jisx0201
  483. fspec2)))
  484. ;; (my-set-ascii-and-jp-font '("dejavu sans mono" 90 "takaogothic" 13))
  485. ;; (my-set-ascii-and-jp-font '("dejavu sans mono" 100 "takaogothic" 14))
  486. ;; (my-set-ascii-and-jp-font '("dejavu sans mono" 100 "ms gothic" 14))
  487. ;; (my-set-ascii-and-jp-font '("monaco" 75 "takaogothic" 11))
  488. ;; (my-set-ascii-and-jp-font '("monaco" 90 "takaogothic" 13))
  489. ;; (my-set-ascii-and-jp-font '("ProggyCleanTTSZ" 120 "takaogothic" 11))
  490. ;; あ a
  491. (and (fetch-library
  492. "https://raw.github.com/10sr/emacs-lisp/master/set-modeline-color.el"
  493. t)
  494. (progn
  495. (require 'set-modeline-color nil t)))
  496. (let ((fg (face-foreground 'default))
  497. (bg (face-background 'default)))
  498. (set-face-background 'mode-line-inactive
  499. (if (face-inverse-video-p 'mode-line) fg bg))
  500. (set-face-foreground 'mode-line-inactive
  501. (if (face-inverse-video-p 'mode-line) bg fg)))
  502. (set-face-underline 'mode-line-inactive
  503. t)
  504. (set-face-underline 'vertical-border
  505. nil)
  506. (and (fetch-library
  507. "https://raw.github.com/tarao/elisp/master/end-mark.el"
  508. t)
  509. (require 'end-mark nil t)
  510. (global-end-mark-mode))
  511. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  512. ;; file handling
  513. (setq revert-without-query '(".+"))
  514. ;; save cursor position
  515. (setq save-place-file (concat user-emacs-directory
  516. "places"))
  517. (when (require 'saveplace nil t)
  518. (setq-default save-place t))
  519. ;; http://www.bookshelf.jp/soft/meadow_24.html#SEC260
  520. (setq make-backup-files t)
  521. ;; (make-directory (expand-file-name "~/.emacsbackup"))
  522. (setq backup-directory-alist
  523. (cons (cons "\\.*$" (expand-file-name "~/.emacs.d/backup"))
  524. backup-directory-alist))
  525. (setq version-control 'never)
  526. (setq delete-old-versions t)
  527. (setq auto-save-list-file-prefix (expand-file-name "~/.emacs.d/auto-save/"))
  528. (setq delete-auto-save-files t)
  529. (add-to-list 'completion-ignored-extensions ".bak")
  530. ;; (setq delete-by-moving-to-trash t
  531. ;; trash-directory "~/.emacs.d/trash")
  532. (add-hook 'after-save-hook
  533. 'executable-make-buffer-file-executable-if-script-p)
  534. (setq bookmark-default-file "~/.emacs.d/bmk")
  535. (add-hook 'recentf-load-hook
  536. (lambda ()
  537. (add-to-list 'recentf-exclude
  538. (regexp-quote bookmark-default-file))))
  539. (and (fetch-library
  540. "https://raw.github.com/10sr/emacs-lisp/master/read-only-only-mode.el"
  541. t)
  542. (lazy-load-eval 'read-only-only-mode))
  543. (and (fetch-library
  544. "https://raw.github.com/10sr/emacs-lisp/master/smart-revert.el"
  545. t)
  546. (require 'smart-revert nil t)
  547. (smart-revert-on))
  548. ;; autosave
  549. (and (fetch-library
  550. "https://raw.github.com/10sr/emacs-lisp/master/autosave.el"
  551. t)
  552. (require 'autosave nil t)
  553. (autosave-set 2))
  554. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  555. ;; editting
  556. (defun my-copy-whole-line ()
  557. "Copy whole line."
  558. (interactive)
  559. (kill-new (concat (buffer-substring (point-at-bol)
  560. (point-at-eol))
  561. "\n")))
  562. (setq require-final-newline t)
  563. (setq kill-whole-line t)
  564. (setq scroll-conservatively 35
  565. scroll-margin 2
  566. scroll-step 0)
  567. (setq-default major-mode 'text-mode)
  568. (setq next-line-add-newlines nil)
  569. (setq kill-read-only-ok t)
  570. (setq truncate-partial-width-windows nil) ; when splitted horizontally
  571. ;; (setq-default line-spacing 0.2)
  572. (setq-default indicate-empty-lines t) ; when using x indicate empty line
  573. (setq-default tab-width 4)
  574. (setq-default indent-tabs-mode nil)
  575. (setq-default indent-line-function nil)
  576. ;; (pc-selection-mode 1) ; make some already defined keybind back to default
  577. (delete-selection-mode 1)
  578. (cua-mode 0)
  579. (setq line-move-visual nil)
  580. ;; key bindings
  581. ;; moving around
  582. ;; (global-set-key (kbd "M-j") 'next-line)
  583. ;; (global-set-key (kbd "M-k") 'previous-line)
  584. ;; (global-set-key (kbd "M-h") 'backward-char)
  585. ;; (global-set-key (kbd "M-l") 'forward-char)
  586. ;;(keyboard-translate ?\M-j ?\C-j)
  587. ;; (global-set-key (kbd "M-p") 'backward-paragraph)
  588. (define-key esc-map "p" 'backward-paragraph)
  589. ;; (global-set-key (kbd "M-n") 'forward-paragraph)
  590. (define-key esc-map "n" 'forward-paragraph)
  591. (global-set-key (kbd "C-<up>") (lambda () (interactive)(scroll-down 1)))
  592. (global-set-key (kbd "C-<down>") (lambda () (interactive)(scroll-up 1)))
  593. (global-set-key (kbd "C-<left>") 'scroll-down)
  594. (global-set-key (kbd "C-<right>") 'scroll-up)
  595. (global-set-key (kbd "<select>") 'ignore) ; 'previous-line-mark)
  596. (define-key ctl-x-map (kbd "ESC x") 'execute-extended-command)
  597. (define-key ctl-x-map (kbd "ESC :") 'eval-expression)
  598. ;; C-h and DEL
  599. (global-set-key (kbd "C-h") (kbd "DEL"))
  600. (global-set-key (kbd "C-m") 'reindent-then-newline-and-indent)
  601. (global-set-key (kbd "C-o") (kbd "C-e C-m"))
  602. (define-key esc-map "k" 'my-copy-whole-line)
  603. ;; (global-set-key "\C-z" 'undo) ; undo is M-u
  604. (define-key esc-map "u" 'undo)
  605. (define-key esc-map "i" (kbd "ESC TAB"))
  606. ;; (global-set-key (kbd "C-r") 'query-replace-regexp)
  607. (global-set-key (kbd "C-s") 'isearch-forward-regexp)
  608. (global-set-key (kbd "C-r") 'isearch-backward-regexp)
  609. (define-key my-prefix-map (kbd "C-o") 'occur)
  610. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  611. ;; japanese input method
  612. (defun my-load-scim ()
  613. "Use scim-bridge.el as japanese im."
  614. ;; Load scim-bridge.
  615. (when (require 'scim-bridge nil t)
  616. ;; Turn on scim-mode automatically after loading .emacs
  617. (add-hook 'after-init-hook 'scim-mode-on)
  618. (setq scim-cursor-color "red")
  619. (scim-define-preedit-key ?\^h t)
  620. (scim-define-common-key ?\* nil)
  621. (scim-define-common-key ?\^/ nil)))
  622. (defun my-load-anthy ()
  623. "Use anthy.el as japanese im."
  624. ;; anthy
  625. (when (require 'anthy nil t)
  626. (global-set-key
  627. (kbd "<muhenkan>") (lambda () (interactive) (anthy-mode-off)))
  628. (global-set-key (kbd "<henkan>") (lambda () (interactive) (anthy-mode-on)))
  629. (when (>= emacs-major-version 23)
  630. (setq anthy-accept-timeout 1))))
  631. ;; quail
  632. ;; aproposs input-method for some information
  633. ;; (setq default-input-method "japanese")
  634. (defun my-load-mozc-el ()
  635. "Use mozc.el as japanese im."
  636. (setq mozc-leim-title "[MZ]")
  637. (when (require 'mozc nil t)
  638. (setq defauit-input-method "japanese-mozc")
  639. ))
  640. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  641. ;; gmail
  642. (setq mail-interactive t
  643. send-mail-function 'smtpmail-send-it
  644. ;; message-send-mail-function 'smtpmail-send-it
  645. smtpmail-smtp-server "smtp.gmail.com"
  646. smtpmail-smtp-service 587
  647. smtpmail-starttls-credentials '(("smtp.gmail.com" 587
  648. "8.slashes@gmail.com" nil))
  649. smtpmail-auth-credentials '(("smtp.gmail.com" 587
  650. "8.slashes@gmail.com" nil))
  651. user-mail-address "8.slashes@gmail.com")
  652. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  653. ;; buffer killing
  654. ;; (defun my-delete-window-killing-buffer () nil)
  655. (defun my-query-kill-current-buffer ()
  656. "Interactively kill current buffer."
  657. (interactive)
  658. (if (y-or-n-p (concat "kill current buffer? :"))
  659. (kill-buffer (current-buffer))))
  660. (substitute-key-definition 'kill-buffer
  661. 'my-query-kill-current-buffer
  662. global-map)
  663. ;;(global-set-key "\C-xk" 'my-query-kill-current-buffer)
  664. (defun my-kill-buffers ()
  665. "Kill buffers that visit files."
  666. (interactive)
  667. (mapcar (lambda (buf)
  668. (when (buffer-file-name buf)
  669. (kill-buffer buf)))
  670. (buffer-list)))
  671. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  672. ;; share clipboard with x
  673. ;; this page describes this in details, but only these sexps seem to be needed
  674. ;; http://garin.jp/doc/Linux/xwindow_clipboard
  675. (and (not window-system)
  676. (not (eq window-system 'mac))
  677. (getenv "DISPLAY")
  678. (not (equal (getenv "DISPLAY") ""))
  679. (executable-find "xclip")
  680. ;; (< emacs-major-version 24)
  681. (fetch-library "http://www.emacswiki.org/emacs/download/xclip.el" t)
  682. (require 'xclip nil t)
  683. (turn-on-xclip))
  684. (and (eq system-type 'darwin)
  685. (fetch-library
  686. "https://raw.github.com/10sr/emacs-lisp/master/pasteboard.el"
  687. t)
  688. (require 'pasteboard nil t)
  689. (turn-on-pasteboard)
  690. (getenv "TMUX")
  691. (pasteboard-enable-rtun))
  692. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  693. ;; https://github.com/lunaryorn/flycheck
  694. (when (require 'flycheck nil t)
  695. (add-hook 'after-init-hook 'global-flycheck-mode))
  696. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  697. ;; window
  698. (and (fetch-library
  699. "https://raw.github.com/10sr/emacs-lisp/master/window-organizer.el"
  700. t)
  701. (lazy-load-eval 'window-organizer)
  702. (define-key ctl-x-map (kbd "w") 'window-organizer))
  703. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  704. ;; some modes and hooks
  705. (and (fetch-library
  706. "https://raw.github.com/10sr/emacs-lisp/master/remember-major-modes-mode.el"
  707. t)
  708. (require 'remember-major-modes-mode nil t)
  709. (remember-major-modes-mode 1)
  710. )
  711. ;; Detect file type from shebang and set major-mode.
  712. (add-to-list 'interpreter-mode-alist
  713. '("python3" . python-mode))
  714. (add-to-list 'interpreter-mode-alist
  715. '("python2" . python-mode))
  716. ;; http://fukuyama.co/foreign-regexp
  717. '(and (fetch-library
  718. "https://raw.github.com/k-talo/foreign-regexp.el/master/foreign-regexp.el"
  719. t)
  720. (require 'foreign-regexp nil t)
  721. (progn
  722. (setq foreign-regexp/regexp-type 'perl)
  723. '(setq reb-re-syntax 'foreign-regexp)
  724. ))
  725. (require 'session nil t)
  726. (lazy-load-eval 'sql '(sql-mode)
  727. (require 'sql-indent nil t))
  728. (and (fetch-library "https://raw.github.com/10sr/emacs-lisp/master/gtkbm.el"
  729. t)
  730. (lazy-load-eval 'gtkbm)
  731. (global-set-key (kbd "C-x C-d") 'gtkbm))
  732. (and (fetch-library
  733. "https://raw.github.com/10sr/emacs-lisp/master/git-command.el"
  734. t)
  735. (lazy-load-eval 'git-command
  736. nil
  737. (add-to-list 'git-command-major-mode-alist
  738. '("di" . diff-mode))
  739. (add-to-list 'git-command-major-mode-alist
  740. '("graph" . fundamental-mode))
  741. (add-to-list 'git-command-major-mode-alist
  742. '("log" . fundamental-mode)))
  743. ;; (setq git-command-default-options "-c color.ui=always")
  744. (define-key ctl-x-map "g" 'git-command))
  745. (and (fetch-library
  746. "http://www.emacswiki.org/emacs/download/sl.el"
  747. t)
  748. (lazy-load-eval 'sl))
  749. (defalias 'qcalc 'quick-calc)
  750. (require 'simple nil t)
  751. (add-hook 'makefile-mode-hook
  752. (lambda ()
  753. (define-key makefile-mode-map (kbd "C-m") 'newline-and-indent)
  754. ;; this functions is set in write-file-functions, i cannot find any
  755. ;; good way to remove this.
  756. (fset 'makefile-warn-suspicious-lines 'ignore)
  757. ))
  758. (add-hook 'verilog-mode-hook
  759. (lambda ()
  760. (define-key verilog-mode-map ";" 'self-insert-command)))
  761. (setq diff-switches "-u")
  762. (add-hook 'diff-mode-hook
  763. (lambda ()
  764. (view-mode 1)
  765. (set-face-attribute 'diff-header nil
  766. :foreground nil
  767. :background nil
  768. :weight 'bold)
  769. (set-face-attribute 'diff-file-header nil
  770. :foreground nil
  771. :background nil
  772. :weight 'bold)
  773. (set-face-foreground 'diff-index-face "blue")
  774. (set-face-attribute 'diff-hunk-header nil
  775. :foreground "cyan"
  776. :weight 'normal)
  777. (set-face-attribute 'diff-context nil
  778. ;; :foreground "white"
  779. :foreground nil
  780. :weight 'normal)
  781. (set-face-foreground 'diff-removed-face "red")
  782. (set-face-foreground 'diff-added-face "green")
  783. (set-face-background 'diff-removed-face nil)
  784. (set-face-background 'diff-added-face nil)
  785. (set-face-attribute 'diff-changed nil
  786. :foreground "magenta"
  787. :weight 'normal)
  788. ))
  789. ;; (ffap-bindings)
  790. (add-hook 'sh-mode-hook
  791. (lambda ()
  792. (define-key sh-mode-map
  793. (kbd "C-x C-e")
  794. 'my-execute-shell-command-current-line)))
  795. (setq sh-here-document-word "__EOC__")
  796. (defun my-execute-shell-command-current-line ()
  797. "Run current line as shell command"
  798. (interactive)
  799. (shell-command (buffer-substring-no-properties (point-at-bol)
  800. (point))))
  801. (setq auto-mode-alist
  802. `(("autostart\\'" . sh-mode)
  803. ("xinitrc\\'" . sh-mode)
  804. ("xprograms\\'" . sh-mode)
  805. ("PKGBUILD\\'" . sh-mode)
  806. ,@auto-mode-alist))
  807. (and (lazy-load-eval 'pkgbuild-mode)
  808. (setq auto-mode-alist (append '(("PKGBUILD\\'" . pkgbuild-mode))
  809. auto-mode-alist)))
  810. (add-hook 'html-mode-hook
  811. (lambda ()
  812. (define-key html-mode-map (kbd "C-m")
  813. 'reindent-then-newline-and-indent)))
  814. (add-hook 'text-mode-hook
  815. (lambda ()
  816. (define-key text-mode-map (kbd "C-m") 'newline)))
  817. (add-to-list 'Info-default-directory-list
  818. (expand-file-name "~/.info/emacs-ja"))
  819. (add-hook 'apropos-mode-hook
  820. (lambda ()
  821. (define-key apropos-mode-map "n" 'next-line)
  822. (define-key apropos-mode-map "p" 'previous-line)
  823. ))
  824. (add-hook 'isearch-mode-hook
  825. (lambda ()
  826. ;; (define-key isearch-mode-map
  827. ;; (kbd "C-j") 'isearch-other-control-char)
  828. ;; (define-key isearch-mode-map
  829. ;; (kbd "C-k") 'isearch-other-control-char)
  830. ;; (define-key isearch-mode-map
  831. ;; (kbd "C-h") 'isearch-other-control-char)
  832. (define-key isearch-mode-map (kbd "C-h") 'isearch-delete-char)
  833. (define-key isearch-mode-map (kbd "M-r")
  834. 'isearch-query-replace-regexp)))
  835. (add-hook 'outline-mode-hook
  836. (lambda ()
  837. (if (string-match "\\.md\\'" buffer-file-name)
  838. (set (make-local-variable 'outline-regexp) "#+ "))))
  839. (add-to-list 'auto-mode-alist (cons "\\.ol\\'" 'outline-mode))
  840. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'outline-mode))
  841. (when (fetch-library
  842. "http://jblevins.org/projects/markdown-mode/markdown-mode.el"
  843. t)
  844. (lazy-load-eval 'markdown-mode)
  845. (setq markdown-command (or (executable-find "markdown")
  846. (executable-find "markdown.pl")))
  847. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'markdown-mode))
  848. (add-hook 'markdown-mode-hook
  849. (lambda ()
  850. (outline-minor-mode 1)
  851. (set (make-local-variable 'comment-start) ";"))))
  852. ;; http://d.hatena.ne.jp/emergent/20070203/1170512717
  853. ;; c-mode
  854. ;; (setq c-default-style "bsd")
  855. (add-hook 'c-mode-common-hook
  856. (lambda ()
  857. (setq c-basic-offset 4
  858. indent-tabs-mode nil)
  859. ;; (set-face-foreground 'font-lock-keyword-face "blue")
  860. (c-toggle-hungry-state -1)
  861. ;; (and (require 'gtags nil t)
  862. ;; (gtags-mode 1))
  863. ))
  864. (when (fetch-library
  865. "https://raw.github.com/mooz/js2-mode/master/js2-mode.el"
  866. t)
  867. (lazy-load-eval 'js2-mode)
  868. ;; (add-to-list 'auto-mode-alist '("\\.js\\'" . js2-mode))
  869. ;; (add-to-list 'auto-mode-alist '("\\.jsm\\'" . js2-mode))
  870. (add-hook 'js2-mode-hook
  871. (lambda ()
  872. (define-key js2-mode-map (kbd "C-m") (lambda ()
  873. (interactive)
  874. (js2-enter-key)
  875. (indent-for-tab-command)))
  876. ;; (add-hook (kill-local-variable 'before-save-hook)
  877. ;; 'js2-before-save)
  878. ;; (add-hook 'before-save-hook
  879. ;; 'my-indent-buffer
  880. ;; nil
  881. ;; t)
  882. )))
  883. (require 'js-doc nil t)
  884. (when (require 'uniquify nil t)
  885. (setq uniquify-buffer-name-style 'post-forward-angle-brackets)
  886. (setq uniquify-ignore-buffers-re "*[^*]+*")
  887. (setq uniquify-min-dir-content 1))
  888. (add-hook 'view-mode-hook
  889. (lambda()
  890. (define-key view-mode-map "j"
  891. (lambda() (interactive) (scroll-up 1)))
  892. (define-key view-mode-map "k"
  893. (lambda() (interactive) (scroll-down 1)))
  894. (define-key view-mode-map "v" 'toggle-read-only)
  895. (define-key view-mode-map "q" 'bury-buffer)
  896. ;; (define-key view-mode-map "/" 'nonincremental-re-search-forward)
  897. ;; (define-key view-mode-map "?" 'nonincremental-re-search-backward)
  898. ;; (define-key view-mode-map
  899. ;; "n" 'nonincremental-repeat-search-forward)
  900. ;; (define-key view-mode-map
  901. ;; "N" 'nonincremental-repeat-search-backward)
  902. (define-key view-mode-map "/" 'isearch-forward-regexp)
  903. (define-key view-mode-map "?" 'isearch-backward-regexp)
  904. (define-key view-mode-map "n" 'isearch-repeat-forward)
  905. (define-key view-mode-map "N" 'isearch-repeat-backward)
  906. (define-key view-mode-map (kbd "C-m") 'my-rgrep-symbol-at-point)
  907. ))
  908. (global-set-key "\M-r" 'view-mode)
  909. (setq view-read-only t)
  910. ;; (defun my-view-mode-search-word (word)
  911. ;; "Search for word current directory and subdirectories.
  912. ;; If called intearctively, find word at point."
  913. ;; (interactive (list (thing-at-point 'symbol)))
  914. ;; (if word
  915. ;; (if (and (require 'gtags nil t)
  916. ;; (gtags-get-rootpath))
  917. ;; (gtags-goto-tag word "s")
  918. ;; (my-rgrep word))
  919. ;; (message "No word at point.")
  920. ;; nil))
  921. (add-hook 'Man-mode-hook
  922. (lambda ()
  923. (view-mode 1)
  924. (setq truncate-lines nil)))
  925. (setq Man-notify-method (if window-system
  926. 'newframe
  927. 'aggressive))
  928. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  929. ;; python
  930. (when (lazy-load-eval 'python '(python-mode))
  931. (setq python-python-command (or (executable-find "python3")
  932. (executable-find "python")))
  933. ;; (defun my-python-run-as-command ()
  934. ;; ""
  935. ;; (interactive)
  936. ;; (shell-command (concat python-python-command " " buffer-file-name)))
  937. (defun my-python-display-python-buffer ()
  938. ""
  939. (interactive)
  940. (set-window-text-height (display-buffer python-buffer
  941. t)
  942. 7))
  943. (add-hook 'python-mode-hook
  944. (lambda ()
  945. (define-key python-mode-map
  946. (kbd "C-c C-e") 'my-python-run-as-command)
  947. (define-key python-mode-map
  948. (kbd "C-c C-b") 'my-python-display-python-buffer)
  949. (define-key python-mode-map (kbd "C-m") 'newline-and-indent)))
  950. (add-hook 'inferior-python-mode-hook
  951. (lambda ()
  952. (my-python-display-python-buffer)
  953. (define-key inferior-python-mode-map
  954. (kbd "<up>") 'comint-previous-input)
  955. (define-key inferior-python-mode-map
  956. (kbd "<down>") 'comint-next-input))))
  957. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  958. ;; GNU GLOBAL(gtags)
  959. ;; http://uguisu.skr.jp/Windows/gtags.html
  960. ;; http://eigyr.dip.jp/gtags.html
  961. ;; http://cha.la.coocan.jp/doc/gnu_global.html
  962. (let ((d "/opt/local/share/gtags/"))
  963. (and (file-directory-p d)
  964. (add-to-list 'load-path
  965. d)))
  966. (when (lazy-load-eval 'gtags '(gtags-mode))
  967. (add-hook 'gtags-mode-hook
  968. (lambda ()
  969. (view-mode gtags-mode)
  970. (setq gtags-select-buffer-single t)
  971. ;; (local-set-key "\M-t" 'gtags-find-tag)
  972. ;; (local-set-key "\M-r" 'gtags-find-rtag)
  973. ;; (local-set-key "\M-s" 'gtags-find-symbol)
  974. ;; (local-set-key "\C-t" 'gtags-pop-stack)
  975. (define-key gtags-mode-map (kbd "C-x t h")
  976. 'gtags-find-tag-from-here)
  977. (define-key gtags-mode-map (kbd "C-x t t") 'gtags-find-tag)
  978. (define-key gtags-mode-map (kbd "C-x t r") 'gtags-find-rtag)
  979. (define-key gtags-mode-map (kbd "C-x t s") 'gtags-find-symbol)
  980. (define-key gtags-mode-map (kbd "C-x t p") 'gtags-find-pattern)
  981. (define-key gtags-mode-map (kbd "C-x t f") 'gtags-find-file)
  982. (define-key gtags-mode-map (kbd "C-x t b") 'gtags-pop-stack) ;back
  983. ))
  984. (add-hook 'gtags-select-mode-hook
  985. (lambda ()
  986. (define-key gtags-select-mode-map (kbd "C-m") 'gtags-select-tag)
  987. ))
  988. )
  989. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  990. ;; term mode
  991. ;; (setq multi-term-program shell-file-name)
  992. (and (fetch-library "http://www.emacswiki.org/emacs/download/multi-term.el"
  993. t)
  994. (lazy-load-eval 'multi-term)
  995. (progn
  996. (setq multi-term-switch-after-close nil)
  997. (setq multi-term-dedicated-select-after-open-p t)
  998. (setq multi-term-dedicated-window-height 20)))
  999. (when (lazy-load-eval 'term '(term ansi-term))
  1000. (defun my-term-quit-or-send-raw ()
  1001. ""
  1002. (interactive)
  1003. (if (get-buffer-process (current-buffer))
  1004. (call-interactively 'term-send-raw)
  1005. (kill-buffer)))
  1006. ;; http://d.hatena.ne.jp/goinger/20100416/1271399150
  1007. ;; (setq term-ansi-default-program shell-file-name)
  1008. (add-hook 'term-setup-hook
  1009. (lambda ()
  1010. (setq term-display-table (make-display-table))))
  1011. (add-hook 'term-mode-hook
  1012. (lambda ()
  1013. (unless (memq (current-buffer)
  1014. (and (featurep 'multi-term)
  1015. ;; current buffer is not multi-term buffer
  1016. (multi-term-list)))
  1017. ;; (define-key term-raw-map "\C-q" 'move-beginning-of-line)
  1018. ;; (define-key term-raw-map "\C-r" 'term-send-raw)
  1019. ;; (define-key term-raw-map "\C-s" 'term-send-raw)
  1020. ;; (define-key term-raw-map "\C-f" 'forward-char)
  1021. ;; (define-key term-raw-map "\C-b" 'backward-char)
  1022. ;; (define-key term-raw-map "\C-t" 'set-mark-command)
  1023. (define-key term-raw-map
  1024. "\C-x" (lookup-key (current-global-map) "\C-x"))
  1025. (define-key term-raw-map
  1026. "\C-z" (lookup-key (current-global-map) "\C-z"))
  1027. )
  1028. ;; (define-key term-raw-map "\C-xl" 'term-line-mode)
  1029. ;; (define-key term-mode-map "\C-xc" 'term-char-mode)
  1030. (define-key term-raw-map (kbd "<up>")
  1031. (lambda () (interactive) (scroll-down 1)))
  1032. (define-key term-raw-map (kbd "<down>")
  1033. (lambda () (interactive) (scroll-up 1)))
  1034. (define-key term-raw-map (kbd "<right>") 'scroll-up)
  1035. (define-key term-raw-map (kbd "<left>") 'scroll-down)
  1036. (define-key term-raw-map (kbd "C-p") 'term-send-raw)
  1037. (define-key term-raw-map (kbd "C-n") 'term-send-raw)
  1038. (define-key term-raw-map "q" 'my-term-quit-or-send-raw)
  1039. ;; (define-key term-raw-map (kbd "ESC") 'term-send-raw)
  1040. (define-key term-raw-map [delete] 'term-send-raw)
  1041. (define-key term-raw-map (kbd "DEL") 'term-send-backspace)
  1042. (define-key term-raw-map "\C-y" 'term-paste)
  1043. (define-key term-raw-map
  1044. "\C-c" 'term-send-raw) ;; 'term-interrupt-subjob)
  1045. '(define-key term-mode-map (kbd "C-x C-q") 'term-pager-toggle)
  1046. ;; (dolist (key '("<up>" "<down>" "<right>" "<left>"))
  1047. ;; (define-key term-raw-map (read-kbd-macro key) 'term-send-raw))
  1048. ;; (define-key term-raw-map "\C-d" 'delete-char)
  1049. (set (make-local-variable 'scroll-margin) 0)
  1050. ;; (set (make-local-variable 'cua-enable-cua-keys) nil)
  1051. ;; (cua-mode 0)
  1052. ;; (and cua-mode
  1053. ;; (local-unset-key (kbd "C-c")))
  1054. ;; (define-key cua--prefix-override-keymap
  1055. ;;"\C-c" 'term-interrupt-subjob)
  1056. (set (make-local-variable 'hl-line-range-function)
  1057. (lambda ()
  1058. '(0 . 0)))
  1059. ))
  1060. ;; (add-hook 'term-exec-hook 'forward-char)
  1061. )
  1062. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1063. ;; buffer switching
  1064. (when (lazy-load-eval 'bs '(bs-show)
  1065. ;; (add-to-list 'bs-configurations
  1066. ;; '("processes" nil get-buffer-process ".*" nil nil))
  1067. (add-to-list 'bs-configurations
  1068. '("this-frame" nil (lambda (buf)
  1069. (memq buf (my-frame-buffer-get)))
  1070. ".*" nil nil))
  1071. (add-to-list 'bs-configurations
  1072. '("files-and-terminals" nil nil nil
  1073. (lambda (buf)
  1074. (and (bs-visits-non-file buf)
  1075. (save-excursion
  1076. (set-buffer buf)
  1077. (not (memq major-mode
  1078. '(term-mode
  1079. eshell-mode))))))))
  1080. ;; (setq bs-configurations (list
  1081. ;; '("processes" nil get-buffer-process ".*" nil nil)
  1082. ;; '("files-and-scratch" "^\\*scratch\\*$" nil nil
  1083. ;; bs-visits-non-file bs-sort-buffer-interns-are-last)))
  1084. )
  1085. ;; (global-set-key "\C-x\C-b" 'bs-show)
  1086. (defalias 'list-buffers 'bs-show)
  1087. (setq bs-default-configuration "files-and-terminals")
  1088. (setq bs-default-sort-name "by nothing")
  1089. (add-hook 'bs-mode-hook
  1090. (lambda ()
  1091. ;; (setq bs-default-configuration "files")
  1092. ;; (and bs--show-all
  1093. ;; (call-interactively 'bs-toggle-show-all))
  1094. (set (make-local-variable 'scroll-margin) 0))))
  1095. (iswitchb-mode 1)
  1096. (defun iswitchb-buffer-display-other-window ()
  1097. "Do iswitchb in other window."
  1098. (interactive)
  1099. (let ((iswitchb-default-method 'display))
  1100. (call-interactively 'iswitchb-buffer)))
  1101. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1102. ;; sdic
  1103. (when (lazy-load-eval 'sdic '(sdic-describe-word-at-point))
  1104. ;; (define-key my-prefix-map "\C-w" 'sdic-describe-word)
  1105. (define-key my-prefix-map "\C-t" 'sdic-describe-word-at-point-echo)
  1106. (defun sdic-describe-word-at-point-echo ()
  1107. ""
  1108. (interactive)
  1109. (save-window-excursion
  1110. (sdic-describe-word-at-point))
  1111. (save-excursion
  1112. (set-buffer sdic-buffer-name)
  1113. (message (buffer-substring (point-min)
  1114. (progn (goto-char (point-min))
  1115. (or (and (re-search-forward "^\\w"
  1116. nil
  1117. t
  1118. 4)
  1119. (progn (previous-line) t)
  1120. (point-at-eol))
  1121. (point-max)))))))
  1122. (setq sdic-eiwa-dictionary-list '((sdicf-client "/usr/share/dict/gene.sdic")))
  1123. (setq sdic-waei-dictionary-list
  1124. '((sdicf-client "/usr/share/dict/jedict.sdic" (add-keys-to-headword t))))
  1125. (setq sdic-disable-select-window t)
  1126. (setq sdic-window-height 7))
  1127. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1128. ;; vc
  1129. ;; (require 'vc)
  1130. (setq vc-handled-backends '())
  1131. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1132. ;; gauche-mode
  1133. ;; http://d.hatena.ne.jp/kobapan/20090305/1236261804
  1134. ;; http://www.katch.ne.jp/~leque/software/repos/gauche-mode/gauche-mode.el
  1135. (when (and (fetch-library
  1136. "http://www.katch.ne.jp/~leque/software/repos/gauche-mode/gauche-mode.el"
  1137. t)
  1138. (lazy-load-eval 'gauche-mode '(gauche-mode run-scheme)))
  1139. (let ((s (executable-find "gosh")))
  1140. (setq scheme-program-name s
  1141. gauche-program-name s))
  1142. (defun run-gauche-other-window ()
  1143. "Run gauche on other window"
  1144. (interactive)
  1145. (switch-to-buffer-other-window
  1146. (get-buffer-create "*scheme*"))
  1147. (run-gauche))
  1148. (defun run-gauche ()
  1149. "run gauche"
  1150. (run-scheme gauche-program-name)
  1151. )
  1152. (defun scheme-send-buffer ()
  1153. ""
  1154. (interactive)
  1155. (scheme-send-region (point-min) (point-max))
  1156. (my-scheme-display-scheme-buffer)
  1157. )
  1158. (defun my-scheme-display-scheme-buffer ()
  1159. ""
  1160. (interactive)
  1161. (set-window-text-height (display-buffer scheme-buffer
  1162. t)
  1163. 7))
  1164. (add-hook 'scheme-mode-hook
  1165. (lambda ()
  1166. nil))
  1167. (add-hook 'inferior-scheme-mode-hook
  1168. (lambda ()
  1169. ;; (my-scheme-display-scheme-buffer)
  1170. ))
  1171. (setq auto-mode-alist
  1172. (cons '("\.gosh\\'" . gauche-mode) auto-mode-alist))
  1173. (setq auto-mode-alist
  1174. (cons '("\.gaucherc\\'" . gauche-mode) auto-mode-alist))
  1175. (add-hook 'gauche-mode-hook
  1176. (lambda ()
  1177. (define-key gauche-mode-map
  1178. (kbd "C-c C-z") 'run-gauche-other-window)
  1179. (define-key scheme-mode-map
  1180. (kbd "C-c C-c") 'scheme-send-buffer)
  1181. (define-key scheme-mode-map
  1182. (kbd "C-c C-b") 'my-scheme-display-scheme-buffer))))
  1183. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1184. ;; recentf-mode
  1185. (setq recentf-save-file (expand-file-name "~/.emacs.d/recentf")
  1186. recentf-max-menu-items 20
  1187. recentf-max-saved-items 30
  1188. recentf-show-file-shortcuts-flag nil)
  1189. (when (require 'recentf nil t)
  1190. (add-to-list 'recentf-exclude (regexp-quote recentf-save-file))
  1191. (define-key ctl-x-map (kbd "C-r") 'recentf-open-files)
  1192. (add-hook 'find-file-hook
  1193. 'recentf-save-list
  1194. t) ; save to file immediately after adding file to recentf list
  1195. (add-hook 'kill-emacs-hook
  1196. 'recentf-load-list)
  1197. ;;(run-with-idle-timer 5 t 'recentf-save-list)
  1198. ;; (add-hook 'find-file-hook
  1199. ;; (lambda ()
  1200. ;; (recentf-add-file default-directory)))
  1201. (and (fetch-library
  1202. "https://raw.github.com/10sr/emacs-lisp/master/recentf-show.el"
  1203. t)
  1204. (lazy-load-eval 'recentf-show)
  1205. (define-key ctl-x-map (kbd "C-r") 'recentf-show)
  1206. (add-hook 'recentf-show-before-listing-hook
  1207. 'recentf-load-list))
  1208. (recentf-mode 1)
  1209. (add-hook 'recentf-dialog-mode-hook
  1210. (lambda ()
  1211. ;; (recentf-save-list)
  1212. ;; (define-key recentf-dialog-mode-map (kbd "C-x C-f")
  1213. ;; 'my-recentf-cd-and-find-file)
  1214. (define-key recentf-dialog-mode-map (kbd "<up>") 'previous-line)
  1215. (define-key recentf-dialog-mode-map (kbd "<down>") 'next-line)
  1216. (define-key recentf-dialog-mode-map "p" 'previous-line)
  1217. (define-key recentf-dialog-mode-map "n" 'next-line)
  1218. (cd "~/"))))
  1219. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1220. ;; dired
  1221. (when (lazy-load-eval 'dired nil)
  1222. (defun my-dired-echo-file-head (arg)
  1223. ""
  1224. (interactive "P")
  1225. (let ((f (dired-get-filename)))
  1226. (message "%s"
  1227. (with-temp-buffer
  1228. (insert-file-contents f)
  1229. (buffer-substring-no-properties
  1230. (point-min)
  1231. (progn (goto-line (if arg
  1232. (prefix-numeric-value arg)
  1233. 10))
  1234. (point-at-eol)))))))
  1235. (defun my-dired-diff ()
  1236. ""
  1237. (interactive)
  1238. (let ((files (dired-get-marked-files nil nil nil t)))
  1239. (if (eq (car files)
  1240. t)
  1241. (diff (cadr files) (dired-get-filename))
  1242. (message "One files must be marked!"))))
  1243. (defun my-pop-to-buffer-erase-noselect (buffer-or-name)
  1244. "pop up buffer using `display-buffer' and return that buffer."
  1245. (let ((bf (get-buffer-create buffer-or-name)))
  1246. (with-current-buffer bf
  1247. (cd ".")
  1248. (erase-buffer))
  1249. (display-buffer bf)
  1250. bf))
  1251. (defun my-replace-nasi-none ()
  1252. ""
  1253. (save-excursion
  1254. (let ((buffer-read-only nil))
  1255. (goto-char (point-min))
  1256. (while (search-forward "なし" nil t)
  1257. (replace-match "none")))))
  1258. (defun dired-get-file-info ()
  1259. "dired get file info"
  1260. (interactive)
  1261. (let ((f (shell-quote-argument (dired-get-filename t))))
  1262. (if (file-directory-p f)
  1263. (progn
  1264. (message "Calculating disk usage...")
  1265. (shell-command (concat "du -hsD "
  1266. f)))
  1267. (shell-command (concat "file "
  1268. f)))))
  1269. (defun my-dired-scroll-up ()
  1270. ""
  1271. (interactive)
  1272. (my-dired-previous-line (- (window-height) 1)))
  1273. (defun my-dired-scroll-down ()
  1274. ""
  1275. (interactive)
  1276. (my-dired-next-line (- (window-height) 1)))
  1277. ;; (defun my-dired-forward-line (arg)
  1278. ;; ""
  1279. ;; (interactive "p"))
  1280. (defun my-dired-previous-line (arg)
  1281. ""
  1282. (interactive "p")
  1283. (if (> arg 0)
  1284. (progn
  1285. (if (eq (line-number-at-pos)
  1286. 1)
  1287. (goto-char (point-max))
  1288. (forward-line -1))
  1289. (my-dired-previous-line (if (or (dired-get-filename nil t)
  1290. (dired-get-subdir))
  1291. (- arg 1)
  1292. arg)))
  1293. (dired-move-to-filename)))
  1294. (defun my-dired-next-line (arg)
  1295. ""
  1296. (interactive "p")
  1297. (if (> arg 0)
  1298. (progn
  1299. (if (eq (point)
  1300. (point-max))
  1301. (goto-char (point-min))
  1302. (forward-line 1))
  1303. (my-dired-next-line (if (or (dired-get-filename nil t)
  1304. (dired-get-subdir))
  1305. (- arg 1)
  1306. arg)))
  1307. (dired-move-to-filename)))
  1308. (defun my-dired-print-current-dir-and-file ()
  1309. (message "%s %s"
  1310. default-directory
  1311. (buffer-substring-no-properties (point-at-bol)
  1312. (point-at-eol))))
  1313. (defun dired-do-execute-as-command ()
  1314. ""
  1315. (interactive)
  1316. (let ((file (dired-get-filename t)))
  1317. (if (file-executable-p file)
  1318. (start-process file nil file)
  1319. (when (y-or-n-p
  1320. "this file cant be executed. mark as executable and go? : ")
  1321. (set-file-modes file
  1322. (file-modes-symbolic-to-number "u+x" (file-modes file)))
  1323. (start-process file nil file)))))
  1324. ;;http://bach.istc.kobe-u.ac.jp/lect/tamlab/ubuntu/emacs.html
  1325. (defun my-dired-x-open ()
  1326. ""
  1327. (interactive)
  1328. (my-x-open (dired-get-filename t t)))
  1329. (if (eq window-system 'mac)
  1330. (setq dired-listing-switches "-lhF")
  1331. (setq dired-listing-switches "-lhF --time-style=long-iso")
  1332. )
  1333. (setq dired-listing-switches "-lhF")
  1334. (put 'dired-find-alternate-file 'disabled nil)
  1335. ;; when using dired-find-alternate-file
  1336. ;; reuse current dired buffer for the file to open
  1337. (setq dired-ls-F-marks-symlinks t)
  1338. (when (require 'ls-lisp nil t)
  1339. (setq ls-lisp-use-insert-directory-program nil) ; always use ls-lisp
  1340. (setq ls-lisp-dirs-first t)
  1341. (setq ls-lisp-use-localized-time-format t)
  1342. (setq ls-lisp-format-time-list
  1343. '("%Y-%m-%d %H:%M"
  1344. "%Y-%m-%d ")))
  1345. (setq dired-dwim-target t)
  1346. ;; (add-hook 'dired-after-readin-hook
  1347. ;; 'my-replace-nasi-none)
  1348. ;; (add-hook 'after-init-hook
  1349. ;; (lambda ()
  1350. ;; (dired ".")))
  1351. (add-hook 'dired-mode-hook
  1352. (lambda ()
  1353. (define-key dired-mode-map "o" 'my-dired-x-open)
  1354. (define-key dired-mode-map "i" 'dired-get-file-info)
  1355. (define-key dired-mode-map "f" 'find-file)
  1356. (define-key dired-mode-map "!" 'shell-command)
  1357. (define-key dired-mode-map "&" 'async-shell-command)
  1358. (define-key dired-mode-map "X" 'dired-do-async-shell-command)
  1359. (define-key dired-mode-map "=" 'my-dired-diff)
  1360. (define-key dired-mode-map "B" 'gtkbm-add-current-dir)
  1361. (define-key dired-mode-map "b" 'gtkbm)
  1362. (define-key dired-mode-map "h" 'my-dired-echo-file-head)
  1363. (define-key dired-mode-map "@" (lambda ()
  1364. (interactive) (my-x-open ".")))
  1365. (define-key dired-mode-map (kbd "TAB") 'other-window)
  1366. ;; (define-key dired-mode-map "P" 'my-dired-do-pack-or-unpack)
  1367. (define-key dired-mode-map "/" 'dired-isearch-filenames)
  1368. (define-key dired-mode-map (kbd "DEL") 'dired-up-directory)
  1369. (define-key dired-mode-map (kbd "C-h") 'dired-up-directory)
  1370. (substitute-key-definition 'dired-next-line
  1371. 'my-dired-next-line dired-mode-map)
  1372. (substitute-key-definition 'dired-previous-line
  1373. 'my-dired-previous-line dired-mode-map)
  1374. (define-key dired-mode-map (kbd "<left>") 'my-dired-scroll-up)
  1375. (define-key dired-mode-map (kbd "<right>") 'my-dired-scroll-down)
  1376. (define-key dired-mode-map (kbd "ESC p") 'my-dired-scroll-up)
  1377. (define-key dired-mode-map (kbd "ESC n") 'my-dired-scroll-down)
  1378. (let ((file "._Icon\015"))
  1379. (when nil (file-readable-p file)
  1380. (delete-file file)))))
  1381. (and (fetch-library "https://raw.github.com/10sr/emacs-lisp/master/pack.el"
  1382. t)
  1383. (lazy-load-eval 'pack '(dired-do-pack-or-unpack pack))
  1384. (add-hook 'dired-mode-hook
  1385. (lambda ()
  1386. (define-key dired-mode-map "P" 'dired-do-pack-or-unpack))))
  1387. (and (fetch-library
  1388. "https://raw.github.com/10sr/emacs-lisp/master/dired-list-all-mode.el"
  1389. t)
  1390. (lazy-load-eval 'dired-list-all-mode)
  1391. (setq dired-listing-switches "-lhF")
  1392. (add-hook 'dired-mode-hook
  1393. (lambda ()
  1394. (define-key dired-mode-map "a" 'dired-list-all-mode)
  1395. )))
  1396. ) ; when dired locate
  1397. ;; http://blog.livedoor.jp/tek_nishi/archives/4693204.html
  1398. (defun my-dired-toggle-mark()
  1399. (let ((cur (cond ((eq (following-char) dired-marker-char) ?\040)
  1400. (t dired-marker-char))))
  1401. (delete-char 1)
  1402. (insert cur)))
  1403. (defun my-dired-mark (arg)
  1404. "Toggle mark the current (or next ARG) files.
  1405. If on a subdir headerline, mark all its files except `.' and `..'.
  1406. Use \\[dired-unmark-all-files] to remove all marks
  1407. and \\[dired-unmark] on a subdir to remove the marks in
  1408. this subdir."
  1409. (interactive "P")
  1410. (if (dired-get-subdir)
  1411. (save-excursion (dired-mark-subdir-files))
  1412. (let ((inhibit-read-only t))
  1413. (dired-repeat-over-lines
  1414. (prefix-numeric-value arg)
  1415. 'my-dired-toggle-mark))))
  1416. (defun my-dired-mark-backward (arg)
  1417. "In Dired, move up lines and toggle mark there.
  1418. Optional prefix ARG says how many lines to unflag; default is one line."
  1419. (interactive "p")
  1420. (my-dired-mark (- arg)))
  1421. (add-hook 'dired-mode-hook
  1422. (lambda ()
  1423. (local-set-key (kbd "SPC") 'my-dired-mark)
  1424. (local-set-key (kbd "S-SPC") 'my-dired-mark-backward))
  1425. )
  1426. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1427. ;; eshell
  1428. (lazy-load-eval 'eshell nil
  1429. (defun my-eshell-backward-delete-char ()
  1430. (interactive)
  1431. (when (< (save-excursion
  1432. (eshell-bol)
  1433. (point))
  1434. (point))
  1435. (backward-delete-char 1)))
  1436. (defun my-file-owner-p (file)
  1437. "t if FILE is owned by me."
  1438. (eq (user-uid) (nth 2 (file-attributes file))))
  1439. "http://www.bookshelf.jp/pukiwiki/pukiwiki.php\
  1440. ?Eshell%A4%F2%BB%C8%A4%A4%A4%B3%A4%CA%A4%B9"
  1441. ;; ;; written by Stefan Reichoer <reichoer@web.de>
  1442. ;; (defun eshell/less (&rest args)
  1443. ;; "Invoke `view-file' on the file.
  1444. ;; \"less +42 foo\" also goes to line 42 in the buffer."
  1445. ;; (if args
  1446. ;; (while args
  1447. ;; (if (string-match "\\`\\+\\([0-9]+\\)\\'" (car args))
  1448. ;; (let* ((line (string-to-number (match-string 1 (pop args))))
  1449. ;; (file (pop args)))
  1450. ;; (view-file file)
  1451. ;; (goto-line line))
  1452. ;; (view-file (pop args))))))
  1453. (defun eshell/o (&optional file)
  1454. (my-x-open (or file ".")))
  1455. ;; (defun eshell/vi (&rest args)
  1456. ;; "Invoke `find-file' on the file.
  1457. ;; \"vi +42 foo\" also goes to line 42 in the buffer."
  1458. ;; (while args
  1459. ;; (if (string-match "\\`\\+\\([0-9]+\\)\\'" (car args))
  1460. ;; (let* ((line (string-to-number (match-string 1 (pop args))))
  1461. ;; (file (pop args)))
  1462. ;; (find-file file)
  1463. ;; (goto-line line))
  1464. ;; (find-file (pop args)))))
  1465. (defun eshell/clear ()
  1466. "Clear the current buffer, leaving one prompt at the top."
  1467. (interactive)
  1468. (let ((inhibit-read-only t))
  1469. (erase-buffer)))
  1470. (defun eshell-clear ()
  1471. (interactive)
  1472. (let ((inhibit-read-only t))
  1473. (erase-buffer)
  1474. (insert (funcall eshell-prompt-function))))
  1475. (defun eshell/d (&optional dirname switches)
  1476. "if first arg is omitted open current directory."
  1477. (dired (or dirname ".") switches))
  1478. (defun eshell/v ()
  1479. (view-mode 1))
  1480. (defun eshell/aaa (&rest args)
  1481. (message "%S"
  1482. args))
  1483. (defun eshell/git (&rest args)
  1484. (if (require 'git-command nil t)
  1485. (git-command (mapconcat 'shell-quote-argument
  1486. args
  1487. " "))
  1488. (apply 'eshell-git-fallback args)))
  1489. (defun eshell-git-fallback (&rest args)
  1490. ""
  1491. (if (member (car args)
  1492. '("di" "diff" "log" "show" "graph"))
  1493. (apply 'eshell-exec-visual "git" args)
  1494. (shell-command-to-string (mapconcat 'shell-quote-argument
  1495. `("git" ,@args)
  1496. " "))
  1497. ;; (eshell-external-command "git" args)
  1498. ))
  1499. (defalias 'eshell/g 'eshell/git)
  1500. (defalias 'eshell/: 'ignore)
  1501. (defalias 'eshell/type 'eshell/which)
  1502. ;; (defalias 'eshell/vim 'eshell/vi)
  1503. (defalias 'eshell/ff 'find-file)
  1504. (defalias 'eshell/q 'eshell/exit)
  1505. (defun eshell-goto-prompt ()
  1506. ""
  1507. (interactive)
  1508. (goto-char (point-max)))
  1509. (defun eshell-delete-char-or-logout (n)
  1510. (interactive "p")
  1511. (if (equal (eshell-get-old-input)
  1512. "")
  1513. (progn
  1514. (insert "exit")
  1515. (eshell-send-input))
  1516. (delete-char n)))
  1517. (defun eshell-kill-input ()
  1518. (interactive)
  1519. (delete-region (point)
  1520. (progn (eshell-bol)
  1521. (point))))
  1522. (defalias 'eshell/logout 'eshell/exit)
  1523. (defun eshell-cd-default-directory (&optional eshell-buffer-or-name)
  1524. "open eshell and change wd
  1525. if arg given, use that eshell buffer, otherwise make new eshell buffer."
  1526. (interactive)
  1527. (let ((dir (expand-file-name default-directory)))
  1528. (switch-to-buffer (or eshell-buffer-or-name
  1529. (eshell t)))
  1530. (unless (equal dir (expand-file-name default-directory))
  1531. ;; (cd dir)
  1532. ;; (eshell-interactive-print (concat "cd " dir "\n"))
  1533. ;; (eshell-emit-prompt)
  1534. (goto-char (point-max))
  1535. (eshell-kill-input)
  1536. (insert "cd " dir)
  1537. (eshell-send-input))))
  1538. (defadvice eshell-next-matching-input-from-input
  1539. ;; do not cycle history
  1540. (around eshell-history-do-not-cycle activate)
  1541. (if (= 0
  1542. (or eshell-history-index
  1543. 0))
  1544. (progn
  1545. (delete-region eshell-last-output-end (point))
  1546. (insert-and-inherit eshell-matching-input-from-input-string)
  1547. (setq eshell-history-index nil))
  1548. ad-do-it))
  1549. (setq eshell-directory-name "~/.emacs.d/eshell/")
  1550. (setq eshell-term-name "eterm-color")
  1551. (setq eshell-scroll-to-bottom-on-input t)
  1552. (setq eshell-cmpl-ignore-case t)
  1553. (setq eshell-cmpl-cycle-completions nil)
  1554. (setq eshell-highlight-prompt nil)
  1555. (setq eshell-ls-initial-args '("-hCFG"
  1556. "--color=auto"
  1557. "--time-style=long-iso")) ; "-hF")
  1558. (setq eshell-prompt-function
  1559. (lambda ()
  1560. (with-temp-buffer
  1561. (let (p1 p2 p3 p4)
  1562. (insert ":: [")
  1563. (setq p1 (point))
  1564. (insert user-login-name
  1565. "@"
  1566. system-name
  1567. )
  1568. (setq p2 (point))
  1569. (insert ":")
  1570. (setq p3 (point))
  1571. (insert (abbreviate-file-name default-directory))
  1572. (setq p4 (point))
  1573. (insert "]")
  1574. (insert "\n:: ")
  1575. (unless (eq 0
  1576. eshell-last-command-status)
  1577. (insert (format "[STATUS:%d] "
  1578. eshell-last-command-status)))
  1579. (insert (if (= (user-uid)
  1580. 0)
  1581. "# "
  1582. "$ "))
  1583. (add-text-properties p1
  1584. p2
  1585. '(face underline))
  1586. (add-text-properties p3
  1587. p4
  1588. '(face underline))
  1589. (buffer-substring (point-min)
  1590. (point-max))))))
  1591. (add-hook 'eshell-mode-hook
  1592. (lambda ()
  1593. ;; (define-key eshell-mode-map (kbd "C-x C-x") (lambda ()
  1594. ;; (interactive)
  1595. ;; (switch-to-buffer (other-buffer))))
  1596. ;; (define-key eshell-mode-map (kbd "C-g") (lambda ()
  1597. ;; (interactive)
  1598. ;; (eshell-goto-prompt)
  1599. ;; (keyboard-quit)))
  1600. (define-key eshell-mode-map (kbd "C-u")
  1601. 'eshell-kill-input)
  1602. (define-key eshell-mode-map (kbd "C-d")
  1603. 'eshell-delete-char-or-logout)
  1604. ;; (define-key eshell-mode-map (kbd "C-l")
  1605. ;; 'eshell-clear)
  1606. (define-key eshell-mode-map (kbd "DEL")
  1607. 'my-eshell-backward-delete-char)
  1608. (define-key eshell-mode-map
  1609. (kbd "C-p") 'eshell-previous-matching-input-from-input)
  1610. (define-key eshell-mode-map
  1611. (kbd "C-n") 'eshell-next-matching-input-from-input)
  1612. (apply 'eshell/addpath exec-path)
  1613. (set (make-local-variable 'scroll-margin) 0)
  1614. ;; (eshell/export "GIT_PAGER=")
  1615. ;; (eshell/export "GIT_EDITOR=")
  1616. (eshell/export "LC_MESSAGES=C")
  1617. (switch-to-buffer (current-buffer)) ; move buffer top of list
  1618. (set (make-local-variable 'hl-line-range-function)
  1619. (lambda ()
  1620. '(0 . 0)))
  1621. (add-to-list 'eshell-virtual-targets
  1622. '("/dev/less"
  1623. (lambda (str)
  1624. (if str
  1625. (with-current-buffer nil)))
  1626. nil))
  1627. ))
  1628. (add-hook 'eshell-mode-hook
  1629. (lambda ()
  1630. (add-to-list 'eshell-visual-commands "vim")
  1631. ;; (add-to-list 'eshell-visual-commands "git")
  1632. (add-to-list 'eshell-output-filter-functions
  1633. 'eshell-truncate-buffer)
  1634. (mapcar (lambda (alias)
  1635. (add-to-list 'eshell-command-aliases-list
  1636. alias))
  1637. '(
  1638. ; ("ll" "ls -l $*")
  1639. ; ("la" "ls -a $*")
  1640. ; ("lla" "ls -al $*")
  1641. ("eless"
  1642. (concat "cat >>> (with-current-buffer "
  1643. "(get-buffer-create \"*eshell output\") "
  1644. "(erase-buffer) "
  1645. "(setq buffer-read-only nil) "
  1646. "(current-buffer)) "
  1647. "(view-buffer (get-buffer \"*eshell output*\"))")
  1648. ))
  1649. )))
  1650. ) ; eval after load eshell
  1651. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1652. ;; frame buffer
  1653. ;; todo: work well when opening files already opened on another window
  1654. (add-hook 'after-make-frame-functions
  1655. (lambda (f)
  1656. (set-window-buffer (frame-selected-window f)
  1657. "*Messages*")))
  1658. (defun make-frame-command-with-name (name)
  1659. "Make frame with NAME specified."
  1660. (interactive "sName for new frame: ")
  1661. (set-frame-parameter (make-frame-command)
  1662. 'name
  1663. name))
  1664. (defvar my-frame-buffer-plist nil)
  1665. (defun my-frame-buffer-add (&optional buf frame)
  1666. "Add BUF to buffer list for FRAME."
  1667. (setq my-frame-buffer-plist
  1668. (plist-put my-frame-buffer-plist
  1669. (or frame
  1670. (selected-frame))
  1671. (let ((lst (my-frame-buffer-get frame)))
  1672. (if lst
  1673. (add-to-list 'lst
  1674. (or buf
  1675. (current-buffer)))
  1676. (list (or buf
  1677. (current-buffer))))))))
  1678. (defun my-frame-buffer-remove (&optional buf frame)
  1679. "Remove BUF from bufferlist for FRAME."
  1680. (setq my-frame-buffer-plist
  1681. (plist-put my-frame-buffer-plist
  1682. (or frame
  1683. (selected-frame))
  1684. (delq (or buf
  1685. (current-buffer))
  1686. (my-frame-buffer-get frame)))))
  1687. (defun my-frame-buffer-get (&optional frame)
  1688. "Get buffer list for FRAME."
  1689. (plist-get my-frame-buffer-plist
  1690. (or frame
  1691. (selected-frame))))
  1692. (defun my-frame-buffer-kill-all-buffer (&optional frame)
  1693. "Kill all buffer of FRAME."
  1694. (mapcar 'kill-buffer
  1695. (my-frame-buffer-get frame)))
  1696. (add-hook 'find-file-hook
  1697. 'my-frame-buffer-add)
  1698. ;; (add-hook 'term-mode-hook
  1699. ;; 'my-frame-buffer-add)
  1700. (add-hook 'eshell-mode-hook
  1701. 'my-frame-buffer-add)
  1702. (add-hook 'Man-mode-hook
  1703. 'my-frame-buffer-add)
  1704. (add-hook 'kill-buffer-hook
  1705. 'my-frame-buffer-remove)
  1706. (add-hook 'delete-frame-functions
  1707. 'my-frame-buffer-kill-all-buffer)
  1708. (defvar my-desktop-terminal "roxterm")
  1709. (defun my-execute-terminal ()
  1710. "Invole terminal program."
  1711. (interactive)
  1712. (if (and (or (eq system-type 'windows-nt)
  1713. window-system)
  1714. my-desktop-terminal
  1715. )
  1716. (let ((process-environment (cons "TERM=xterm" process-environment)))
  1717. (start-process "terminal"
  1718. nil
  1719. my-desktop-terminal))
  1720. (my-term)))
  1721. (defun my-delete-frame-or-kill-emacs ()
  1722. "Delete frame when opening multiple frame, kill Emacs when only one."
  1723. (interactive)
  1724. (if (eq 1
  1725. (length (frame-list)))
  1726. (save-buffers-kill-emacs)
  1727. (delete-frame)))
  1728. ;; (define-key my-prefix-map (kbd "C-s") 'my-execute-terminal)
  1729. (define-key my-prefix-map (kbd "C-f") 'make-frame-command-with-name)
  1730. (global-set-key (kbd "C-x C-c") 'my-delete-frame-or-kill-emacs)
  1731. (define-key my-prefix-map (kbd "C-x C-c") 'save-buffers-kill-emacs)
  1732. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1733. ;; my-term
  1734. (defvar my-term nil
  1735. "My terminal buffer.")
  1736. (defvar my-term-function nil
  1737. "Function to create terminal buffer.
  1738. This function accept no argument and return newly created buffer of terminal.")
  1739. (defun my-term (&optional arg)
  1740. "Open terminal buffer and return that buffer.
  1741. ARG is ignored."
  1742. (interactive "P")
  1743. (if (and my-term
  1744. (buffer-name my-term))
  1745. (pop-to-buffer my-term)
  1746. (setq my-term
  1747. (save-window-excursion
  1748. (funcall my-term-function)))
  1749. (and my-term
  1750. (my-term))))
  1751. ;; (setq my-term-function
  1752. ;; (lambda ()
  1753. ;; (if (eq system-type 'windows-nt)
  1754. ;; (eshell)
  1755. ;; (if (require 'multi-term nil t)
  1756. ;; (multi-term)
  1757. ;; (ansi-term shell-file-name)))))
  1758. (setq my-term-function 'eshell)
  1759. (define-key my-prefix-map (kbd "C-s") 'my-term)
  1760. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1761. ;; x open
  1762. (defvar my-filer nil)
  1763. (setq my-filer (or (executable-find "pcmanfm")
  1764. (executable-find "nautilus")))
  1765. (defun my-x-open (file)
  1766. "open FILE."
  1767. (interactive "FOpen File: ")
  1768. (setq file (expand-file-name file))
  1769. (message "Opening %s..." file)
  1770. (cond ((eq system-type 'windows-nt)
  1771. (call-process "cmd.exe" nil 0 nil
  1772. "/c" "start" "" (convert-standard-filename file)))
  1773. ((eq system-type 'darwin)
  1774. (call-process "open" nil 0 nil file))
  1775. ((getenv "DISPLAY")
  1776. (call-process (or my-filer "xdg-open") nil 0 nil file))
  1777. (t
  1778. (find-file file))
  1779. )
  1780. ;; (recentf-add-file file)
  1781. (message "Opening %s...done" file))
  1782. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1783. ;; misc funcs
  1784. (defun memo (&optional dir)
  1785. "Open memo.txt in DIR."
  1786. (interactive)
  1787. (pop-to-buffer (find-file-noselect (concat (if dir
  1788. (file-name-as-directory dir)
  1789. "")
  1790. "memo.txt"))))
  1791. (defvar my-rgrep-alist
  1792. `(
  1793. ;; gnu global
  1794. ("global"
  1795. (and (require 'gtags nil t)
  1796. (gtags-get-rootpath))
  1797. "global --result grep ")
  1798. ;; git grep
  1799. ("gitgrep"
  1800. (eq 0
  1801. (shell-command "git rev-parse --git-dir"))
  1802. "git --no-pager -c color.grep=false grep -nH -e ")
  1803. ;; the silver searcher
  1804. ("ag"
  1805. (executable-find "ag")
  1806. "ag --nocolor --nogroup --nopager ")
  1807. ;; ack
  1808. ("ack"
  1809. (executable-find "ack")
  1810. "ack --nocolor --nogroup --nopager ")
  1811. ;; grep
  1812. ("grep"
  1813. t
  1814. ,(concat "find . "
  1815. "-path '*/.git' -prune -o "
  1816. "-path '*/.svn' -prune -o "
  1817. "-type f -print0 | "
  1818. "xargs -0 grep -nH -e "))
  1819. )
  1820. "Alist of rgrep command.
  1821. Each element is in the form like (NAME SEXP COMMAND), where SEXP returns the
  1822. condition to choose COMMAND when evaluated.")
  1823. (defvar my-rgrep-default nil
  1824. "Default command name for my-rgrep.")
  1825. (defun my-rgrep-grep-command (&optional name alist)
  1826. "Return recursive grep command for current directory or nil.
  1827. If NAME is given, use that without testing.
  1828. Commands are searched from ALIST."
  1829. (if alist
  1830. (if name
  1831. ;; if name is given search that from alist and return the command
  1832. (nth 2 (assoc name
  1833. alist))
  1834. ;; if name is not given try test in 1th elem
  1835. (let ((car (car alist))
  1836. (cdr (cdr alist)))
  1837. (if (eval (nth 1 car))
  1838. ;; if the condition is true return the command
  1839. (nth 2 car)
  1840. ;; try next one
  1841. (and cdr
  1842. (my-rgrep-grep-command name cdr)))))
  1843. ;; if alist is not given set default value
  1844. (my-rgrep-grep-command name my-rgrep-alist)))
  1845. (my-rgrep-grep-command "ag" nil)
  1846. (defun my-rgrep (command-args)
  1847. "My recursive grep. Run COMMAND-ARGS."
  1848. (interactive (let ((cmd (my-rgrep-grep-command my-rgrep-default
  1849. nil)))
  1850. (if cmd
  1851. (list (read-shell-command "grep command: "
  1852. cmd
  1853. 'grep-find-history))
  1854. (error "my-rgrep: Command for rgrep not found")
  1855. )))
  1856. (compilation-start command-args
  1857. 'grep-mode))
  1858. ;; (defun my-rgrep-symbol-at-point (command-args)
  1859. ;; "My recursive grep. Run COMMAND-ARGS."
  1860. ;; (interactive (list (read-shell-command "grep command: "
  1861. ;; (concat (my-rgrep-grep-command)
  1862. ;; " "
  1863. ;; (thing-at-point 'symbol))
  1864. ;; 'grep-find-history)))
  1865. ;; (compilation-start command-args
  1866. ;; 'grep-mode))
  1867. (defun my-rgrep-ack ()
  1868. "My recursive grep by ack."
  1869. (interactive)
  1870. (let ((my-rgrep-default "ack"))
  1871. (if (called-interactively-p 'any)
  1872. (call-interactively 'my-rgrep)
  1873. (error "Not intended to be called noninteractively. Use `my-rgrep'"))))
  1874. (defun my-rgrep-ag ()
  1875. "My recursive grep by ack."
  1876. (interactive)
  1877. (let ((my-rgrep-default "ag"))
  1878. (if (called-interactively-p 'any)
  1879. (call-interactively 'my-rgrep)
  1880. (error "Not intended to be called noninteractively. Use `my-rgrep'"))))
  1881. (defun my-rgrep-gitgrep ()
  1882. "My recursive grep by ack."
  1883. (interactive)
  1884. (let ((my-rgrep-default "gitgrep"))
  1885. (if (called-interactively-p 'any)
  1886. (call-interactively 'my-rgrep)
  1887. (error "Not intended to be called noninteractively. Use `my-rgrep'"))))
  1888. (defun my-rgrep-grep ()
  1889. "My recursive grep by ack."
  1890. (interactive)
  1891. (let ((my-rgrep-default "grep"))
  1892. (if (called-interactively-p 'any)
  1893. (call-interactively 'my-rgrep)
  1894. (error "Not intended to be called noninteractively. Use `my-rgrep'"))))
  1895. (defun my-rgrep-global ()
  1896. "My recursive grep by ack."
  1897. (interactive)
  1898. (let ((my-rgrep-default "global"))
  1899. (if (called-interactively-p 'any)
  1900. (call-interactively 'my-rgrep)
  1901. (error "Not intended to be called noninteractively. Use `my-rgrep'"))))
  1902. (define-key ctl-x-map "s" 'my-rgrep)
  1903. ;; (defun make ()
  1904. ;; "Run \"make -k\" in current directory."
  1905. ;; (interactive)
  1906. ;; (compile "make -k"))
  1907. (defalias 'make 'compile)
  1908. (defvar sed-in-place-history nil
  1909. "History of `sed-in-place'.")
  1910. (defvar sed-in-place-command "sed --in-place=.bak -e")
  1911. (defun sed-in-place (command)
  1912. "Issue sed in place COMMAND."
  1913. (interactive (list (read-shell-command "sed in place: "
  1914. (concat sed-in-place-command " ")
  1915. 'sed-in-place-history)))
  1916. (shell-command command
  1917. "*sed in place*"))
  1918. (defun dired-do-sed-in-place (&optional arg)
  1919. "Issue sed in place dired. If ARG is given, use the next ARG files."
  1920. (interactive "p")
  1921. (require 'dired-aux)
  1922. (let* ((files (dired-get-marked-files t arg))
  1923. (expr (dired-mark-read-string "Run sed-in-place for %s: "
  1924. nil
  1925. 'sed-in-place
  1926. arg
  1927. files)))
  1928. (if (equal expr
  1929. "")
  1930. (error "No expression specified")
  1931. (shell-command (concat sed-in-place-command
  1932. " '"
  1933. expr
  1934. "' "
  1935. (mapconcat 'shell-quote-argument
  1936. files
  1937. " "))
  1938. "*sed in place*"))))
  1939. (defun dir-show (&optional dir)
  1940. "Show DIR list."
  1941. (interactive)
  1942. (let ((bf (get-buffer-create "*dir show*"))
  1943. (list-directory-brief-switches "-C"))
  1944. (with-current-buffer bf
  1945. (list-directory (or nil
  1946. default-directory)
  1947. nil))
  1948. ))
  1949. (defun my-convmv-sjis2utf8-test ()
  1950. "Run `convmv -r -f sjis -t utf8 *'.
  1951. this is test, does not rename files."
  1952. (interactive)
  1953. (shell-command "convmv -r -f sjis -t utf8 *"))
  1954. (defun my-convmv-sjis2utf8-notest ()
  1955. "Run `convmv -r -f sjis -t utf8 * --notest'."
  1956. (interactive)
  1957. (shell-command "convmv -r -f sjis -t utf8 * --notest"))
  1958. (defun kill-ring-save-buffer-file-name ()
  1959. "Get current filename."
  1960. (interactive)
  1961. (let ((file buffer-file-name))
  1962. (if file
  1963. (progn (kill-new file)
  1964. (message file))
  1965. (message "not visiting file."))))
  1966. (defvar kill-ring-buffer-name "*kill-ring*"
  1967. "Buffer name for `kill-ring-buffer'.")
  1968. (defun open-kill-ring-buffer ()
  1969. "Open kill- ring buffer."
  1970. (interactive)
  1971. (pop-to-buffer
  1972. (with-current-buffer (get-buffer-create kill-ring-buffer-name)
  1973. (erase-buffer)
  1974. (yank)
  1975. (text-mode)
  1976. (current-local-map)
  1977. (goto-char (point-min)))))
  1978. ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1979. ;; ;; savage emacs
  1980. ;; ;; when enabled emacs fails to complete
  1981. ;; ;; http://e-arrows.sakura.ne.jp/2010/05/emacs-should-be-more-savage.html
  1982. ;; (defadvice message (before message-for-stupid (arg &rest arg2) activate)
  1983. ;; (setq arg
  1984. ;; (concat arg
  1985. ;; (if (eq nil
  1986. ;; (string-match "\\. *$"
  1987. ;; arg))
  1988. ;; ".")
  1989. ;; " Stupid!")))
  1990. (defvar my-system-info
  1991. nil
  1992. "System info in the form of \"[user@host] \".")
  1993. (setq my-system-info
  1994. (concat "["
  1995. user-login-name
  1996. "@"
  1997. (car (split-string system-name
  1998. "\\."))
  1999. "] "))
  2000. (defadvice read-from-minibuffer (before info-in-prompt activate)
  2001. "Show system info when use `read-from-minibuffer'."
  2002. (ad-set-arg 0
  2003. (concat my-system-info
  2004. (ad-get-arg 0))))
  2005. (defadvice read-string (before info-in-prompt activate)
  2006. "Show system info when use `read-string'."
  2007. (ad-set-arg 0
  2008. (concat my-system-info
  2009. (ad-get-arg 0))))
  2010. (when (< emacs-major-version 24)
  2011. (defadvice completing-read (before info-in-prompt activate)
  2012. "Show system info when use `completing-read'."
  2013. (ad-set-arg 0
  2014. (concat my-system-info
  2015. (ad-get-arg 0)))))
  2016. (defun my-real-function-subr-p (function)
  2017. "Return t if FUNCTION is a built-in function even if it is advised."
  2018. (let* ((advised (and (symbolp function)
  2019. (featurep 'advice)
  2020. (ad-get-advice-info function)))
  2021. (real-function
  2022. (or (and advised (let ((origname (cdr (assq 'origname advised))))
  2023. (and (fboundp origname)
  2024. origname)))
  2025. function))
  2026. (def (if (symbolp real-function)
  2027. (symbol-function real-function)
  2028. function)))
  2029. (subrp def)))
  2030. ;; (my-real-function-subr-p 'my-real-function-subr-p)
  2031. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2032. ;; isdcv
  2033. ;; TODO: remove dependant of sdcv, and change the name to something like
  2034. ;; `ilookup`
  2035. ;; TODO: enter to lookup word at point
  2036. (defvar ilookup-prompt ">>> "
  2037. "Prompt string for ilookup input.")
  2038. (defvar ilookup-interval 0.5
  2039. "Time in second to show sdcv result for current input.")
  2040. (defvar ilookup-current-prompt-point nil
  2041. "Point of beginning of current prompt.")
  2042. (make-variable-buffer-local 'ilookup-current-prompt-point)
  2043. (defvar ilookup-mode-map
  2044. (let ((map (make-sparse-keymap)))
  2045. (define-key map (kbd "C-m") 'ilookup-enter)
  2046. (define-key map (kbd "C-u") 'ilookup-kill-input)
  2047. (define-key map (kbd "C-a") 'ilookup-goto-bol)
  2048. map))
  2049. (define-derived-mode ilookup-mode fundamental-mode
  2050. "iLookUp"
  2051. "Major mode for incremental sdcv buffer."
  2052. (set (make-local-variable 'font-lock-function)
  2053. 'ignore))
  2054. (defun ilookup-enter ()
  2055. "Enter function for `ilookup-mode'.
  2056. Freeze current input and show next prompt."
  2057. (interactive)
  2058. (goto-char (point-max))
  2059. (unless (eq (point)
  2060. (point-at-bol))
  2061. (newline))
  2062. (setq ilookup-current-prompt-point
  2063. (point))
  2064. (insert ilookup-prompt))
  2065. (defun ilookup-kill-input ()
  2066. "Delete `ilookup-bol' to current point."
  2067. (interactive)
  2068. (delete-region (ilookup-bol)
  2069. (point)))
  2070. (defun ilookup-goto-bol ()
  2071. "Go to ilookup bol."
  2072. (interactive)
  2073. (goto-char (ilookup-bol)))
  2074. (defvar ilookup-sdcv-command "sdcv -n '%s'"
  2075. "Command of sdcv.")
  2076. (setq ilookup-sdcv-command "sdcv -n -u jmdict-en-ja '%s'")
  2077. (defvar ilookup-buffer nil
  2078. "Pointer to ilookup buffer.")
  2079. (defvar ilookup--timer nil
  2080. "Idle timer object for ilookup.")
  2081. (defvar ilookup--last-input nil
  2082. "Last input queryed.")
  2083. (defvar ilookup-default "sdcv"
  2084. "Default command for ilookup.
  2085. This value should be a key of `ilookup-alist'.")
  2086. (defvar ilookup-alist
  2087. '(
  2088. ("sdcv" . (lambda (word)
  2089. (shell-command-to-string (format "sdcv -n '%s'"
  2090. word))))
  2091. )
  2092. "Alist of ilookup functions.
  2093. Each element should be in the form of (NAME . FUNCTION).
  2094. FUNCTION must accept one argument as word to search and return the string of
  2095. result for that word.")
  2096. (setq ilookup-alist
  2097. '(
  2098. ("en" . (lambda (word)
  2099. (shell-command-to-string
  2100. (format "sdcv -n -u dictd_www.dict.org_gcide '%s'"
  2101. word))))
  2102. ("ja" . (lambda (word)
  2103. (shell-command-to-string
  2104. (format "sdcv -n -u jmdict-en-ja '%s'"
  2105. word))))
  2106. ("alc" . (lambda (word)
  2107. (shell-command-to-string
  2108. (format "alc '%s' | head -n 20"
  2109. word))))
  2110. ("app" . (lambda (word)
  2111. (shell-command-to-string
  2112. (format "dict_app '%s'"
  2113. word))))
  2114. ;; letters broken
  2115. ("ms" .
  2116. (lambda (word)
  2117. (let ((url (concat "http://api.microsofttranslator.com/V2/Ajax.svc/"
  2118. "Translate?appId=%s&text=%s&to=%s"))
  2119. (apikey "3C9778666C5BA4B406FFCBEE64EF478963039C51")
  2120. (target "ja")
  2121. (eword (url-hexify-string word)))
  2122. (with-current-buffer (url-retrieve-synchronously
  2123. (format url
  2124. apikey
  2125. eword
  2126. target))
  2127. (message "")
  2128. (goto-char (point-min))
  2129. (search-forward-regexp "^$"
  2130. nil
  2131. t)
  2132. (url-unhex-string (buffer-substring-no-properties (point)
  2133. (point-max)))))))
  2134. ))
  2135. ;; (funcall (cdr (assoc "ms"
  2136. ;; ilookup-alist))
  2137. ;; "dictionary")
  2138. ;; (switch-to-buffer (url-retrieve-synchronously "http://api.microsofttranslator.com/V2/Ajax.svc/Translate?appId=3C9778666C5BA4B406FFCBEE64EF478963039C51&text=dictionary&to=ja"))
  2139. ;; (switch-to-buffer (url-retrieve-synchronously "http://google.com"))
  2140. (setq ilookup-default "ja")
  2141. (defun ilookup--timer-add ()
  2142. "Entry idle timer for ilookup."
  2143. (and (eq ilookup-buffer
  2144. (current-buffer))
  2145. ;; do not duplicate timer
  2146. (not ilookup--timer)
  2147. (setq ilookup--timer (run-with-idle-timer ilookup-interval
  2148. t
  2149. 'ilookup--timer-function))))
  2150. (defun ilookup--timer-remove ()
  2151. "Remove idle timer for ilookup."
  2152. ;; remote only when killing buffer is ilookup buffer
  2153. (and (eq ilookup-buffer
  2154. (current-buffer))
  2155. ilookup--timer
  2156. (cancel-timer ilookup--timer)))
  2157. (defun ilookup-bol ()
  2158. "Return point to bol ignoring prompt."
  2159. (save-excursion
  2160. (beginning-of-line)
  2161. (or (search-forward ilookup-prompt
  2162. (point-at-eol)
  2163. t)
  2164. (point))))
  2165. (defun ilookup--get-output-start ()
  2166. "Return point where outputs should be inserted.
  2167. This function insert newline if required."
  2168. (when ilookup-current-prompt-point
  2169. (save-excursion
  2170. (goto-char ilookup-current-prompt-point)
  2171. (forward-line 1)
  2172. (when (eq (line-number-at-pos)
  2173. (line-number-at-pos ilookup-current-prompt-point))
  2174. (end-of-line)
  2175. (newline))
  2176. (point-at-bol))))
  2177. (defun ilookup--timer-function ()
  2178. "Get entry for current ilookup input."
  2179. (let ((input (ilookup--get-input))
  2180. (outpoint (ilookup--get-output-start)))
  2181. (and input
  2182. ;; do not query same word twice
  2183. (not (equal ilookup--last-input
  2184. input))
  2185. outpoint
  2186. (let* (
  2187. ;; colon sepatated list of input
  2188. (inputl (split-string input
  2189. ":"))
  2190. ;; funcname for `ilookup-alist'
  2191. (fname (if (eq (length inputl)
  2192. 2)
  2193. (car inputl)
  2194. ilookup-default))
  2195. (func (cdr (assoc fname
  2196. ilookup-alist)))
  2197. (word (if (eq (length inputl)
  2198. 2)
  2199. (nth 1
  2200. inputl)
  2201. (car inputl))))
  2202. (save-excursion
  2203. (setq ilookup--last-input input)
  2204. (goto-char outpoint)
  2205. (delete-region (point)
  2206. (point-max))
  2207. (insert (if func
  2208. (funcall func
  2209. word)
  2210. (format "No func found for `%s'"
  2211. fname))))))))
  2212. (defun ilookup--get-input ()
  2213. "Get current input for ilookup buffer.
  2214. Return nil if current position is not on prompt line."
  2215. (and (eq ilookup-buffer
  2216. (current-buffer))
  2217. (eq (line-number-at-pos)
  2218. (line-number-at-pos ilookup-current-prompt-point))
  2219. (buffer-substring-no-properties (ilookup-bol)
  2220. (point-at-eol))))
  2221. (defun ilookup-open ()
  2222. "Open ilookup buffer."
  2223. (interactive)
  2224. (if ilookup-buffer
  2225. (pop-to-buffer ilookup-buffer)
  2226. (with-current-buffer (setq ilookup-buffer
  2227. (get-buffer-create "*ilookup*"))
  2228. (ilookup-mode)
  2229. (font-lock-mode t)
  2230. (ilookup-enter)
  2231. (ilookup--timer-add)
  2232. (add-hook 'kill-buffer-hook
  2233. 'ilookup--timer-remove)
  2234. )
  2235. (pop-to-buffer ilookup-buffer)))
  2236. ;;; emacs.el ends here