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