Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

2686 wiersze
93 KiB

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