Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

2688 Zeilen
93 KiB

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