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

2205 lines
71 KiB

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