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.
 
 
 
 
 
 

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