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

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