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.
 
 
 
 
 
 

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