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.
 
 
 
 
 
 

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