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.
 
 
 
 
 
 

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