Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

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