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

2662 rader
92 KiB

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