Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

2111 linhas
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. 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 nil)
  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 (make-sparse-keymap))
  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 (make-sparse-keymap))
  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 (make-sparse-keymap))
  770. (define-key yaml-mode-map (kbd "C-m") 'newline))
  771. (with-eval-after-load 'html-mode
  772. (defvar html-mode-map (make-sparse-keymap))
  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 (make-sparse-keymap))
  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 (make-sparse-keymap))
  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 (make-sparse-keymap))
  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 (make-sparse-keymap))
  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 (make-sparse-keymap))
  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 (make-sparse-keymap))
  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 nil)
  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 (make-sparse-keymap))
  942. (defvar scheme-mode-map (make-sparse-keymap))
  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 nil)
  953. (defvar scheme-buffer nil)
  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. ;; term mode
  991. ;; (setq multi-term-program shell-file-name)
  992. (when (autoload-eval-lazily 'multi-term)
  993. (set-variable 'multi-term-switch-after-close nil)
  994. (set-variable 'multi-term-dedicated-select-after-open-p t)
  995. (set-variable 'multi-term-dedicated-window-height 20))
  996. (when (autoload-eval-lazily 'term '(term ansi-term)
  997. (defvar term-raw-map (make-sparse-keymap))
  998. ;; (define-key term-raw-map "\C-xl" 'term-line-mode)
  999. ;; (define-key term-mode-map "\C-xc" 'term-char-mode)
  1000. (define-key term-raw-map (kbd "<up>") 'scroll-down-line)
  1001. (define-key term-raw-map (kbd "<down>") 'scroll-up-line)
  1002. (define-key term-raw-map (kbd "<right>") 'scroll-up)
  1003. (define-key term-raw-map (kbd "<left>") 'scroll-down)
  1004. (define-key term-raw-map (kbd "C-p") 'term-send-raw)
  1005. (define-key term-raw-map (kbd "C-n") 'term-send-raw)
  1006. (define-key term-raw-map "q" 'my-term-quit-or-send-raw)
  1007. ;; (define-key term-raw-map (kbd "ESC") 'term-send-raw)
  1008. (define-key term-raw-map [delete] 'term-send-raw)
  1009. (define-key term-raw-map (kbd "DEL") 'term-send-backspace)
  1010. (define-key term-raw-map "\C-y" 'term-paste)
  1011. (define-key term-raw-map
  1012. "\C-c" 'term-send-raw) ;; 'term-interrupt-subjob)
  1013. '(define-key term-mode-map (kbd "C-x C-q") 'term-pager-toggle)
  1014. ;; (dolist (key '("<up>" "<down>" "<right>" "<left>"))
  1015. ;; (define-key term-raw-map (read-kbd-macro key) 'term-send-raw))
  1016. ;; (define-key term-raw-map "\C-d" 'delete-char)
  1017. ;; (define-key term-raw-map "\C-q" 'move-beginning-of-line)
  1018. ;; (define-key term-raw-map "\C-r" 'term-send-raw)
  1019. ;; (define-key term-raw-map "\C-s" 'term-send-raw)
  1020. ;; (define-key term-raw-map "\C-f" 'forward-char)
  1021. ;; (define-key term-raw-map "\C-b" 'backward-char)
  1022. ;; (define-key term-raw-map "\C-t" 'set-mark-command)
  1023. )
  1024. (defun my-term-quit-or-send-raw ()
  1025. ""
  1026. (interactive)
  1027. (if (get-buffer-process (current-buffer))
  1028. (call-interactively 'term-send-raw)
  1029. (kill-buffer)))
  1030. ;; http://d.hatena.ne.jp/goinger/20100416/1271399150
  1031. ;; (setq term-ansi-default-program shell-file-name)
  1032. (add-hook 'term-setup-hook
  1033. (lambda ()
  1034. (set-variable 'term-display-table (make-display-table))))
  1035. (add-hook 'term-mode-hook
  1036. (lambda ()
  1037. (defvar term-raw-map (make-sparse-keymap))
  1038. ;; (unless (memq (current-buffer)
  1039. ;; (and (featurep 'multi-term)
  1040. ;; (defvar multi-term-buffer-list)
  1041. ;; ;; current buffer is not multi-term buffer
  1042. ;; multi-term-buffer-list))
  1043. ;; )
  1044. (set (make-local-variable 'scroll-margin) 0)
  1045. ;; (set (make-local-variable 'cua-enable-cua-keys) nil)
  1046. ;; (cua-mode 0)
  1047. ;; (and cua-mode
  1048. ;; (local-unset-key (kbd "C-c")))
  1049. ;; (define-key cua--prefix-override-keymap
  1050. ;;"\C-c" 'term-interrupt-subjob)
  1051. (set (make-local-variable (defvar hl-line-range-function))
  1052. (lambda ()
  1053. '(0 . 0)))
  1054. (define-key term-raw-map
  1055. "\C-x" (lookup-key (current-global-map) "\C-x"))
  1056. (define-key term-raw-map
  1057. "\C-z" (lookup-key (current-global-map) "\C-z"))
  1058. ))
  1059. ;; (add-hook 'term-exec-hook 'forward-char)
  1060. )
  1061. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1062. ;; buffer switching
  1063. (defvar bs-configurations)
  1064. (when (autoload-eval-lazily 'bs '(bs-show)
  1065. ;; (add-to-list 'bs-configurations
  1066. ;; '("processes" nil get-buffer-process ".*" nil nil))
  1067. (add-to-list 'bs-configurations
  1068. '("files-and-terminals" nil nil nil
  1069. (lambda (buf)
  1070. (and (bs-visits-non-file buf)
  1071. (save-excursion
  1072. (set-buffer buf)
  1073. (not (memq major-mode
  1074. '(term-mode
  1075. eshell-mode))))))))
  1076. ;; (setq bs-configurations (list
  1077. ;; '("processes" nil get-buffer-process ".*" nil nil)
  1078. ;; '("files-and-scratch" "^\\*scratch\\*$" nil nil
  1079. ;; bs-visits-non-file bs-sort-buffer-interns-are-last)))
  1080. )
  1081. ;; (global-set-key "\C-x\C-b" 'bs-show)
  1082. (defalias 'list-buffers 'bs-show)
  1083. (set-variable 'bs-default-configuration "files-and-terminals")
  1084. (set-variable 'bs-default-sort-name "by nothing")
  1085. (add-hook 'bs-mode-hook
  1086. (lambda ()
  1087. ;; (setq bs-default-configuration "files")
  1088. ;; (and bs--show-all
  1089. ;; (call-interactively 'bs-toggle-show-all))
  1090. (set (make-local-variable 'scroll-margin) 0))))
  1091. ;;(iswitchb-mode 1)
  1092. (icomplete-mode)
  1093. (defun iswitchb-buffer-display-other-window ()
  1094. "Do iswitchb in other window."
  1095. (interactive)
  1096. (let ((iswitchb-default-method 'display))
  1097. (call-interactively 'iswitchb-buffer)))
  1098. ;;;;;;;;;;;;;;;;;;;;;;;;
  1099. ;; ilookup
  1100. (with-eval-after-load 'ilookup
  1101. (set-variable 'ilookup-dict-alist
  1102. '(
  1103. ("sdcv" . (lambda (word)
  1104. (shell-command-to-string
  1105. (format "sdcv -n '%s'"
  1106. word))))
  1107. ("en" . (lambda (word)
  1108. (shell-command-to-string
  1109. (format "sdcv -n -u dictd_www.dict.org_gcide '%s'"
  1110. word))))
  1111. ("ja" . (lambda (word)
  1112. (shell-command-to-string
  1113. (format "sdcv -n -u EJ-GENE95 -u jmdict-en-ja '%s'"
  1114. word))))
  1115. ("jaj" . (lambda (word)
  1116. (shell-command-to-string
  1117. (format "sdcv -n -u jmdict-en-ja '%s'"
  1118. word))))
  1119. ("jag" .
  1120. (lambda (word)
  1121. (with-temp-buffer
  1122. (insert (shell-command-to-string
  1123. (format "sdcv -n -u 'Genius English-Japanese' '%s'"
  1124. word)))
  1125. (html2text)
  1126. (buffer-substring (point-min)
  1127. (point-max)))))
  1128. ("alc" . (lambda (word)
  1129. (shell-command-to-string
  1130. (format "alc '%s' | head -n 20"
  1131. word))))
  1132. ("app" . (lambda (word)
  1133. (shell-command-to-string
  1134. (format "dict_app '%s'"
  1135. word))))
  1136. ;; letters broken
  1137. ("ms" .
  1138. (lambda (word)
  1139. (let ((url (concat
  1140. "http://api.microsofttranslator.com/V2/Ajax.svc/"
  1141. "Translate?appId=%s&text=%s&to=%s"))
  1142. (apikey "3C9778666C5BA4B406FFCBEE64EF478963039C51")
  1143. (target "ja")
  1144. (eword (url-hexify-string word)))
  1145. (with-current-buffer (url-retrieve-synchronously
  1146. (format url
  1147. apikey
  1148. eword
  1149. target))
  1150. (message "")
  1151. (goto-char (point-min))
  1152. (search-forward-regexp "^$"
  1153. nil
  1154. t)
  1155. (url-unhex-string (buffer-substring-no-properties
  1156. (point)
  1157. (point-max)))))))
  1158. ))
  1159. ;; (funcall (cdr (assoc "ms"
  1160. ;; ilookup-alist))
  1161. ;; "dictionary")
  1162. ;; (switch-to-buffer (url-retrieve-synchronously "http://api.microsofttranslator.com/V2/Ajax.svc/Translate?appId=3C9778666C5BA4B406FFCBEE64EF478963039C51&text=dictionary&to=ja"))
  1163. ;; (switch-to-buffer (url-retrieve-synchronously "http://google.com"))
  1164. (set-variable 'ilookup-default "ja")
  1165. (when (locate-library "google-translate")
  1166. (defvar ilookup-dict-alist nil)
  1167. (add-to-list 'ilookup-dict-alist
  1168. '("gt" .
  1169. (lambda (word)
  1170. (save-excursion
  1171. (google-translate-translate "auto"
  1172. "ja"
  1173. word))
  1174. (with-current-buffer "*Google Translate*"
  1175. (buffer-substring-no-properties (point-min)
  1176. (point-max)))))))
  1177. )
  1178. (when (autoload-eval-lazily 'google-translate '(google-translate-translate
  1179. google-translate-at-point))
  1180. (set-variable 'google-translate-default-source-language "auto")
  1181. (set-variable 'google-translate-default-target-language "ja"))
  1182. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1183. ;; vc
  1184. (require 'vc)
  1185. (setq vc-handled-backends '())
  1186. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1187. ;; recentf-mode
  1188. (set-variable 'recentf-save-file (expand-file-name (concat user-emacs-directory
  1189. "recentf")))
  1190. (set-variable 'recentf-max-menu-items 20)
  1191. (set-variable 'recentf-max-saved-items 30)
  1192. (set-variable 'recentf-show-file-shortcuts-flag nil)
  1193. (when (safe-require-or-eval 'recentf)
  1194. (add-to-list 'recentf-exclude
  1195. (regexp-quote recentf-save-file))
  1196. (add-to-list 'recentf-exclude
  1197. (regexp-quote (expand-file-name user-emacs-directory)))
  1198. (define-key ctl-x-map (kbd "C-r") 'recentf-open-files)
  1199. (remove-hook 'find-file-hook
  1200. 'recentf-track-opened-file)
  1201. (defun my-recentf-load-track-save-list ()
  1202. "Load current recentf list from file, track current visiting file, then save
  1203. the list."
  1204. (recentf-load-list)
  1205. (recentf-track-opened-file)
  1206. (recentf-save-list))
  1207. (add-hook 'find-file-hook
  1208. 'my-recentf-load-track-save-list)
  1209. (add-hook 'kill-emacs-hook
  1210. 'recentf-load-list)
  1211. ;;(run-with-idle-timer 5 t 'recentf-save-list)
  1212. ;; (add-hook 'find-file-hook
  1213. ;; (lambda ()
  1214. ;; (recentf-add-file default-directory)))
  1215. (and (autoload-eval-lazily 'recentf-show)
  1216. (define-key ctl-x-map (kbd "C-r") 'recentf-show)
  1217. (add-hook 'recentf-show-before-listing-hook
  1218. 'recentf-load-list))
  1219. (recentf-mode 1)
  1220. (define-key recentf-dialog-mode-map (kbd "<up>") 'previous-line)
  1221. (define-key recentf-dialog-mode-map (kbd "<down>") 'next-line)
  1222. (define-key recentf-dialog-mode-map "p" 'previous-line)
  1223. (define-key recentf-dialog-mode-map "n" 'next-line)
  1224. (add-hook 'recentf-dialog-mode-hook
  1225. (lambda ()
  1226. ;; (recentf-save-list)
  1227. ;; (define-key recentf-dialog-mode-map (kbd "C-x C-f")
  1228. ;; 'my-recentf-cd-and-find-file)
  1229. (cd "~/"))))
  1230. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1231. ;; dired
  1232. (defun my-dired-echo-file-head (arg)
  1233. ""
  1234. (interactive "P")
  1235. (let ((f (dired-get-filename)))
  1236. (message "%s"
  1237. (with-temp-buffer
  1238. (insert-file-contents f)
  1239. (buffer-substring-no-properties
  1240. (point-min)
  1241. (progn (goto-char (point-min))
  1242. (forward-line (1- (if arg
  1243. (prefix-numeric-value arg)
  1244. 7)))
  1245. (point-at-eol)))))))
  1246. (defun my-dired-diff ()
  1247. ""
  1248. (interactive)
  1249. (let ((files (dired-get-marked-files nil nil nil t)))
  1250. (if (eq (car files)
  1251. t)
  1252. (diff (cadr files) (dired-get-filename))
  1253. (message "One file must be marked!"))))
  1254. (defun dired-get-file-info ()
  1255. "dired get file info"
  1256. (interactive)
  1257. (let ((f (shell-quote-argument (dired-get-filename t))))
  1258. (if (file-directory-p f)
  1259. (progn
  1260. (message "Calculating disk usage...")
  1261. (shell-command (concat "du -hsD "
  1262. f)))
  1263. (shell-command (concat "file "
  1264. f)))))
  1265. (defun my-dired-scroll-up ()
  1266. ""
  1267. (interactive)
  1268. (my-dired-previous-line (- (window-height) 1)))
  1269. (defun my-dired-scroll-down ()
  1270. ""
  1271. (interactive)
  1272. (my-dired-next-line (- (window-height) 1)))
  1273. ;; (defun my-dired-forward-line (arg)
  1274. ;; ""
  1275. ;; (interactive "p"))
  1276. (defun my-dired-previous-line (arg)
  1277. ""
  1278. (interactive "p")
  1279. (if (> arg 0)
  1280. (progn
  1281. (if (eq (line-number-at-pos)
  1282. 1)
  1283. (goto-char (point-max))
  1284. (forward-line -1))
  1285. (my-dired-previous-line (if (or (dired-get-filename nil t)
  1286. (dired-get-subdir))
  1287. (- arg 1)
  1288. arg)))
  1289. (dired-move-to-filename)))
  1290. (defun my-dired-next-line (arg)
  1291. ""
  1292. (interactive "p")
  1293. (if (> arg 0)
  1294. (progn
  1295. (if (eq (point)
  1296. (point-max))
  1297. (goto-char (point-min))
  1298. (forward-line 1))
  1299. (my-dired-next-line (if (or (dired-get-filename nil t)
  1300. (dired-get-subdir))
  1301. (- arg 1)
  1302. arg)))
  1303. (dired-move-to-filename)))
  1304. ;;http://bach.istc.kobe-u.ac.jp/lect/tamlab/ubuntu/emacs.html
  1305. (if (eq window-system 'mac)
  1306. (setq dired-listing-switches "-lhF")
  1307. (setq dired-listing-switches "-lhF --time-style=long-iso")
  1308. )
  1309. (setq dired-listing-switches "-lhF")
  1310. (put 'dired-find-alternate-file 'disabled nil)
  1311. ;; when using dired-find-alternate-file
  1312. ;; reuse current dired buffer for the file to open
  1313. (set-variable 'dired-ls-F-marks-symlinks t)
  1314. (when (safe-require-or-eval 'ls-lisp)
  1315. (setq ls-lisp-use-insert-directory-program nil) ; always use ls-lisp
  1316. (setq ls-lisp-dirs-first t)
  1317. (setq ls-lisp-use-localized-time-format t)
  1318. (setq ls-lisp-format-time-list
  1319. '("%Y-%m-%d %H:%M"
  1320. "%Y-%m-%d ")))
  1321. (set-variable 'dired-dwim-target t)
  1322. (set-variable 'dired-isearch-filenames t)
  1323. (set-variable 'dired-hide-details-hide-symlink-targets nil)
  1324. (set-variable 'dired-hide-details-hide-information-lines nil)
  1325. ;; (add-hook 'dired-after-readin-hook
  1326. ;; 'my-replace-nasi-none)
  1327. ;; (add-hook 'after-init-hook
  1328. ;; (lambda ()
  1329. ;; (dired ".")))
  1330. (with-eval-after-load 'dired
  1331. (defvar dired-mode-map (make-sparse-keymap))
  1332. (define-key dired-mode-map "o" 'my-dired-x-open)
  1333. (define-key dired-mode-map "i" 'dired-get-file-info)
  1334. (define-key dired-mode-map "f" 'find-file)
  1335. (define-key dired-mode-map "!" 'shell-command)
  1336. (define-key dired-mode-map "&" 'async-shell-command)
  1337. (define-key dired-mode-map "X" 'dired-do-async-shell-command)
  1338. (define-key dired-mode-map "=" 'my-dired-diff)
  1339. (define-key dired-mode-map "B" 'gtkbm-add-current-dir)
  1340. (define-key dired-mode-map "b" 'gtkbm)
  1341. (define-key dired-mode-map "h" 'my-dired-echo-file-head)
  1342. (define-key dired-mode-map "@" (lambda ()
  1343. (interactive) (my-x-open ".")))
  1344. (define-key dired-mode-map (kbd "TAB") 'other-window)
  1345. ;; (define-key dired-mode-map "P" 'my-dired-do-pack-or-unpack)
  1346. (define-key dired-mode-map "/" 'dired-isearch-filenames)
  1347. (define-key dired-mode-map (kbd "DEL") 'dired-up-directory)
  1348. (define-key dired-mode-map (kbd "C-h") 'dired-up-directory)
  1349. (substitute-key-definition 'dired-next-line
  1350. 'my-dired-next-line
  1351. dired-mode-map)
  1352. (substitute-key-definition 'dired-previous-line
  1353. 'my-dired-previous-line
  1354. dired-mode-map)
  1355. ;; (define-key dired-mode-map (kbd "C-p") 'my-dired-previous-line)
  1356. ;; (define-key dired-mode-map (kbd "p") 'my-dired-previous-line)
  1357. ;; (define-key dired-mode-map (kbd "C-n") 'my-dired-next-line)
  1358. ;; (define-key dired-mode-map (kbd "n") 'my-dired-next-line)
  1359. (define-key dired-mode-map (kbd "<left>") 'my-dired-scroll-up)
  1360. (define-key dired-mode-map (kbd "<right>") 'my-dired-scroll-down)
  1361. (define-key dired-mode-map (kbd "ESC p") 'my-dired-scroll-up)
  1362. (define-key dired-mode-map (kbd "ESC n") 'my-dired-scroll-down)
  1363. (add-hook 'dired-mode-hook
  1364. (lambda ()
  1365. (when (fboundp 'dired-hide-details-mode)
  1366. (dired-hide-details-mode t)
  1367. (local-set-key "l" 'dired-hide-details-mode))
  1368. (let ((file "._Icon\015"))
  1369. (when nil
  1370. '(file-readable-p file)
  1371. (delete-file file)))))
  1372. (when (autoload-eval-lazily 'pack '(dired-do-pack-or-unpack pack-pack))
  1373. (with-eval-after-load 'dired
  1374. (define-key dired-mode-map "P" 'dired-do-pack-or-unpack)))
  1375. (when (autoload-eval-lazily 'dired-list-all-mode)
  1376. (setq dired-listing-switches "-lhF")
  1377. (with-eval-after-load 'dired
  1378. (local-set-key "a" 'dired-list-all-mode))))
  1379. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1380. ;; eshell
  1381. (set-variable 'eshell-banner-message (format "Welcome to the Emacs shell
  1382. %s
  1383. C-x t to toggling emacs-text-mode
  1384. "
  1385. (shell-command-to-string "uname -a")
  1386. ))
  1387. (defvar eshell-text-mode-map
  1388. (let ((map (make-sparse-keymap)))
  1389. (define-key map (kbd "C-x t") 'eshell-text-mode-toggle)
  1390. map))
  1391. (define-derived-mode eshell-text-mode text-mode
  1392. "Eshell-Text"
  1393. "Text-mode for Eshell."
  1394. nil)
  1395. (defun eshell-text-mode-toggle ()
  1396. "Toggle eshell-text-mode and eshell-mode."
  1397. (interactive)
  1398. (cond ((eq major-mode
  1399. 'eshell-text-mode)
  1400. (goto-char (point-max))
  1401. (message "Eshell text mode disabled")
  1402. (eshell-mode))
  1403. ((eq major-mode
  1404. 'eshell-mode)
  1405. (message "Eshell text mode enabled")
  1406. (eshell-write-history)
  1407. (eshell-text-mode))
  1408. (t
  1409. (message "Not in eshell buffer")
  1410. nil)))
  1411. (defun my-eshell-backward-delete-char ()
  1412. (interactive)
  1413. (when (< (save-excursion
  1414. (eshell-bol)
  1415. (point))
  1416. (point))
  1417. (backward-delete-char 1)))
  1418. (defun my-file-owner-p (file)
  1419. "t if FILE is owned by me."
  1420. (eq (user-uid) (nth 2 (file-attributes file))))
  1421. "http://www.bookshelf.jp/pukiwiki/pukiwiki.php\
  1422. ?Eshell%A4%F2%BB%C8%A4%A4%A4%B3%A4%CA%A4%B9"
  1423. ;; ;; written by Stefan Reichoer <reichoer@web.de>
  1424. ;; (defun eshell/less (&rest args)
  1425. ;; "Invoke `view-file' on the file.
  1426. ;; \"less +42 foo\" also goes to line 42 in the buffer."
  1427. ;; (if args
  1428. ;; (while args
  1429. ;; (if (string-match "\\`\\+\\([0-9]+\\)\\'" (car args))
  1430. ;; (let* ((line (string-to-number (match-string 1 (pop args))))
  1431. ;; (file (pop args)))
  1432. ;; (view-file file)
  1433. ;; (goto-line line))
  1434. ;; (view-file (pop args))))))
  1435. (defun eshell/o (&optional file)
  1436. (my-x-open (or file ".")))
  1437. ;; (defun eshell/vi (&rest args)
  1438. ;; "Invoke `find-file' on the file.
  1439. ;; \"vi +42 foo\" also goes to line 42 in the buffer."
  1440. ;; (while args
  1441. ;; (if (string-match "\\`\\+\\([0-9]+\\)\\'" (car args))
  1442. ;; (let* ((line (string-to-number (match-string 1 (pop args))))
  1443. ;; (file (pop args)))
  1444. ;; (find-file file)
  1445. ;; (goto-line line))
  1446. ;; (find-file (pop args)))))
  1447. (defun eshell/clear ()
  1448. "Clear the current buffer, leaving one prompt at the top."
  1449. (interactive)
  1450. (let ((inhibit-read-only t))
  1451. (erase-buffer)))
  1452. (defvar eshell-prompt-function)
  1453. (defun eshell-clear ()
  1454. (interactive)
  1455. (let ((inhibit-read-only t))
  1456. (erase-buffer)
  1457. (insert (funcall eshell-prompt-function))))
  1458. (defun eshell/d (&optional dirname switches)
  1459. "if first arg is omitted open current directory."
  1460. (dired (or dirname ".") switches))
  1461. (defun eshell/v ()
  1462. (view-mode 1))
  1463. ;; (defun eshell/aaa (&rest args)
  1464. ;; (message "%S"
  1465. ;; args))
  1466. (defalias 'eshell/: 'ignore)
  1467. (defalias 'eshell/type 'eshell/which)
  1468. ;; (defalias 'eshell/vim 'eshell/vi)
  1469. (defalias 'eshell/ff 'find-file)
  1470. (defalias 'eshell/q 'eshell/exit)
  1471. (defun eshell-goto-prompt ()
  1472. ""
  1473. (interactive)
  1474. (goto-char (point-max)))
  1475. (defun eshell-delete-char-or-logout (n)
  1476. (interactive "p")
  1477. (if (equal (eshell-get-old-input)
  1478. "")
  1479. (progn
  1480. (insert "exit")
  1481. (eshell-send-input))
  1482. (delete-char n)))
  1483. (defun eshell-kill-input ()
  1484. (interactive)
  1485. (delete-region (point)
  1486. (progn (eshell-bol)
  1487. (point))))
  1488. (defalias 'eshell/logout 'eshell/exit)
  1489. (defun eshell-cd-default-directory (&optional eshell-buffer-or-name)
  1490. "open eshell and change wd
  1491. if arg given, use that eshell buffer, otherwise make new eshell buffer."
  1492. (interactive)
  1493. (let ((dir (expand-file-name default-directory)))
  1494. (switch-to-buffer (or eshell-buffer-or-name
  1495. (eshell t)))
  1496. (unless (equal dir (expand-file-name default-directory))
  1497. ;; (cd dir)
  1498. ;; (eshell-interactive-print (concat "cd " dir "\n"))
  1499. ;; (eshell-emit-prompt)
  1500. (goto-char (point-max))
  1501. (eshell-kill-input)
  1502. (insert "cd " dir)
  1503. (eshell-send-input))))
  1504. (defadvice eshell-next-matching-input-from-input
  1505. ;; do not cycle history
  1506. (around eshell-history-do-not-cycle activate)
  1507. (if (= 0
  1508. (or eshell-history-index
  1509. 0))
  1510. (progn
  1511. (delete-region eshell-last-output-end (point))
  1512. (insert-and-inherit eshell-matching-input-from-input-string)
  1513. (setq eshell-history-index nil))
  1514. ad-do-it))
  1515. (set-variable 'eshell-directory-name (concat user-emacs-directory
  1516. "eshell/"))
  1517. (set-variable 'eshell-term-name "eterm-color")
  1518. (set-variable 'eshell-scroll-to-bottom-on-input 'this)
  1519. (set-variable 'eshell-cmpl-ignore-case t)
  1520. (set-variable 'eshell-cmpl-cycle-completions nil)
  1521. (set-variable 'eshell-highlight-prompt nil)
  1522. (if (eq system-type 'darwin)
  1523. (set-variable 'eshell-ls-initial-args '("-hCFG")
  1524. (set-variable 'eshell-ls-initial-args '("-hCFG"
  1525. "--color=auto"
  1526. "--time-style=long-iso")) ; "-hF")
  1527. ))
  1528. (set (defvar eshell-prompt-function)
  1529. 'my-eshell-prompt-function)
  1530. (defvar eshell-last-command-status)
  1531. (defun my-eshell-prompt-function()
  1532. "Prompt function.
  1533. It looks like:
  1534. :: [10sr@darwin:~/][ESHELL]
  1535. :: $
  1536. "
  1537. (concat ":: ["
  1538. (let ((str (concat user-login-name
  1539. "@"
  1540. (car (split-string system-name
  1541. "\\."))
  1542. )))
  1543. (put-text-property 0
  1544. (length str)
  1545. 'face
  1546. 'underline
  1547. str)
  1548. str)
  1549. ":"
  1550. (let ((str (abbreviate-file-name default-directory)))
  1551. (put-text-property 0
  1552. (length str)
  1553. 'face
  1554. 'underline
  1555. str)
  1556. str)
  1557. "][ESHELL]\n:: "
  1558. (if (eq 0
  1559. eshell-last-command-status)
  1560. ""
  1561. (format "[STATUS:%d] "
  1562. eshell-last-command-status))
  1563. (if (= (user-uid)
  1564. 0)
  1565. "# "
  1566. "$ ")
  1567. ))
  1568. (with-eval-after-load 'eshell
  1569. (defvar eshell-mode-map (make-sparse-keymap))
  1570. ;; (define-key eshell-mode-map (kbd "C-x C-x") (lambda ()
  1571. ;; (interactive)
  1572. ;; (switch-to-buffer (other-buffer))))
  1573. ;; (define-key eshell-mode-map (kbd "C-g") (lambda ()
  1574. ;; (interactive)
  1575. ;; (eshell-goto-prompt)
  1576. ;; (keyboard-quit)))
  1577. (define-key eshell-mode-map (kbd "C-x t") 'eshell-text-mode-toggle)
  1578. (define-key eshell-mode-map (kbd "C-u") 'eshell-kill-input)
  1579. (define-key eshell-mode-map (kbd "C-d") 'eshell-delete-char-or-logout)
  1580. ;; (define-key eshell-mode-map (kbd "C-l")
  1581. ;; 'eshell-clear)
  1582. (define-key eshell-mode-map (kbd "DEL") 'my-eshell-backward-delete-char)
  1583. (define-key eshell-mode-map (kbd "<up>") 'scroll-down-line)
  1584. (define-key eshell-mode-map (kbd "<down>") 'scroll-up-line)
  1585. ;; (define-key eshell-mode-map
  1586. ;; (kbd "C-p") 'eshell-previous-matching-input-from-input)
  1587. ;; (define-key eshell-mode-map
  1588. ;; (kbd "C-n") 'eshell-next-matching-input-from-input)
  1589. (defvar eshell-virtual-targets nil)
  1590. (add-to-list 'eshell-virtual-targets
  1591. '("/dev/less"
  1592. (lambda (str)
  1593. (if str
  1594. (with-current-buffer nil)))
  1595. nil))
  1596. (defvar eshell-visual-commands nil)
  1597. (add-to-list 'eshell-visual-commands "vim")
  1598. (defvar eshell-output-filter-functions nil)
  1599. (add-to-list 'eshell-output-filter-functions
  1600. 'eshell-truncate-buffer)
  1601. (defvar eshell-command-aliases-list nil)
  1602. (mapcar (lambda (alias)
  1603. (add-to-list 'eshell-command-aliases-list
  1604. alias))
  1605. '(
  1606. ;; ("ll" "ls -l $*")
  1607. ;; ("la" "ls -a $*")
  1608. ;; ("lla" "ls -al $*")
  1609. ("git" "git -c color.ui=always $*")
  1610. ("g" "git $*")
  1611. ("eless"
  1612. (concat "cat >>> (with-current-buffer "
  1613. "(get-buffer-create \"*eshell output\") "
  1614. "(erase-buffer) "
  1615. "(setq buffer-read-only nil) "
  1616. "(current-buffer)) "
  1617. "(view-buffer (get-buffer \"*eshell output*\"))"))
  1618. ))
  1619. )
  1620. (add-hook 'eshell-mode-hook
  1621. (lambda ()
  1622. (apply 'eshell/addpath exec-path)
  1623. (set (make-local-variable 'scroll-margin) 0)
  1624. ;; (eshell/export "GIT_PAGER=")
  1625. ;; (eshell/export "GIT_EDITOR=")
  1626. (eshell/export "LC_MESSAGES=C")
  1627. (switch-to-buffer (current-buffer)) ; move buffer top of list
  1628. (set (make-local-variable (defvar hl-line-range-function))
  1629. (lambda ()
  1630. '(0 . 0)))
  1631. ;; (add-to-list 'eshell-visual-commands "git")
  1632. ))
  1633. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1634. ;; my-term
  1635. (defvar my-term nil
  1636. "My terminal buffer.")
  1637. (defvar my-term-function nil
  1638. "Function to create terminal buffer.
  1639. This function accept no argument and return newly created buffer of terminal.")
  1640. (defun my-term (&optional arg)
  1641. "Open terminal buffer and return that buffer.
  1642. If ARG is given or called with prefix argument, create new buffer."
  1643. (interactive "P")
  1644. (if (and (not arg)
  1645. my-term
  1646. (buffer-name my-term))
  1647. (pop-to-buffer my-term)
  1648. (setq my-term
  1649. (save-window-excursion
  1650. (funcall my-term-function)))
  1651. (and my-term
  1652. (my-term))))
  1653. ;; (setq my-term-function
  1654. ;; (lambda ()
  1655. ;; (if (eq system-type 'windows-nt)
  1656. ;; (eshell)
  1657. ;; (if (require 'multi-term nil t)
  1658. ;; (multi-term)
  1659. ;; (ansi-term shell-file-name)))))
  1660. (setq my-term-function (lambda () (eshell t)))
  1661. ;;(define-key my-prefix-map (kbd "C-s") 'my-term)
  1662. (define-key ctl-x-map "i" 'my-term)
  1663. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1664. ;; misc funcs
  1665. (defalias 'qcalc 'quick-calc)
  1666. (defun memo (&optional dir)
  1667. "Open memo.txt in DIR."
  1668. (interactive)
  1669. (pop-to-buffer (find-file-noselect (concat (if dir
  1670. (file-name-as-directory dir)
  1671. "")
  1672. "memo.txt"))))
  1673. (defvar my-rgrep-alist
  1674. `(
  1675. ;; the silver searcher
  1676. ("ag"
  1677. (executable-find "ag")
  1678. "ag --nocolor --nogroup --nopager --filename ")
  1679. ;; ack
  1680. ("ack"
  1681. (executable-find "ack")
  1682. "ack --nocolor --nogroup --nopager --with-filename ")
  1683. ;; gnu global
  1684. ("global"
  1685. (and (require 'gtags nil t)
  1686. (executable-find "global")
  1687. (gtags-get-rootpath))
  1688. "global --result grep ")
  1689. ;; git grep
  1690. ("gitgrep"
  1691. (eq 0
  1692. (shell-command "git rev-parse --git-dir"))
  1693. "git --no-pager -c color.grep=false grep -nH -e ")
  1694. ;; grep
  1695. ("grep"
  1696. t
  1697. ,(concat "find . "
  1698. "-path '*/.git' -prune -o "
  1699. "-path '*/.svn' -prune -o "
  1700. "-type f -print0 | "
  1701. "xargs -0 grep -nH -e "))
  1702. )
  1703. "Alist of rgrep command.
  1704. Each element is in the form like (NAME SEXP COMMAND), where SEXP returns the
  1705. condition to choose COMMAND when evaluated.")
  1706. (defvar my-rgrep-default nil
  1707. "Default command name for my-rgrep.")
  1708. (defun my-rgrep-grep-command (&optional name alist)
  1709. "Return recursive grep command for current directory or nil.
  1710. If NAME is given, use that without testing.
  1711. Commands are searched from ALIST."
  1712. (if alist
  1713. (if name
  1714. ;; if name is given search that from alist and return the command
  1715. (nth 2 (assoc name
  1716. alist))
  1717. ;; if name is not given try test in 1th elem
  1718. (let ((car (car alist))
  1719. (cdr (cdr alist)))
  1720. (if (eval (nth 1 car))
  1721. ;; if the condition is true return the command
  1722. (nth 2 car)
  1723. ;; try next one
  1724. (and cdr
  1725. (my-rgrep-grep-command name cdr)))))
  1726. ;; if alist is not given set default value
  1727. (my-rgrep-grep-command name my-rgrep-alist)))
  1728. (defun my-rgrep (command-args)
  1729. "My recursive grep. Run COMMAND-ARGS."
  1730. (interactive (let ((cmd (my-rgrep-grep-command my-rgrep-default
  1731. nil)))
  1732. (if cmd
  1733. (list (read-shell-command "grep command: "
  1734. cmd
  1735. 'grep-find-history))
  1736. (error "My-Rgrep: Command for rgrep not found")
  1737. )))
  1738. (compilation-start command-args
  1739. 'grep-mode))
  1740. ;; (defun my-rgrep-symbol-at-point (command-args)
  1741. ;; "My recursive grep. Run COMMAND-ARGS."
  1742. ;; (interactive (list (read-shell-command "grep command: "
  1743. ;; (concat (my-rgrep-grep-command)
  1744. ;; " "
  1745. ;; (thing-at-point 'symbol))
  1746. ;; 'grep-find-history)))
  1747. ;; (compilation-start command-args
  1748. ;; 'grep-mode))
  1749. (defmacro define-my-rgrep (name)
  1750. "Define rgrep for NAME."
  1751. `(defun ,(intern (concat "my-rgrep-"
  1752. name)) ()
  1753. ,(format "My recursive grep by %s."
  1754. name)
  1755. (interactive)
  1756. (let ((my-rgrep-default ,name))
  1757. (if (called-interactively-p 'any)
  1758. (call-interactively 'my-rgrep)
  1759. (error "Not intended to be called noninteractively. Use `my-rgrep'"))))
  1760. )
  1761. (define-my-rgrep "ack")
  1762. (define-my-rgrep "ag")
  1763. (define-my-rgrep "gitgrep")
  1764. (define-my-rgrep "grep")
  1765. (define-my-rgrep "global")
  1766. (define-key ctl-x-map "s" 'my-rgrep)
  1767. ;; (defun make ()
  1768. ;; "Run \"make -k\" in current directory."
  1769. ;; (interactive)
  1770. ;; (compile "make -k"))
  1771. (defalias 'make 'compile)
  1772. (define-key ctl-x-map "c" 'compile)
  1773. ;; TODO: make these a library
  1774. (defvar info-in-prompt
  1775. nil
  1776. "System info in the form of \"[user@host] \".")
  1777. (setq info-in-prompt
  1778. (concat "["
  1779. user-login-name
  1780. "@"
  1781. (car (split-string system-name
  1782. "\\."))
  1783. "]"))
  1784. (defun my-real-function-subr-p (function)
  1785. "Return t if FUNCTION is a built-in function even if it is advised."
  1786. (let* ((advised (and (symbolp function)
  1787. (featurep 'advice)
  1788. (ad-get-advice-info function)))
  1789. (real-function
  1790. (or (and advised (let ((origname (cdr (assq 'origname advised))))
  1791. (and (fboundp origname)
  1792. origname)))
  1793. function))
  1794. (def (if (symbolp real-function)
  1795. (symbol-function real-function)
  1796. function)))
  1797. (subrp def)))
  1798. ;; (my-real-function-subr-p 'my-real-function-subr-p)
  1799. ;; (defadvice read-from-minibuffer (before info-in-prompt activate)
  1800. ;; "Show system info when use `read-from-minibuffer'."
  1801. ;; (ad-set-arg 0
  1802. ;; (concat my-system-info
  1803. ;; (ad-get-arg 0))))
  1804. ;; (defadvice read-string (before info-in-prompt activate)
  1805. ;; "Show system info when use `read-string'."
  1806. ;; (ad-set-arg 0
  1807. ;; (concat my-system-info
  1808. ;; (ad-get-arg 0))))
  1809. ;; (when (< emacs-major-version 24)
  1810. ;; (defadvice completing-read (before info-in-prompt activate)
  1811. ;; "Show system info when use `completing-read'."
  1812. ;; (ad-set-arg 0
  1813. ;; (concat my-system-info
  1814. ;; (ad-get-arg 0)))))
  1815. (defmacro info-in-prompt-set (&rest functions)
  1816. "Set info-in-prompt advices for FUNCTIONS."
  1817. `(progn
  1818. ,@(mapcar (lambda (f)
  1819. `(defadvice ,f (before info-in-prompt activate)
  1820. "Show info in prompt."
  1821. (let ((orig (ad-get-arg 0)))
  1822. (unless (string-match-p (regexp-quote info-in-prompt)
  1823. orig)
  1824. (ad-set-arg 0
  1825. (concat info-in-prompt
  1826. " "
  1827. orig))))))
  1828. functions)))
  1829. (info-in-prompt-set read-from-minibuffer
  1830. read-string
  1831. completing-read)
  1832. ;;; emacs.el ends here