Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

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