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.
 
 
 
 
 
 

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