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.
 
 
 
 
 
 

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