No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

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