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.
 
 
 
 
 
 

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