25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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