You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

2687 lines
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. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'markdown-mode))
  997. (setq markdown-command (or (executable-find "markdown")
  998. (executable-find "markdown.pl")))
  999. (add-hook 'markdown-mode-hook
  1000. (lambda ()
  1001. (outline-minor-mode 1)
  1002. (flyspell-mode)
  1003. (set (make-local-variable 'comment-start) ";"))))
  1004. ;; c-mode
  1005. ;; http://www.emacswiki.org/emacs/IndentingC
  1006. ;; http://en.wikipedia.org/wiki/Indent_style
  1007. ;; http://d.hatena.ne.jp/emergent/20070203/1170512717
  1008. (when (autoload-eval-lazily 'cc-vars
  1009. nil
  1010. (add-to-list 'c-default-style
  1011. '(c-mode . "k&r"))
  1012. (add-to-list 'c-default-style
  1013. '(c++-mode . "k&r"))
  1014. (add-hook 'c-mode-common-hook
  1015. (lambda ()
  1016. ;; why c-basic-offset in k&r style defaults to 5 ???
  1017. (setq c-basic-offset 4
  1018. indent-tabs-mode nil)
  1019. ;; (set-face-foreground 'font-lock-keyword-face "blue")
  1020. (c-toggle-hungry-state -1)
  1021. ;; (and (require 'gtags nil t)
  1022. ;; (gtags-mode 1))
  1023. ))))
  1024. (when (autoload-eval-lazily 'js2-mode)
  1025. ;; currently do not use js2-mode
  1026. ;; (add-to-list 'auto-mode-alist '("\\.js\\'" . js2-mode))
  1027. ;; (add-to-list 'auto-mode-alist '("\\.jsm\\'" . js2-mode))
  1028. (add-hook 'js2-mode-hook
  1029. (lambda ()
  1030. (define-key js2-mode-map (kbd "C-m") (lambda ()
  1031. (interactive)
  1032. (js2-enter-key)
  1033. (indent-for-tab-command)))
  1034. ;; (add-hook (kill-local-variable 'before-save-hook)
  1035. ;; 'js2-before-save)
  1036. ;; (add-hook 'before-save-hook
  1037. ;; 'my-indent-buffer
  1038. ;; nil
  1039. ;; t)
  1040. )))
  1041. (eval-after-load "js"
  1042. (setq js-indent-level 2))
  1043. (add-to-list 'interpreter-mode-alist
  1044. '("node" . js-mode))
  1045. (when (autoload-eval-lazily 'flymake-jslint
  1046. '(flymake-jslint-load))
  1047. (autoload-eval-lazily 'js nil
  1048. (add-hook 'js-mode-hook
  1049. 'flymake-jslint-load)))
  1050. (require 'js-doc nil t)
  1051. (add-hook 'haskell-mode-hook 'turn-on-haskell-indentation)
  1052. (when (require 'uniquify nil t)
  1053. (setq uniquify-buffer-name-style 'post-forward-angle-brackets)
  1054. (setq uniquify-ignore-buffers-re "*[^*]+*")
  1055. (setq uniquify-min-dir-content 1))
  1056. (add-hook 'view-mode-hook
  1057. (lambda()
  1058. (define-key view-mode-map "j" 'scroll-up-line)
  1059. (define-key view-mode-map "k" 'scroll-down-line)
  1060. (define-key view-mode-map "v" 'toggle-read-only)
  1061. (define-key view-mode-map "q" 'bury-buffer)
  1062. ;; (define-key view-mode-map "/" 'nonincremental-re-search-forward)
  1063. ;; (define-key view-mode-map "?" 'nonincremental-re-search-backward)
  1064. ;; (define-key view-mode-map
  1065. ;; "n" 'nonincremental-repeat-search-forward)
  1066. ;; (define-key view-mode-map
  1067. ;; "N" 'nonincremental-repeat-search-backward)
  1068. (define-key view-mode-map "/" 'isearch-forward-regexp)
  1069. (define-key view-mode-map "?" 'isearch-backward-regexp)
  1070. (define-key view-mode-map "n" 'isearch-repeat-forward)
  1071. (define-key view-mode-map "N" 'isearch-repeat-backward)
  1072. (define-key view-mode-map (kbd "C-m") 'my-rgrep-symbol-at-point)
  1073. ))
  1074. (global-set-key "\M-r" 'view-mode)
  1075. ;; (setq view-read-only t)
  1076. ;; (defun my-view-mode-search-word (word)
  1077. ;; "Search for word current directory and subdirectories.
  1078. ;; If called intearctively, find word at point."
  1079. ;; (interactive (list (thing-at-point 'symbol)))
  1080. ;; (if word
  1081. ;; (if (and (require 'gtags nil t)
  1082. ;; (gtags-get-rootpath))
  1083. ;; (gtags-goto-tag word "s")
  1084. ;; (my-rgrep word))
  1085. ;; (message "No word at point.")
  1086. ;; nil))
  1087. (add-hook 'Man-mode-hook
  1088. (lambda ()
  1089. (view-mode 1)
  1090. (setq truncate-lines nil)))
  1091. (setq Man-notify-method (if window-system
  1092. 'newframe
  1093. 'aggressive))
  1094. (setq woman-cache-filename (expand-file-name (concat user-emacs-directory
  1095. "woman_cache.el")))
  1096. (defalias 'man 'woman)
  1097. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1098. ;; python
  1099. (when (autoload-eval-lazily 'python '(python-mode))
  1100. (setq python-python-command (or (executable-find "python3")
  1101. (executable-find "python")))
  1102. ;; (defun my-python-run-as-command ()
  1103. ;; ""
  1104. ;; (interactive)
  1105. ;; (shell-command (concat python-python-command " " buffer-file-name)))
  1106. (defun my-python-display-python-buffer ()
  1107. ""
  1108. (interactive)
  1109. (set-window-text-height (display-buffer python-buffer
  1110. t)
  1111. 7))
  1112. (add-hook 'python-mode-hook
  1113. (lambda ()
  1114. (define-key python-mode-map
  1115. (kbd "C-c C-e") 'my-python-run-as-command)
  1116. (define-key python-mode-map
  1117. (kbd "C-c C-b") 'my-python-display-python-buffer)
  1118. (define-key python-mode-map (kbd "C-m") 'newline-and-indent)))
  1119. (add-hook 'inferior-python-mode-hook
  1120. (lambda ()
  1121. (my-python-display-python-buffer)
  1122. (define-key inferior-python-mode-map
  1123. (kbd "<up>") 'comint-previous-input)
  1124. (define-key inferior-python-mode-map
  1125. (kbd "<down>") 'comint-next-input))))
  1126. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1127. ;; GNU GLOBAL(gtags)
  1128. ;; http://uguisu.skr.jp/Windows/gtags.html
  1129. ;; http://eigyr.dip.jp/gtags.html
  1130. ;; http://cha.la.coocan.jp/doc/gnu_global.html
  1131. (let ((d "/opt/local/share/gtags/"))
  1132. (and (file-directory-p d)
  1133. (add-to-list 'load-path
  1134. d)))
  1135. (when (autoload-eval-lazily 'gtags '(gtags-mode))
  1136. (add-hook 'gtags-mode-hook
  1137. (lambda ()
  1138. (view-mode gtags-mode)
  1139. (setq gtags-select-buffer-single t)
  1140. ;; (local-set-key "\M-t" 'gtags-find-tag)
  1141. ;; (local-set-key "\M-r" 'gtags-find-rtag)
  1142. ;; (local-set-key "\M-s" 'gtags-find-symbol)
  1143. ;; (local-set-key "\C-t" 'gtags-pop-stack)
  1144. (define-key gtags-mode-map (kbd "C-x t h")
  1145. 'gtags-find-tag-from-here)
  1146. (define-key gtags-mode-map (kbd "C-x t t") 'gtags-find-tag)
  1147. (define-key gtags-mode-map (kbd "C-x t r") 'gtags-find-rtag)
  1148. (define-key gtags-mode-map (kbd "C-x t s") 'gtags-find-symbol)
  1149. (define-key gtags-mode-map (kbd "C-x t p") 'gtags-find-pattern)
  1150. (define-key gtags-mode-map (kbd "C-x t f") 'gtags-find-file)
  1151. (define-key gtags-mode-map (kbd "C-x t b") 'gtags-pop-stack) ;back
  1152. ))
  1153. (add-hook 'gtags-select-mode-hook
  1154. (lambda ()
  1155. (define-key gtags-select-mode-map (kbd "C-m") 'gtags-select-tag)
  1156. ))
  1157. )
  1158. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1159. ;; term mode
  1160. ;; (setq multi-term-program shell-file-name)
  1161. (when (autoload-eval-lazily 'multi-term)
  1162. (setq multi-term-switch-after-close nil)
  1163. (setq multi-term-dedicated-select-after-open-p t)
  1164. (setq multi-term-dedicated-window-height 20))
  1165. (when (autoload-eval-lazily 'term '(term ansi-term))
  1166. (defun my-term-quit-or-send-raw ()
  1167. ""
  1168. (interactive)
  1169. (if (get-buffer-process (current-buffer))
  1170. (call-interactively 'term-send-raw)
  1171. (kill-buffer)))
  1172. ;; http://d.hatena.ne.jp/goinger/20100416/1271399150
  1173. ;; (setq term-ansi-default-program shell-file-name)
  1174. (add-hook 'term-setup-hook
  1175. (lambda ()
  1176. (setq term-display-table (make-display-table))))
  1177. (add-hook 'term-mode-hook
  1178. (lambda ()
  1179. (unless (memq (current-buffer)
  1180. (and (featurep 'multi-term)
  1181. ;; current buffer is not multi-term buffer
  1182. multi-term-buffer-list))
  1183. ;; (define-key term-raw-map "\C-q" 'move-beginning-of-line)
  1184. ;; (define-key term-raw-map "\C-r" 'term-send-raw)
  1185. ;; (define-key term-raw-map "\C-s" 'term-send-raw)
  1186. ;; (define-key term-raw-map "\C-f" 'forward-char)
  1187. ;; (define-key term-raw-map "\C-b" 'backward-char)
  1188. ;; (define-key term-raw-map "\C-t" 'set-mark-command)
  1189. (define-key term-raw-map
  1190. "\C-x" (lookup-key (current-global-map) "\C-x"))
  1191. (define-key term-raw-map
  1192. "\C-z" (lookup-key (current-global-map) "\C-z"))
  1193. )
  1194. ;; (define-key term-raw-map "\C-xl" 'term-line-mode)
  1195. ;; (define-key term-mode-map "\C-xc" 'term-char-mode)
  1196. (define-key term-raw-map (kbd "<up>") 'scroll-down-line)
  1197. (define-key term-raw-map (kbd "<down>") 'scroll-up-line)
  1198. (define-key term-raw-map (kbd "<right>") 'scroll-up)
  1199. (define-key term-raw-map (kbd "<left>") 'scroll-down)
  1200. (define-key term-raw-map (kbd "C-p") 'term-send-raw)
  1201. (define-key term-raw-map (kbd "C-n") 'term-send-raw)
  1202. (define-key term-raw-map "q" 'my-term-quit-or-send-raw)
  1203. ;; (define-key term-raw-map (kbd "ESC") 'term-send-raw)
  1204. (define-key term-raw-map [delete] 'term-send-raw)
  1205. (define-key term-raw-map (kbd "DEL") 'term-send-backspace)
  1206. (define-key term-raw-map "\C-y" 'term-paste)
  1207. (define-key term-raw-map
  1208. "\C-c" 'term-send-raw) ;; 'term-interrupt-subjob)
  1209. '(define-key term-mode-map (kbd "C-x C-q") 'term-pager-toggle)
  1210. ;; (dolist (key '("<up>" "<down>" "<right>" "<left>"))
  1211. ;; (define-key term-raw-map (read-kbd-macro key) 'term-send-raw))
  1212. ;; (define-key term-raw-map "\C-d" 'delete-char)
  1213. (set (make-local-variable 'scroll-margin) 0)
  1214. ;; (set (make-local-variable 'cua-enable-cua-keys) nil)
  1215. ;; (cua-mode 0)
  1216. ;; (and cua-mode
  1217. ;; (local-unset-key (kbd "C-c")))
  1218. ;; (define-key cua--prefix-override-keymap
  1219. ;;"\C-c" 'term-interrupt-subjob)
  1220. (set (make-local-variable 'hl-line-range-function)
  1221. (lambda ()
  1222. '(0 . 0)))
  1223. ))
  1224. ;; (add-hook 'term-exec-hook 'forward-char)
  1225. )
  1226. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1227. ;; buffer switching
  1228. (when (autoload-eval-lazily 'bs '(bs-show)
  1229. ;; (add-to-list 'bs-configurations
  1230. ;; '("processes" nil get-buffer-process ".*" nil nil))
  1231. (add-to-list 'bs-configurations
  1232. '("files-and-terminals" nil nil nil
  1233. (lambda (buf)
  1234. (and (bs-visits-non-file buf)
  1235. (save-excursion
  1236. (set-buffer buf)
  1237. (not (memq major-mode
  1238. '(term-mode
  1239. eshell-mode))))))))
  1240. ;; (setq bs-configurations (list
  1241. ;; '("processes" nil get-buffer-process ".*" nil nil)
  1242. ;; '("files-and-scratch" "^\\*scratch\\*$" nil nil
  1243. ;; bs-visits-non-file bs-sort-buffer-interns-are-last)))
  1244. )
  1245. ;; (global-set-key "\C-x\C-b" 'bs-show)
  1246. (defalias 'list-buffers 'bs-show)
  1247. (setq bs-default-configuration "files-and-terminals")
  1248. (setq bs-default-sort-name "by nothing")
  1249. (add-hook 'bs-mode-hook
  1250. (lambda ()
  1251. ;; (setq bs-default-configuration "files")
  1252. ;; (and bs--show-all
  1253. ;; (call-interactively 'bs-toggle-show-all))
  1254. (set (make-local-variable 'scroll-margin) 0))))
  1255. ;;(iswitchb-mode 1)
  1256. (icomplete-mode)
  1257. (defun iswitchb-buffer-display-other-window ()
  1258. "Do iswitchb in other window."
  1259. (interactive)
  1260. (let ((iswitchb-default-method 'display))
  1261. (call-interactively 'iswitchb-buffer)))
  1262. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1263. ;; sdic
  1264. (when (autoload-eval-lazily 'sdic '(sdic-describe-word-at-point))
  1265. ;; (define-key my-prefix-map "\C-w" 'sdic-describe-word)
  1266. (define-key my-prefix-map "\C-t" 'sdic-describe-word-at-point-echo)
  1267. (defun sdic-describe-word-at-point-echo ()
  1268. ""
  1269. (interactive)
  1270. (save-window-excursion
  1271. (sdic-describe-word-at-point))
  1272. (save-excursion
  1273. (set-buffer sdic-buffer-name)
  1274. (message (buffer-substring (point-min)
  1275. (progn (goto-char (point-min))
  1276. (or (and (re-search-forward "^\\w"
  1277. nil
  1278. t
  1279. 4)
  1280. (progn (previous-line) t)
  1281. (point-at-eol))
  1282. (point-max)))))))
  1283. (setq sdic-eiwa-dictionary-list '((sdicf-client "/usr/share/dict/gene.sdic")))
  1284. (setq sdic-waei-dictionary-list
  1285. '((sdicf-client "/usr/share/dict/jedict.sdic" (add-keys-to-headword t))))
  1286. (setq sdic-disable-select-window t)
  1287. (setq sdic-window-height 7))
  1288. ;;;;;;;;;;;;;;;;;;;;;;;;
  1289. ;; ilookup
  1290. (when (fetch-library
  1291. "https://raw.github.com/10sr/emacs-lisp/master/ilookup.el"
  1292. t)
  1293. (autoload-eval-lazily 'ilookup
  1294. '(ilookup-open)
  1295. (setq ilookup-dict-alist
  1296. '(
  1297. ("en" . (lambda (word)
  1298. (shell-command-to-string
  1299. (format "sdcv -n -u dictd_www.dict.org_gcide '%s'"
  1300. word))))
  1301. ("ja" . (lambda (word)
  1302. (shell-command-to-string
  1303. (format "sdcv -n -u EJ-GENE95 -u jmdict-en-ja '%s'"
  1304. word))))
  1305. ("jaj" . (lambda (word)
  1306. (shell-command-to-string
  1307. (format "sdcv -n -u jmdict-en-ja '%s'"
  1308. word))))
  1309. ("jag" .
  1310. (lambda (word)
  1311. (with-temp-buffer
  1312. (insert (shell-command-to-string
  1313. (format "sdcv -n -u 'Genius English-Japanese' '%s'"
  1314. word)))
  1315. (html2text)
  1316. (buffer-substring (point-min)
  1317. (point-max)))))
  1318. ("alc" . (lambda (word)
  1319. (shell-command-to-string
  1320. (format "alc '%s' | head -n 20"
  1321. word))))
  1322. ("app" . (lambda (word)
  1323. (shell-command-to-string
  1324. (format "dict_app '%s'"
  1325. word))))
  1326. ;; letters broken
  1327. ("ms" .
  1328. (lambda (word)
  1329. (let ((url (concat
  1330. "http://api.microsofttranslator.com/V2/Ajax.svc/"
  1331. "Translate?appId=%s&text=%s&to=%s"))
  1332. (apikey "3C9778666C5BA4B406FFCBEE64EF478963039C51")
  1333. (target "ja")
  1334. (eword (url-hexify-string word)))
  1335. (with-current-buffer (url-retrieve-synchronously
  1336. (format url
  1337. apikey
  1338. eword
  1339. target))
  1340. (message "")
  1341. (goto-char (point-min))
  1342. (search-forward-regexp "^$"
  1343. nil
  1344. t)
  1345. (url-unhex-string (buffer-substring-no-properties
  1346. (point)
  1347. (point-max)))))))
  1348. ))
  1349. ;; (funcall (cdr (assoc "ms"
  1350. ;; ilookup-alist))
  1351. ;; "dictionary")
  1352. ;; (switch-to-buffer (url-retrieve-synchronously "http://api.microsofttranslator.com/V2/Ajax.svc/Translate?appId=3C9778666C5BA4B406FFCBEE64EF478963039C51&text=dictionary&to=ja"))
  1353. ;; (switch-to-buffer (url-retrieve-synchronously "http://google.com"))
  1354. (setq ilookup-default "ja")
  1355. (when (locate-library "google-translate")
  1356. (add-to-list 'ilookup-dict-alist
  1357. '("gt" .
  1358. (lambda (word)
  1359. (save-excursion
  1360. (google-translate-translate "auto"
  1361. "ja"
  1362. word))
  1363. (with-current-buffer "*Google Translate*"
  1364. (buffer-substring-no-properties (point-min)
  1365. (point-max)))))))
  1366. ))
  1367. (when (autoload-eval-lazily 'google-translate '(google-translate-translate
  1368. google-translate-at-point))
  1369. (setq google-translate-default-source-language "auto")
  1370. (setq google-translate-default-target-language "ja"))
  1371. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1372. ;; vc
  1373. ;; (require 'vc)
  1374. (setq vc-handled-backends '())
  1375. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1376. ;; gauche-mode
  1377. ;; http://d.hatena.ne.jp/kobapan/20090305/1236261804
  1378. ;; http://www.katch.ne.jp/~leque/software/repos/gauche-mode/gauche-mode.el
  1379. (when (and (fetch-library
  1380. "http://www.katch.ne.jp/~leque/software/repos/gauche-mode/gauche-mode.el"
  1381. t)
  1382. (autoload-eval-lazily 'gauche-mode '(gauche-mode run-scheme)))
  1383. (let ((s (executable-find "gosh")))
  1384. (setq scheme-program-name s
  1385. gauche-program-name s))
  1386. (defun run-gauche-other-window ()
  1387. "Run gauche on other window"
  1388. (interactive)
  1389. (switch-to-buffer-other-window
  1390. (get-buffer-create "*scheme*"))
  1391. (run-gauche))
  1392. (defun run-gauche ()
  1393. "run gauche"
  1394. (run-scheme gauche-program-name)
  1395. )
  1396. (defun scheme-send-buffer ()
  1397. ""
  1398. (interactive)
  1399. (scheme-send-region (point-min) (point-max))
  1400. (my-scheme-display-scheme-buffer)
  1401. )
  1402. (defun my-scheme-display-scheme-buffer ()
  1403. ""
  1404. (interactive)
  1405. (set-window-text-height (display-buffer scheme-buffer
  1406. t)
  1407. 7))
  1408. (add-hook 'scheme-mode-hook
  1409. (lambda ()
  1410. nil))
  1411. (add-hook 'inferior-scheme-mode-hook
  1412. (lambda ()
  1413. ;; (my-scheme-display-scheme-buffer)
  1414. ))
  1415. (setq auto-mode-alist
  1416. (cons '("\.gosh\\'" . gauche-mode) auto-mode-alist))
  1417. (setq auto-mode-alist
  1418. (cons '("\.gaucherc\\'" . gauche-mode) auto-mode-alist))
  1419. (add-hook 'gauche-mode-hook
  1420. (lambda ()
  1421. (define-key gauche-mode-map
  1422. (kbd "C-c C-z") 'run-gauche-other-window)
  1423. (define-key scheme-mode-map
  1424. (kbd "C-c C-c") 'scheme-send-buffer)
  1425. (define-key scheme-mode-map
  1426. (kbd "C-c C-b") 'my-scheme-display-scheme-buffer))))
  1427. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1428. ;; recentf-mode
  1429. (setq recentf-save-file (expand-file-name "~/.emacs.d/recentf")
  1430. recentf-max-menu-items 20
  1431. recentf-max-saved-items 30
  1432. recentf-show-file-shortcuts-flag nil)
  1433. (when (require 'recentf nil t)
  1434. (add-to-list 'recentf-exclude
  1435. (regexp-quote recentf-save-file))
  1436. (add-to-list 'recentf-exclude
  1437. (regexp-quote (expand-file-name user-emacs-directory)))
  1438. (define-key ctl-x-map (kbd "C-r") 'recentf-open-files)
  1439. (add-hook 'find-file-hook
  1440. 'recentf-save-list
  1441. t) ; save to file immediately after adding file to recentf list
  1442. (add-hook 'kill-emacs-hook
  1443. 'recentf-load-list)
  1444. ;;(run-with-idle-timer 5 t 'recentf-save-list)
  1445. ;; (add-hook 'find-file-hook
  1446. ;; (lambda ()
  1447. ;; (recentf-add-file default-directory)))
  1448. (and (fetch-library
  1449. "https://raw.github.com/10sr/emacs-lisp/master/recentf-show.el"
  1450. t)
  1451. (autoload-eval-lazily 'recentf-show)
  1452. (define-key ctl-x-map (kbd "C-r") 'recentf-show)
  1453. (add-hook 'recentf-show-before-listing-hook
  1454. 'recentf-load-list))
  1455. (recentf-mode 1)
  1456. (add-hook 'recentf-dialog-mode-hook
  1457. (lambda ()
  1458. ;; (recentf-save-list)
  1459. ;; (define-key recentf-dialog-mode-map (kbd "C-x C-f")
  1460. ;; 'my-recentf-cd-and-find-file)
  1461. (define-key recentf-dialog-mode-map (kbd "<up>") 'previous-line)
  1462. (define-key recentf-dialog-mode-map (kbd "<down>") 'next-line)
  1463. (define-key recentf-dialog-mode-map "p" 'previous-line)
  1464. (define-key recentf-dialog-mode-map "n" 'next-line)
  1465. (cd "~/"))))
  1466. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1467. ;; dired
  1468. (when (autoload-eval-lazily 'dired nil)
  1469. (defun my-dired-echo-file-head (arg)
  1470. ""
  1471. (interactive "P")
  1472. (let ((f (dired-get-filename)))
  1473. (message "%s"
  1474. (with-temp-buffer
  1475. (insert-file-contents f)
  1476. (buffer-substring-no-properties
  1477. (point-min)
  1478. (progn (goto-line (if arg
  1479. (prefix-numeric-value arg)
  1480. 7))
  1481. (point-at-eol)))))))
  1482. (defun my-dired-diff ()
  1483. ""
  1484. (interactive)
  1485. (let ((files (dired-get-marked-files nil nil nil t)))
  1486. (if (eq (car files)
  1487. t)
  1488. (diff (cadr files) (dired-get-filename))
  1489. (message "One files must be marked!"))))
  1490. (defun my-pop-to-buffer-erase-noselect (buffer-or-name)
  1491. "pop up buffer using `display-buffer' and return that buffer."
  1492. (let ((bf (get-buffer-create buffer-or-name)))
  1493. (with-current-buffer bf
  1494. (cd ".")
  1495. (erase-buffer))
  1496. (display-buffer bf)
  1497. bf))
  1498. (defun my-replace-nasi-none ()
  1499. ""
  1500. (save-excursion
  1501. (let ((buffer-read-only nil))
  1502. (goto-char (point-min))
  1503. (while (search-forward "なし" nil t)
  1504. (replace-match "none")))))
  1505. (defun dired-get-file-info ()
  1506. "dired get file info"
  1507. (interactive)
  1508. (let ((f (shell-quote-argument (dired-get-filename t))))
  1509. (if (file-directory-p f)
  1510. (progn
  1511. (message "Calculating disk usage...")
  1512. (shell-command (concat "du -hsD "
  1513. f)))
  1514. (shell-command (concat "file "
  1515. f)))))
  1516. (defun my-dired-scroll-up ()
  1517. ""
  1518. (interactive)
  1519. (my-dired-previous-line (- (window-height) 1)))
  1520. (defun my-dired-scroll-down ()
  1521. ""
  1522. (interactive)
  1523. (my-dired-next-line (- (window-height) 1)))
  1524. ;; (defun my-dired-forward-line (arg)
  1525. ;; ""
  1526. ;; (interactive "p"))
  1527. (defun my-dired-previous-line (arg)
  1528. ""
  1529. (interactive "p")
  1530. (if (> arg 0)
  1531. (progn
  1532. (if (eq (line-number-at-pos)
  1533. 1)
  1534. (goto-char (point-max))
  1535. (forward-line -1))
  1536. (my-dired-previous-line (if (or (dired-get-filename nil t)
  1537. (dired-get-subdir))
  1538. (- arg 1)
  1539. arg)))
  1540. (dired-move-to-filename)))
  1541. (defun my-dired-next-line (arg)
  1542. ""
  1543. (interactive "p")
  1544. (if (> arg 0)
  1545. (progn
  1546. (if (eq (point)
  1547. (point-max))
  1548. (goto-char (point-min))
  1549. (forward-line 1))
  1550. (my-dired-next-line (if (or (dired-get-filename nil t)
  1551. (dired-get-subdir))
  1552. (- arg 1)
  1553. arg)))
  1554. (dired-move-to-filename)))
  1555. (defun my-dired-print-current-dir-and-file ()
  1556. (message "%s %s"
  1557. default-directory
  1558. (buffer-substring-no-properties (point-at-bol)
  1559. (point-at-eol))))
  1560. (defun dired-do-execute-as-command ()
  1561. ""
  1562. (interactive)
  1563. (let ((file (dired-get-filename t)))
  1564. (if (file-executable-p file)
  1565. (start-process file nil file)
  1566. (when (y-or-n-p
  1567. "this file cant be executed. mark as executable and go? : ")
  1568. (set-file-modes file
  1569. (file-modes-symbolic-to-number "u+x" (file-modes file)))
  1570. (start-process file nil file)))))
  1571. ;;http://bach.istc.kobe-u.ac.jp/lect/tamlab/ubuntu/emacs.html
  1572. (defun my-dired-x-open ()
  1573. ""
  1574. (interactive)
  1575. (my-x-open (dired-get-filename t t)))
  1576. (if (eq window-system 'mac)
  1577. (setq dired-listing-switches "-lhF")
  1578. (setq dired-listing-switches "-lhF --time-style=long-iso")
  1579. )
  1580. (setq dired-listing-switches "-lhF")
  1581. (put 'dired-find-alternate-file 'disabled nil)
  1582. ;; when using dired-find-alternate-file
  1583. ;; reuse current dired buffer for the file to open
  1584. (setq dired-ls-F-marks-symlinks t)
  1585. (when (require 'ls-lisp nil t)
  1586. (setq ls-lisp-use-insert-directory-program nil) ; always use ls-lisp
  1587. (setq ls-lisp-dirs-first t)
  1588. (setq ls-lisp-use-localized-time-format t)
  1589. (setq ls-lisp-format-time-list
  1590. '("%Y-%m-%d %H:%M"
  1591. "%Y-%m-%d ")))
  1592. (setq dired-dwim-target t)
  1593. ;; (add-hook 'dired-after-readin-hook
  1594. ;; 'my-replace-nasi-none)
  1595. ;; (add-hook 'after-init-hook
  1596. ;; (lambda ()
  1597. ;; (dired ".")))
  1598. (add-hook 'dired-mode-hook
  1599. (lambda ()
  1600. (define-key dired-mode-map "o" 'my-dired-x-open)
  1601. (define-key dired-mode-map "i" 'dired-get-file-info)
  1602. (define-key dired-mode-map "f" 'find-file)
  1603. (define-key dired-mode-map "!" 'shell-command)
  1604. (define-key dired-mode-map "&" 'async-shell-command)
  1605. (define-key dired-mode-map "X" 'dired-do-async-shell-command)
  1606. (define-key dired-mode-map "=" 'my-dired-diff)
  1607. (define-key dired-mode-map "B" 'gtkbm-add-current-dir)
  1608. (define-key dired-mode-map "b" 'gtkbm)
  1609. (define-key dired-mode-map "h" 'my-dired-echo-file-head)
  1610. (define-key dired-mode-map "@" (lambda ()
  1611. (interactive) (my-x-open ".")))
  1612. (define-key dired-mode-map (kbd "TAB") 'other-window)
  1613. ;; (define-key dired-mode-map "P" 'my-dired-do-pack-or-unpack)
  1614. (define-key dired-mode-map "/" 'dired-isearch-filenames)
  1615. (define-key dired-mode-map (kbd "DEL") 'dired-up-directory)
  1616. (define-key dired-mode-map (kbd "C-h") 'dired-up-directory)
  1617. (substitute-key-definition 'dired-next-line
  1618. 'my-dired-next-line dired-mode-map)
  1619. (substitute-key-definition 'dired-previous-line
  1620. 'my-dired-previous-line dired-mode-map)
  1621. ;; (define-key dired-mode-map (kbd "C-p") 'my-dired-previous-line)
  1622. ;; (define-key dired-mode-map (kbd "p") 'my-dired-previous-line)
  1623. ;; (define-key dired-mode-map (kbd "C-n") 'my-dired-next-line)
  1624. ;; (define-key dired-mode-map (kbd "n") 'my-dired-next-line)
  1625. (define-key dired-mode-map (kbd "<left>") 'my-dired-scroll-up)
  1626. (define-key dired-mode-map (kbd "<right>") 'my-dired-scroll-down)
  1627. (define-key dired-mode-map (kbd "ESC p") 'my-dired-scroll-up)
  1628. (define-key dired-mode-map (kbd "ESC n") 'my-dired-scroll-down)
  1629. (let ((file "._Icon\015"))
  1630. (when nil (file-readable-p file)
  1631. (delete-file file)))))
  1632. (and (fetch-library "https://raw.github.com/10sr/emacs-lisp/master/pack.el"
  1633. t)
  1634. (autoload-eval-lazily 'pack '(dired-do-pack-or-unpack pack))
  1635. (add-hook 'dired-mode-hook
  1636. (lambda ()
  1637. (define-key dired-mode-map "P" 'dired-do-pack-or-unpack))))
  1638. (and (fetch-library
  1639. "https://raw.github.com/10sr/emacs-lisp/master/dired-list-all-mode.el"
  1640. t)
  1641. (autoload-eval-lazily 'dired-list-all-mode)
  1642. (setq dired-listing-switches "-lhF")
  1643. (add-hook 'dired-mode-hook
  1644. (lambda ()
  1645. (define-key dired-mode-map "a" 'dired-list-all-mode)
  1646. )))
  1647. ) ; when dired locate
  1648. ;; http://blog.livedoor.jp/tek_nishi/archives/4693204.html
  1649. (defun my-dired-toggle-mark()
  1650. (let ((cur (cond ((eq (following-char) dired-marker-char) ?\040)
  1651. (t dired-marker-char))))
  1652. (delete-char 1)
  1653. (insert cur)))
  1654. (defun my-dired-mark (arg)
  1655. "Toggle mark the current (or next ARG) files.
  1656. If on a subdir headerline, mark all its files except `.' and `..'.
  1657. Use \\[dired-unmark-all-files] to remove all marks
  1658. and \\[dired-unmark] on a subdir to remove the marks in
  1659. this subdir."
  1660. (interactive "P")
  1661. (if (dired-get-subdir)
  1662. (save-excursion (dired-mark-subdir-files))
  1663. (let ((inhibit-read-only t))
  1664. (dired-repeat-over-lines
  1665. (prefix-numeric-value arg)
  1666. 'my-dired-toggle-mark))))
  1667. (defun my-dired-mark-backward (arg)
  1668. "In Dired, move up lines and toggle mark there.
  1669. Optional prefix ARG says how many lines to unflag; default is one line."
  1670. (interactive "p")
  1671. (my-dired-mark (- arg)))
  1672. (add-hook 'dired-mode-hook
  1673. (lambda ()
  1674. (local-set-key (kbd "SPC") 'my-dired-mark)
  1675. (local-set-key (kbd "S-SPC") 'my-dired-mark-backward))
  1676. )
  1677. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1678. ;; eshell
  1679. (autoload-eval-lazily 'eshell nil
  1680. (setq eshell-banner-message (format "Welcome to the Emacs shell
  1681. %s
  1682. C-x t to toggling emacs-text-mode
  1683. "
  1684. (shell-command-to-string "uname -a")
  1685. ))
  1686. (defvar eshell-text-mode-map
  1687. (let ((map (make-sparse-keymap)))
  1688. (define-key map (kbd "C-x t") 'eshell-text-mode-toggle)
  1689. map))
  1690. (define-derived-mode eshell-text-mode text-mode
  1691. "Eshell-Text"
  1692. "Text-mode for Eshell."
  1693. nil)
  1694. (defun eshell-text-mode-toggle ()
  1695. "Toggle eshell-text-mode and eshell-mode."
  1696. (interactive)
  1697. (cond ((eq major-mode
  1698. 'eshell-text-mode)
  1699. (goto-char (point-max))
  1700. (message "Eshell text mode disabled")
  1701. (eshell-mode))
  1702. ((eq major-mode
  1703. 'eshell-mode)
  1704. (message "Eshell text mode enabled")
  1705. (eshell-write-history)
  1706. (eshell-text-mode))
  1707. (t
  1708. (message "Not in eshell buffer")
  1709. nil)))
  1710. (defun my-eshell-backward-delete-char ()
  1711. (interactive)
  1712. (when (< (save-excursion
  1713. (eshell-bol)
  1714. (point))
  1715. (point))
  1716. (backward-delete-char 1)))
  1717. (defun my-file-owner-p (file)
  1718. "t if FILE is owned by me."
  1719. (eq (user-uid) (nth 2 (file-attributes file))))
  1720. "http://www.bookshelf.jp/pukiwiki/pukiwiki.php\
  1721. ?Eshell%A4%F2%BB%C8%A4%A4%A4%B3%A4%CA%A4%B9"
  1722. ;; ;; written by Stefan Reichoer <reichoer@web.de>
  1723. ;; (defun eshell/less (&rest args)
  1724. ;; "Invoke `view-file' on the file.
  1725. ;; \"less +42 foo\" also goes to line 42 in the buffer."
  1726. ;; (if args
  1727. ;; (while args
  1728. ;; (if (string-match "\\`\\+\\([0-9]+\\)\\'" (car args))
  1729. ;; (let* ((line (string-to-number (match-string 1 (pop args))))
  1730. ;; (file (pop args)))
  1731. ;; (view-file file)
  1732. ;; (goto-line line))
  1733. ;; (view-file (pop args))))))
  1734. (defun eshell/o (&optional file)
  1735. (my-x-open (or file ".")))
  1736. ;; (defun eshell/vi (&rest args)
  1737. ;; "Invoke `find-file' on the file.
  1738. ;; \"vi +42 foo\" also goes to line 42 in the buffer."
  1739. ;; (while args
  1740. ;; (if (string-match "\\`\\+\\([0-9]+\\)\\'" (car args))
  1741. ;; (let* ((line (string-to-number (match-string 1 (pop args))))
  1742. ;; (file (pop args)))
  1743. ;; (find-file file)
  1744. ;; (goto-line line))
  1745. ;; (find-file (pop args)))))
  1746. (defun eshell/clear ()
  1747. "Clear the current buffer, leaving one prompt at the top."
  1748. (interactive)
  1749. (let ((inhibit-read-only t))
  1750. (erase-buffer)))
  1751. (defun eshell-clear ()
  1752. (interactive)
  1753. (let ((inhibit-read-only t))
  1754. (erase-buffer)
  1755. (insert (funcall eshell-prompt-function))))
  1756. (defun eshell/d (&optional dirname switches)
  1757. "if first arg is omitted open current directory."
  1758. (dired (or dirname ".") switches))
  1759. (defun eshell/v ()
  1760. (view-mode 1))
  1761. ;; (defun eshell/aaa (&rest args)
  1762. ;; (message "%S"
  1763. ;; args))
  1764. (defvar eshell/git-cat-command
  1765. nil
  1766. "List of git commands that cat just return strings as results.")
  1767. (setq eshell/git-cat-command
  1768. '("status" "st" "b" "branch" "ls" "ls-files")
  1769. )
  1770. (defun eshell/git (&rest args)
  1771. (if (member (car args)
  1772. eshell/git-cat-command)
  1773. (shell-command-to-string (mapconcat 'shell-quote-argument
  1774. `("git"
  1775. "-c"
  1776. "color.ui=always"
  1777. ,@args)
  1778. " "))
  1779. ;; (eshell-git-shell-command-to-string args)
  1780. (if (require 'git-command nil t)
  1781. (git-command (mapconcat 'shell-quote-argument
  1782. args
  1783. " "))
  1784. (apply 'eshell-exec-visual "git" args))))
  1785. ;; (defun eshell-git-shell-command-to-string (args)
  1786. ;; "Return string of output of ARGS."
  1787. ;; (let ((sargs (mapconcat 'shell-quote-argument
  1788. ;; args
  1789. ;; " ")))
  1790. ;; (if (require 'ansi-color nil t)
  1791. ;; (identity
  1792. ;; (shell-command-to-string (concat "git "
  1793. ;; "-c color.ui=always "
  1794. ;; sargs)))
  1795. ;; (shell-command-to-string (concat "git "
  1796. ;; sargs)))))
  1797. (defalias 'eshell/g 'eshell/git)
  1798. (defalias 'eshell/: 'ignore)
  1799. (defalias 'eshell/type 'eshell/which)
  1800. ;; (defalias 'eshell/vim 'eshell/vi)
  1801. (defalias 'eshell/ff 'find-file)
  1802. (defalias 'eshell/q 'eshell/exit)
  1803. (defun eshell-goto-prompt ()
  1804. ""
  1805. (interactive)
  1806. (goto-char (point-max)))
  1807. (defun eshell-delete-char-or-logout (n)
  1808. (interactive "p")
  1809. (if (equal (eshell-get-old-input)
  1810. "")
  1811. (progn
  1812. (insert "exit")
  1813. (eshell-send-input))
  1814. (delete-char n)))
  1815. (defun eshell-kill-input ()
  1816. (interactive)
  1817. (delete-region (point)
  1818. (progn (eshell-bol)
  1819. (point))))
  1820. (defalias 'eshell/logout 'eshell/exit)
  1821. (defun eshell-cd-default-directory (&optional eshell-buffer-or-name)
  1822. "open eshell and change wd
  1823. if arg given, use that eshell buffer, otherwise make new eshell buffer."
  1824. (interactive)
  1825. (let ((dir (expand-file-name default-directory)))
  1826. (switch-to-buffer (or eshell-buffer-or-name
  1827. (eshell t)))
  1828. (unless (equal dir (expand-file-name default-directory))
  1829. ;; (cd dir)
  1830. ;; (eshell-interactive-print (concat "cd " dir "\n"))
  1831. ;; (eshell-emit-prompt)
  1832. (goto-char (point-max))
  1833. (eshell-kill-input)
  1834. (insert "cd " dir)
  1835. (eshell-send-input))))
  1836. (defadvice eshell-next-matching-input-from-input
  1837. ;; do not cycle history
  1838. (around eshell-history-do-not-cycle activate)
  1839. (if (= 0
  1840. (or eshell-history-index
  1841. 0))
  1842. (progn
  1843. (delete-region eshell-last-output-end (point))
  1844. (insert-and-inherit eshell-matching-input-from-input-string)
  1845. (setq eshell-history-index nil))
  1846. ad-do-it))
  1847. (setq eshell-directory-name "~/.emacs.d/eshell/")
  1848. (setq eshell-term-name "eterm-color")
  1849. (setq eshell-scroll-to-bottom-on-input t)
  1850. (setq eshell-cmpl-ignore-case t)
  1851. (setq eshell-cmpl-cycle-completions nil)
  1852. (setq eshell-highlight-prompt nil)
  1853. (setq eshell-ls-initial-args '("-hCFG"
  1854. "--color=auto"
  1855. "--time-style=long-iso")) ; "-hF")
  1856. (setq eshell-prompt-function
  1857. 'my-eshell-prompt-function)
  1858. (defun my-eshell-prompt-function ()
  1859. (with-temp-buffer
  1860. (let (p1 p2 p3 p4)
  1861. (insert ":: [")
  1862. (setq p1 (point))
  1863. (insert user-login-name
  1864. "@"
  1865. (car (split-string system-name
  1866. "\\."))
  1867. )
  1868. (setq p2 (point))
  1869. (insert ":")
  1870. (setq p3 (point))
  1871. (insert (abbreviate-file-name default-directory))
  1872. (setq p4 (point))
  1873. (insert "]")
  1874. (insert "\n:: ")
  1875. (unless (eq 0
  1876. eshell-last-command-status)
  1877. (insert (format "[STATUS:%d] "
  1878. eshell-last-command-status)))
  1879. (insert (if (= (user-uid)
  1880. 0)
  1881. "# "
  1882. "$ "))
  1883. (add-text-properties p1
  1884. p2
  1885. '(face underline))
  1886. (add-text-properties p3
  1887. p4
  1888. '(face underline))
  1889. (buffer-substring (point-min)
  1890. (point-max)))))
  1891. (add-hook 'eshell-mode-hook
  1892. (lambda ()
  1893. ;; (define-key eshell-mode-map (kbd "C-x C-x") (lambda ()
  1894. ;; (interactive)
  1895. ;; (switch-to-buffer (other-buffer))))
  1896. ;; (define-key eshell-mode-map (kbd "C-g") (lambda ()
  1897. ;; (interactive)
  1898. ;; (eshell-goto-prompt)
  1899. ;; (keyboard-quit)))
  1900. (define-key eshell-mode-map (kbd "C-x t")
  1901. 'eshell-text-mode-toggle)
  1902. (define-key eshell-mode-map (kbd "C-u")
  1903. 'eshell-kill-input)
  1904. (define-key eshell-mode-map (kbd "C-d")
  1905. 'eshell-delete-char-or-logout)
  1906. ;; (define-key eshell-mode-map (kbd "C-l")
  1907. ;; 'eshell-clear)
  1908. (define-key eshell-mode-map (kbd "DEL")
  1909. 'my-eshell-backward-delete-char)
  1910. (define-key eshell-mode-map (kbd "<up>")
  1911. 'scroll-down-line)
  1912. (define-key eshell-mode-map (kbd "<down>")
  1913. 'scroll-up-line)
  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 "Shell Command: "
  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