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.
 
 
 
 
 
 

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