You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

2105 line
71 KiB

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