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.
 
 
 
 
 
 

2126 regels
72 KiB

  1. ;;; emacs.el --- 10sr emacs initialization
  2. ;;; Commentary:
  3. ;;; Code:
  4. ;; SETUP_LOAD: (let ((file "DOTFILES_DIR/emacs.el"))
  5. ;; SETUP_LOAD: (and (file-readable-p file)
  6. ;; SETUP_LOAD: (load-file file)))
  7. (setq debug-on-error t)
  8. ;; make directories
  9. (unless (file-directory-p (expand-file-name user-emacs-directory))
  10. (make-directory (expand-file-name user-emacs-directory)))
  11. (let ((d (expand-file-name (concat user-emacs-directory
  12. "lisp"))))
  13. (unless (file-directory-p d)
  14. (make-directory d))
  15. (add-to-list 'load-path d))
  16. (require 'cl-lib)
  17. (require 'simple)
  18. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  19. ;; Some macros for internals
  20. ;; `emacs --load emacs.el` with Emacs 24.3 requires with-eval-after-load to be
  21. ;; defined at the toplevel (means that it should not be defined inside of some
  22. ;; special forms like `when'. I do not now how to do with about this...)
  23. (unless (fboundp 'with-eval-after-load)
  24. ;; polyfill for Emacs < 24.4
  25. (defmacro with-eval-after-load (file &rest body)
  26. "After FILE is loaded execute BODY."
  27. (declare (indent 1) (debug t))
  28. `(eval-after-load ,file (quote (progn ,@body)))))
  29. (defun call-after-init (func)
  30. "If `after-init-hook' has been run, call FUNC immediately.
  31. Otherwize hook it."
  32. (if after-init-time
  33. (funcall func)
  34. (add-hook 'after-init-hook
  35. func)))
  36. (defmacro safe-require-or-eval (feature)
  37. "Require FEATURE if available.
  38. At compile time the feature will be loaded immediately."
  39. `(eval-and-compile
  40. (require ,feature nil t)))
  41. (defmacro autoload-eval-lazily (feature &optional functions &rest body)
  42. "Define autoloading FEATURE that defines FUNCTIONS.
  43. FEATURE is a symbol. FUNCTIONS is a list of symbols. If FUNCTIONS is nil,
  44. the function same as FEATURE is defined as autoloaded function. BODY is passed
  45. to `eval-after-load'.
  46. After this macro is expanded, this returns the path to library if FEATURE
  47. found, otherwise returns nil."
  48. (declare (indent 2) (debug t))
  49. (let* ((libname (symbol-name (eval feature)))
  50. (libpath (locate-library libname)))
  51. `(progn
  52. (when (locate-library ,libname)
  53. ,@(mapcar (lambda (f)
  54. `(unless (fboundp ',f)
  55. (progn
  56. (message "Autoloaded function `%S' defined (%s)"
  57. (quote ,f)
  58. ,libpath)
  59. (autoload (quote ,f)
  60. ,libname
  61. ,(concat "Autoloaded function defined in \""
  62. libpath
  63. "\".")
  64. t))))
  65. (or (eval functions)
  66. `(,(eval feature)))))
  67. (eval-after-load ,feature
  68. (quote (progn
  69. ,@body)))
  70. (locate-library ,libname))))
  71. (when (autoload-eval-lazily 'tetris nil
  72. (message "Tetris loaded!"))
  73. (message "Tetris found!"))
  74. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  75. ;; download library from web
  76. (defvar fetch-library-enabled-p t
  77. "Set nil to skip downloading with `fetch-library'.")
  78. (defun fetch-library (url &optional byte-compile-p force-download-p)
  79. "Download a library from URL and locate it in \"~/emacs.d/lisp/\".
  80. Return nil if library unfound and failed to download,
  81. otherwise the path where the library installed.
  82. If BYTE-COMPILE-P is t byte compile the file after downloading.
  83. If FORCE-DOWNLOAD-P it t ignore exisiting library and always download.
  84. This function also checks the value of `fetch-library-enabled-p' and do not
  85. fetch libraries if this value is nil. In this case all arguments (including
  86. FORCE-DOWNLOAD-P) will be ignored."
  87. (let* ((dir (expand-file-name (concat user-emacs-directory "lisp/")))
  88. (lib (file-name-sans-extension (file-name-nondirectory url)))
  89. (lpath (concat dir lib ".el"))
  90. (locate-p (locate-library lib)))
  91. (if (and fetch-library-enabled-p
  92. (or force-download-p
  93. (not locate-p)))
  94. (if (progn (message "Downloading %s..."
  95. url)
  96. (download-file url
  97. lpath
  98. t))
  99. (progn (message "Downloading %s...done"
  100. url)
  101. (when (and byte-compile-p
  102. (require 'bytecomp nil t))
  103. (and (file-exists-p (byte-compile-dest-file lpath))
  104. (delete-file (byte-compile-dest-file lpath)))
  105. (message "Byte-compiling %s..."
  106. lpath)
  107. (byte-compile-file lpath)
  108. (message "Byte-compiling %s...done"
  109. lpath)))
  110. (progn (and (file-writable-p lpath)
  111. (delete-file lpath))
  112. (message "Downloading %s...failed"
  113. url))))
  114. (locate-library lib)))
  115. ;; If EMACS_EL_DRY_RUN is set and it is not an empty string, fetch-library
  116. ;; does not actually fetch library.
  117. (let ((dryrun (getenv "EMACS_EL_DRY_RUN")))
  118. (when (and dryrun
  119. (< 0
  120. (length dryrun)))
  121. (setq fetch-library-enabled-p
  122. nil)
  123. (message "EMACS_EL_DRY_RUN is set. Skip fetching libraries.")))
  124. (defun download-file (url path &optional ok-if-already-exists)
  125. "Download file from URL and output to PATH.
  126. IF OK-IF-ALREADY-EXISTS is true force download."
  127. (let ((curl (executable-find "curl"))
  128. (wget (executable-find "wget")))
  129. (cond (wget
  130. (if (and (not ok-if-already-exists)
  131. (file-exists-p path))
  132. nil
  133. (and (eq 0
  134. (call-process wget
  135. nil
  136. nil
  137. nil
  138. "-O"
  139. path
  140. url
  141. ))
  142. path)))
  143. (curl
  144. (if (and (not ok-if-already-exists)
  145. (file-exists-p path))
  146. nil
  147. (and (eq 0
  148. (call-process curl
  149. nil
  150. nil
  151. nil
  152. "--output"
  153. path
  154. "-L"
  155. url
  156. ))
  157. path)))
  158. (t
  159. (ignore-errors
  160. (require 'url)
  161. (url-copy-file url
  162. path
  163. ok-if-already-exists)
  164. path)))))
  165. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  166. ;; package
  167. (set (defvar 10sr-package-list)
  168. '(
  169. markdown-mode
  170. yaml-mode
  171. gnuplot-mode
  172. erlang
  173. js2-mode
  174. git-commit
  175. gitignore-mode
  176. ;; ack
  177. color-moccur
  178. ggtags
  179. flycheck
  180. ;; is flymake installs are required?
  181. ;;flymake-jshint
  182. ;;flymake-python-pyflakes
  183. xclip
  184. foreign-regexp
  185. multi-term
  186. term-run
  187. editorconfig
  188. git-ps1-mode
  189. restart-emacs
  190. fill-column-indicator
  191. pkgbuild-mode
  192. minibuffer-line
  193. scala-mode2
  194. ensime
  195. editorconfig
  196. git-command
  197. ;; 10sr repository
  198. ;; 10sr-extras
  199. terminal-title
  200. recentf-show
  201. dired-list-all-mode
  202. pack
  203. set-modeline-color
  204. read-only-only-mode
  205. smart-revert
  206. autosave
  207. ;;window-organizer
  208. remember-major-modes-mode
  209. ilookup
  210. pasteboard
  211. ))
  212. (when (safe-require-or-eval 'package)
  213. (setq package-archives
  214. `(,@package-archives
  215. ("melpa" . "https://melpa.org/packages/")
  216. ("10sr-el" . "https://10sr.github.io/emacs-lisp/p/")))
  217. (package-initialize)
  218. (defun my-auto-install-package ()
  219. "Install packages semi-automatically."
  220. (interactive)
  221. (package-refresh-contents)
  222. (mapc (lambda (pkg)
  223. (or (package-installed-p pkg)
  224. (locate-library (symbol-name pkg))
  225. (package-install pkg)))
  226. 10sr-package-list))
  227. )
  228. ;; (lazy-load-eval 'sudoku)
  229. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  230. ;; my-idle-hook
  231. (defvar my-idle-hook nil
  232. "Hook run when idle for several secs.")
  233. (defvar my-idle-hook-sec 5
  234. "Second to run `my-idle-hook'.")
  235. (run-with-idle-timer my-idle-hook-sec
  236. t
  237. (lambda ()
  238. (run-hooks 'my-idle-hook)))
  239. ;; (add-hook 'my-idle-hook
  240. ;; (lambda ()
  241. ;; (message "idle hook message")))
  242. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  243. ;; start and quit
  244. (setq inhibit-startup-message t)
  245. (setq confirm-kill-emacs 'y-or-n-p)
  246. (setq gc-cons-threshold (* 1024 1024 4))
  247. (when window-system
  248. (add-to-list 'default-frame-alist '(cursor-type . box))
  249. (add-to-list 'default-frame-alist '(background-color . "white"))
  250. (add-to-list 'default-frame-alist '(foreground-color . "gray10"))
  251. ;; (add-to-list 'default-frame-alist '(alpha . (80 100 100 100)))
  252. ;; does not work?
  253. )
  254. ;; (add-to-list 'default-frame-alist '(cursor-type . box))
  255. (if window-system (menu-bar-mode 1) (menu-bar-mode 0))
  256. (and (fboundp 'tool-bar-mode)
  257. (tool-bar-mode 0))
  258. (and (fboundp 'set-scroll-bar-mode)
  259. (set-scroll-bar-mode nil))
  260. (add-hook 'kill-emacs-hook
  261. ;; load init file when terminating emacs to ensure file is not broken
  262. 'reload-init-file)
  263. (defun my-force-kill-emacs ()
  264. "My force kill Emacs."
  265. (interactive)
  266. (let ((kill-emacs-hook nil))
  267. (kill-emacs)))
  268. (call-after-init
  269. (lambda ()
  270. (message "%s %s" invocation-name emacs-version)
  271. (message "%s was taken to initialize emacs." (emacs-init-time))
  272. (switch-to-buffer "*Messages*")))
  273. (cd ".") ; when using windows use / instead of \ in `default-directory'
  274. ;; locale
  275. (set-language-environment "Japanese")
  276. (set-default-coding-systems 'utf-8-unix)
  277. (prefer-coding-system 'utf-8-unix)
  278. (setq system-time-locale "C")
  279. ;; my prefix map
  280. (defvar my-prefix-map nil
  281. "My prefix map.")
  282. (define-prefix-command 'my-prefix-map)
  283. (define-key ctl-x-map (kbd "C-x") 'my-prefix-map)
  284. (define-key my-prefix-map (kbd "C-q") 'quoted-insert)
  285. (define-key my-prefix-map (kbd "C-z") 'suspend-frame)
  286. ;; (comint-show-maximum-output)
  287. ;; kill scratch
  288. (call-after-init (lambda ()
  289. (let ((buf (get-buffer "*scratch*")))
  290. (when buf
  291. (kill-buffer buf)))))
  292. ;; modifier keys
  293. ;; (setq mac-option-modifier 'control)
  294. ;; display
  295. (setq visible-bell t)
  296. (setq ring-bell-function 'ignore)
  297. (mouse-avoidance-mode 'banish)
  298. (defun reload-init-file ()
  299. "Reload Emacs init file."
  300. (interactive)
  301. (when (and user-init-file
  302. (file-readable-p user-init-file))
  303. (load-file user-init-file)))
  304. (safe-require-or-eval 'session)
  305. ;; server
  306. (when (safe-require-or-eval 'server)
  307. (setq server-name (concat "server"
  308. (number-to-string (emacs-pid))))
  309. ;; In Cygwin Environment `server-runnning-p' stops when server-use-tcp is nil
  310. ;; In Darwin environment, init fails with message like 'Service name too long'
  311. ;; when server-use-tcp is nil
  312. (when (or (eq system-type
  313. 'cygwin)
  314. (eq system-type
  315. 'darwin))
  316. (setq server-use-tcp t))
  317. (server-start))
  318. ;; MSYS2 fix
  319. (when (eq system-type
  320. 'windows-nt)
  321. (setq shell-file-name
  322. (executable-find "bash"))
  323. '(setq function-key-map
  324. `(,@function-key-map ([pause] . [?\C-c])
  325. ))
  326. (define-key key-translation-map
  327. (kbd "<pause>")
  328. (kbd "C-c"))
  329. '(keyboard-translate [pause]
  330. (kbd "C-c")p))
  331. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  332. ;; global keys
  333. (global-set-key (kbd "<up>") 'scroll-down-line)
  334. (global-set-key (kbd "<down>") 'scroll-up-line)
  335. (global-set-key (kbd "<left>") 'scroll-down)
  336. (global-set-key (kbd "<right>") 'scroll-up)
  337. ;; (define-key my-prefix-map (kbd "C-h") help-map)
  338. (global-set-key (kbd "C-\\") help-map)
  339. (define-key ctl-x-map (kbd "DEL") help-map)
  340. (define-key ctl-x-map (kbd "C-h") help-map)
  341. (define-key help-map "a" 'apropos)
  342. ;; disable annoying keys
  343. (global-set-key [prior] 'ignore)
  344. (global-set-key (kbd "<next>") 'ignore)
  345. (global-set-key [menu] 'ignore)
  346. (global-set-key [down-mouse-1] 'ignore)
  347. (global-set-key [down-mouse-2] 'ignore)
  348. (global-set-key [down-mouse-3] 'ignore)
  349. (global-set-key [mouse-1] 'ignore)
  350. (global-set-key [mouse-2] 'ignore)
  351. (global-set-key [mouse-3] 'ignore)
  352. (global-set-key (kbd "<eisu-toggle>") 'ignore)
  353. (global-set-key (kbd "C-<eisu-toggle>") 'ignore)
  354. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  355. ;; editting
  356. (defun my-copy-whole-line ()
  357. "Copy whole line."
  358. (interactive)
  359. (kill-new (concat (buffer-substring (point-at-bol)
  360. (point-at-eol))
  361. "\n")))
  362. (setq require-final-newline t)
  363. (setq kill-whole-line t)
  364. (setq scroll-conservatively 35
  365. scroll-margin 2
  366. scroll-step 0)
  367. (setq-default major-mode 'text-mode)
  368. (setq next-line-add-newlines nil)
  369. (setq kill-read-only-ok t)
  370. (setq truncate-partial-width-windows nil) ; when splitted horizontally
  371. ;; (setq-default line-spacing 0.2)
  372. (setq-default indicate-empty-lines t) ; when using x indicate empty line
  373. (setq-default tab-width 4)
  374. (setq-default indent-tabs-mode nil)
  375. (setq-default indent-line-function nil)
  376. ;; (pc-selection-mode 1) ; make some already defined keybind back to default
  377. (delete-selection-mode 1)
  378. (cua-mode 0)
  379. (setq line-move-visual nil)
  380. ;; key bindings
  381. ;; moving around
  382. ;; (global-set-key (kbd "M-j") 'next-line)
  383. ;; (global-set-key (kbd "M-k") 'previous-line)
  384. ;; (global-set-key (kbd "M-h") 'backward-char)
  385. ;; (global-set-key (kbd "M-l") 'forward-char)
  386. ;;(keyboard-translate ?\M-j ?\C-j)
  387. ;; (global-set-key (kbd "M-p") 'backward-paragraph)
  388. (define-key esc-map "p" 'backward-paragraph)
  389. ;; (global-set-key (kbd "M-n") 'forward-paragraph)
  390. (define-key esc-map "n" 'forward-paragraph)
  391. (global-set-key (kbd "C-<up>") 'scroll-down-line)
  392. (global-set-key (kbd "C-<down>") 'scroll-up-line)
  393. (global-set-key (kbd "C-<left>") 'scroll-down)
  394. (global-set-key (kbd "C-<right>") 'scroll-up)
  395. (global-set-key (kbd "<select>") 'ignore) ; 'previous-line-mark)
  396. (define-key ctl-x-map (kbd "ESC x") 'execute-extended-command)
  397. (define-key ctl-x-map (kbd "ESC :") 'eval-expression)
  398. ;; C-h and DEL
  399. (global-set-key (kbd "C-h") (kbd "DEL"))
  400. (global-set-key (kbd "C-m") 'reindent-then-newline-and-indent)
  401. (global-set-key (kbd "C-o") (kbd "C-e C-m"))
  402. (define-key esc-map "k" 'my-copy-whole-line)
  403. ;; (global-set-key "\C-z" 'undo) ; undo is M-u
  404. (define-key esc-map "u" 'undo)
  405. (define-key esc-map "i" (kbd "ESC TAB"))
  406. ;; (global-set-key (kbd "C-r") 'query-replace-regexp)
  407. (global-set-key (kbd "C-s") 'isearch-forward-regexp)
  408. (global-set-key (kbd "C-r") 'isearch-backward-regexp)
  409. (define-key my-prefix-map (kbd "C-o") 'occur)
  410. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  411. ;; title and mode-line
  412. (when (safe-require-or-eval 'terminal-title)
  413. ;; if TERM is not screen use default value
  414. (if (getenv "TMUX")
  415. ;; if use tmux locally just basename of current dir
  416. (set-variable 'terminal-title-format
  417. '((file-name-nondirectory (directory-file-name
  418. default-directory))))
  419. (if (and (let ((tty-type (frame-parameter nil
  420. 'tty-type)))
  421. (and tty-type
  422. (equal (car (split-string tty-type
  423. "-"))
  424. "screen")))
  425. (not (getenv "SSH_CONNECTION")))
  426. (set-variable 'terminal-title-format
  427. '((file-name-nondirectory (directory-file-name
  428. default-directory))))
  429. ;; seems that TMUX is used locally and ssh to remote host
  430. (set-variable 'terminal-title-format
  431. `("em:"
  432. ,user-login-name
  433. "@"
  434. ,(car (split-string system-name
  435. "\\."))
  436. ":"
  437. default-directory))
  438. )
  439. )
  440. (terminal-title-mode))
  441. (setq eol-mnemonic-dos "\\r\\n")
  442. (setq eol-mnemonic-mac "\\r")
  443. (setq eol-mnemonic-unix "\\n")
  444. (which-function-mode 0)
  445. (line-number-mode 0)
  446. (column-number-mode 0)
  447. (size-indication-mode 0)
  448. (setq mode-line-position
  449. '(:eval (format "L%%l/%d,C%%c"
  450. (count-lines (point-max)
  451. (point-min)))))
  452. (when (safe-require-or-eval 'git-ps1-mode)
  453. (git-ps1-mode))
  454. ;; http://www.geocities.jp/simizu_daisuke/bunkei-meadow.html#frame-title
  455. ;; display date
  456. (when (safe-require-or-eval 'time)
  457. (setq display-time-interval 29)
  458. (setq display-time-day-and-date t)
  459. (setq display-time-format "%a, %d %b %Y %T")
  460. (if window-system
  461. (display-time-mode 0)
  462. (display-time-mode 1))
  463. (when display-time-mode
  464. (display-time-update)))
  465. ;; ;; current directory
  466. ;; (let ((ls (member 'mode-line-buffer-identification
  467. ;; mode-line-format)))
  468. ;; (setcdr ls
  469. ;; (cons '(:eval (concat " ("
  470. ;; (abbreviate-file-name default-directory)
  471. ;; ")"))
  472. ;; (cdr ls))))
  473. ;; ;; display last modified time
  474. ;; (let ((ls (member 'mode-line-buffer-identification
  475. ;; mode-line-format)))
  476. ;; (setcdr ls
  477. ;; (cons '(:eval (concat " "
  478. ;; my-buffer-file-last-modified-time))
  479. ;; (cdr ls))))
  480. (defun buffer-list-not-start-with-space ()
  481. "Return a list of buffers that not start with whitespaces."
  482. (let ((bl (buffer-list))
  483. b nbl)
  484. (while bl
  485. (setq b (pop bl))
  486. (unless (string-equal " "
  487. (substring (buffer-name b)
  488. 0
  489. 1))
  490. (add-to-list 'nbl b)))
  491. nbl))
  492. ;; http://www.masteringemacs.org/articles/2012/09/10/hiding-replacing-modeline-strings/
  493. ;; (add-to-list 'minor-mode-alist
  494. ;; '(global-whitespace-mode ""))
  495. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  496. ;; minibuffer
  497. (setq insert-default-directory t)
  498. (setq completion-ignore-case t
  499. read-file-name-completion-ignore-case t
  500. read-buffer-completion-ignore-case t)
  501. (setq resize-mini-windows t)
  502. (temp-buffer-resize-mode 1)
  503. (savehist-mode 1)
  504. (fset 'yes-or-no-p 'y-or-n-p)
  505. ;; complete symbol when `eval'
  506. (define-key read-expression-map (kbd "TAB") 'completion-at-point)
  507. (define-key minibuffer-local-map (kbd "C-u")
  508. (lambda () (interactive) (delete-region (point-at-bol) (point))))
  509. ;; I dont know these bindings are good
  510. (define-key minibuffer-local-map (kbd "C-p") (kbd "ESC p"))
  511. (define-key minibuffer-local-map (kbd "C-n") (kbd "ESC n"))
  512. (when (safe-require-or-eval 'minibuffer-line)
  513. (set-face-underline 'minibuffer-line nil)
  514. (set-variable 'minibuffer-line-format
  515. `(,(concat user-login-name
  516. "@"
  517. (car (split-string system-name
  518. "\\."))
  519. ":")
  520. (:eval (abbreviate-file-name default-directory))
  521. " "
  522. (:eval (and (fboundp 'git-ps1-mode-get-current)
  523. (git-ps1-mode-get-current "[GIT:%s]")))))
  524. (minibuffer-line-mode 1)
  525. )
  526. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  527. ;; letters, font-lock mode and fonts
  528. ;; (set-face-background 'vertical-border (face-foreground 'mode-line))
  529. ;; (set-window-margins (selected-window) 1 1)
  530. (and (or (eq system-type 'Darwin)
  531. (eq system-type 'darwin))
  532. (fboundp 'mac-set-input-method-parameter)
  533. (mac-set-input-method-parameter 'japanese 'cursor-color "red")
  534. (mac-set-input-method-parameter 'roman 'cursor-color "black"))
  535. (when (and (boundp 'input-method-activate-hook) ; i dont know this is correct
  536. (boundp 'input-method-inactivate-hook))
  537. (add-hook 'input-method-activate-hook
  538. (lambda () (set-cursor-color "red")))
  539. (add-hook 'input-method-inactivate-hook
  540. (lambda () (set-cursor-color "black"))))
  541. (when (safe-require-or-eval 'paren)
  542. (show-paren-mode 1)
  543. (setq show-paren-delay 0.5
  544. show-paren-style 'parenthesis) ; mixed is hard to read
  545. ;; (set-face-background 'show-paren-match
  546. ;; "black")
  547. ;; ;; (face-foreground 'default))
  548. ;; (set-face-foreground 'show-paren-match
  549. ;; "white")
  550. ;; (set-face-inverse-video-p 'show-paren-match
  551. ;; t)
  552. )
  553. (transient-mark-mode 1)
  554. (global-font-lock-mode 1)
  555. (setq font-lock-global-modes
  556. '(not
  557. help-mode
  558. eshell-mode
  559. ;;term-mode
  560. Man-mode))
  561. ;; (standard-display-ascii ?\n "$\n")
  562. ;; (defvar my-eol-face
  563. ;; '(("\n" . (0 font-lock-comment-face t nil)))
  564. ;; )
  565. ;; (defvar my-tab-face
  566. ;; '(("\t" . '(0 highlight t nil))))
  567. (defvar my-jspace-face
  568. '(("\u3000" . '(0 highlight t nil))))
  569. (add-hook 'font-lock-mode-hook
  570. (lambda ()
  571. ;; (font-lock-add-keywords nil my-eol-face)
  572. (font-lock-add-keywords nil my-jspace-face)
  573. ))
  574. (when (safe-require-or-eval 'whitespace)
  575. (add-to-list 'whitespace-display-mappings ; not work
  576. `(tab-mark ?\t ,(vconcat "^I\t")))
  577. (add-to-list 'whitespace-display-mappings
  578. `(newline-mark ?\n ,(vconcat "$\n")))
  579. (setq whitespace-style '(face
  580. trailing ; trailing blanks
  581. newline ; newlines
  582. newline-mark ; use display table for newline
  583. ;; tab-mark
  584. empty ; empty lines at beg or end of buffer
  585. lines-tail ; lines over 80
  586. ))
  587. ;; (setq whitespace-newline 'font-lock-comment-face)
  588. (global-whitespace-mode t)
  589. (if (eq (display-color-cells)
  590. 256)
  591. (set-face-foreground 'whitespace-newline "brightblack")
  592. ;; (progn
  593. ;; (set-face-bold-p 'whitespace-newline
  594. ;; t))
  595. ))
  596. (and nil
  597. (safe-require-or-eval 'fill-column-indicator)
  598. (setq fill-column-indicator))
  599. ;; highlight current line
  600. ;; http://wiki.riywo.com/index.php?Meadow
  601. (defface 10sr-hl-line
  602. '((((min-colors 256)
  603. (background dark))
  604. (:background "color-234"))
  605. (((min-colors 256)
  606. (background light))
  607. (:background "color-234"))
  608. (t
  609. (:underline "black")))
  610. "*Face used by hl-line."
  611. :group '10sr)
  612. (global-hl-line-mode 1) ;; (hl-line-mode 1)
  613. (set-variable 'hl-line-face '10sr-hl-line) ;; (setq hl-line-face nil)
  614. (set-variable 'hl-line-global-modes
  615. '(not
  616. term-mode))
  617. (set-face-foreground 'font-lock-regexp-grouping-backslash "#666")
  618. (set-face-foreground 'font-lock-regexp-grouping-construct "#f60")
  619. (safe-require-or-eval 'set-modeline-color)
  620. (let ((fg (face-foreground 'default))
  621. (bg (face-background 'default)))
  622. (set-face-background 'mode-line-inactive
  623. (if (face-inverse-video-p 'mode-line) fg bg))
  624. (set-face-foreground 'mode-line-inactive
  625. (if (face-inverse-video-p 'mode-line) bg fg)))
  626. (set-face-underline 'mode-line-inactive
  627. t)
  628. (set-face-underline 'vertical-border
  629. nil)
  630. ;; Not found in MELPA nor any other package repositories
  631. (and (fetch-library
  632. "https://raw.github.com/tarao/elisp/master/end-mark.el"
  633. t)
  634. (safe-require-or-eval 'end-mark)
  635. (global-end-mark-mode))
  636. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  637. ;; file handling
  638. (when (safe-require-or-eval 'editorconfig)
  639. (editorconfig-mode 1))
  640. (setq revert-without-query '(".+"))
  641. ;; save cursor position
  642. (when (safe-require-or-eval 'saveplace)
  643. (setq-default save-place t)
  644. (setq save-place-file (concat user-emacs-directory
  645. "places")))
  646. ;; http://www.bookshelf.jp/soft/meadow_24.html#SEC260
  647. (setq make-backup-files t)
  648. ;; (make-directory (expand-file-name "~/.emacsbackup"))
  649. (setq backup-directory-alist
  650. (cons (cons "\\.*$" (expand-file-name (concat user-emacs-directory
  651. "backup")))
  652. backup-directory-alist))
  653. (setq version-control 'never)
  654. (setq delete-old-versions t)
  655. (setq auto-save-list-file-prefix (expand-file-name (concat user-emacs-directory
  656. "auto-save/")))
  657. (setq delete-auto-save-files t)
  658. (add-to-list 'completion-ignored-extensions ".bak")
  659. ;; (setq delete-by-moving-to-trash t
  660. ;; trash-directory "~/.emacs.d/trash")
  661. (add-hook 'after-save-hook
  662. 'executable-make-buffer-file-executable-if-script-p)
  663. (set (defvar bookmark-default-file)
  664. (expand-file-name (concat user-emacs-directory
  665. "bmk")))
  666. (with-eval-after-load 'recentf
  667. (defvar recentf-exclude nil)
  668. (add-to-list 'recentf-exclude
  669. (regexp-quote bookmark-default-file)))
  670. (when (safe-require-or-eval 'smart-revert)
  671. (smart-revert-on))
  672. ;; autosave
  673. (when (safe-require-or-eval 'autosave)
  674. (autosave-set 2))
  675. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  676. ;; buffer killing
  677. ;; (defun my-delete-window-killing-buffer () nil)
  678. (defun my-query-kill-current-buffer ()
  679. "Interactively kill current buffer."
  680. (interactive)
  681. (if (y-or-n-p (concat "kill current buffer? :"))
  682. (kill-buffer (current-buffer))))
  683. ;;(global-set-key "\C-xk" 'my-query-kill-current-buffer)
  684. (substitute-key-definition 'kill-buffer
  685. 'my-query-kill-current-buffer
  686. global-map)
  687. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  688. ;; share clipboard with x
  689. ;; this page describes this in details, but only these sexps seem to be needed
  690. ;; http://garin.jp/doc/Linux/xwindow_clipboard
  691. (and (not window-system)
  692. (not (eq window-system 'mac))
  693. (getenv "DISPLAY")
  694. (not (equal (getenv "DISPLAY") ""))
  695. (executable-find "xclip")
  696. ;; (< emacs-major-version 24)
  697. (safe-require-or-eval 'xclip)
  698. nil
  699. (turn-on-xclip))
  700. (and (eq system-type 'darwin)
  701. (safe-require-or-eval 'pasteboard)
  702. (turn-on-pasteboard)
  703. (getenv "TMUX")
  704. (pasteboard-enable-rtun))
  705. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  706. ;; https://github.com/lunaryorn/flycheck
  707. (when (safe-require-or-eval 'flycheck)
  708. (call-after-init 'global-flycheck-mode))
  709. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  710. ;; some modes and hooks
  711. (set-variable 'ac-ignore-case nil)
  712. (when (autoload-eval-lazily 'term-run '(term-run-shell-command term-run))
  713. (define-key ctl-x-map "t" 'term-run-shell-command))
  714. (add-to-list 'safe-local-variable-values
  715. '(encoding utf-8))
  716. (setq enable-local-variables :safe)
  717. (when (safe-require-or-eval 'remember-major-modes-mode)
  718. (remember-major-modes-mode 1))
  719. ;; Detect file type from shebang and set major-mode.
  720. (add-to-list 'interpreter-mode-alist
  721. '("python3" . python-mode))
  722. (add-to-list 'interpreter-mode-alist
  723. '("python2" . python-mode))
  724. ;; http://fukuyama.co/foreign-regexp
  725. '(and (safe-require-or-eval 'foreign-regexp)
  726. (progn
  727. (setq foreign-regexp/regexp-type 'perl)
  728. '(setq reb-re-syntax 'foreign-regexp)
  729. ))
  730. (autoload-eval-lazily 'sql '(sql-mode)
  731. (safe-require-or-eval 'sql-indent))
  732. (when (autoload-eval-lazily 'git-command)
  733. (define-key ctl-x-map "g" 'git-command))
  734. (when (fetch-library
  735. "http://www.emacswiki.org/emacs/download/sl.el"
  736. t)
  737. (autoload-eval-lazily 'sl))
  738. (with-eval-after-load 'make-mode
  739. (defvar makefile-mode-map (make-sparse-keymap))
  740. (define-key makefile-mode-map (kbd "C-m") 'newline-and-indent)
  741. ;; this functions is set in write-file-functions, i cannot find any
  742. ;; good way to remove this.
  743. (fset 'makefile-warn-suspicious-lines 'ignore))
  744. (with-eval-after-load 'verilog-mode
  745. (defvar verilog-mode-map (make-sparse-keymap))
  746. (define-key verilog-mode-map ";" 'self-insert-command))
  747. (setq diff-switches "-u")
  748. (with-eval-after-load 'diff-mode
  749. ;; (when (and (eq major-mode
  750. ;; 'diff-mode)
  751. ;; (not buffer-file-name))
  752. ;; ;; do not pass when major-mode is derived mode of diff-mode
  753. ;; (view-mode 1))
  754. (set-face-attribute 'diff-header nil
  755. :foreground nil
  756. :background nil
  757. :weight 'bold)
  758. (set-face-attribute 'diff-file-header nil
  759. :foreground nil
  760. :background nil
  761. :weight 'bold)
  762. (set-face-foreground 'diff-index-face "blue")
  763. (set-face-attribute 'diff-hunk-header nil
  764. :foreground "cyan"
  765. :weight 'normal)
  766. (set-face-attribute 'diff-context nil
  767. ;; :foreground "white"
  768. :foreground nil
  769. :weight 'normal)
  770. (set-face-foreground 'diff-removed-face "red")
  771. (set-face-foreground 'diff-added-face "green")
  772. (set-face-background 'diff-removed-face nil)
  773. (set-face-background 'diff-added-face nil)
  774. (set-face-attribute 'diff-changed nil
  775. :foreground "magenta"
  776. :weight 'normal)
  777. (set-face-attribute 'diff-refine-change nil
  778. :foreground nil
  779. :background nil
  780. :weight 'bold
  781. :inverse-video t)
  782. ;; Annoying !
  783. ;;(diff-auto-refine-mode)
  784. )
  785. ;; (ffap-bindings)
  786. (set-variable 'sh-here-document-word "__EOC__")
  787. (setq auto-mode-alist
  788. `(("autostart\\'" . sh-mode)
  789. ("xinitrc\\'" . sh-mode)
  790. ("xprograms\\'" . sh-mode)
  791. ("PKGBUILD\\'" . sh-mode)
  792. ,@auto-mode-alist))
  793. ;; TODO: check if this is required
  794. (and (autoload-eval-lazily 'groovy-mode)
  795. (add-to-list 'auto-mode-alist
  796. '("build.gradle\\'" . groovy-mode)))
  797. (with-eval-after-load 'yaml-mode
  798. (defvar yaml-mode-map (make-sparse-keymap))
  799. (define-key yaml-mode-map (kbd "C-m") 'newline))
  800. (with-eval-after-load 'html-mode
  801. (defvar html-mode-map (make-sparse-keymap))
  802. (define-key html-mode-map (kbd "C-m") 'reindent-then-newline-and-indent))
  803. (with-eval-after-load 'text-mode
  804. (define-key text-mode-map (kbd "C-m") 'newline))
  805. (add-to-list 'Info-default-directory-list
  806. (expand-file-name "~/.info/emacs-ja"))
  807. (with-eval-after-load 'apropos
  808. (defvar apropos-mode-map (make-sparse-keymap))
  809. (define-key apropos-mode-map "n" 'next-line)
  810. (define-key apropos-mode-map "p" 'previous-line))
  811. (with-eval-after-load 'isearch
  812. ;; (define-key isearch-mode-map
  813. ;; (kbd "C-j") 'isearch-other-control-char)
  814. ;; (define-key isearch-mode-map
  815. ;; (kbd "C-k") 'isearch-other-control-char)
  816. ;; (define-key isearch-mode-map
  817. ;; (kbd "C-h") 'isearch-other-control-char)
  818. (define-key isearch-mode-map (kbd "C-h") 'isearch-delete-char)
  819. (define-key isearch-mode-map (kbd "M-r")
  820. 'isearch-query-replace-regexp))
  821. ;; do not cleanup isearch highlight: use `lazy-highlight-cleanup' to remove
  822. (setq lazy-highlight-cleanup nil)
  823. ;; face for isearch highlighing
  824. (set-face-attribute 'lazy-highlight
  825. nil
  826. :foreground `unspecified
  827. :background `unspecified
  828. :underline t
  829. ;; :weight `bold
  830. )
  831. (add-hook 'outline-mode-hook
  832. (lambda ()
  833. (when (string-match "\\.md\\'" buffer-file-name)
  834. (set (make-local-variable 'outline-regexp) "#+ "))))
  835. (add-to-list 'auto-mode-alist (cons "\\.ol\\'" 'outline-mode))
  836. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'outline-mode))
  837. (when (autoload-eval-lazily 'markdown-mode
  838. '(markdown-mode gfm-mode)
  839. (defvar gfm-mode-map (make-sparse-keymap))
  840. (define-key gfm-mode-map (kbd "C-m") 'electric-indent-just-newline))
  841. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'gfm-mode))
  842. (set-variable 'markdown-command (or (executable-find "markdown")
  843. (executable-find "markdown.pl")
  844. ""))
  845. (add-hook 'markdown-mode-hook
  846. (lambda ()
  847. (outline-minor-mode 1)
  848. (flyspell-mode)
  849. (set (make-local-variable 'comment-start) ";")))
  850. )
  851. ;; c-mode
  852. ;; http://www.emacswiki.org/emacs/IndentingC
  853. ;; http://en.wikipedia.org/wiki/Indent_style
  854. ;; http://d.hatena.ne.jp/emergent/20070203/1170512717
  855. ;; http://seesaawiki.jp/whiteflare503/d/Emacs%20%a5%a4%a5%f3%a5%c7%a5%f3%a5%c8
  856. (with-eval-after-load 'cc-vars
  857. (defvar c-default-style nil)
  858. (add-to-list 'c-default-style
  859. '(c-mode . "k&r"))
  860. (add-to-list 'c-default-style
  861. '(c++-mode . "k&r"))
  862. (add-hook 'c-mode-common-hook
  863. (lambda ()
  864. ;; why c-basic-offset in k&r style defaults to 5 ???
  865. (set-variable 'c-basic-offset 4)
  866. (set-variable 'indent-tabs-mode nil)
  867. ;; (set-face-foreground 'font-lock-keyword-face "blue")
  868. (c-toggle-hungry-state -1)
  869. ;; (and (require 'gtags nil t)
  870. ;; (gtags-mode 1))
  871. )))
  872. (when (autoload-eval-lazily 'php-mode)
  873. (add-hook 'php-mode-hook
  874. (lambda ()
  875. (set-variable 'c-basic-offset 2))))
  876. (autoload-eval-lazily 'js2-mode nil
  877. ;; currently do not use js2-mode
  878. ;; (add-to-list 'auto-mode-alist '("\\.js\\'" . js2-mode))
  879. ;; (add-to-list 'auto-mode-alist '("\\.jsm\\'" . js2-mode))
  880. (defvar js2-mode-map (make-sparse-keymap))
  881. (define-key js2-mode-map (kbd "C-m") (lambda ()
  882. (interactive)
  883. (js2-enter-key)
  884. (indent-for-tab-command)))
  885. ;; (add-hook (kill-local-variable 'before-save-hook)
  886. ;; 'js2-before-save)
  887. ;; (add-hook 'before-save-hook
  888. ;; 'my-indent-buffer
  889. ;; nil
  890. ;; t)
  891. )
  892. (with-eval-after-load 'js
  893. (set-variable 'js-indent-level 2))
  894. (add-to-list 'interpreter-mode-alist
  895. '("node" . js-mode))
  896. (when (autoload-eval-lazily 'flymake-jslint
  897. '(flymake-jslint-load))
  898. (autoload-eval-lazily 'js nil
  899. (add-hook 'js-mode-hook
  900. 'flymake-jslint-load)))
  901. (safe-require-or-eval 'js-doc)
  902. (add-hook 'haskell-mode-hook 'turn-on-haskell-indentation)
  903. (when (safe-require-or-eval 'uniquify)
  904. (setq uniquify-buffer-name-style 'post-forward-angle-brackets)
  905. (setq uniquify-ignore-buffers-re "*[^*]+*")
  906. (setq uniquify-min-dir-content 1))
  907. (with-eval-after-load 'view
  908. (defvar view-mode-map (make-sparse-keymap))
  909. (define-key view-mode-map "j" 'scroll-up-line)
  910. (define-key view-mode-map "k" 'scroll-down-line)
  911. (define-key view-mode-map "v" 'toggle-read-only)
  912. (define-key view-mode-map "q" 'bury-buffer)
  913. ;; (define-key view-mode-map "/" 'nonincremental-re-search-forward)
  914. ;; (define-key view-mode-map "?" 'nonincremental-re-search-backward)
  915. ;; (define-key view-mode-map
  916. ;; "n" 'nonincremental-repeat-search-forward)
  917. ;; (define-key view-mode-map
  918. ;; "N" 'nonincremental-repeat-search-backward)
  919. (define-key view-mode-map "/" 'isearch-forward-regexp)
  920. (define-key view-mode-map "?" 'isearch-backward-regexp)
  921. (define-key view-mode-map "n" 'isearch-repeat-forward)
  922. (define-key view-mode-map "N" 'isearch-repeat-backward)
  923. (define-key view-mode-map (kbd "C-m") 'my-rgrep-symbol-at-point))
  924. (global-set-key "\M-r" 'view-mode)
  925. ;; (setq view-read-only t)
  926. (add-hook 'Man-mode-hook
  927. (lambda ()
  928. (view-mode 1)
  929. (setq truncate-lines nil)))
  930. (set-variable 'Man-notify-method (if window-system
  931. 'newframe
  932. 'aggressive))
  933. (set-variable 'woman-cache-filename (expand-file-name (concat user-emacs-directory
  934. "woman_cache.el")))
  935. (defalias 'man 'woman)
  936. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  937. ;; python
  938. (when (autoload-eval-lazily 'python '(python-mode)
  939. (defvar python-mode-map (make-sparse-keymap))
  940. (define-key python-mode-map (kbd "C-c C-e") 'my-python-run-as-command)
  941. (define-key python-mode-map (kbd "C-c C-b") 'my-python-display-python-buffer)
  942. (define-key python-mode-map (kbd "C-m") 'newline-and-indent)
  943. (defvar inferior-python-mode-map (make-sparse-keymap))
  944. (define-key inferior-python-mode-map (kbd "<up>") 'comint-previous-input)
  945. (define-key inferior-python-mode-map (kbd "<down>") 'comint-next-input)
  946. )
  947. (set-variable 'python-python-command (or (executable-find "python3")
  948. (executable-find "python")))
  949. ;; (defun my-python-run-as-command ()
  950. ;; ""
  951. ;; (interactive)
  952. ;; (shell-command (concat python-python-command " " buffer-file-name)))
  953. (defun my-python-display-python-buffer ()
  954. ""
  955. (interactive)
  956. (defvar python-buffer nil)
  957. (set-window-text-height (display-buffer python-buffer
  958. t)
  959. 7))
  960. (add-hook 'inferior-python-mode-hook
  961. (lambda ()
  962. (my-python-display-python-buffer))))
  963. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  964. ;; gauche-mode
  965. ;; http://d.hatena.ne.jp/kobapan/20090305/1236261804
  966. ;; http://www.katch.ne.jp/~leque/software/repos/gauche-mode/gauche-mode.el
  967. (when (and (fetch-library
  968. "http://www.katch.ne.jp/~leque/software/repos/gauche-mode/gauche-mode.el"
  969. t)
  970. (autoload-eval-lazily 'gauche-mode '(gauche-mode run-scheme)
  971. (defvar gauche-mode-map (make-sparse-keymap))
  972. (defvar scheme-mode-map (make-sparse-keymap))
  973. (define-key gauche-mode-map
  974. (kbd "C-c C-z") 'run-gauche-other-window)
  975. (define-key scheme-mode-map
  976. (kbd "C-c C-c") 'scheme-send-buffer)
  977. (define-key scheme-mode-map
  978. (kbd "C-c C-b") 'my-scheme-display-scheme-buffer)))
  979. (let ((s (executable-find "gosh")))
  980. (set-variable 'scheme-program-name s)
  981. (set-variable 'gauche-program-name s))
  982. (defvar gauche-program-name nil)
  983. (defvar scheme-buffer nil)
  984. (defun run-gauche-other-window ()
  985. "Run gauche on other window"
  986. (interactive)
  987. (switch-to-buffer-other-window
  988. (get-buffer-create "*scheme*"))
  989. (run-gauche))
  990. (defun run-gauche ()
  991. "run gauche"
  992. (interactive)
  993. (run-scheme gauche-program-name)
  994. )
  995. (defun scheme-send-buffer ()
  996. ""
  997. (interactive)
  998. (scheme-send-region (point-min) (point-max))
  999. (my-scheme-display-scheme-buffer)
  1000. )
  1001. (defun my-scheme-display-scheme-buffer ()
  1002. ""
  1003. (interactive)
  1004. (set-window-text-height (display-buffer scheme-buffer
  1005. t)
  1006. 7))
  1007. (add-hook 'scheme-mode-hook
  1008. (lambda ()
  1009. nil))
  1010. (add-hook 'inferior-scheme-mode-hook
  1011. (lambda ()
  1012. ;; (my-scheme-display-scheme-buffer)
  1013. ))
  1014. (setq auto-mode-alist
  1015. (cons '("\.gosh\\'" . gauche-mode) auto-mode-alist))
  1016. (setq auto-mode-alist
  1017. (cons '("\.gaucherc\\'" . gauche-mode) auto-mode-alist))
  1018. )
  1019. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1020. ;; term mode
  1021. ;; (setq multi-term-program shell-file-name)
  1022. (when (autoload-eval-lazily 'multi-term)
  1023. (set-variable 'multi-term-switch-after-close nil)
  1024. (set-variable 'multi-term-dedicated-select-after-open-p t)
  1025. (set-variable 'multi-term-dedicated-window-height 20))
  1026. (when (autoload-eval-lazily 'term '(term ansi-term)
  1027. (defvar term-raw-map (make-sparse-keymap))
  1028. ;; (define-key term-raw-map "\C-xl" 'term-line-mode)
  1029. ;; (define-key term-mode-map "\C-xc" 'term-char-mode)
  1030. (define-key term-raw-map (kbd "<up>") 'scroll-down-line)
  1031. (define-key term-raw-map (kbd "<down>") 'scroll-up-line)
  1032. (define-key term-raw-map (kbd "<right>") 'scroll-up)
  1033. (define-key term-raw-map (kbd "<left>") 'scroll-down)
  1034. (define-key term-raw-map (kbd "C-p") 'term-send-raw)
  1035. (define-key term-raw-map (kbd "C-n") 'term-send-raw)
  1036. (define-key term-raw-map "q" 'my-term-quit-or-send-raw)
  1037. ;; (define-key term-raw-map (kbd "ESC") 'term-send-raw)
  1038. (define-key term-raw-map [delete] 'term-send-raw)
  1039. (define-key term-raw-map (kbd "DEL") 'term-send-backspace)
  1040. (define-key term-raw-map "\C-y" 'term-paste)
  1041. (define-key term-raw-map
  1042. "\C-c" 'term-send-raw) ;; 'term-interrupt-subjob)
  1043. '(define-key term-mode-map (kbd "C-x C-q") 'term-pager-toggle)
  1044. ;; (dolist (key '("<up>" "<down>" "<right>" "<left>"))
  1045. ;; (define-key term-raw-map (read-kbd-macro key) 'term-send-raw))
  1046. ;; (define-key term-raw-map "\C-d" 'delete-char)
  1047. ;; (define-key term-raw-map "\C-q" 'move-beginning-of-line)
  1048. ;; (define-key term-raw-map "\C-r" 'term-send-raw)
  1049. ;; (define-key term-raw-map "\C-s" 'term-send-raw)
  1050. ;; (define-key term-raw-map "\C-f" 'forward-char)
  1051. ;; (define-key term-raw-map "\C-b" 'backward-char)
  1052. ;; (define-key term-raw-map "\C-t" 'set-mark-command)
  1053. )
  1054. (defun my-term-quit-or-send-raw ()
  1055. ""
  1056. (interactive)
  1057. (if (get-buffer-process (current-buffer))
  1058. (call-interactively 'term-send-raw)
  1059. (kill-buffer)))
  1060. ;; http://d.hatena.ne.jp/goinger/20100416/1271399150
  1061. ;; (setq term-ansi-default-program shell-file-name)
  1062. (add-hook 'term-setup-hook
  1063. (lambda ()
  1064. (set-variable 'term-display-table (make-display-table))))
  1065. (add-hook 'term-mode-hook
  1066. (lambda ()
  1067. (defvar term-raw-map (make-sparse-keymap))
  1068. ;; (unless (memq (current-buffer)
  1069. ;; (and (featurep 'multi-term)
  1070. ;; (defvar multi-term-buffer-list)
  1071. ;; ;; current buffer is not multi-term buffer
  1072. ;; multi-term-buffer-list))
  1073. ;; )
  1074. (set (make-local-variable 'scroll-margin) 0)
  1075. ;; (set (make-local-variable 'cua-enable-cua-keys) nil)
  1076. ;; (cua-mode 0)
  1077. ;; (and cua-mode
  1078. ;; (local-unset-key (kbd "C-c")))
  1079. ;; (define-key cua--prefix-override-keymap
  1080. ;;"\C-c" 'term-interrupt-subjob)
  1081. (set (make-local-variable (defvar hl-line-range-function))
  1082. (lambda ()
  1083. '(0 . 0)))
  1084. (define-key term-raw-map
  1085. "\C-x" (lookup-key (current-global-map) "\C-x"))
  1086. (define-key term-raw-map
  1087. "\C-z" (lookup-key (current-global-map) "\C-z"))
  1088. ))
  1089. ;; (add-hook 'term-exec-hook 'forward-char)
  1090. )
  1091. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1092. ;; buffer switching
  1093. (defvar bs-configurations)
  1094. (when (autoload-eval-lazily 'bs '(bs-show)
  1095. ;; (add-to-list 'bs-configurations
  1096. ;; '("processes" nil get-buffer-process ".*" nil nil))
  1097. (add-to-list 'bs-configurations
  1098. '("files-and-terminals" nil nil nil
  1099. (lambda (buf)
  1100. (and (bs-visits-non-file buf)
  1101. (save-excursion
  1102. (set-buffer buf)
  1103. (not (memq major-mode
  1104. '(term-mode
  1105. eshell-mode))))))))
  1106. ;; (setq bs-configurations (list
  1107. ;; '("processes" nil get-buffer-process ".*" nil nil)
  1108. ;; '("files-and-scratch" "^\\*scratch\\*$" nil nil
  1109. ;; bs-visits-non-file bs-sort-buffer-interns-are-last)))
  1110. )
  1111. ;; (global-set-key "\C-x\C-b" 'bs-show)
  1112. (defalias 'list-buffers 'bs-show)
  1113. (set-variable 'bs-default-configuration "files-and-terminals")
  1114. (set-variable 'bs-default-sort-name "by nothing")
  1115. (add-hook 'bs-mode-hook
  1116. (lambda ()
  1117. ;; (setq bs-default-configuration "files")
  1118. ;; (and bs--show-all
  1119. ;; (call-interactively 'bs-toggle-show-all))
  1120. (set (make-local-variable 'scroll-margin) 0))))
  1121. ;;(iswitchb-mode 1)
  1122. (icomplete-mode)
  1123. (defun iswitchb-buffer-display-other-window ()
  1124. "Do iswitchb in other window."
  1125. (interactive)
  1126. (let ((iswitchb-default-method 'display))
  1127. (call-interactively 'iswitchb-buffer)))
  1128. ;;;;;;;;;;;;;;;;;;;;;;;;
  1129. ;; ilookup
  1130. (with-eval-after-load 'ilookup
  1131. (set-variable 'ilookup-dict-alist
  1132. '(
  1133. ("sdcv" . (lambda (word)
  1134. (shell-command-to-string
  1135. (format "sdcv -n '%s'"
  1136. word))))
  1137. ("en" . (lambda (word)
  1138. (shell-command-to-string
  1139. (format "sdcv -n -u dictd_www.dict.org_gcide '%s'"
  1140. word))))
  1141. ("ja" . (lambda (word)
  1142. (shell-command-to-string
  1143. (format "sdcv -n -u EJ-GENE95 -u jmdict-en-ja '%s'"
  1144. word))))
  1145. ("jaj" . (lambda (word)
  1146. (shell-command-to-string
  1147. (format "sdcv -n -u jmdict-en-ja '%s'"
  1148. word))))
  1149. ("jag" .
  1150. (lambda (word)
  1151. (with-temp-buffer
  1152. (insert (shell-command-to-string
  1153. (format "sdcv -n -u 'Genius English-Japanese' '%s'"
  1154. word)))
  1155. (html2text)
  1156. (buffer-substring (point-min)
  1157. (point-max)))))
  1158. ("alc" . (lambda (word)
  1159. (shell-command-to-string
  1160. (format "alc '%s' | head -n 20"
  1161. word))))
  1162. ("app" . (lambda (word)
  1163. (shell-command-to-string
  1164. (format "dict_app '%s'"
  1165. word))))
  1166. ;; letters broken
  1167. ("ms" .
  1168. (lambda (word)
  1169. (let ((url (concat
  1170. "http://api.microsofttranslator.com/V2/Ajax.svc/"
  1171. "Translate?appId=%s&text=%s&to=%s"))
  1172. (apikey "3C9778666C5BA4B406FFCBEE64EF478963039C51")
  1173. (target "ja")
  1174. (eword (url-hexify-string word)))
  1175. (with-current-buffer (url-retrieve-synchronously
  1176. (format url
  1177. apikey
  1178. eword
  1179. target))
  1180. (message "")
  1181. (goto-char (point-min))
  1182. (search-forward-regexp "^$"
  1183. nil
  1184. t)
  1185. (url-unhex-string (buffer-substring-no-properties
  1186. (point)
  1187. (point-max)))))))
  1188. ))
  1189. ;; (funcall (cdr (assoc "ms"
  1190. ;; ilookup-alist))
  1191. ;; "dictionary")
  1192. ;; (switch-to-buffer (url-retrieve-synchronously "http://api.microsofttranslator.com/V2/Ajax.svc/Translate?appId=3C9778666C5BA4B406FFCBEE64EF478963039C51&text=dictionary&to=ja"))
  1193. ;; (switch-to-buffer (url-retrieve-synchronously "http://google.com"))
  1194. (set-variable 'ilookup-default "ja")
  1195. (when (locate-library "google-translate")
  1196. (defvar ilookup-dict-alist nil)
  1197. (add-to-list 'ilookup-dict-alist
  1198. '("gt" .
  1199. (lambda (word)
  1200. (save-excursion
  1201. (google-translate-translate "auto"
  1202. "ja"
  1203. word))
  1204. (with-current-buffer "*Google Translate*"
  1205. (buffer-substring-no-properties (point-min)
  1206. (point-max)))))))
  1207. )
  1208. (when (autoload-eval-lazily 'google-translate '(google-translate-translate
  1209. google-translate-at-point))
  1210. (set-variable 'google-translate-default-source-language "auto")
  1211. (set-variable 'google-translate-default-target-language "ja"))
  1212. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1213. ;; vc
  1214. (require 'vc)
  1215. (setq vc-handled-backends '())
  1216. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1217. ;; recentf-mode
  1218. (set-variable 'recentf-save-file (expand-file-name (concat user-emacs-directory
  1219. "recentf")))
  1220. (set-variable 'recentf-max-menu-items 20)
  1221. (set-variable 'recentf-max-saved-items 30)
  1222. (set-variable 'recentf-show-file-shortcuts-flag nil)
  1223. (when (safe-require-or-eval 'recentf)
  1224. (add-to-list 'recentf-exclude
  1225. (regexp-quote recentf-save-file))
  1226. (add-to-list 'recentf-exclude
  1227. (regexp-quote (expand-file-name user-emacs-directory)))
  1228. (define-key ctl-x-map (kbd "C-r") 'recentf-open-files)
  1229. (remove-hook 'find-file-hook
  1230. 'recentf-track-opened-file)
  1231. (defun my-recentf-load-track-save-list ()
  1232. "Load current recentf list from file, track current visiting file, then save
  1233. the list."
  1234. (recentf-load-list)
  1235. (recentf-track-opened-file)
  1236. (recentf-save-list))
  1237. (add-hook 'find-file-hook
  1238. 'my-recentf-load-track-save-list)
  1239. (add-hook 'kill-emacs-hook
  1240. 'recentf-load-list)
  1241. ;;(run-with-idle-timer 5 t 'recentf-save-list)
  1242. ;; (add-hook 'find-file-hook
  1243. ;; (lambda ()
  1244. ;; (recentf-add-file default-directory)))
  1245. (and (autoload-eval-lazily 'recentf-show)
  1246. (define-key ctl-x-map (kbd "C-r") 'recentf-show)
  1247. (add-hook 'recentf-show-before-listing-hook
  1248. 'recentf-load-list))
  1249. (recentf-mode 1)
  1250. (define-key recentf-dialog-mode-map (kbd "<up>") 'previous-line)
  1251. (define-key recentf-dialog-mode-map (kbd "<down>") 'next-line)
  1252. (define-key recentf-dialog-mode-map "p" 'previous-line)
  1253. (define-key recentf-dialog-mode-map "n" 'next-line)
  1254. (add-hook 'recentf-dialog-mode-hook
  1255. (lambda ()
  1256. ;; (recentf-save-list)
  1257. ;; (define-key recentf-dialog-mode-map (kbd "C-x C-f")
  1258. ;; 'my-recentf-cd-and-find-file)
  1259. (cd "~/"))))
  1260. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1261. ;; dired
  1262. (defun my-dired-echo-file-head (arg)
  1263. ""
  1264. (interactive "P")
  1265. (let ((f (dired-get-filename)))
  1266. (message "%s"
  1267. (with-temp-buffer
  1268. (insert-file-contents f)
  1269. (buffer-substring-no-properties
  1270. (point-min)
  1271. (progn (goto-char (point-min))
  1272. (forward-line (1- (if arg
  1273. (prefix-numeric-value arg)
  1274. 7)))
  1275. (point-at-eol)))))))
  1276. (defun my-dired-diff ()
  1277. ""
  1278. (interactive)
  1279. (let ((files (dired-get-marked-files nil nil nil t)))
  1280. (if (eq (car files)
  1281. t)
  1282. (diff (cadr files) (dired-get-filename))
  1283. (message "One file must be marked!"))))
  1284. (defun dired-get-file-info ()
  1285. "dired get file info"
  1286. (interactive)
  1287. (let ((f (shell-quote-argument (dired-get-filename t))))
  1288. (if (file-directory-p f)
  1289. (progn
  1290. (message "Calculating disk usage...")
  1291. (shell-command (concat "du -hsD "
  1292. f)))
  1293. (shell-command (concat "file "
  1294. f)))))
  1295. (defun my-dired-scroll-up ()
  1296. ""
  1297. (interactive)
  1298. (my-dired-previous-line (- (window-height) 1)))
  1299. (defun my-dired-scroll-down ()
  1300. ""
  1301. (interactive)
  1302. (my-dired-next-line (- (window-height) 1)))
  1303. ;; (defun my-dired-forward-line (arg)
  1304. ;; ""
  1305. ;; (interactive "p"))
  1306. (defun my-dired-previous-line (arg)
  1307. ""
  1308. (interactive "p")
  1309. (if (> arg 0)
  1310. (progn
  1311. (if (eq (line-number-at-pos)
  1312. 1)
  1313. (goto-char (point-max))
  1314. (forward-line -1))
  1315. (my-dired-previous-line (if (or (dired-get-filename nil t)
  1316. (dired-get-subdir))
  1317. (- arg 1)
  1318. arg)))
  1319. (dired-move-to-filename)))
  1320. (defun my-dired-next-line (arg)
  1321. ""
  1322. (interactive "p")
  1323. (if (> arg 0)
  1324. (progn
  1325. (if (eq (point)
  1326. (point-max))
  1327. (goto-char (point-min))
  1328. (forward-line 1))
  1329. (my-dired-next-line (if (or (dired-get-filename nil t)
  1330. (dired-get-subdir))
  1331. (- arg 1)
  1332. arg)))
  1333. (dired-move-to-filename)))
  1334. ;;http://bach.istc.kobe-u.ac.jp/lect/tamlab/ubuntu/emacs.html
  1335. (if (eq window-system 'mac)
  1336. (setq dired-listing-switches "-lhF")
  1337. (setq dired-listing-switches "-lhF --time-style=long-iso")
  1338. )
  1339. (setq dired-listing-switches "-lhF")
  1340. (put 'dired-find-alternate-file 'disabled nil)
  1341. ;; when using dired-find-alternate-file
  1342. ;; reuse current dired buffer for the file to open
  1343. (set-variable 'dired-ls-F-marks-symlinks t)
  1344. (when (safe-require-or-eval 'ls-lisp)
  1345. (setq ls-lisp-use-insert-directory-program nil) ; always use ls-lisp
  1346. (setq ls-lisp-dirs-first t)
  1347. (setq ls-lisp-use-localized-time-format t)
  1348. (setq ls-lisp-format-time-list
  1349. '("%Y-%m-%d %H:%M"
  1350. "%Y-%m-%d ")))
  1351. (set-variable 'dired-dwim-target t)
  1352. (set-variable 'dired-isearch-filenames t)
  1353. (set-variable 'dired-hide-details-hide-symlink-targets nil)
  1354. (set-variable 'dired-hide-details-hide-information-lines nil)
  1355. ;; (add-hook 'dired-after-readin-hook
  1356. ;; 'my-replace-nasi-none)
  1357. ;; (add-hook 'after-init-hook
  1358. ;; (lambda ()
  1359. ;; (dired ".")))
  1360. (with-eval-after-load 'dired
  1361. (defvar dired-mode-map (make-sparse-keymap))
  1362. (define-key dired-mode-map "o" 'my-dired-x-open)
  1363. (define-key dired-mode-map "i" 'dired-get-file-info)
  1364. (define-key dired-mode-map "f" 'find-file)
  1365. (define-key dired-mode-map "!" 'shell-command)
  1366. (define-key dired-mode-map "&" 'async-shell-command)
  1367. (define-key dired-mode-map "X" 'dired-do-async-shell-command)
  1368. (define-key dired-mode-map "=" 'my-dired-diff)
  1369. (define-key dired-mode-map "B" 'gtkbm-add-current-dir)
  1370. (define-key dired-mode-map "b" 'gtkbm)
  1371. (define-key dired-mode-map "h" 'my-dired-echo-file-head)
  1372. (define-key dired-mode-map "@" (lambda ()
  1373. (interactive) (my-x-open ".")))
  1374. (define-key dired-mode-map (kbd "TAB") 'other-window)
  1375. ;; (define-key dired-mode-map "P" 'my-dired-do-pack-or-unpack)
  1376. (define-key dired-mode-map "/" 'dired-isearch-filenames)
  1377. (define-key dired-mode-map (kbd "DEL") 'dired-up-directory)
  1378. (define-key dired-mode-map (kbd "C-h") 'dired-up-directory)
  1379. (substitute-key-definition 'dired-next-line
  1380. 'my-dired-next-line
  1381. dired-mode-map)
  1382. (substitute-key-definition 'dired-previous-line
  1383. 'my-dired-previous-line
  1384. dired-mode-map)
  1385. ;; (define-key dired-mode-map (kbd "C-p") 'my-dired-previous-line)
  1386. ;; (define-key dired-mode-map (kbd "p") 'my-dired-previous-line)
  1387. ;; (define-key dired-mode-map (kbd "C-n") 'my-dired-next-line)
  1388. ;; (define-key dired-mode-map (kbd "n") 'my-dired-next-line)
  1389. (define-key dired-mode-map (kbd "<left>") 'my-dired-scroll-up)
  1390. (define-key dired-mode-map (kbd "<right>") 'my-dired-scroll-down)
  1391. (define-key dired-mode-map (kbd "ESC p") 'my-dired-scroll-up)
  1392. (define-key dired-mode-map (kbd "ESC n") 'my-dired-scroll-down)
  1393. (add-hook 'dired-mode-hook
  1394. (lambda ()
  1395. (when (fboundp 'dired-hide-details-mode)
  1396. (dired-hide-details-mode t)
  1397. (local-set-key "l" 'dired-hide-details-mode))
  1398. (let ((file "._Icon\015"))
  1399. (when nil
  1400. '(file-readable-p file)
  1401. (delete-file file)))))
  1402. (when (autoload-eval-lazily 'pack '(dired-do-pack-or-unpack pack-pack))
  1403. (with-eval-after-load 'dired
  1404. (define-key dired-mode-map "P" 'dired-do-pack-or-unpack)))
  1405. (when (autoload-eval-lazily 'dired-list-all-mode)
  1406. (setq dired-listing-switches "-lhF")
  1407. (with-eval-after-load 'dired
  1408. (define-key dired-mode-map "a" 'dired-list-all-mode))))
  1409. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1410. ;; eshell
  1411. (set-variable 'eshell-banner-message (format "Welcome to the Emacs shell
  1412. %s
  1413. C-x t to toggling emacs-text-mode
  1414. "
  1415. (shell-command-to-string "uname -a")
  1416. ))
  1417. (defvar eshell-text-mode-map
  1418. (let ((map (make-sparse-keymap)))
  1419. (define-key map (kbd "C-x t") 'eshell-text-mode-toggle)
  1420. map))
  1421. (define-derived-mode eshell-text-mode text-mode
  1422. "Eshell-Text"
  1423. "Text-mode for Eshell."
  1424. nil)
  1425. (defun eshell-text-mode-toggle ()
  1426. "Toggle eshell-text-mode and eshell-mode."
  1427. (interactive)
  1428. (cond ((eq major-mode
  1429. 'eshell-text-mode)
  1430. (goto-char (point-max))
  1431. (message "Eshell text mode disabled")
  1432. (eshell-mode))
  1433. ((eq major-mode
  1434. 'eshell-mode)
  1435. (message "Eshell text mode enabled")
  1436. (eshell-write-history)
  1437. (eshell-text-mode))
  1438. (t
  1439. (message "Not in eshell buffer")
  1440. nil)))
  1441. (defun my-eshell-backward-delete-char ()
  1442. (interactive)
  1443. (when (< (save-excursion
  1444. (eshell-bol)
  1445. (point))
  1446. (point))
  1447. (backward-delete-char 1)))
  1448. (defun my-file-owner-p (file)
  1449. "t if FILE is owned by me."
  1450. (eq (user-uid) (nth 2 (file-attributes file))))
  1451. "http://www.bookshelf.jp/pukiwiki/pukiwiki.php\
  1452. ?Eshell%A4%F2%BB%C8%A4%A4%A4%B3%A4%CA%A4%B9"
  1453. ;; ;; written by Stefan Reichoer <reichoer@web.de>
  1454. ;; (defun eshell/less (&rest args)
  1455. ;; "Invoke `view-file' on the file.
  1456. ;; \"less +42 foo\" also goes to line 42 in the buffer."
  1457. ;; (if args
  1458. ;; (while args
  1459. ;; (if (string-match "\\`\\+\\([0-9]+\\)\\'" (car args))
  1460. ;; (let* ((line (string-to-number (match-string 1 (pop args))))
  1461. ;; (file (pop args)))
  1462. ;; (view-file file)
  1463. ;; (goto-line line))
  1464. ;; (view-file (pop args))))))
  1465. (defun eshell/o (&optional file)
  1466. (my-x-open (or file ".")))
  1467. ;; (defun eshell/vi (&rest args)
  1468. ;; "Invoke `find-file' on the file.
  1469. ;; \"vi +42 foo\" also goes to line 42 in the buffer."
  1470. ;; (while args
  1471. ;; (if (string-match "\\`\\+\\([0-9]+\\)\\'" (car args))
  1472. ;; (let* ((line (string-to-number (match-string 1 (pop args))))
  1473. ;; (file (pop args)))
  1474. ;; (find-file file)
  1475. ;; (goto-line line))
  1476. ;; (find-file (pop args)))))
  1477. (defun eshell/clear ()
  1478. "Clear the current buffer, leaving one prompt at the top."
  1479. (interactive)
  1480. (let ((inhibit-read-only t))
  1481. (erase-buffer)))
  1482. (defvar eshell-prompt-function)
  1483. (defun eshell-clear ()
  1484. (interactive)
  1485. (let ((inhibit-read-only t))
  1486. (erase-buffer)
  1487. (insert (funcall eshell-prompt-function))))
  1488. (defun eshell/d (&optional dirname switches)
  1489. "if first arg is omitted open current directory."
  1490. (dired (or dirname ".") switches))
  1491. (defun eshell/v ()
  1492. (view-mode 1))
  1493. ;; (defun eshell/aaa (&rest args)
  1494. ;; (message "%S"
  1495. ;; args))
  1496. (defalias 'eshell/: 'ignore)
  1497. (defalias 'eshell/type 'eshell/which)
  1498. ;; (defalias 'eshell/vim 'eshell/vi)
  1499. (defalias 'eshell/ff 'find-file)
  1500. (defalias 'eshell/q 'eshell/exit)
  1501. (defun eshell-goto-prompt ()
  1502. ""
  1503. (interactive)
  1504. (goto-char (point-max)))
  1505. (defun eshell-delete-char-or-logout (n)
  1506. (interactive "p")
  1507. (if (equal (eshell-get-old-input)
  1508. "")
  1509. (progn
  1510. (insert "exit")
  1511. (eshell-send-input))
  1512. (delete-char n)))
  1513. (defun eshell-kill-input ()
  1514. (interactive)
  1515. (delete-region (point)
  1516. (progn (eshell-bol)
  1517. (point))))
  1518. (defalias 'eshell/logout 'eshell/exit)
  1519. (defun eshell-cd-default-directory (&optional eshell-buffer-or-name)
  1520. "open eshell and change wd
  1521. if arg given, use that eshell buffer, otherwise make new eshell buffer."
  1522. (interactive)
  1523. (let ((dir (expand-file-name default-directory)))
  1524. (switch-to-buffer (or eshell-buffer-or-name
  1525. (eshell t)))
  1526. (unless (equal dir (expand-file-name default-directory))
  1527. ;; (cd dir)
  1528. ;; (eshell-interactive-print (concat "cd " dir "\n"))
  1529. ;; (eshell-emit-prompt)
  1530. (goto-char (point-max))
  1531. (eshell-kill-input)
  1532. (insert "cd " dir)
  1533. (eshell-send-input))))
  1534. (defadvice eshell-next-matching-input-from-input
  1535. ;; do not cycle history
  1536. (around eshell-history-do-not-cycle activate)
  1537. (if (= 0
  1538. (or eshell-history-index
  1539. 0))
  1540. (progn
  1541. (delete-region eshell-last-output-end (point))
  1542. (insert-and-inherit eshell-matching-input-from-input-string)
  1543. (setq eshell-history-index nil))
  1544. ad-do-it))
  1545. (set-variable 'eshell-directory-name (concat user-emacs-directory
  1546. "eshell/"))
  1547. (set-variable 'eshell-term-name "eterm-color")
  1548. (set-variable 'eshell-scroll-to-bottom-on-input 'this)
  1549. (set-variable 'eshell-cmpl-ignore-case t)
  1550. (set-variable 'eshell-cmpl-cycle-completions nil)
  1551. (set-variable 'eshell-highlight-prompt nil)
  1552. (if (eq system-type 'darwin)
  1553. (set-variable 'eshell-ls-initial-args '("-hCFG")
  1554. (set-variable 'eshell-ls-initial-args '("-hCFG"
  1555. "--color=auto"
  1556. "--time-style=long-iso")) ; "-hF")
  1557. ))
  1558. (set (defvar eshell-prompt-function)
  1559. 'my-eshell-prompt-function)
  1560. (defvar eshell-last-command-status)
  1561. (defun my-eshell-prompt-function()
  1562. "Prompt function.
  1563. It looks like:
  1564. :: [10sr@darwin:~/][ESHELL]
  1565. :: $
  1566. "
  1567. (concat ":: ["
  1568. (let ((str (concat user-login-name
  1569. "@"
  1570. (car (split-string system-name
  1571. "\\."))
  1572. )))
  1573. (put-text-property 0
  1574. (length str)
  1575. 'face
  1576. 'underline
  1577. str)
  1578. str)
  1579. ":"
  1580. (let ((str (abbreviate-file-name default-directory)))
  1581. (put-text-property 0
  1582. (length str)
  1583. 'face
  1584. 'underline
  1585. str)
  1586. str)
  1587. "][ESHELL]\n:: "
  1588. (if (eq 0
  1589. eshell-last-command-status)
  1590. ""
  1591. (format "[STATUS:%d] "
  1592. eshell-last-command-status))
  1593. (if (= (user-uid)
  1594. 0)
  1595. "# "
  1596. "$ ")
  1597. ))
  1598. (with-eval-after-load 'eshell
  1599. (defvar eshell-mode-map (make-sparse-keymap))
  1600. ;; (define-key eshell-mode-map (kbd "C-x C-x") (lambda ()
  1601. ;; (interactive)
  1602. ;; (switch-to-buffer (other-buffer))))
  1603. ;; (define-key eshell-mode-map (kbd "C-g") (lambda ()
  1604. ;; (interactive)
  1605. ;; (eshell-goto-prompt)
  1606. ;; (keyboard-quit)))
  1607. (define-key eshell-mode-map (kbd "C-x t") 'eshell-text-mode-toggle)
  1608. (define-key eshell-mode-map (kbd "C-u") 'eshell-kill-input)
  1609. (define-key eshell-mode-map (kbd "C-d") 'eshell-delete-char-or-logout)
  1610. ;; (define-key eshell-mode-map (kbd "C-l")
  1611. ;; 'eshell-clear)
  1612. (define-key eshell-mode-map (kbd "DEL") 'my-eshell-backward-delete-char)
  1613. (define-key eshell-mode-map (kbd "<up>") 'scroll-down-line)
  1614. (define-key eshell-mode-map (kbd "<down>") 'scroll-up-line)
  1615. ;; (define-key eshell-mode-map
  1616. ;; (kbd "C-p") 'eshell-previous-matching-input-from-input)
  1617. ;; (define-key eshell-mode-map
  1618. ;; (kbd "C-n") 'eshell-next-matching-input-from-input)
  1619. (defvar eshell-virtual-targets nil)
  1620. (add-to-list 'eshell-virtual-targets
  1621. '("/dev/less"
  1622. (lambda (str)
  1623. (if str
  1624. (with-current-buffer nil)))
  1625. nil))
  1626. (defvar eshell-visual-commands nil)
  1627. (add-to-list 'eshell-visual-commands "vim")
  1628. (defvar eshell-output-filter-functions nil)
  1629. (add-to-list 'eshell-output-filter-functions
  1630. 'eshell-truncate-buffer)
  1631. (defvar eshell-command-aliases-list nil)
  1632. (mapcar (lambda (alias)
  1633. (add-to-list 'eshell-command-aliases-list
  1634. alias))
  1635. '(
  1636. ;; ("ll" "ls -l $*")
  1637. ;; ("la" "ls -a $*")
  1638. ;; ("lla" "ls -al $*")
  1639. ("git" "git -c color.ui=always $*")
  1640. ("g" "git $*")
  1641. ("eless"
  1642. (concat "cat >>> (with-current-buffer "
  1643. "(get-buffer-create \"*eshell output\") "
  1644. "(erase-buffer) "
  1645. "(setq buffer-read-only nil) "
  1646. "(current-buffer)) "
  1647. "(view-buffer (get-buffer \"*eshell output*\"))"))
  1648. ))
  1649. )
  1650. (add-hook 'eshell-mode-hook
  1651. (lambda ()
  1652. (apply 'eshell/addpath exec-path)
  1653. (set (make-local-variable 'scroll-margin) 0)
  1654. ;; (eshell/export "GIT_PAGER=")
  1655. ;; (eshell/export "GIT_EDITOR=")
  1656. (eshell/export "LC_MESSAGES=C")
  1657. (switch-to-buffer (current-buffer)) ; move buffer top of list
  1658. (set (make-local-variable (defvar hl-line-range-function))
  1659. (lambda ()
  1660. '(0 . 0)))
  1661. ;; (add-to-list 'eshell-visual-commands "git")
  1662. ))
  1663. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1664. ;; my-term
  1665. (defvar my-term nil
  1666. "My terminal buffer.")
  1667. (defvar my-term-function nil
  1668. "Function to create terminal buffer.
  1669. This function accept no argument and return newly created buffer of terminal.")
  1670. (defun my-term (&optional arg)
  1671. "Open terminal buffer and return that buffer.
  1672. If ARG is given or called with prefix argument, create new buffer."
  1673. (interactive "P")
  1674. (if (and (not arg)
  1675. my-term
  1676. (buffer-name my-term))
  1677. (pop-to-buffer my-term)
  1678. (setq my-term
  1679. (save-window-excursion
  1680. (funcall my-term-function)))
  1681. (and my-term
  1682. (my-term))))
  1683. ;; (setq my-term-function
  1684. ;; (lambda ()
  1685. ;; (if (eq system-type 'windows-nt)
  1686. ;; (eshell)
  1687. ;; (if (require 'multi-term nil t)
  1688. ;; (multi-term)
  1689. ;; (ansi-term shell-file-name)))))
  1690. (setq my-term-function (lambda () (eshell t)))
  1691. ;;(define-key my-prefix-map (kbd "C-s") 'my-term)
  1692. (define-key ctl-x-map "i" 'my-term)
  1693. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1694. ;; misc funcs
  1695. (defalias 'qcalc 'quick-calc)
  1696. (defun memo (&optional dir)
  1697. "Open memo.txt in DIR."
  1698. (interactive)
  1699. (pop-to-buffer (find-file-noselect (concat (if dir
  1700. (file-name-as-directory dir)
  1701. "")
  1702. "memo.txt"))))
  1703. (defvar my-rgrep-alist
  1704. `(
  1705. ;; the silver searcher
  1706. ("ag"
  1707. (executable-find "ag")
  1708. "ag --nocolor --nogroup --nopager --filename ")
  1709. ;; ack
  1710. ("ack"
  1711. (executable-find "ack")
  1712. "ack --nocolor --nogroup --nopager --with-filename ")
  1713. ;; gnu global
  1714. ("global"
  1715. (and (require 'gtags nil t)
  1716. (executable-find "global")
  1717. (gtags-get-rootpath))
  1718. "global --result grep ")
  1719. ;; git grep
  1720. ("gitgrep"
  1721. (eq 0
  1722. (shell-command "git rev-parse --git-dir"))
  1723. "git --no-pager -c color.grep=false grep -nH -e ")
  1724. ;; grep
  1725. ("grep"
  1726. t
  1727. ,(concat "find . "
  1728. "-path '*/.git' -prune -o "
  1729. "-path '*/.svn' -prune -o "
  1730. "-type f -print0 | "
  1731. "xargs -0 grep -nH -e "))
  1732. )
  1733. "Alist of rgrep command.
  1734. Each element is in the form like (NAME SEXP COMMAND), where SEXP returns the
  1735. condition to choose COMMAND when evaluated.")
  1736. (defvar my-rgrep-default nil
  1737. "Default command name for my-rgrep.")
  1738. (defun my-rgrep-grep-command (&optional name alist)
  1739. "Return recursive grep command for current directory or nil.
  1740. If NAME is given, use that without testing.
  1741. Commands are searched from ALIST."
  1742. (if alist
  1743. (if name
  1744. ;; if name is given search that from alist and return the command
  1745. (nth 2 (assoc name
  1746. alist))
  1747. ;; if name is not given try test in 1th elem
  1748. (let ((car (car alist))
  1749. (cdr (cdr alist)))
  1750. (if (eval (nth 1 car))
  1751. ;; if the condition is true return the command
  1752. (nth 2 car)
  1753. ;; try next one
  1754. (and cdr
  1755. (my-rgrep-grep-command name cdr)))))
  1756. ;; if alist is not given set default value
  1757. (my-rgrep-grep-command name my-rgrep-alist)))
  1758. (defun my-rgrep (command-args)
  1759. "My recursive grep. Run COMMAND-ARGS."
  1760. (interactive (let ((cmd (my-rgrep-grep-command my-rgrep-default
  1761. nil)))
  1762. (if cmd
  1763. (list (read-shell-command "grep command: "
  1764. cmd
  1765. 'grep-find-history))
  1766. (error "My-Rgrep: Command for rgrep not found")
  1767. )))
  1768. (compilation-start command-args
  1769. 'grep-mode))
  1770. ;; (defun my-rgrep-symbol-at-point (command-args)
  1771. ;; "My recursive grep. Run COMMAND-ARGS."
  1772. ;; (interactive (list (read-shell-command "grep command: "
  1773. ;; (concat (my-rgrep-grep-command)
  1774. ;; " "
  1775. ;; (thing-at-point 'symbol))
  1776. ;; 'grep-find-history)))
  1777. ;; (compilation-start command-args
  1778. ;; 'grep-mode))
  1779. (defmacro define-my-rgrep (name)
  1780. "Define rgrep for NAME."
  1781. `(defun ,(intern (concat "my-rgrep-"
  1782. name)) ()
  1783. ,(format "My recursive grep by %s."
  1784. name)
  1785. (interactive)
  1786. (let ((my-rgrep-default ,name))
  1787. (if (called-interactively-p 'any)
  1788. (call-interactively 'my-rgrep)
  1789. (error "Not intended to be called noninteractively. Use `my-rgrep'"))))
  1790. )
  1791. (define-my-rgrep "ack")
  1792. (define-my-rgrep "ag")
  1793. (define-my-rgrep "gitgrep")
  1794. (define-my-rgrep "grep")
  1795. (define-my-rgrep "global")
  1796. (define-key ctl-x-map "s" 'my-rgrep)
  1797. ;; (defun make ()
  1798. ;; "Run \"make -k\" in current directory."
  1799. ;; (interactive)
  1800. ;; (compile "make -k"))
  1801. (defalias 'make 'compile)
  1802. (define-key ctl-x-map "c" 'compile)
  1803. (set (defvar prompt-text-format nil
  1804. "Format text to be prepended to prompt texts of minibuffer.
  1805. The value should be a mode-line format: see `mode-line-fomat' for details.")
  1806. `(,(concat "["
  1807. user-login-name
  1808. "@"
  1809. (car (split-string system-name
  1810. "\\."))
  1811. "][")
  1812. (:eval (abbreviate-file-name default-directory))
  1813. "]"
  1814. (:eval (and (fboundp 'git-ps1-mode-get-current)
  1815. (git-ps1-mode-get-current "[GIT:%s]")))
  1816. "\n"))
  1817. (defmacro prompt-text--defadvice (&rest functions)
  1818. "Set prompt-text advices for FUNCTIONS."
  1819. `(progn
  1820. ,@(mapcar (lambda (f)
  1821. `(defadvice ,f (before prompt-text-modify)
  1822. "Show info in prompt."
  1823. (let ((orig (ad-get-arg 0))
  1824. (str (format-mode-line prompt-text-format)))
  1825. (unless (string-match-p (concat "^"
  1826. (regexp-quote str))
  1827. orig)
  1828. (ad-set-arg 0
  1829. (concat str
  1830. orig))))))
  1831. functions)))
  1832. (prompt-text--defadvice read-from-minibuffer
  1833. read-string
  1834. completing-read)
  1835. (define-minor-mode prompt-text-mode
  1836. "Prepend some infomation to prompt of minibuffer.
  1837. Set `prompt-text-format' to configure what text to prepend."
  1838. :init-value nil
  1839. :global t
  1840. :lighter ""
  1841. (if prompt-text-mode
  1842. (ad-activate-regexp "^prompt-text-modify$")
  1843. (ad-deactivate-regexp "^prompt-text-modify$")))
  1844. (prompt-text-mode 1)
  1845. ;;; emacs.el ends here