Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

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