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.
 
 
 
 
 
 

1599 lines
52 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. (require 'cl-lib)
  12. (require 'simple)
  13. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  14. ;; Some macros for internals
  15. (defun call-after-init (func)
  16. "If `after-init-hook' has been run, call FUNC immediately.
  17. Otherwize hook it."
  18. (if after-init-time
  19. (funcall func)
  20. (add-hook 'after-init-hook
  21. func)))
  22. (defmacro safe-require-or-eval (feature)
  23. "Require FEATURE if available.
  24. At compile time the feature will be loaded immediately."
  25. `(eval-and-compile
  26. (message "safe-require-or-eval: Trying to require %s" ,feature)
  27. (require ,feature nil t)))
  28. (defmacro autoload-eval-lazily (feature &optional functions &rest body)
  29. "Define autoloading FEATURE that defines FUNCTIONS.
  30. FEATURE is a symbol. FUNCTIONS is a list of symbols. If FUNCTIONS is nil,
  31. the function same as FEATURE is defined as autoloaded function. BODY is passed
  32. to `eval-after-load'.
  33. After this macro is expanded, this returns the path to library if FEATURE
  34. found, otherwise returns nil."
  35. (declare (indent 2) (debug t))
  36. (let* ((libname (symbol-name (eval feature)))
  37. (libpath (locate-library libname)))
  38. `(progn
  39. (when (locate-library ,libname)
  40. ,@(mapcar (lambda (f)
  41. `(unless (fboundp ',f)
  42. (progn
  43. (message "Autoloaded function `%S' defined (%s)"
  44. (quote ,f)
  45. ,libpath)
  46. (autoload (quote ,f)
  47. ,libname
  48. ,(concat "Autoloaded function defined in \""
  49. libpath
  50. "\".")
  51. t))))
  52. (or (eval functions)
  53. `(,(eval feature)))))
  54. (eval-after-load ,feature
  55. (quote (progn
  56. ,@body)))
  57. (locate-library ,libname))))
  58. (when (autoload-eval-lazily 'tetris nil
  59. (message "Tetris loaded!"))
  60. (message "Tetris found!"))
  61. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  62. ;; package
  63. (set (defvar 10sr-package-list)
  64. '(
  65. vimrc-mode
  66. markdown-mode
  67. yaml-mode
  68. gnuplot-mode
  69. php-mode
  70. erlang
  71. js2-mode
  72. js-doc
  73. git-commit
  74. gitignore-mode
  75. adoc-mode
  76. ;; It seems malabar has been merged into jdee and this package
  77. ;; already removed
  78. ;; malabar-mode
  79. ;; ack
  80. color-moccur
  81. ggtags
  82. flycheck
  83. auto-highlight-symbol
  84. hl-todo
  85. ;; Currently not available
  86. ;; pp-c-l
  87. ;; is flymake installs are required?
  88. ;;flymake-jshint
  89. ;;flymake-python-pyflakes
  90. xclip
  91. foreign-regexp
  92. multi-term
  93. term-run
  94. editorconfig
  95. git-ps1-mode
  96. restart-emacs
  97. fill-column-indicator
  98. pkgbuild-mode
  99. minibuffer-line
  100. which-key
  101. ;; I think this works in place of my autosave lib
  102. super-save
  103. pipenv
  104. imenu-list
  105. scala-mode
  106. ;;ensime
  107. editorconfig
  108. editorconfig-custom-majormode
  109. git-command
  110. prompt-text
  111. ;; 10sr repository
  112. ;; 10sr-extras
  113. terminal-title
  114. recentf-show
  115. dired-list-all-mode
  116. pack
  117. set-modeline-color
  118. read-only-only-mode
  119. smart-revert
  120. autosave
  121. ;;window-organizer
  122. remember-major-modes-mode
  123. ilookup
  124. pasteboard
  125. end-mark
  126. sl
  127. gosh-mode
  128. ))
  129. (when (safe-require-or-eval 'package)
  130. (setq package-archives
  131. `(,@package-archives
  132. ("melpa" . "https://melpa.org/packages/")
  133. ;; Somehow fails to download via https
  134. ("10sr-el" . "http://10sr.github.io/emacs-lisp/elpa/")))
  135. (package-initialize)
  136. (defun my-auto-install-package ()
  137. "Install packages semi-automatically."
  138. (interactive)
  139. (package-refresh-contents)
  140. (mapc (lambda (pkg)
  141. (or (package-installed-p pkg)
  142. (package-install pkg)))
  143. 10sr-package-list))
  144. )
  145. ;; (lazy-load-eval 'sudoku)
  146. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  147. ;; my-idle-hook
  148. (defvar my-idle-hook nil
  149. "Hook run when idle for several secs.")
  150. (defvar my-idle-hook-sec 5
  151. "Second to run `my-idle-hook'.")
  152. (run-with-idle-timer my-idle-hook-sec
  153. t
  154. (lambda ()
  155. (run-hooks 'my-idle-hook)))
  156. ;; (add-hook 'my-idle-hook
  157. ;; (lambda ()
  158. ;; (message "idle hook message")))
  159. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  160. ;; start and quit
  161. (setq inhibit-startup-message t)
  162. (setq confirm-kill-emacs 'y-or-n-p)
  163. (setq gc-cons-threshold (* 1024 1024 4))
  164. (when window-system
  165. (add-to-list 'default-frame-alist '(cursor-type . box))
  166. (add-to-list 'default-frame-alist '(background-color . "white"))
  167. (add-to-list 'default-frame-alist '(foreground-color . "gray10"))
  168. ;; (add-to-list 'default-frame-alist '(alpha . (80 100 100 100)))
  169. ;; does not work?
  170. )
  171. ;; (add-to-list 'default-frame-alist '(cursor-type . box))
  172. (if window-system (menu-bar-mode 1) (menu-bar-mode 0))
  173. (and (fboundp 'tool-bar-mode)
  174. (tool-bar-mode 0))
  175. (and (fboundp 'set-scroll-bar-mode)
  176. (set-scroll-bar-mode nil))
  177. (call-after-init
  178. (lambda ()
  179. (message "%s %s" invocation-name emacs-version)
  180. (message "Invocation directory: %s" default-directory)
  181. (message "%s was taken to initialize emacs." (emacs-init-time))
  182. (switch-to-buffer "*Messages*")))
  183. (cd ".") ; when using windows use / instead of \ in `default-directory'
  184. ;; locale
  185. (set-language-environment "Japanese")
  186. (set-default-coding-systems 'utf-8-unix)
  187. (prefer-coding-system 'utf-8-unix)
  188. (setq system-time-locale "C")
  189. ;; my prefix map
  190. (defvar my-prefix-map nil
  191. "My prefix map.")
  192. (define-prefix-command 'my-prefix-map)
  193. (define-key ctl-x-map (kbd "C-x") 'my-prefix-map)
  194. (define-key my-prefix-map (kbd "C-q") 'quoted-insert)
  195. (define-key my-prefix-map (kbd "C-z") 'suspend-frame)
  196. ;; (comint-show-maximum-output)
  197. ;; kill scratch
  198. (call-after-init (lambda ()
  199. (let ((buf (get-buffer "*scratch*")))
  200. (when buf
  201. (kill-buffer buf)))))
  202. ;; modifier keys
  203. ;; (setq mac-option-modifier 'control)
  204. ;; display
  205. (setq visible-bell t)
  206. (setq ring-bell-function 'ignore)
  207. (mouse-avoidance-mode 'banish)
  208. (setq echo-keystrokes 0.1)
  209. (defun reload-init-file ()
  210. "Reload Emacs init file."
  211. (interactive)
  212. (when (and user-init-file
  213. (file-readable-p user-init-file))
  214. (load-file user-init-file)))
  215. (safe-require-or-eval 'session)
  216. ;; server
  217. (set-variable 'server-name (concat "server"
  218. (number-to-string (emacs-pid))))
  219. ;; In Cygwin Environment `server-runnning-p' stops when server-use-tcp is nil
  220. ;; In Darwin environment, init fails with message like 'Service name too long'
  221. ;; when server-use-tcp is nil
  222. (when (or (eq system-type
  223. 'cygwin)
  224. (eq system-type
  225. 'darwin))
  226. (set-variable 'server-use-tcp t))
  227. ;; MSYS2 fix
  228. (when (eq system-type
  229. 'windows-nt)
  230. (setq shell-file-name
  231. (executable-find "bash"))
  232. '(setq function-key-map
  233. `(,@function-key-map ([pause] . [?\C-c])
  234. ))
  235. (define-key key-translation-map
  236. (kbd "<pause>")
  237. (kbd "C-c"))
  238. '(keyboard-translate [pause]
  239. (kbd "C-c")p)
  240. ;; TODO: move to other place later
  241. (when (not window-system)
  242. (setq interprogram-paste-function nil)
  243. (setq interprogram-cut-function nil)))
  244. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  245. ;; global keys
  246. (global-set-key (kbd "<up>") 'scroll-down-line)
  247. (global-set-key (kbd "<down>") 'scroll-up-line)
  248. (global-set-key (kbd "<left>") 'scroll-down)
  249. (global-set-key (kbd "<right>") 'scroll-up)
  250. ;; (define-key my-prefix-map (kbd "C-h") help-map)
  251. (global-set-key (kbd "C-\\") help-map)
  252. (define-key ctl-x-map (kbd "DEL") help-map)
  253. (define-key ctl-x-map (kbd "C-h") help-map)
  254. (define-key help-map "a" 'apropos)
  255. ;; disable annoying keys
  256. (global-set-key [prior] 'ignore)
  257. (global-set-key (kbd "<next>") 'ignore)
  258. (global-set-key [menu] 'ignore)
  259. (global-set-key [down-mouse-1] 'ignore)
  260. (global-set-key [down-mouse-2] 'ignore)
  261. (global-set-key [down-mouse-3] 'ignore)
  262. (global-set-key [mouse-1] 'ignore)
  263. (global-set-key [mouse-2] 'ignore)
  264. (global-set-key [mouse-3] 'ignore)
  265. (global-set-key (kbd "<eisu-toggle>") 'ignore)
  266. (global-set-key (kbd "C-<eisu-toggle>") 'ignore)
  267. (when (safe-require-or-eval 'which-key)
  268. (which-key-mode))
  269. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  270. ;; editting
  271. (defun my-copy-whole-line ()
  272. "Copy whole line."
  273. (interactive)
  274. (kill-new (concat (buffer-substring (point-at-bol)
  275. (point-at-eol))
  276. "\n")))
  277. (setq require-final-newline t)
  278. (setq kill-whole-line t)
  279. (setq scroll-conservatively 35
  280. scroll-margin 2
  281. scroll-step 0)
  282. (setq-default major-mode 'text-mode)
  283. (setq next-line-add-newlines nil)
  284. (setq kill-read-only-ok t)
  285. (setq truncate-partial-width-windows nil) ; when splitted horizontally
  286. ;; (setq-default line-spacing 0.2)
  287. (setq-default indicate-empty-lines t) ; when using x indicate empty line
  288. ;; (setq-default tab-width 4)
  289. (setq-default indent-tabs-mode nil)
  290. (setq-default indent-line-function 'indent-to-left-margin)
  291. ;; (setq-default indent-line-function nil)
  292. (setq-default truncate-lines nil)
  293. ;; (pc-selection-mode 1) ; make some already defined keybind back to default
  294. (delete-selection-mode 1)
  295. (cua-mode 0)
  296. (setq line-move-visual nil)
  297. ;; key bindings
  298. ;; moving around
  299. ;;(keyboard-translate ?\M-j ?\C-j)
  300. ;; (global-set-key (kbd "M-p") 'backward-paragraph)
  301. (define-key esc-map "p" 'backward-paragraph)
  302. ;; (global-set-key (kbd "M-n") 'forward-paragraph)
  303. (define-key esc-map "n" 'forward-paragraph)
  304. (global-set-key (kbd "C-<up>") 'scroll-down-line)
  305. (global-set-key (kbd "C-<down>") 'scroll-up-line)
  306. (global-set-key (kbd "C-<left>") 'scroll-down)
  307. (global-set-key (kbd "C-<right>") 'scroll-up)
  308. (global-set-key (kbd "<select>") 'ignore) ; 'previous-line-mark)
  309. (define-key ctl-x-map (kbd "ESC x") 'execute-extended-command)
  310. (define-key ctl-x-map (kbd "ESC :") 'eval-expression)
  311. ;; C-h and DEL
  312. (global-set-key (kbd "C-h") (kbd "DEL"))
  313. ;;(global-set-key (kbd "C-m") 'reindent-then-newline-and-indent)
  314. (global-set-key (kbd "C-m") 'newline-and-indent)
  315. ;; (global-set-key (kbd "C-o") (kbd "C-e C-m"))
  316. (define-key esc-map "k" 'my-copy-whole-line)
  317. ;; (global-set-key "\C-z" 'undo) ; undo is M-u
  318. (define-key esc-map "u" 'undo)
  319. (define-key esc-map "i" (kbd "ESC TAB"))
  320. ;; (global-set-key (kbd "C-r") 'query-replace-regexp)
  321. (global-set-key (kbd "C-s") 'isearch-forward-regexp)
  322. (global-set-key (kbd "C-r") 'isearch-backward-regexp)
  323. (define-key my-prefix-map (kbd "C-o") 'occur)
  324. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  325. ;; title and mode-line
  326. (when (safe-require-or-eval 'terminal-title)
  327. ;; if TERM is not screen use default value
  328. (if (getenv "TMUX")
  329. ;; if use tmux locally just basename of current dir
  330. (set-variable 'terminal-title-format
  331. '((file-name-nondirectory (directory-file-name
  332. default-directory))))
  333. (if (and (let ((tty-type (frame-parameter nil
  334. 'tty-type)))
  335. (and tty-type
  336. (equal (car (split-string tty-type
  337. "-"))
  338. "screen")))
  339. (not (getenv "SSH_CONNECTION")))
  340. (set-variable 'terminal-title-format
  341. '((file-name-nondirectory (directory-file-name
  342. default-directory))))
  343. ;; seems that TMUX is used locally and ssh to remote host
  344. (set-variable 'terminal-title-format
  345. `("em:"
  346. ,user-login-name
  347. "@"
  348. ,(car (split-string (system-name)
  349. "\\."))
  350. ":"
  351. default-directory))
  352. )
  353. )
  354. (terminal-title-mode))
  355. (setq eol-mnemonic-dos "\\r\\n")
  356. (setq eol-mnemonic-mac "\\r")
  357. (setq eol-mnemonic-unix "")
  358. (which-function-mode 0)
  359. (line-number-mode 0)
  360. (column-number-mode 0)
  361. (size-indication-mode 0)
  362. (setq mode-line-position
  363. '(:eval (format "L%%l/%d:C%%c"
  364. (count-lines (point-max)
  365. (point-min)))))
  366. ;; http://www.geocities.jp/simizu_daisuke/bunkei-meadow.html#frame-title
  367. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  368. ;; minibuffer
  369. (setq insert-default-directory t)
  370. (setq completion-ignore-case t
  371. read-file-name-completion-ignore-case t
  372. read-buffer-completion-ignore-case t)
  373. (setq resize-mini-windows t)
  374. (temp-buffer-resize-mode 1)
  375. (savehist-mode 1)
  376. (defvar display-time-format "%Y/%m/%d %a %H:%M")
  377. (fset 'yes-or-no-p 'y-or-n-p)
  378. ;; complete symbol when `eval'
  379. (define-key read-expression-map (kbd "TAB") 'completion-at-point)
  380. (define-key minibuffer-local-map (kbd "C-u")
  381. (lambda () (interactive) (delete-region (point-at-bol) (point))))
  382. ;; I dont know these bindings are good
  383. (define-key minibuffer-local-map (kbd "C-p") (kbd "ESC p"))
  384. (define-key minibuffer-local-map (kbd "C-n") (kbd "ESC n"))
  385. (when (safe-require-or-eval 'minibuffer-line)
  386. (set-face-underline 'minibuffer-line nil)
  387. (set-variable 'minibuffer-line-refresh-interval
  388. 25)
  389. (set-variable 'minibuffer-line-format
  390. `(,(concat user-login-name
  391. "@"
  392. (car (split-string (system-name)
  393. "\\."))
  394. ":")
  395. (:eval (abbreviate-file-name (or buffer-file-name
  396. default-directory)))
  397. (:eval (and (fboundp 'git-ps1-mode-get-current)
  398. (git-ps1-mode-get-current " [GIT:%s]")))
  399. " "
  400. (:eval (format-time-string display-time-format))))
  401. (minibuffer-line-mode 1)
  402. )
  403. (when (safe-require-or-eval 'prompt-text)
  404. (set-variable 'prompt-text-format
  405. `(,(concat ""
  406. user-login-name
  407. "@"
  408. (car (split-string (system-name)
  409. "\\."))
  410. ":")
  411. (:eval (abbreviate-file-name (or buffer-file-name
  412. default-directory)))
  413. (:eval (and (fboundp 'git-ps1-mode-get-current)
  414. (git-ps1-mode-get-current " [GIT:%s]")))
  415. " "
  416. (:eval (format-time-string display-time-format))
  417. "\n"
  418. (:eval (symbol-name this-command))
  419. ": "))
  420. (prompt-text-mode 1))
  421. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  422. ;; letters, font-lock mode and fonts
  423. ;; (set-face-background 'vertical-border (face-foreground 'mode-line))
  424. ;; (set-window-margins (selected-window) 1 1)
  425. (and (or (eq system-type 'Darwin)
  426. (eq system-type 'darwin))
  427. (fboundp 'mac-set-input-method-parameter)
  428. (mac-set-input-method-parameter 'japanese 'cursor-color "red")
  429. (mac-set-input-method-parameter 'roman 'cursor-color "black"))
  430. (when (and (boundp 'input-method-activate-hook) ; i dont know this is correct
  431. (boundp 'input-method-inactivate-hook))
  432. (add-hook 'input-method-activate-hook
  433. (lambda () (set-cursor-color "red")))
  434. (add-hook 'input-method-inactivate-hook
  435. (lambda () (set-cursor-color "black"))))
  436. (when (safe-require-or-eval 'paren)
  437. (show-paren-mode 1)
  438. (setq show-paren-delay 0.5
  439. show-paren-style 'parenthesis) ; mixed is hard to read
  440. ;; (set-face-background 'show-paren-match
  441. ;; "black")
  442. ;; ;; (face-foreground 'default))
  443. ;; (set-face-foreground 'show-paren-match
  444. ;; "white")
  445. ;; (set-face-inverse-video-p 'show-paren-match
  446. ;; t)
  447. )
  448. (transient-mark-mode 1)
  449. (global-font-lock-mode 1)
  450. (setq font-lock-global-modes
  451. '(not
  452. help-mode
  453. eshell-mode
  454. ;;term-mode
  455. Man-mode))
  456. ;; (standard-display-ascii ?\n "$\n")
  457. ;; (defvar my-eol-face
  458. ;; '(("\n" . (0 font-lock-comment-face t nil)))
  459. ;; )
  460. ;; (defvar my-tab-face
  461. ;; '(("\t" . '(0 highlight t nil))))
  462. (defvar my-jspace-face
  463. '(("\u3000" . '(0 highlight t nil))))
  464. (add-hook 'font-lock-mode-hook
  465. (lambda ()
  466. ;; (font-lock-add-keywords nil my-eol-face)
  467. (font-lock-add-keywords nil my-jspace-face)
  468. ))
  469. (when (safe-require-or-eval 'whitespace)
  470. (add-to-list 'whitespace-display-mappings
  471. ;; We need t since last one takes precedence
  472. `(tab-mark ?\t ,(vconcat "^I\t")) t)
  473. ;; (add-to-list 'whitespace-display-mappings
  474. ;; `(newline-mark ?\n ,(vconcat "$\n")))
  475. (setq whitespace-style '(face
  476. trailing ; trailing blanks
  477. newline ; newlines
  478. newline-mark ; use display table for newline
  479. tab-mark
  480. empty ; empty lines at beg or end of buffer
  481. lines-tail ; lines over 80
  482. ))
  483. ;; (setq whitespace-newline 'font-lock-comment-face)
  484. (set-variable 'whitespace-line-column nil)
  485. (global-whitespace-mode t)
  486. (add-hook 'dired-mode-hook
  487. (lambda ()
  488. (set (make-local-variable 'whitespace-style) nil)))
  489. (if (eq (display-color-cells)
  490. 256)
  491. (set-face-foreground 'whitespace-newline "color-109")
  492. ;; (progn
  493. ;; (set-face-bold-p 'whitespace-newline
  494. ;; t))
  495. ))
  496. (and nil
  497. '(safe-require-or-eval 'fill-column-indicator)
  498. (setq fill-column-indicator))
  499. ;; highlight current line
  500. ;; http://wiki.riywo.com/index.php?Meadow
  501. (face-spec-set 'hl-line
  502. '((((min-colors 256)
  503. (background dark))
  504. (:background "color-234"))
  505. (((min-colors 256)
  506. (background light))
  507. (:background "color-234"))
  508. (t
  509. (:underline "black"))))
  510. (set-variable 'hl-line-global-modes
  511. '(not
  512. term-mode))
  513. (global-hl-line-mode 1) ;; (hl-line-mode 1)
  514. (set-face-foreground 'font-lock-regexp-grouping-backslash "#666")
  515. (set-face-foreground 'font-lock-regexp-grouping-construct "#f60")
  516. ;;(safe-require-or-eval 'set-modeline-color)
  517. ;; (let ((fg (face-foreground 'default))
  518. ;; (bg (face-background 'default)))
  519. ;; (set-face-background 'mode-line-inactive
  520. ;; (if (face-inverse-video-p 'mode-line) fg bg))
  521. ;; (set-face-foreground 'mode-line-inactive
  522. ;; (if (face-inverse-video-p 'mode-line) bg fg)))
  523. ;; (set-face-underline 'mode-line-inactive
  524. ;; t)
  525. ;; (set-face-underline 'vertical-border
  526. ;; nil)
  527. (when (safe-require-or-eval 'end-mark)
  528. (global-end-mark-mode))
  529. (when (safe-require-or-eval 'auto-highlight-symbol)
  530. (set-variable 'ahs-idle-interval 0.6)
  531. (global-auto-highlight-symbol-mode 1))
  532. ;; (when (safe-require-or-eval 'cyberpunk-theme)
  533. ;; (load-theme 'cyberpunk t)
  534. ;; (set-face-attribute 'button
  535. ;; nil
  536. ;; :inherit 'highlight)
  537. ;; (set-face-foreground 'mode-line-inactive
  538. ;; "white")
  539. ;; (face-spec-set 'term-color-black (face-default-spec 'term-color-black))
  540. ;; (face-spec-set 'term-color-red (face-default-spec 'term-color-red))
  541. ;; (face-spec-set 'term-color-green (face-default-spec 'term-color-green))
  542. ;; (face-spec-set 'term-color-yellow (face-default-spec 'term-color-yellow))
  543. ;; (face-spec-set 'term-color-blue (face-default-spec 'term-color-blue))
  544. ;; (face-spec-set 'term-color-magenta (face-default-spec 'term-color-magenta))
  545. ;; (face-spec-set 'term-color-cyan (face-default-spec 'term-color-cyan))
  546. ;; (face-spec-set 'term-color-white (face-default-spec 'term-color-white))
  547. ;; )
  548. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  549. ;; file handling
  550. (when (safe-require-or-eval 'editorconfig)
  551. ;; (set-variable 'editorconfig-get-properties-function
  552. ;; 'editorconfig-core-get-properties-hash)
  553. (editorconfig-mode 1)
  554. (set-variable 'editorconfig-mode-lighter " EC")
  555. (with-eval-after-load 'org-src
  556. ;; [*.org\[\*Org Src*\[ c \]*\]]
  557. (add-hook 'org-src-mode-hook
  558. 'editorconfig-mode-apply t)))
  559. (when (fboundp 'editorconfig-custom-majormode)
  560. (add-hook 'editorconfig-custom-hooks
  561. 'editorconfig-custom-majormode))
  562. ;; (when (fboundp 'editorconfig-charset-extras)
  563. ;; (add-hook 'editorconfig-custom-hooks
  564. ;; 'editorconfig-charset-extras))
  565. (setq revert-without-query '(".+"))
  566. ;; save cursor position
  567. (when (safe-require-or-eval 'saveplace)
  568. (setq-default save-place t)
  569. (setq save-place-file (concat user-emacs-directory
  570. "places")))
  571. ;; http://www.bookshelf.jp/soft/meadow_24.html#SEC260
  572. (setq make-backup-files t)
  573. ;; (make-directory (expand-file-name "~/.emacsbackup"))
  574. (setq backup-directory-alist
  575. (cons (cons "\\.*$" (expand-file-name (concat user-emacs-directory
  576. "backup")))
  577. backup-directory-alist))
  578. (setq version-control 'never)
  579. (setq delete-old-versions t)
  580. (setq auto-save-list-file-prefix (expand-file-name (concat user-emacs-directory
  581. "auto-save/")))
  582. (setq delete-auto-save-files t)
  583. (add-to-list 'completion-ignored-extensions ".bak")
  584. (setq delete-by-moving-to-trash t)
  585. ;; trash-directory "~/.emacs.d/trash")
  586. (add-hook 'after-save-hook
  587. 'executable-make-buffer-file-executable-if-script-p)
  588. (set (defvar bookmark-default-file)
  589. (expand-file-name (concat user-emacs-directory
  590. "bmk")))
  591. (with-eval-after-load 'recentf
  592. (defvar recentf-exclude nil)
  593. (add-to-list 'recentf-exclude
  594. (regexp-quote bookmark-default-file)))
  595. (when (safe-require-or-eval 'smart-revert)
  596. (smart-revert-on))
  597. ;; autosave
  598. (when (safe-require-or-eval 'autosave)
  599. (autosave-set 8))
  600. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  601. ;; buffer killing
  602. ;; (defun my-delete-window-killing-buffer () nil)
  603. (defun my-query-kill-current-buffer ()
  604. "Interactively kill current buffer."
  605. (interactive)
  606. (if (y-or-n-p (concat "kill current buffer? :"))
  607. (kill-buffer (current-buffer))))
  608. ;;(global-set-key "\C-xk" 'my-query-kill-current-buffer)
  609. (substitute-key-definition 'kill-buffer
  610. 'my-query-kill-current-buffer
  611. global-map)
  612. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  613. ;; share clipboard with x
  614. ;; this page describes this in details, but only these sexps seem to be needed
  615. ;; http://garin.jp/doc/Linux/xwindow_clipboard
  616. (and nil
  617. (not window-system)
  618. (not (eq window-system 'mac))
  619. (getenv "DISPLAY")
  620. (not (equal (getenv "DISPLAY") ""))
  621. (executable-find "xclip")
  622. ;; (< emacs-major-version 24)
  623. '(safe-require-or-eval 'xclip)
  624. nil
  625. (turn-on-xclip))
  626. (and (eq system-type 'darwin)
  627. (safe-require-or-eval 'pasteboard)
  628. (turn-on-pasteboard))
  629. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  630. ;; some modes and hooks
  631. ;; Include some extra modes
  632. (require 'generic-x)
  633. (with-eval-after-load 'compile
  634. (defvar compilation-filter-start)
  635. (defvar compilation-error-regexp-alist)
  636. (require 'ansi-color)
  637. (add-hook 'compilation-filter-hook
  638. (lambda ()
  639. (let ((inhibit-read-only t))
  640. (ansi-color-apply-on-region compilation-filter-start
  641. (point)))))
  642. (add-to-list 'compilation-error-regexp-alist
  643. ;; ansible-lint
  644. '("^\\([^ \n]+\\):\\([0-9]+\\)$" 1 2)))
  645. ;; Workaround to avoid ensime error
  646. (defvar ensime-mode-key-prefix nil)
  647. ;; http://qiita.com/sune2/items/b73037f9e85962f5afb7
  648. (when (safe-require-or-eval 'company)
  649. (global-company-mode)
  650. (set-variable 'company-idle-delay 0.5)
  651. (set-variable 'company-minimum-prefix-length 2)
  652. (set-variable 'company-selection-wrap-around t))
  653. ;; https://github.com/lunaryorn/flycheck
  654. (when (safe-require-or-eval 'flycheck)
  655. (call-after-init 'global-flycheck-mode))
  656. (when (autoload-eval-lazily 'ilookup)
  657. (define-key ctl-x-map "d" 'ilookup-open-word))
  658. (set-variable 'ac-ignore-case nil)
  659. (when (autoload-eval-lazily 'term-run '(term-run-shell-command term-run))
  660. (define-key ctl-x-map "t" 'term-run-shell-command))
  661. (add-to-list 'safe-local-variable-values
  662. '(encoding utf-8))
  663. (setq enable-local-variables :safe)
  664. ;; (when (safe-require-or-eval 'remember-major-modes-mode)
  665. ;; (remember-major-modes-mode 1))
  666. ;; Detect file type from shebang and set major-mode.
  667. (add-to-list 'interpreter-mode-alist
  668. '("python3" . python-mode))
  669. (add-to-list 'interpreter-mode-alist
  670. '("python2" . python-mode))
  671. (with-eval-after-load 'python
  672. (defvar python-mode-map (make-sparse-keymap))
  673. (define-key python-mode-map (kbd "C-m") 'newline-and-indent))
  674. ;; http://fukuyama.co/foreign-regexp
  675. '(and (safe-require-or-eval 'foreign-regexp)
  676. (progn
  677. (setq foreign-regexp/regexp-type 'perl)
  678. '(setq reb-re-syntax 'foreign-regexp)
  679. ))
  680. (autoload-eval-lazily 'sql '(sql-mode)
  681. (require 'sql-indent nil t))
  682. (when (autoload-eval-lazily 'git-command)
  683. (define-key ctl-x-map "g" 'git-command))
  684. (when (safe-require-or-eval 'git-commit)
  685. (global-git-commit-mode 1))
  686. (with-eval-after-load 'git-commit
  687. (add-hook 'git-commit-setup-hook
  688. 'turn-off-auto-fill t))
  689. (autoload-eval-lazily 'sl)
  690. (with-eval-after-load 'rst
  691. (defvar rst-mode-map)
  692. (define-key rst-mode-map (kbd "C-m") 'newline-and-indent))
  693. (with-eval-after-load 'jdee
  694. (add-hook 'jdee-mode-hook
  695. (lambda ()
  696. (make-local-variable 'global-mode-string)
  697. (add-to-list 'global-mode-string
  698. mode-line-position))))
  699. ;; Cannot enable error thrown. Why???
  700. ;; https://github.com/m0smith/malabar-mode#Installation
  701. ;; (when (autoload-eval-lazily 'malabar-mode)
  702. ;; (add-to-list 'load-path
  703. ;; (expand-file-name (concat user-emacs-directory "/cedet")))
  704. ;; (safe-require-or-eval 'cedet-devel-load)
  705. ;; (call-after-init 'activate-malabar-mode))
  706. (with-eval-after-load 'make-mode
  707. (defvar makefile-mode-map (make-sparse-keymap))
  708. (define-key makefile-mode-map (kbd "C-m") 'newline-and-indent)
  709. ;; this functions is set in write-file-functions, i cannot find any
  710. ;; good way to remove this.
  711. (fset 'makefile-warn-suspicious-lines 'ignore))
  712. (with-eval-after-load 'verilog-mode
  713. (defvar verilog-mode-map (make-sparse-keymap))
  714. (define-key verilog-mode-map ";" 'self-insert-command))
  715. (setq diff-switches "-u")
  716. (with-eval-after-load 'diff-mode
  717. ;; (when (and (eq major-mode
  718. ;; 'diff-mode)
  719. ;; (not buffer-file-name))
  720. ;; ;; do not pass when major-mode is derived mode of diff-mode
  721. ;; (view-mode 1))
  722. (set-face-attribute 'diff-header nil
  723. :foreground nil
  724. :background nil
  725. :weight 'bold)
  726. (set-face-attribute 'diff-file-header nil
  727. :foreground nil
  728. :background nil
  729. :weight 'bold)
  730. (set-face-foreground 'diff-index "blue")
  731. (set-face-attribute 'diff-hunk-header nil
  732. :foreground "cyan"
  733. :weight 'normal)
  734. (set-face-attribute 'diff-context nil
  735. ;; :foreground "white"
  736. :foreground nil
  737. :weight 'normal)
  738. (set-face-foreground 'diff-removed "red")
  739. (set-face-foreground 'diff-added "green")
  740. (set-face-background 'diff-removed nil)
  741. (set-face-background 'diff-added nil)
  742. (set-face-attribute 'diff-changed nil
  743. :foreground "magenta"
  744. :weight 'normal)
  745. (set-face-attribute 'diff-refine-changed nil
  746. :foreground nil
  747. :background nil
  748. :weight 'bold
  749. :inverse-video t)
  750. ;; Annoying !
  751. ;;(diff-auto-refine-mode)
  752. )
  753. ;; (ffap-bindings)
  754. (set-variable 'browse-url-browser-function
  755. 'eww-browse-url)
  756. (set-variable 'sh-here-document-word "__EOC__")
  757. (when (autoload-eval-lazily 'adoc-mode
  758. nil
  759. (defvar adoc-mode-map (make-sparse-keymap))
  760. (define-key adoc-mode-map (kbd "C-m") 'newline))
  761. (setq auto-mode-alist
  762. `(("\\.adoc\\'" . adoc-mode)
  763. ("\\.asciidoc\\'" . adoc-mode)
  764. ,@auto-mode-alist)))
  765. (with-eval-after-load 'markup-faces
  766. ;; Is this too match ?
  767. (set-face-foreground 'markup-meta-face
  768. "color-245")
  769. (set-face-foreground 'markup-meta-hide-face
  770. "color-245")
  771. )
  772. ;; TODO: check if this is required
  773. (when (autoload-eval-lazily 'groovy-mode nil
  774. (defvar groovy-mode-map (make-sparse-keymap))
  775. (define-key groovy-mode-map "(" 'self-insert-command)
  776. (define-key groovy-mode-map ")" 'self-insert-command)
  777. (define-key groovy-mode-map (kbd "C-m") 'newline-and-indent)
  778. )
  779. (add-to-list 'auto-mode-alist
  780. '("build\\.gradle\\'" . groovy-mode)))
  781. (with-eval-after-load 'yaml-mode
  782. (defvar yaml-mode-map (make-sparse-keymap))
  783. (define-key yaml-mode-map (kbd "C-m") 'newline))
  784. (with-eval-after-load 'html-mode
  785. (defvar html-mode-map (make-sparse-keymap))
  786. (define-key html-mode-map (kbd "C-m") 'reindent-then-newline-and-indent))
  787. (with-eval-after-load 'text-mode
  788. (define-key text-mode-map (kbd "C-m") 'newline))
  789. (with-eval-after-load 'info
  790. (defvar Info-directory-list nil)
  791. (add-to-list 'Info-directory-list
  792. (expand-file-name "~/.brew/share/info")))
  793. (with-eval-after-load 'apropos
  794. (defvar apropos-mode-map (make-sparse-keymap))
  795. (define-key apropos-mode-map "n" 'next-line)
  796. (define-key apropos-mode-map "p" 'previous-line))
  797. ;; `isearch' library does not call `provide' so cannot use with-eval-after-load
  798. ;; (define-key isearch-mode-map
  799. ;; (kbd "C-j") 'isearch-other-control-char)
  800. ;; (define-key isearch-mode-map
  801. ;; (kbd "C-k") 'isearch-other-control-char)
  802. ;; (define-key isearch-mode-map
  803. ;; (kbd "C-h") 'isearch-other-control-char)
  804. (define-key isearch-mode-map (kbd "C-h") 'isearch-delete-char)
  805. (define-key isearch-mode-map (kbd "M-r")
  806. 'isearch-query-replace-regexp)
  807. ;; do not cleanup isearch highlight: use `lazy-highlight-cleanup' to remove
  808. (setq lazy-highlight-cleanup nil)
  809. ;; face for isearch highlighing
  810. (set-face-attribute 'lazy-highlight
  811. nil
  812. :foreground `unspecified
  813. :background `unspecified
  814. :underline t
  815. ;; :weight `bold
  816. )
  817. (add-hook 'outline-mode-hook
  818. (lambda ()
  819. (when (string-match "\\.md\\'" buffer-file-name)
  820. (set (make-local-variable 'outline-regexp) "#+ "))))
  821. (add-to-list 'auto-mode-alist (cons "\\.ol\\'" 'outline-mode))
  822. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'outline-mode))
  823. (when (autoload-eval-lazily 'markdown-mode
  824. '(markdown-mode gfm-mode)
  825. (defvar gfm-mode-map (make-sparse-keymap))
  826. (define-key gfm-mode-map (kbd "C-m") 'electric-indent-just-newline))
  827. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'gfm-mode))
  828. (set-variable 'markdown-command (or (executable-find "markdown")
  829. (executable-find "markdown.pl")
  830. ""))
  831. (add-hook 'markdown-mode-hook
  832. (lambda ()
  833. (outline-minor-mode 1)
  834. (flyspell-mode)
  835. (set (make-local-variable 'comment-start) ";")))
  836. )
  837. ;; c-mode
  838. ;; http://www.emacswiki.org/emacs/IndentingC
  839. ;; http://en.wikipedia.org/wiki/Indent_style
  840. ;; http://d.hatena.ne.jp/emergent/20070203/1170512717
  841. ;; http://seesaawiki.jp/whiteflare503/d/Emacs%20%a5%a4%a5%f3%a5%c7%a5%f3%a5%c8
  842. (with-eval-after-load 'cc-vars
  843. (defvar c-default-style nil)
  844. (add-to-list 'c-default-style
  845. '(c-mode . "k&r"))
  846. (add-to-list 'c-default-style
  847. '(c++-mode . "k&r")))
  848. (autoload-eval-lazily 'js2-mode nil
  849. ;; currently do not use js2-mode
  850. ;; (add-to-list 'auto-mode-alist '("\\.js\\'" . js2-mode))
  851. ;; (add-to-list 'auto-mode-alist '("\\.jsm\\'" . js2-mode))
  852. ;; (defvar js2-mode-map (make-sparse-keymap))
  853. ;; (define-key js2-mode-map (kbd "C-m") (lambda ()
  854. ;; (interactive)
  855. ;; (js2-enter-key)
  856. ;; (indent-for-tab-command)))
  857. ;; (add-hook (kill-local-variable 'before-save-hook)
  858. ;; 'js2-before-save)
  859. ;; (add-hook 'before-save-hook
  860. ;; 'my-indent-buffer
  861. ;; nil
  862. ;; t)
  863. )
  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. (add-hook 'haskell-mode-hook 'turn-on-haskell-indentation)
  872. (with-eval-after-load 'uniquify
  873. (setq uniquify-buffer-name-style 'post-forward-angle-brackets)
  874. (setq uniquify-ignore-buffers-re "*[^*]+*")
  875. (setq uniquify-min-dir-content 1))
  876. (with-eval-after-load 'view
  877. (defvar view-mode-map (make-sparse-keymap))
  878. (define-key view-mode-map "j" 'scroll-up-line)
  879. (define-key view-mode-map "k" 'scroll-down-line)
  880. (define-key view-mode-map "v" 'toggle-read-only)
  881. (define-key view-mode-map "q" 'bury-buffer)
  882. ;; (define-key view-mode-map "/" 'nonincremental-re-search-forward)
  883. ;; (define-key view-mode-map "?" 'nonincremental-re-search-backward)
  884. ;; (define-key view-mode-map
  885. ;; "n" 'nonincremental-repeat-search-forward)
  886. ;; (define-key view-mode-map
  887. ;; "N" 'nonincremental-repeat-search-backward)
  888. (define-key view-mode-map "/" 'isearch-forward-regexp)
  889. (define-key view-mode-map "?" 'isearch-backward-regexp)
  890. (define-key view-mode-map "n" 'isearch-repeat-forward)
  891. (define-key view-mode-map "N" 'isearch-repeat-backward)
  892. (define-key view-mode-map (kbd "C-m") 'my-rgrep-symbol-at-point))
  893. (global-set-key "\M-r" 'view-mode)
  894. ;; (setq view-read-only t)
  895. (with-eval-after-load 'term
  896. (defvar term-raw-map (make-sparse-keymap))
  897. (define-key term-raw-map (kbd "C-x")
  898. (lookup-key (current-global-map)
  899. (kbd "C-x"))))
  900. (add-hook 'term-mode-hook
  901. (lambda ()
  902. ;; Stop current line highlighting
  903. (set (make-local-variable (defvar hl-line-range-function))
  904. (lambda () '(0 . 0)))
  905. (set (make-local-variable 'scroll-margin)
  906. 0)))
  907. (add-hook 'Man-mode-hook
  908. (lambda ()
  909. (view-mode 1)
  910. (setq truncate-lines nil)))
  911. (set-variable 'Man-notify-method (if window-system
  912. 'newframe
  913. 'aggressive))
  914. (set-variable 'woman-cache-filename (expand-file-name (concat user-emacs-directory
  915. "woman_cache.el")))
  916. ;; not work because man.el will be loaded when man called
  917. (defalias 'man 'woman)
  918. (add-to-list 'auto-mode-alist
  919. '("tox\\.ini\\'" . conf-unix-mode))
  920. (when (autoload-eval-lazily 'toml-mode)
  921. (add-to-list 'auto-mode-alist
  922. '("/Pipfile\\'" . toml-mode)))
  923. (when (autoload-eval-lazily 'json-mode)
  924. (add-to-list 'auto-mode-alist
  925. '("/Pipfile\\.lock\\'" . json-mode)))
  926. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  927. ;; buffer switching
  928. (defvar bs-configurations)
  929. (when (autoload-eval-lazily 'bs '(bs-show)
  930. (add-to-list 'bs-configurations
  931. '("specials" "^\\*" nil ".*" nil nil))
  932. (add-to-list 'bs-configurations
  933. '("files-and-specials" "^\\*" buffer-file-name ".*" nil nil))
  934. (defvar bs-mode-map)
  935. (defvar bs-current-configuration)
  936. (define-key bs-mode-map (kbd "t")
  937. ;; TODO: fix toggle feature
  938. (lambda ()
  939. (interactive)
  940. (if (string= "specials"
  941. bs-current-configuration)
  942. (bs-set-configuration "files")
  943. (bs-set-configuration "specials"))
  944. (bs-refresh)
  945. (bs-message-without-log "%s"
  946. (bs--current-config-message))))
  947. ;; (setq bs-configurations (list
  948. ;; '("processes" nil get-buffer-process ".*" nil nil)
  949. ;; '("files-and-scratch" "^\\*scratch\\*$" nil nil
  950. ;; bs-visits-non-file bs-sort-buffer-interns-are-last)))
  951. )
  952. (defalias 'list-buffers 'bs-show)
  953. (set-variable 'bs-default-configuration "files-and-specials")
  954. (set-variable 'bs-default-sort-name "by nothing")
  955. (add-hook 'bs-mode-hook
  956. (lambda ()
  957. (set (make-local-variable 'scroll-margin) 0))))
  958. ;;(iswitchb-mode 1)
  959. (icomplete-mode)
  960. (defun iswitchb-buffer-display-other-window ()
  961. "Do iswitchb in other window."
  962. (interactive)
  963. (let ((iswitchb-default-method 'display))
  964. (call-interactively 'iswitchb-buffer)))
  965. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  966. ;; vc
  967. (set-variable 'vc-handled-backends '())
  968. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  969. ;; recentf-mode
  970. (set-variable 'recentf-save-file (expand-file-name (concat user-emacs-directory
  971. "recentf")))
  972. (set-variable 'recentf-max-menu-items 20)
  973. (set-variable 'recentf-max-saved-items 30)
  974. (set-variable 'recentf-show-file-shortcuts-flag nil)
  975. (when (safe-require-or-eval 'recentf)
  976. (add-to-list 'recentf-exclude
  977. (regexp-quote recentf-save-file))
  978. (add-to-list 'recentf-exclude
  979. (regexp-quote (expand-file-name user-emacs-directory)))
  980. (define-key ctl-x-map (kbd "C-r") 'recentf-open-files)
  981. (remove-hook 'find-file-hook
  982. 'recentf-track-opened-file)
  983. (defun my-recentf-load-track-save-list ()
  984. "Load current recentf list from file, track current visiting file, then save
  985. the list."
  986. (recentf-load-list)
  987. (recentf-track-opened-file)
  988. (recentf-save-list))
  989. (add-hook 'find-file-hook
  990. 'my-recentf-load-track-save-list)
  991. (add-hook 'kill-emacs-hook
  992. 'recentf-load-list)
  993. ;;(run-with-idle-timer 5 t 'recentf-save-list)
  994. ;; (add-hook 'find-file-hook
  995. ;; (lambda ()
  996. ;; (recentf-add-file default-directory)))
  997. (and (autoload-eval-lazily 'recentf-show)
  998. (define-key ctl-x-map (kbd "C-r") 'recentf-show)
  999. (add-hook 'recentf-show-before-listing-hook
  1000. 'recentf-load-list))
  1001. (recentf-mode 1)
  1002. (define-key recentf-dialog-mode-map (kbd "<up>") 'previous-line)
  1003. (define-key recentf-dialog-mode-map (kbd "<down>") 'next-line)
  1004. (define-key recentf-dialog-mode-map "p" 'previous-line)
  1005. (define-key recentf-dialog-mode-map "n" 'next-line)
  1006. (add-hook 'recentf-dialog-mode-hook
  1007. (lambda ()
  1008. ;; (recentf-save-list)
  1009. ;; (define-key recentf-dialog-mode-map (kbd "C-x C-f")
  1010. ;; 'my-recentf-cd-and-find-file)
  1011. (cd "~/"))))
  1012. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1013. ;; dired
  1014. (defun my-dired-echo-file-head (arg)
  1015. "Echo head of current file.
  1016. ARG is num to show, or defaults to 7."
  1017. (interactive "P")
  1018. (let ((f (dired-get-filename)))
  1019. (message "%s"
  1020. (with-temp-buffer
  1021. (insert-file-contents f)
  1022. (buffer-substring-no-properties
  1023. (point-min)
  1024. (progn (goto-char (point-min))
  1025. (forward-line (1- (if arg
  1026. (prefix-numeric-value arg)
  1027. 7)))
  1028. (point-at-eol)))))))
  1029. (defun my-dired-diff ()
  1030. "Show diff of marked file and file of current line."
  1031. (interactive)
  1032. (let ((files (dired-get-marked-files nil nil nil t)))
  1033. (if (eq (car files)
  1034. t)
  1035. (diff (cadr files) (dired-get-filename))
  1036. (message "One file must be marked!"))))
  1037. (defun dired-get-file-info ()
  1038. "Print information of current line file."
  1039. (interactive)
  1040. (let ((f (shell-quote-argument (dired-get-filename t))))
  1041. (if (file-directory-p f)
  1042. (progn
  1043. (message "Calculating disk usage...")
  1044. (shell-command (concat "du -hsD "
  1045. f)))
  1046. (shell-command (concat "file "
  1047. f)))))
  1048. (defun my-dired-scroll-up ()
  1049. "Scroll up."
  1050. (interactive)
  1051. (my-dired-previous-line (- (window-height) 1)))
  1052. (defun my-dired-scroll-down ()
  1053. "Scroll down."
  1054. (interactive)
  1055. (my-dired-next-line (- (window-height) 1)))
  1056. ;; (defun my-dired-forward-line (arg)
  1057. ;; ""
  1058. ;; (interactive "p"))
  1059. (defun my-dired-previous-line (arg)
  1060. "Move ARG lines up."
  1061. (interactive "p")
  1062. (if (> arg 0)
  1063. (progn
  1064. (if (eq (line-number-at-pos)
  1065. 1)
  1066. (goto-char (point-max))
  1067. (forward-line -1))
  1068. (my-dired-previous-line (if (or (dired-get-filename nil t)
  1069. (dired-get-subdir))
  1070. (- arg 1)
  1071. arg)))
  1072. (dired-move-to-filename)))
  1073. (defun my-dired-next-line (arg)
  1074. "Move ARG lines down."
  1075. (interactive "p")
  1076. (if (> arg 0)
  1077. (progn
  1078. (if (eq (point)
  1079. (point-max))
  1080. (goto-char (point-min))
  1081. (forward-line 1))
  1082. (my-dired-next-line (if (or (dired-get-filename nil t)
  1083. (dired-get-subdir))
  1084. (- arg 1)
  1085. arg)))
  1086. (dired-move-to-filename)))
  1087. (defun my-dired-tramp-open (host)
  1088. "Open HOST home directory."
  1089. (interactive "sHostname: ")
  1090. (find-file (format "/scp:%s:" host)))
  1091. ;;http://bach.istc.kobe-u.ac.jp/lect/tamlab/ubuntu/emacs.html
  1092. (if (eq window-system 'mac)
  1093. (setq dired-listing-switches "-lhF")
  1094. (setq dired-listing-switches "-lhF --time-style=long-iso")
  1095. )
  1096. (setq dired-listing-switches "-lhF")
  1097. (put 'dired-find-alternate-file 'disabled nil)
  1098. ;; when using dired-find-alternate-file
  1099. ;; reuse current dired buffer for the file to open
  1100. (set-variable 'dired-ls-F-marks-symlinks t)
  1101. (set-variable 'ls-lisp-use-insert-directory-program nil) ; always use ls-lisp
  1102. (set-variable 'ls-lisp-dirs-first t)
  1103. (set-variable 'ls-lisp-use-localized-time-format t)
  1104. (set-variable 'ls-lisp-format-time-list
  1105. '("%Y-%m-%d %H:%M"
  1106. "%Y-%m-%d "))
  1107. (set-variable 'dired-dwim-target t)
  1108. (set-variable 'dired-isearch-filenames t)
  1109. (set-variable 'dired-hide-details-hide-symlink-targets nil)
  1110. (set-variable 'dired-hide-details-hide-information-lines nil)
  1111. (set-variable 'dired-deletion-confirmer 'y-or-n-p)
  1112. (set-variable 'dired-recursive-deletes 'always)
  1113. ;; (add-hook 'dired-after-readin-hook
  1114. ;; 'my-replace-nasi-none)
  1115. (with-eval-after-load 'dired
  1116. (safe-require-or-eval 'ls-lisp)
  1117. (defvar dired-mode-map (make-sparse-keymap))
  1118. (define-key dired-mode-map "i" 'dired-get-file-info)
  1119. (define-key dired-mode-map "f" 'find-file)
  1120. (define-key dired-mode-map "!" 'shell-command)
  1121. (define-key dired-mode-map "&" 'async-shell-command)
  1122. (define-key dired-mode-map "X" 'dired-do-async-shell-command)
  1123. (define-key dired-mode-map "=" 'my-dired-diff)
  1124. (define-key dired-mode-map "B" 'gtkbm-add-current-dir)
  1125. (define-key dired-mode-map "b" 'gtkbm)
  1126. (define-key dired-mode-map "h" 'my-dired-echo-file-head)
  1127. (define-key dired-mode-map (kbd "TAB") 'other-window)
  1128. ;; (define-key dired-mode-map "P" 'my-dired-do-pack-or-unpack)
  1129. (define-key dired-mode-map "/" 'dired-isearch-filenames)
  1130. (define-key dired-mode-map (kbd "DEL") 'dired-up-directory)
  1131. (define-key dired-mode-map (kbd "C-h") 'dired-up-directory)
  1132. (substitute-key-definition 'dired-next-line
  1133. 'my-dired-next-line
  1134. dired-mode-map)
  1135. (substitute-key-definition 'dired-previous-line
  1136. 'my-dired-previous-line
  1137. dired-mode-map)
  1138. ;; (define-key dired-mode-map (kbd "C-p") 'my-dired-previous-line)
  1139. ;; (define-key dired-mode-map (kbd "p") 'my-dired-previous-line)
  1140. ;; (define-key dired-mode-map (kbd "C-n") 'my-dired-next-line)
  1141. ;; (define-key dired-mode-map (kbd "n") 'my-dired-next-line)
  1142. (define-key dired-mode-map (kbd "<left>") 'my-dired-scroll-up)
  1143. (define-key dired-mode-map (kbd "<right>") 'my-dired-scroll-down)
  1144. (define-key dired-mode-map (kbd "ESC p") 'my-dired-scroll-up)
  1145. (define-key dired-mode-map (kbd "ESC n") 'my-dired-scroll-down)
  1146. (add-hook 'dired-mode-hook
  1147. (lambda ()
  1148. (when (fboundp 'dired-hide-details-mode)
  1149. (dired-hide-details-mode t)
  1150. (local-set-key "l" 'dired-hide-details-mode))
  1151. (let ((file "._Icon\015"))
  1152. (when nil
  1153. '(file-readable-p file)
  1154. (delete-file file)))))
  1155. (when (autoload-eval-lazily 'pack '(dired-do-pack-or-unpack pack-pack))
  1156. (with-eval-after-load 'dired
  1157. (define-key dired-mode-map "P" 'pack-dired-dwim)))
  1158. (when (autoload-eval-lazily 'dired-list-all-mode)
  1159. (setq dired-listing-switches "-lhF")
  1160. (with-eval-after-load 'dired
  1161. (define-key dired-mode-map "a" 'dired-list-all-mode))))
  1162. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1163. ;; misc funcs
  1164. (defalias 'qcalc 'quick-calc)
  1165. (defun memo (&optional dir)
  1166. "Open memo.txt in DIR."
  1167. (interactive)
  1168. (pop-to-buffer (find-file-noselect (concat (if dir
  1169. (file-name-as-directory dir)
  1170. "")
  1171. "memo.txt"))))
  1172. (defvar my-rgrep-alist
  1173. `(
  1174. ;; git grep
  1175. ("gitgrep"
  1176. (eq 0
  1177. (shell-command "git rev-parse --git-dir"))
  1178. "git --no-pager -c color.grep=false grep -nH -e ")
  1179. ;; ripgrep
  1180. ("rg"
  1181. (executable-find "rg")
  1182. "rg --no-heading --color=never --smart-case ")
  1183. ;; sift
  1184. ("sift"
  1185. (executable-find "sift")
  1186. ("sift --no-color --binary-skip --filename --line-number --git --smart-case "))
  1187. ;; the silver searcher
  1188. ("ag"
  1189. (executable-find "ag")
  1190. "ag --nocolor --nogroup --nopager --filename ")
  1191. ;; ack
  1192. ("ack"
  1193. (executable-find "ack")
  1194. "ack --nocolor --nogroup --nopager --with-filename ")
  1195. ;; gnu global
  1196. ("global"
  1197. (and (require 'gtags nil t)
  1198. (executable-find "global")
  1199. (gtags-get-rootpath))
  1200. "global --result grep ")
  1201. ;; grep
  1202. ("grep"
  1203. t
  1204. ,(concat "find . "
  1205. "-path '*/.git' -prune -o "
  1206. "-path '*/.svn' -prune -o "
  1207. "-type f -print0 | "
  1208. "xargs -0 grep -nH -e "))
  1209. )
  1210. "Alist of rgrep command.
  1211. Each element is in the form like (NAME SEXP COMMAND), where SEXP returns the
  1212. condition to choose COMMAND when evaluated.")
  1213. (defvar my-rgrep-default nil
  1214. "Default command name for my-rgrep.")
  1215. (defun my-rgrep-grep-command (&optional name alist)
  1216. "Return recursive grep command for current directory or nil.
  1217. If NAME is given, use that without testing.
  1218. Commands are searched from ALIST."
  1219. (if alist
  1220. (if name
  1221. ;; if name is given search that from alist and return the command
  1222. (nth 2 (assoc name
  1223. alist))
  1224. ;; if name is not given try test in 1th elem
  1225. (let ((car (car alist))
  1226. (cdr (cdr alist)))
  1227. (if (eval (nth 1 car))
  1228. ;; if the condition is true return the command
  1229. (nth 2 car)
  1230. ;; try next one
  1231. (and cdr
  1232. (my-rgrep-grep-command name cdr)))))
  1233. ;; if alist is not given set default value
  1234. (my-rgrep-grep-command name my-rgrep-alist)))
  1235. (defun my-rgrep (command-args)
  1236. "My recursive grep. Run COMMAND-ARGS."
  1237. (interactive (let ((cmd (my-rgrep-grep-command my-rgrep-default
  1238. nil)))
  1239. (if cmd
  1240. (list (read-shell-command "grep command: "
  1241. cmd
  1242. 'grep-find-history))
  1243. (error "My-Rgrep: Command for rgrep not found")
  1244. )))
  1245. (compilation-start command-args
  1246. 'grep-mode))
  1247. ;; (defun my-rgrep-symbol-at-point (command-args)
  1248. ;; "My recursive grep. Run COMMAND-ARGS."
  1249. ;; (interactive (list (read-shell-command "grep command: "
  1250. ;; (concat (my-rgrep-grep-command)
  1251. ;; " "
  1252. ;; (thing-at-point 'symbol))
  1253. ;; 'grep-find-history)))
  1254. ;; (compilation-start command-args
  1255. ;; 'grep-mode))
  1256. (defmacro define-my-rgrep (name)
  1257. "Define rgrep for NAME."
  1258. `(defun ,(intern (concat "my-rgrep-"
  1259. name)) ()
  1260. ,(format "My recursive grep by %s."
  1261. name)
  1262. (interactive)
  1263. (let ((my-rgrep-default ,name))
  1264. (if (called-interactively-p 'any)
  1265. (call-interactively 'my-rgrep)
  1266. (error "Not intended to be called noninteractively. Use `my-rgrep'"))))
  1267. )
  1268. (define-my-rgrep "ack")
  1269. (define-my-rgrep "ag")
  1270. (define-my-rgrep "rg")
  1271. (define-my-rgrep "sift")
  1272. (define-my-rgrep "gitgrep")
  1273. (define-my-rgrep "grep")
  1274. (define-my-rgrep "global")
  1275. (define-key ctl-x-map "s" 'my-rgrep)
  1276. (defalias 'make 'compile)
  1277. (define-key ctl-x-map "c" 'compile)
  1278. ;;;;;;;;;;;;;;;;;;;;;;;
  1279. ;; adoc-simple-mode
  1280. (when (safe-require-or-eval 'adoc-mode)
  1281. (defvar adoc-simple-font-lock-keywords
  1282. nil)
  1283. (define-derived-mode adoc-simple-mode adoc-mode
  1284. "Adoc-Simple"
  1285. "Major mode for editing AsciiDoc text files.
  1286. This mode is a simplified version of `adoc-mode'."
  1287. '(set (make-local-variable 'font-lock-defaults)
  1288. '(adoc-simple-font-lock-keywords
  1289. nil nil nil nil
  1290. (font-lock-multiline . t)
  1291. (font-lock-mark-block-function . adoc-font-lock-mark-block-function))))
  1292. (add-to-list 'auto-mode-alist
  1293. '("\\.adoc\\'" . adoc-simple-mode)))
  1294. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1295. ;; awk-preview
  1296. (defgroup awk-preview nil
  1297. "Awk previewer."
  1298. :tag "Awk Preview"
  1299. :prefix "awk-preview-"
  1300. :group 'tools)
  1301. (defcustom awk-preview-program
  1302. (or (executable-find "gawk")
  1303. (executable-find "awk")
  1304. "awk")
  1305. "Awk program to execute."
  1306. :type 'string
  1307. :group 'awk-preview)
  1308. (defcustom awk-preview-switches
  1309. '("--sandbox" "--" "{print}")
  1310. "String of awk options appended when running awk preview."
  1311. :type '(repeat string)
  1312. :group 'awk-preview)
  1313. (defun awk-preview--invoke (start end output)
  1314. "Execute awk process and get result."
  1315. (let ((proc (apply 'start-process
  1316. "awk-preview"
  1317. output
  1318. awk-preview-program
  1319. awk-preview-switches)))
  1320. (message "%S" proc)
  1321. (process-send-region proc start end)
  1322. (process-send-eof proc)
  1323. (accept-process-output proc)
  1324. )
  1325. output)
  1326. (defun awk-preview (start end)
  1327. "Run awk and preview result."
  1328. (interactive "r")
  1329. (let ((output (with-current-buffer (get-buffer-create "*awk-preview output*")
  1330. (erase-buffer)
  1331. (current-buffer))))
  1332. (awk-preview--invoke start end output)
  1333. (message "awk-preview: %s" (with-current-buffer output
  1334. (buffer-substring-no-properties (point-min) (point-max))))
  1335. ))
  1336. (defun my-file-head (filename &optional n)
  1337. "Return list of first N lines of file FILENAME."
  1338. (let ((num (or n 10))
  1339. (size 100)
  1340. (beg 0)
  1341. (end 0)
  1342. (result '()))
  1343. (with-temp-buffer
  1344. (while (<= (count-lines (point-min)
  1345. (point-max))
  1346. num)
  1347. (setq end (+ beg size))
  1348. (insert-file-contents filename
  1349. nil
  1350. beg
  1351. end)
  1352. (setq beg end))
  1353. (goto-char (point-min))
  1354. (while (< (length result) num)
  1355. (let ((start (point)))
  1356. (forward-line 1)
  1357. (setq result
  1358. `(,@result ,(buffer-substring-no-properties start
  1359. (point))))))
  1360. result)))
  1361. ;; (apply 'concat (car (my-file-head "./emacs.el"))
  1362. ;; Local Variables:
  1363. ;; flycheck-disabled-checkers: (emacs-lisp-checkdoc)
  1364. ;; flycheck-checker: emacs-lisp
  1365. ;; End:
  1366. ;;; emancs.el ends here