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

3090 lines
106 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. gosh-mode
  80. scala-mode
  81. ;;ensime
  82. ;; ack
  83. color-moccur
  84. ggtags
  85. flycheck
  86. auto-highlight-symbol
  87. hl-todo
  88. ;; Currently not available
  89. ;; pp-c-l
  90. xclip
  91. foreign-regexp
  92. multi-term
  93. term-run
  94. editorconfig
  95. git-ps1-mode
  96. restart-emacs
  97. fill-column-indicator
  98. pkgbuild-mode
  99. minibuffer-line
  100. which-key
  101. ;; I think this works in place of my autosave lib
  102. super-save
  103. pipenv
  104. imenu-list
  105. page-break-lines
  106. ;; sync-recentf
  107. aggressive-indent
  108. ;; fancy-narrow
  109. dired-filter
  110. wgrep
  111. magit
  112. git-gutter
  113. end-mark
  114. sl
  115. editorconfig
  116. editorconfig-custom-majormode
  117. git-command
  118. prompt-text
  119. ;; 10sr repository
  120. ;; 10sr-extras
  121. terminal-title
  122. recentf-show
  123. dired-list-all-mode
  124. pack
  125. set-modeline-color
  126. read-only-only-mode
  127. smart-revert
  128. autosave
  129. ;;window-organizer
  130. ilookup
  131. pasteboard
  132. ))
  133. (when (safe-require-or-eval 'package)
  134. (setq package-archives
  135. `(,@package-archives
  136. ("melpa" . "https://melpa.org/packages/")
  137. ;; Somehow fails to download via https
  138. ("10sr-el" . "http://10sr.github.io/emacs-lisp/elpa/")))
  139. (package-initialize)
  140. (defun my-auto-install-package ()
  141. "Install packages semi-automatically."
  142. (interactive)
  143. (package-refresh-contents)
  144. (mapc (lambda (pkg)
  145. (or (package-installed-p pkg)
  146. (package-install pkg)))
  147. 10sr-package-list))
  148. )
  149. ;; (lazy-load-eval 'sudoku)
  150. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  151. ;; my-idle-hook
  152. (defvar my-idle-hook nil
  153. "Hook run when idle for several secs.")
  154. (defvar my-idle-hook-sec 5
  155. "Second to run `my-idle-hook'.")
  156. (run-with-idle-timer my-idle-hook-sec
  157. t
  158. (lambda ()
  159. (run-hooks 'my-idle-hook)))
  160. ;; (add-hook 'my-idle-hook
  161. ;; (lambda ()
  162. ;; (message "idle hook message")))
  163. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  164. ;; start and quit
  165. (setq inhibit-startup-message t)
  166. (setq confirm-kill-emacs 'y-or-n-p)
  167. (setq gc-cons-threshold (* 1024 1024 16))
  168. (setq garbage-collection-messages nil)
  169. (when window-system
  170. (add-to-list 'default-frame-alist '(cursor-type . box))
  171. (add-to-list 'default-frame-alist '(background-color . "white"))
  172. (add-to-list 'default-frame-alist '(foreground-color . "gray10"))
  173. ;; (add-to-list 'default-frame-alist '(alpha . (80 100 100 100)))
  174. ;; does not work?
  175. )
  176. ;; (add-to-list 'default-frame-alist '(cursor-type . box))
  177. (if window-system (menu-bar-mode 1) (menu-bar-mode 0))
  178. (and (fboundp 'tool-bar-mode)
  179. (tool-bar-mode 0))
  180. (and (fboundp 'set-scroll-bar-mode)
  181. (set-scroll-bar-mode nil))
  182. (call-after-init
  183. (lambda ()
  184. (message "%s %s" invocation-name emacs-version)
  185. (message "Invocation directory: %s" default-directory)
  186. (message "%s was taken to initialize emacs." (emacs-init-time))
  187. (view-echo-area-messages)
  188. ;; (view-emacs-news)
  189. ))
  190. (cd ".") ; when using windows use / instead of \ in `default-directory'
  191. ;; locale
  192. (set-language-environment "Japanese")
  193. (set-default-coding-systems 'utf-8-unix)
  194. (prefer-coding-system 'utf-8-unix)
  195. (setq system-time-locale "C")
  196. ;; my prefix map
  197. (defvar my-prefix-map nil
  198. "My prefix map.")
  199. (define-prefix-command 'my-prefix-map)
  200. ;; (define-key ctl-x-map (kbd "C-x") 'my-prefix-map)
  201. ;; (define-key my-prefix-map (kbd "C-q") 'quoted-insert)
  202. ;; (define-key my-prefix-map (kbd "C-z") 'suspend-frame)
  203. ;; (comint-show-maximum-output)
  204. ;; kill scratch
  205. (call-after-init (lambda ()
  206. (let ((buf (get-buffer "*scratch*")))
  207. (when buf
  208. (kill-buffer buf)))))
  209. ;; modifier keys
  210. ;; (setq mac-option-modifier 'control)
  211. ;; display
  212. (setq visible-bell t)
  213. (setq ring-bell-function 'ignore)
  214. (mouse-avoidance-mode 'banish)
  215. (setq echo-keystrokes 0.1)
  216. (defun reload-init-file ()
  217. "Reload Emacs init file."
  218. (interactive)
  219. (when (and user-init-file
  220. (file-readable-p user-init-file))
  221. (load-file user-init-file)))
  222. (safe-require-or-eval 'session)
  223. ;; server
  224. (set-variable 'server-name (concat "server"
  225. (number-to-string (emacs-pid))))
  226. ;; In Cygwin Environment `server-runnning-p' stops when server-use-tcp is nil
  227. ;; In Darwin environment, init fails with message like 'Service name too long'
  228. ;; when server-use-tcp is nil
  229. (when (or (eq system-type
  230. 'cygwin)
  231. (eq system-type
  232. 'darwin))
  233. (set-variable 'server-use-tcp t))
  234. ;; MSYS2 fix
  235. (when (eq system-type
  236. 'windows-nt)
  237. (setq shell-file-name
  238. (executable-find "bash"))
  239. '(setq function-key-map
  240. `(,@function-key-map ([pause] . [?\C-c])
  241. ))
  242. (define-key key-translation-map
  243. (kbd "<pause>")
  244. (kbd "C-c"))
  245. '(keyboard-translate [pause]
  246. (kbd "C-c")p)
  247. ;; TODO: move to other place later
  248. (when (not window-system)
  249. (setq interprogram-paste-function nil)
  250. (setq interprogram-cut-function nil)))
  251. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  252. ;; global keys
  253. (global-set-key (kbd "<up>") 'scroll-down-line)
  254. (global-set-key (kbd "<down>") 'scroll-up-line)
  255. (global-set-key (kbd "<left>") 'scroll-down)
  256. (global-set-key (kbd "<right>") 'scroll-up)
  257. ;; (define-key my-prefix-map (kbd "C-h") help-map)
  258. (global-set-key (kbd "C-\\") help-map)
  259. (define-key ctl-x-map (kbd "DEL") help-map)
  260. (define-key ctl-x-map (kbd "C-h") help-map)
  261. (define-key help-map "a" 'apropos)
  262. ;; disable annoying keys
  263. (global-set-key [prior] 'ignore)
  264. (global-set-key (kbd "<next>") 'ignore)
  265. (global-set-key [menu] 'ignore)
  266. (global-set-key [down-mouse-1] 'ignore)
  267. (global-set-key [down-mouse-2] 'ignore)
  268. (global-set-key [down-mouse-3] 'ignore)
  269. (global-set-key [mouse-1] 'ignore)
  270. (global-set-key [mouse-2] 'ignore)
  271. (global-set-key [mouse-3] 'ignore)
  272. (global-set-key (kbd "<eisu-toggle>") 'ignore)
  273. (global-set-key (kbd "C-<eisu-toggle>") 'ignore)
  274. (when (safe-require-or-eval 'which-key)
  275. (which-key-mode))
  276. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  277. ;; editor
  278. (setq kill-whole-line t)
  279. (setq scroll-conservatively 35
  280. scroll-margin 2)
  281. (setq-default major-mode 'text-mode)
  282. (setq next-line-add-newlines nil)
  283. (setq kill-read-only-ok t)
  284. (setq truncate-partial-width-windows nil) ; when splitted horizontally
  285. ;; (setq-default line-spacing 0.2)
  286. (setq-default indicate-empty-lines t) ; when using x indicate empty line
  287. ;; (setq-default tab-width 4)
  288. (setq-default indent-tabs-mode nil)
  289. (setq-default indent-line-function 'indent-to-left-margin)
  290. ;; (setq-default indent-line-function nil)
  291. (setq-default truncate-lines nil)
  292. ;; (pc-selection-mode 1) ; make some already defined keybind back to default
  293. (delete-selection-mode 1)
  294. (cua-mode 0)
  295. (setq line-move-visual nil)
  296. (setq create-lockfiles nil)
  297. (add-hook 'before-save-hook
  298. 'time-stamp)
  299. ;; Add Time-stamp: <> to insert timestamp there
  300. (set-variable 'time-stamp-format
  301. "%:y-%02m-%02d %02H:%02M:%02S %Z 10sr")
  302. ;; key bindings
  303. ;; moving around
  304. ;;(keyboard-translate ?\M-j ?\C-j)
  305. ;; (global-set-key (kbd "M-p") 'backward-paragraph)
  306. (define-key esc-map "p" 'backward-paragraph)
  307. ;; (global-set-key (kbd "M-n") 'forward-paragraph)
  308. (define-key esc-map "n" 'forward-paragraph)
  309. (global-set-key (kbd "C-<up>") 'scroll-down-line)
  310. (global-set-key (kbd "C-<down>") 'scroll-up-line)
  311. (global-set-key (kbd "C-<left>") 'scroll-down)
  312. (global-set-key (kbd "C-<right>") 'scroll-up)
  313. (global-set-key (kbd "<select>") 'ignore) ; 'previous-line-mark)
  314. (define-key ctl-x-map (kbd "ESC x") 'execute-extended-command)
  315. (define-key ctl-x-map (kbd "ESC :") 'eval-expression)
  316. ;; C-h and DEL
  317. (global-set-key (kbd "C-h") (kbd "DEL"))
  318. ;;(global-set-key (kbd "C-m") 'reindent-then-newline-and-indent)
  319. (global-set-key (kbd "C-m") 'newline-and-indent)
  320. ;; (global-set-key (kbd "C-o") (kbd "C-e C-m"))
  321. ;; (global-set-key "\C-z" 'undo) ; undo is M-u
  322. (define-key esc-map "u" 'undo)
  323. (define-key esc-map "i" (kbd "ESC TAB"))
  324. ;; (global-set-key (kbd "C-r") 'query-replace-regexp)
  325. (global-set-key (kbd "C-s") 'isearch-forward-regexp)
  326. (global-set-key (kbd "C-r") 'isearch-backward-regexp)
  327. (require 'page-ext nil t)
  328. (when (safe-require-or-eval 'page-break-lines)
  329. (global-page-break-lines-mode 1))
  330. (when (safe-require-or-eval 'git-gutter)
  331. (custom-set-variables
  332. '(git-gutter:update-interval 2))
  333. (custom-set-variables
  334. '(git-gutter:unchanged-sign " "))
  335. (when (= (display-color-cells)
  336. 256)
  337. (let ((c "color-233"))
  338. (set-face-background 'git-gutter:modified c)
  339. (set-face-background 'git-gutter:added c)
  340. (set-face-background 'git-gutter:deleted c)
  341. (set-face-background 'git-gutter:unchanged c)))
  342. (global-git-gutter-mode 1)
  343. )
  344. ;; (when (safe-require-or-eval 'fancy-narrow)
  345. ;; (fancy-narrow-mode 1))
  346. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  347. ;; title and mode-line
  348. (when (safe-require-or-eval 'terminal-title)
  349. ;; if TERM is not screen use default value
  350. (if (getenv "TMUX")
  351. ;; if use tmux locally just basename of current dir
  352. (set-variable 'terminal-title-format
  353. '((file-name-nondirectory (directory-file-name
  354. default-directory))))
  355. (if (and (let ((tty-type (frame-parameter nil
  356. 'tty-type)))
  357. (and tty-type
  358. (equal (car (split-string tty-type
  359. "-"))
  360. "screen")))
  361. (not (getenv "SSH_CONNECTION")))
  362. (set-variable 'terminal-title-format
  363. '((file-name-nondirectory (directory-file-name
  364. default-directory))))
  365. ;; seems that TMUX is used locally and ssh to remote host
  366. (set-variable 'terminal-title-format
  367. `("em:"
  368. ,user-login-name
  369. "@"
  370. ,(car (split-string (system-name)
  371. "\\."))
  372. ":"
  373. default-directory))
  374. )
  375. )
  376. (terminal-title-mode))
  377. (setq eol-mnemonic-dos "\\r\\n")
  378. (setq eol-mnemonic-mac "\\r")
  379. (setq eol-mnemonic-unix "")
  380. (which-function-mode 1)
  381. (line-number-mode 0)
  382. (column-number-mode 0)
  383. (size-indication-mode 0)
  384. (setq mode-line-position
  385. '(:eval (format "L%%l/%d%s:C%%c"
  386. (count-lines (point-max)
  387. (point-min))
  388. (if (buffer-narrowed-p)
  389. "[N]"
  390. "")
  391. )))
  392. ;; http://www.geocities.jp/simizu_daisuke/bunkei-meadow.html#frame-title
  393. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  394. ;; minibuffer
  395. (setq insert-default-directory t)
  396. (setq completion-ignore-case t
  397. read-file-name-completion-ignore-case t
  398. read-buffer-completion-ignore-case t)
  399. (setq resize-mini-windows t)
  400. (temp-buffer-resize-mode 1)
  401. (savehist-mode 1)
  402. (defvar display-time-format "%Y/%m/%d %a %H:%M")
  403. (set-variable 'help-at-pt-display-when-idle t)
  404. (fset 'yes-or-no-p 'y-or-n-p)
  405. ;; complete symbol when `eval'
  406. (define-key read-expression-map (kbd "TAB") 'completion-at-point)
  407. (define-key minibuffer-local-map (kbd "C-u")
  408. (lambda () (interactive) (delete-region (point-at-bol) (point))))
  409. ;; I dont know these bindings are good
  410. (define-key minibuffer-local-map (kbd "C-p") (kbd "ESC p"))
  411. (define-key minibuffer-local-map (kbd "C-n") (kbd "ESC n"))
  412. (when (safe-require-or-eval 'minibuffer-line)
  413. (set-face-underline 'minibuffer-line nil)
  414. (set-variable 'minibuffer-line-refresh-interval
  415. 25)
  416. ;; Set idle timer
  417. (defvar my-minibuffer-line--idle-timer nil)
  418. (defvar minibuffer-line-mode)
  419. (add-hook 'minibuffer-line-mode-hook
  420. (lambda ()
  421. (when my-minibuffer-line--idle-timer
  422. (cancel-timer my-minibuffer-line--idle-timer)
  423. (setq my-minibuffer-line--idle-timer nil))
  424. (when minibuffer-line-mode
  425. (setq my-minibuffer-line--idle-timer
  426. (run-with-idle-timer 0.5
  427. t
  428. 'minibuffer-line--update)))))
  429. (set-variable 'minibuffer-line-format
  430. `(,(concat user-login-name
  431. "@"
  432. (car (split-string (system-name)
  433. "\\."))
  434. ":")
  435. (:eval (abbreviate-file-name (or buffer-file-name
  436. default-directory)))
  437. (:eval (and (fboundp 'git-ps1-mode-get-current)
  438. (git-ps1-mode-get-current " [GIT:%s]")))
  439. " "
  440. (:eval (format-time-string display-time-format))))
  441. (minibuffer-line-mode 1)
  442. )
  443. (when (safe-require-or-eval 'prompt-text)
  444. (set-variable 'prompt-text-format
  445. `(,(concat ""
  446. user-login-name
  447. "@"
  448. (car (split-string (system-name)
  449. "\\."))
  450. ":")
  451. (:eval (abbreviate-file-name (or buffer-file-name
  452. default-directory)))
  453. (:eval (and (fboundp 'git-ps1-mode-get-current)
  454. (git-ps1-mode-get-current " [GIT:%s]")))
  455. " "
  456. (:eval (format-time-string display-time-format))
  457. "\n"
  458. (:eval (symbol-name this-command))
  459. ": "))
  460. (prompt-text-mode 1))
  461. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  462. ;; letters, font-lock mode and fonts
  463. (setq text-quoting-style 'grave)
  464. ;; (set-face-background 'vertical-border (face-foreground 'mode-line))
  465. ;; (set-window-margins (selected-window) 1 1)
  466. (unless window-system
  467. (setq frame-background-mode 'dark))
  468. (and (or (eq system-type 'Darwin)
  469. (eq system-type 'darwin))
  470. (fboundp 'mac-set-input-method-parameter)
  471. (mac-set-input-method-parameter 'japanese 'cursor-color "red")
  472. (mac-set-input-method-parameter 'roman 'cursor-color "black"))
  473. (when (and (boundp 'input-method-activate-hook) ; i dont know this is correct
  474. (boundp 'input-method-inactivate-hook))
  475. (add-hook 'input-method-activate-hook
  476. (lambda () (set-cursor-color "red")))
  477. (add-hook 'input-method-inactivate-hook
  478. (lambda () (set-cursor-color "black"))))
  479. (when (safe-require-or-eval 'paren)
  480. (show-paren-mode 1)
  481. (setq show-paren-delay 0.5
  482. show-paren-style 'parenthesis) ; mixed is hard to read
  483. ;; (set-face-background 'show-paren-match
  484. ;; "black")
  485. ;; ;; (face-foreground 'default))
  486. ;; (set-face-foreground 'show-paren-match
  487. ;; "white")
  488. ;; (set-face-inverse-video-p 'show-paren-match
  489. ;; t)
  490. )
  491. (transient-mark-mode 1)
  492. (global-font-lock-mode 1)
  493. (setq font-lock-global-modes
  494. '(not
  495. help-mode
  496. eshell-mode
  497. ;;term-mode
  498. Man-mode
  499. magit-diff-mode
  500. magit-revision-mode))
  501. ;; (standard-display-ascii ?\n "$\n")
  502. ;; (defvar my-eol-face
  503. ;; '(("\n" . (0 font-lock-comment-face t nil)))
  504. ;; )
  505. ;; (defvar my-tab-face
  506. ;; '(("\t" . '(0 highlight t nil))))
  507. (defvar my-jspace-face
  508. '(("\u3000" . '(0 highlight t nil))))
  509. (add-hook 'font-lock-mode-hook
  510. (lambda ()
  511. ;; (font-lock-add-keywords nil my-eol-face)
  512. (font-lock-add-keywords nil my-jspace-face)
  513. ))
  514. (when (safe-require-or-eval 'whitespace)
  515. (add-to-list 'whitespace-display-mappings
  516. ;; We need t since last one takes precedence
  517. `(tab-mark ?\t ,(vconcat "^I\t")) t)
  518. ;; (add-to-list 'whitespace-display-mappings
  519. ;; `(newline-mark ?\n ,(vconcat "$\n")))
  520. (setq whitespace-style '(face
  521. trailing ; trailing blanks
  522. ;; tabs
  523. ;; spaces
  524. ;; lines
  525. lines-tail ; lines over 80
  526. newline ; newlines
  527. empty ; empty lines at beg or end of buffer
  528. ;; big-indent
  529. ;; space-mark
  530. tab-mark
  531. newline-mark ; use display table for newline
  532. ))
  533. ;; (setq whitespace-newline 'font-lock-comment-face)
  534. ;; (setq whitespace-style (delq 'newline-mark whitespace-style))
  535. (defun my-whitesspace-mode-reload ()
  536. "Reload whitespace-mode config."
  537. (interactive)
  538. (when whitespace-mode
  539. (whitespace-mode 0)
  540. (whitespace-mode 1)))
  541. (set-variable 'whitespace-line-column nil)
  542. (global-whitespace-mode t)
  543. (add-hook 'dired-mode-hook
  544. (lambda ()
  545. (set (make-local-variable 'whitespace-style) nil)))
  546. (if (= (display-color-cells)
  547. 256)
  548. (set-face-foreground 'whitespace-newline "color-109")
  549. ;; (progn
  550. ;; (set-face-bold-p 'whitespace-newline
  551. ;; t))
  552. ))
  553. (and nil
  554. '(safe-require-or-eval 'fill-column-indicator)
  555. (setq fill-column-indicator))
  556. ;; highlight current line
  557. ;; http://wiki.riywo.com/index.php?Meadow
  558. (face-spec-set 'hl-line
  559. '((((min-colors 256)
  560. (background dark))
  561. (:background "color-234"))
  562. (((min-colors 256)
  563. (background light))
  564. (:background "color-234"))
  565. (t
  566. (:underline "black"))))
  567. (set-variable 'hl-line-global-modes
  568. '(not
  569. term-mode))
  570. (global-hl-line-mode 1) ;; (hl-line-mode 1)
  571. (set-face-foreground 'font-lock-regexp-grouping-backslash "#666")
  572. (set-face-foreground 'font-lock-regexp-grouping-construct "#f60")
  573. ;;(safe-require-or-eval 'set-modeline-color)
  574. ;; (let ((fg (face-foreground 'default))
  575. ;; (bg (face-background 'default)))
  576. ;; (set-face-background 'mode-line-inactive
  577. ;; (if (face-inverse-video-p 'mode-line) fg bg))
  578. ;; (set-face-foreground 'mode-line-inactive
  579. ;; (if (face-inverse-video-p 'mode-line) bg fg)))
  580. ;; (set-face-underline 'mode-line-inactive
  581. ;; t)
  582. ;; (set-face-underline 'vertical-border
  583. ;; nil)
  584. ;; (when (safe-require-or-eval 'end-mark)
  585. ;; (global-end-mark-mode))
  586. ;; M-x highlight-* to highlight things
  587. (global-hi-lock-mode 1)
  588. (unless (fboundp 'highlight-region-text)
  589. (defun highlight-region-text (beg end)
  590. "Highlight text between BEG and END."
  591. (interactive "r")
  592. (highlight-regexp (regexp-quote (buffer-substring-no-properties beg
  593. end)))
  594. (setq deactivate-mark t)))
  595. (when (safe-require-or-eval 'auto-highlight-symbol)
  596. (set-variable 'ahs-idle-interval 0.6)
  597. (global-auto-highlight-symbol-mode 1))
  598. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  599. ;; file handling
  600. (when (safe-require-or-eval 'editorconfig)
  601. ;; (set-variable 'editorconfig-get-properties-function
  602. ;; 'editorconfig-core-get-properties-hash)
  603. (editorconfig-mode 1)
  604. (set-variable 'editorconfig-mode-lighter " EC")
  605. (with-eval-after-load 'org-src
  606. ;; [*.org\[\*Org Src*\[ c \]*\]]
  607. (add-hook 'org-src-mode-hook
  608. 'editorconfig-mode-apply t)))
  609. (when (fboundp 'editorconfig-custom-majormode)
  610. (add-hook 'editorconfig-custom-hooks
  611. 'editorconfig-custom-majormode))
  612. ;; Add readonly=true to set read-only-mode
  613. (add-hook 'editorconfig-custom-hooks
  614. (lambda (props)
  615. (let ((r (gethash 'readonly props)))
  616. (when (and (string= r "true")
  617. (not buffer-read-only))
  618. (read-only-mode 1)))))
  619. ;; (when (fboundp 'editorconfig-charset-extras)
  620. ;; (add-hook 'editorconfig-custom-hooks
  621. ;; 'editorconfig-charset-extras))
  622. (setq revert-without-query '(".+"))
  623. ;; save cursor position
  624. (when (safe-require-or-eval 'saveplace)
  625. (setq-default save-place t)
  626. (setq save-place-file (concat user-emacs-directory
  627. "places")))
  628. ;; http://www.bookshelf.jp/soft/meadow_24.html#SEC260
  629. (setq make-backup-files t)
  630. (setq vc-make-backup-files t)
  631. ;; (make-directory (expand-file-name "~/.emacsbackup"))
  632. (setq backup-directory-alist
  633. (cons (cons "." (expand-file-name (concat user-emacs-directory
  634. "backup")))
  635. backup-directory-alist))
  636. (setq version-control 't)
  637. (setq delete-old-versions t)
  638. (setq kept-new-versions 20)
  639. (setq auto-save-list-file-prefix (expand-file-name (concat user-emacs-directory
  640. "auto-save/")))
  641. ;; (setq delete-auto-save-files t)
  642. (setq auto-save-visited-interval 8)
  643. (auto-save-visited-mode 1)
  644. (add-to-list 'completion-ignored-extensions ".bak")
  645. (set-variable 'completion-cycle-threshold nil) ;; NEVER use
  646. (setq delete-by-moving-to-trash t)
  647. ;; trash-directory "~/.emacs.d/trash")
  648. (add-hook 'after-save-hook
  649. 'executable-make-buffer-file-executable-if-script-p)
  650. (set-variable 'bookmark-default-file
  651. (expand-file-name (concat user-emacs-directory
  652. "bmk")))
  653. (set-variable 'bookmark-save-flag
  654. 1)
  655. (with-eval-after-load 'recentf
  656. (defvar recentf-exclude)
  657. (defvar bookmark-default-file)
  658. (add-to-list 'recentf-exclude
  659. (regexp-quote bookmark-default-file)))
  660. (when (safe-require-or-eval 'smart-revert)
  661. (smart-revert-on))
  662. ;; autosave
  663. ;; auto-save-visited-mode can be used instead?
  664. ;; (when (safe-require-or-eval 'autosave)
  665. ;; (autosave-set 8))
  666. ;; bookmarks
  667. (define-key ctl-x-map "m" 'list-bookmarks)
  668. ;; vc
  669. (set-variable 'vc-handled-backends '(RCS))
  670. (set-variable 'vc-rcs-register-switches "-l")
  671. (set-variable 'vc-rcs-checkin-switches "-l")
  672. (set-variable 'vc-command-messages t)
  673. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  674. ;; share clipboard with x
  675. ;; this page describes this in details, but only these sexps seem to be needed
  676. ;; http://garin.jp/doc/Linux/xwindow_clipboard
  677. (and nil
  678. (not window-system)
  679. (not (eq window-system 'mac))
  680. (getenv "DISPLAY")
  681. (not (equal (getenv "DISPLAY") ""))
  682. (executable-find "xclip")
  683. ;; (< emacs-major-version 24)
  684. '(safe-require-or-eval 'xclip)
  685. nil
  686. (turn-on-xclip))
  687. (and (eq system-type 'darwin)
  688. (safe-require-or-eval 'pasteboard)
  689. (turn-on-pasteboard))
  690. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  691. ;; some modes and hooks
  692. ;; Include some extra modes
  693. (require 'generic-x)
  694. (when (autoload-eval-lazily 'wgrep)
  695. (set-variable 'wgrep-auto-save-buffer t)
  696. (with-eval-after-load 'grep
  697. (defvar grep-mode-map)
  698. (define-key grep-mode-map
  699. "e"
  700. 'wgrep-change-to-wgrep-mode)))
  701. (with-eval-after-load 'remember
  702. (defvar remember-mode-map (make-sparse-keymap))
  703. (define-key remember-mode-map (kbd "C-x C-s") 'ignore))
  704. (with-eval-after-load 'magit-files
  705. ;; `global-magit-file-mode' is enabled by default and this mode overwrites
  706. ;; existing keybindings.
  707. ;; Apparently it is a HARMFUL behavior and it is really awful that I have
  708. ;; to disable thie mode here, but do anyway.
  709. ;; See also https://github.com/magit/magit/issues/3517
  710. (global-magit-file-mode -1))
  711. (with-eval-after-load 'magit-section
  712. (set-face-background 'magit-section-highlight
  713. nil))
  714. (with-eval-after-load 'magit-diff
  715. (set-face-background 'magit-diff-added-highlight
  716. nil)
  717. (set-face-background 'magit-diff-removed-highlight
  718. nil)
  719. (set-face-background 'magit-diff-context-highlight
  720. nil)
  721. )
  722. (when (boundp 'git-rebase-filename-regexp)
  723. (add-to-list 'auto-mode-alist
  724. `(,git-rebase-filename-regexp . text-mode)))
  725. (when (safe-require-or-eval 'aggressive-indent)
  726. (defvar aggressive-indent-excluded-modes)
  727. (setq aggressive-indent-excluded-modes
  728. `(diff-mode
  729. toml-mode
  730. conf-mode
  731. dockerfile-mode
  732. groovy-mode
  733. ,@aggressive-indent-excluded-modes))
  734. (global-aggressive-indent-mode 1))
  735. (when (autoload-eval-lazily 'ggtags)
  736. (add-hook 'c-mode-common-hook
  737. (lambda ()
  738. (when (derived-mode-p 'c-mode 'c++-mode 'java-mode)
  739. (ggtags-mode 1))))
  740. (add-hook 'python-mode-hook
  741. (lambda ()
  742. (ggtags-mode 1))))
  743. (when (autoload-eval-lazily 'imenu-list)
  744. ;; (set-variable 'imenu-list-auto-resize t)
  745. (set-variable 'imenu-list-focus-after-activation t)
  746. (define-key ctl-x-map "l" 'imenu-list-smart-toggle))
  747. (add-hook 'emacs-lisp-mode-hook
  748. (lambda ()
  749. (setq imenu-generic-expression
  750. `(("Sections" ";;;\+\n;; \\(.*\\)\n" 1)
  751. ,@imenu-generic-expression))))
  752. (with-eval-after-load 'compile
  753. (defvar compilation-filter-start)
  754. (defvar compilation-error-regexp-alist)
  755. (require 'ansi-color)
  756. (add-hook 'compilation-filter-hook
  757. (lambda ()
  758. (let ((inhibit-read-only t))
  759. (ansi-color-apply-on-region compilation-filter-start
  760. (point)))))
  761. (add-to-list 'compilation-error-regexp-alist
  762. ;; ansible-lint
  763. '("^\\([^ \n]+\\):\\([0-9]+\\)$" 1 2)))
  764. ;; Workaround to avoid ensime error
  765. (defvar ensime-mode-key-prefix nil)
  766. ;; http://qiita.com/sune2/items/b73037f9e85962f5afb7
  767. (when (safe-require-or-eval 'company)
  768. (global-company-mode)
  769. (set-variable 'company-idle-delay 0.5)
  770. (set-variable 'company-minimum-prefix-length 2)
  771. (set-variable 'company-selection-wrap-around t))
  772. ;; https://github.com/lunaryorn/flycheck
  773. (when (safe-require-or-eval 'flycheck)
  774. (call-after-init 'global-flycheck-mode))
  775. (when (autoload-eval-lazily 'ilookup)
  776. (define-key ctl-x-map "d" 'ilookup-open-word))
  777. (set-variable 'ac-ignore-case nil)
  778. (when (autoload-eval-lazily 'term-run '(term-run-shell-command term-run))
  779. (define-key ctl-x-map "t" 'term-run-shell-command))
  780. (add-to-list 'safe-local-variable-values
  781. '(encoding utf-8))
  782. (setq enable-local-variables :safe)
  783. ;; Detect file type from shebang and set major-mode.
  784. (add-to-list 'interpreter-mode-alist
  785. '("python3" . python-mode))
  786. (add-to-list 'interpreter-mode-alist
  787. '("python2" . python-mode))
  788. (with-eval-after-load 'python
  789. (defvar python-mode-map (make-sparse-keymap))
  790. (define-key python-mode-map (kbd "C-m") 'newline-and-indent))
  791. (when (autoload-eval-lazily 'pipenv)
  792. (add-hook 'python-mode-hook
  793. (lambda ()
  794. (pipenv-mode 1)
  795. (pipenv-projectile-after-switch-default)))
  796. )
  797. (set-variable 'flycheck-python-pycompile-executable "python3")
  798. (set-variable 'python-indent-guess-indent-offset nil)
  799. ;; http://fukuyama.co/foreign-regexp
  800. '(and (safe-require-or-eval 'foreign-regexp)
  801. (progn
  802. (setq foreign-regexp/regexp-type 'perl)
  803. '(setq reb-re-syntax 'foreign-regexp)
  804. ))
  805. (autoload-eval-lazily 'sql '(sql-mode)
  806. (require 'sql-indent nil t))
  807. (when (autoload-eval-lazily 'git-command)
  808. (define-key ctl-x-map "g" 'git-command))
  809. (when (safe-require-or-eval 'git-commit)
  810. (global-git-commit-mode 1))
  811. (with-eval-after-load 'git-commit
  812. (add-hook 'git-commit-setup-hook
  813. 'turn-off-auto-fill t))
  814. (autoload-eval-lazily 'sl)
  815. (with-eval-after-load 'rst
  816. (defvar rst-mode-map)
  817. (define-key rst-mode-map (kbd "C-m") 'newline-and-indent))
  818. (with-eval-after-load 'jdee
  819. (add-hook 'jdee-mode-hook
  820. (lambda ()
  821. (make-local-variable 'global-mode-string)
  822. (add-to-list 'global-mode-string
  823. mode-line-position))))
  824. ;; Cannot enable error thrown. Why???
  825. ;; https://github.com/m0smith/malabar-mode#Installation
  826. ;; (when (autoload-eval-lazily 'malabar-mode)
  827. ;; (add-to-list 'load-path
  828. ;; (expand-file-name (concat user-emacs-directory "/cedet")))
  829. ;; (safe-require-or-eval 'cedet-devel-load)
  830. ;; (call-after-init 'activate-malabar-mode))
  831. (with-eval-after-load 'make-mode
  832. (defvar makefile-mode-map (make-sparse-keymap))
  833. (define-key makefile-mode-map (kbd "C-m") 'newline-and-indent)
  834. ;; this functions is set in write-file-functions, i cannot find any
  835. ;; good way to remove this.
  836. (fset 'makefile-warn-suspicious-lines 'ignore))
  837. (with-eval-after-load 'verilog-mode
  838. (defvar verilog-mode-map (make-sparse-keymap))
  839. (define-key verilog-mode-map ";" 'self-insert-command))
  840. (setq diff-switches "-u")
  841. (with-eval-after-load 'diff-mode
  842. ;; (when (and (eq major-mode
  843. ;; 'diff-mode)
  844. ;; (not buffer-file-name))
  845. ;; ;; do not pass when major-mode is derived mode of diff-mode
  846. ;; (view-mode 1))
  847. (set-face-attribute 'diff-header nil
  848. :foreground nil
  849. :background nil
  850. :weight 'bold)
  851. (set-face-attribute 'diff-file-header nil
  852. :foreground nil
  853. :background nil
  854. :weight 'bold)
  855. (set-face-foreground 'diff-index "blue")
  856. (set-face-attribute 'diff-hunk-header nil
  857. :foreground "cyan"
  858. :weight 'normal)
  859. (set-face-attribute 'diff-context nil
  860. ;; :foreground "white"
  861. :foreground nil
  862. :weight 'normal)
  863. (set-face-foreground 'diff-removed "red")
  864. (set-face-foreground 'diff-added "green")
  865. (set-face-background 'diff-removed nil)
  866. (set-face-background 'diff-added nil)
  867. (set-face-attribute 'diff-changed nil
  868. :foreground "magenta"
  869. :weight 'normal)
  870. (set-face-attribute 'diff-refine-changed nil
  871. :foreground nil
  872. :background nil
  873. :weight 'bold
  874. :inverse-video t)
  875. ;; Annoying !
  876. ;;(diff-auto-refine-mode)
  877. )
  878. ;; (ffap-bindings)
  879. (set-variable 'browse-url-browser-function
  880. 'eww-browse-url)
  881. (set-variable 'sh-here-document-word "__EOC__")
  882. (when (autoload-eval-lazily 'adoc-mode
  883. nil
  884. (defvar adoc-mode-map (make-sparse-keymap))
  885. (define-key adoc-mode-map (kbd "C-m") 'newline))
  886. (setq auto-mode-alist
  887. `(("\\.adoc\\'" . adoc-mode)
  888. ("\\.asciidoc\\'" . adoc-mode)
  889. ,@auto-mode-alist)))
  890. (with-eval-after-load 'markup-faces
  891. ;; Is this too match ?
  892. (set-face-foreground 'markup-meta-face
  893. "color-245")
  894. (set-face-foreground 'markup-meta-hide-face
  895. "color-245")
  896. )
  897. ;; TODO: check if this is required
  898. (when (autoload-eval-lazily 'groovy-mode nil
  899. (defvar groovy-mode-map (make-sparse-keymap))
  900. (define-key groovy-mode-map "(" 'self-insert-command)
  901. (define-key groovy-mode-map ")" 'self-insert-command)
  902. (define-key groovy-mode-map (kbd "C-m") 'newline-and-indent)
  903. )
  904. (add-to-list 'auto-mode-alist
  905. '("build\\.gradle\\'" . groovy-mode)))
  906. (with-eval-after-load 'yaml-mode
  907. (defvar yaml-mode-map (make-sparse-keymap))
  908. (define-key yaml-mode-map (kbd "C-m") 'newline))
  909. (with-eval-after-load 'html-mode
  910. (defvar html-mode-map (make-sparse-keymap))
  911. (define-key html-mode-map (kbd "C-m") 'reindent-then-newline-and-indent))
  912. (with-eval-after-load 'text-mode
  913. (define-key text-mode-map (kbd "C-m") 'newline))
  914. (autoload-eval-lazily 'info nil
  915. (defvar Info-additional-directory-list)
  916. (dolist (dir (directory-files (concat user-emacs-directory
  917. "info")
  918. t
  919. "^[^.].*"))
  920. (when (file-directory-p dir)
  921. (add-to-list 'Info-additional-directory-list
  922. dir)))
  923. (let ((dir (expand-file-name "~/.brew/share/info")))
  924. (when (file-directory-p dir)
  925. (add-to-list 'Info-additional-directory-list
  926. dir))))
  927. (with-eval-after-load 'apropos
  928. (defvar apropos-mode-map (make-sparse-keymap))
  929. (define-key apropos-mode-map "n" 'next-line)
  930. (define-key apropos-mode-map "p" 'previous-line))
  931. ;; `isearch' library does not call `provide' so cannot use with-eval-after-load
  932. ;; (define-key isearch-mode-map
  933. ;; (kbd "C-j") 'isearch-other-control-char)
  934. ;; (define-key isearch-mode-map
  935. ;; (kbd "C-k") 'isearch-other-control-char)
  936. ;; (define-key isearch-mode-map
  937. ;; (kbd "C-h") 'isearch-other-control-char)
  938. (define-key isearch-mode-map (kbd "C-h") 'isearch-del-char)
  939. (define-key isearch-mode-map (kbd "M-r")
  940. 'isearch-query-replace-regexp)
  941. ;; do not cleanup isearch highlight: use `lazy-highlight-cleanup' to remove
  942. (setq lazy-highlight-cleanup nil)
  943. ;; face for isearch highlighing
  944. (set-face-attribute 'lazy-highlight
  945. nil
  946. :foreground `unspecified
  947. :background `unspecified
  948. :underline t
  949. ;; :weight `bold
  950. )
  951. (add-hook 'outline-mode-hook
  952. (lambda ()
  953. (when (string-match "\\.md\\'" buffer-file-name)
  954. (set (make-local-variable 'outline-regexp) "#+ "))))
  955. (add-hook 'outline-mode-hook
  956. 'outline-show-all)
  957. (add-to-list 'auto-mode-alist (cons "\\.ol\\'" 'outline-mode))
  958. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'outline-mode))
  959. (when (autoload-eval-lazily 'markdown-mode
  960. '(markdown-mode gfm-mode)
  961. (defvar gfm-mode-map (make-sparse-keymap))
  962. (define-key gfm-mode-map (kbd "C-m") 'electric-indent-just-newline))
  963. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'gfm-mode))
  964. (set-variable 'markdown-command (or (executable-find "markdown")
  965. (executable-find "markdown.pl")
  966. ""))
  967. (add-hook 'markdown-mode-hook
  968. (lambda ()
  969. (outline-minor-mode 1)
  970. (flyspell-mode)
  971. (set (make-local-variable 'comment-start) ";")))
  972. )
  973. ;; c-mode
  974. ;; http://www.emacswiki.org/emacs/IndentingC
  975. ;; http://en.wikipedia.org/wiki/Indent_style
  976. ;; http://d.hatena.ne.jp/emergent/20070203/1170512717
  977. ;; http://seesaawiki.jp/whiteflare503/d/Emacs%20%a5%a4%a5%f3%a5%c7%a5%f3%a5%c8
  978. (with-eval-after-load 'cc-vars
  979. (defvar c-default-style nil)
  980. (add-to-list 'c-default-style
  981. '(c-mode . "k&r"))
  982. (add-to-list 'c-default-style
  983. '(c++-mode . "k&r")))
  984. (autoload-eval-lazily 'js2-mode nil
  985. ;; currently do not use js2-mode
  986. ;; (add-to-list 'auto-mode-alist '("\\.js\\'" . js2-mode))
  987. ;; (add-to-list 'auto-mode-alist '("\\.jsm\\'" . js2-mode))
  988. ;; (defvar js2-mode-map (make-sparse-keymap))
  989. ;; (define-key js2-mode-map (kbd "C-m") (lambda ()
  990. ;; (interactive)
  991. ;; (js2-enter-key)
  992. ;; (indent-for-tab-command)))
  993. ;; (add-hook (kill-local-variable 'before-save-hook)
  994. ;; 'js2-before-save)
  995. ;; (add-hook 'before-save-hook
  996. ;; 'my-indent-buffer
  997. ;; nil
  998. ;; t)
  999. )
  1000. (add-to-list 'interpreter-mode-alist
  1001. '("node" . js-mode))
  1002. (add-hook 'haskell-mode-hook 'turn-on-haskell-indentation)
  1003. (with-eval-after-load 'uniquify
  1004. (setq uniquify-buffer-name-style 'post-forward-angle-brackets)
  1005. (setq uniquify-ignore-buffers-re "*[^*]+*")
  1006. (setq uniquify-min-dir-content 1))
  1007. (with-eval-after-load 'view
  1008. (defvar view-mode-map (make-sparse-keymap))
  1009. (define-key view-mode-map "j" 'scroll-up-line)
  1010. (define-key view-mode-map "k" 'scroll-down-line)
  1011. (define-key view-mode-map "v" 'toggle-read-only)
  1012. (define-key view-mode-map "q" 'bury-buffer)
  1013. ;; (define-key view-mode-map "/" 'nonincremental-re-search-forward)
  1014. ;; (define-key view-mode-map "?" 'nonincremental-re-search-backward)
  1015. ;; (define-key view-mode-map
  1016. ;; "n" 'nonincremental-repeat-search-forward)
  1017. ;; (define-key view-mode-map
  1018. ;; "N" 'nonincremental-repeat-search-backward)
  1019. (define-key view-mode-map "/" 'isearch-forward-regexp)
  1020. (define-key view-mode-map "?" 'isearch-backward-regexp)
  1021. (define-key view-mode-map "n" 'isearch-repeat-forward)
  1022. (define-key view-mode-map "N" 'isearch-repeat-backward)
  1023. (define-key view-mode-map (kbd "C-m") 'my-rgrep-symbol-at-point))
  1024. (global-set-key "\M-r" 'view-mode)
  1025. ;; (setq view-read-only t)
  1026. (with-eval-after-load 'term
  1027. (defvar term-raw-map (make-sparse-keymap))
  1028. (define-key term-raw-map (kbd "C-x")
  1029. (lookup-key (current-global-map)
  1030. (kbd "C-x"))))
  1031. (add-hook 'term-mode-hook
  1032. (lambda ()
  1033. ;; Stop current line highlighting
  1034. (set (make-local-variable (defvar hl-line-range-function))
  1035. (lambda () '(0 . 0)))
  1036. (set (make-local-variable 'scroll-margin)
  1037. 0)))
  1038. (add-hook 'Man-mode-hook
  1039. (lambda ()
  1040. (view-mode 1)
  1041. (setq truncate-lines nil)))
  1042. (set-variable 'Man-notify-method (if window-system
  1043. 'newframe
  1044. 'aggressive))
  1045. (set-variable 'woman-cache-filename (expand-file-name (concat user-emacs-directory
  1046. "woman_cache.el")))
  1047. ;; not work because man.el will be loaded when man called
  1048. (defalias 'man 'woman)
  1049. (add-to-list 'auto-mode-alist
  1050. '("tox\\.ini\\'" . conf-unix-mode))
  1051. (when (autoload-eval-lazily 'toml-mode)
  1052. (add-to-list 'auto-mode-alist
  1053. '("/tox\\.ini\\'" . toml-mode))
  1054. (add-to-list 'auto-mode-alist
  1055. '("/Pipfile\\'" . toml-mode))
  1056. (add-to-list 'auto-mode-alist
  1057. '("/poetry\\.lock\\'" . toml-mode))
  1058. )
  1059. (when (autoload-eval-lazily 'json-mode)
  1060. (add-to-list 'auto-mode-alist
  1061. '("/Pipfile\\.lock\\'" . json-mode)))
  1062. (add-hook 'go-mode-hook
  1063. (lambda()
  1064. (defvar go-mode-map)
  1065. (add-hook 'before-save-hook' 'gofmt-before-save)
  1066. (define-key go-mode-map (kbd "M-.") 'godef-jump)))
  1067. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1068. ;; buffers
  1069. (defvar bs-configurations)
  1070. (when (autoload-eval-lazily 'bs '(bs-show)
  1071. (add-to-list 'bs-configurations
  1072. '("specials" "^\\*" nil ".*" nil nil))
  1073. (add-to-list 'bs-configurations
  1074. '("files-and-specials" "^\\*" buffer-file-name ".*" nil nil))
  1075. (defvar bs-mode-map)
  1076. (defvar bs-current-configuration)
  1077. (define-key bs-mode-map (kbd "t")
  1078. ;; TODO: fix toggle feature
  1079. (lambda ()
  1080. (interactive)
  1081. (if (string= "specials"
  1082. bs-current-configuration)
  1083. (bs-set-configuration "files")
  1084. (bs-set-configuration "specials"))
  1085. (bs-refresh)
  1086. (bs-message-without-log "%s"
  1087. (bs--current-config-message))))
  1088. ;; (setq bs-configurations (list
  1089. ;; '("processes" nil get-buffer-process ".*" nil nil)
  1090. ;; '("files-and-scratch" "^\\*scratch\\*$" nil nil
  1091. ;; bs-visits-non-file bs-sort-buffer-interns-are-last)))
  1092. )
  1093. (defalias 'list-buffers 'bs-show)
  1094. (set-variable 'bs-default-configuration "files-and-specials")
  1095. (set-variable 'bs-default-sort-name "by nothing")
  1096. (add-hook 'bs-mode-hook
  1097. (lambda ()
  1098. (set (make-local-variable 'scroll-margin) 0))))
  1099. ;;(iswitchb-mode 1)
  1100. (icomplete-mode)
  1101. (defun iswitchb-buffer-display-other-window ()
  1102. "Do iswitchb in other window."
  1103. (interactive)
  1104. (let ((iswitchb-default-method 'display))
  1105. (call-interactively 'iswitchb-buffer)))
  1106. ;; buffer killing
  1107. ;; (defun my-delete-window-killing-buffer () nil)
  1108. (defun my-query-kill-current-buffer ()
  1109. "Interactively kill current buffer."
  1110. (interactive)
  1111. (if (y-or-n-p (concat "kill current buffer? :"))
  1112. (kill-buffer (current-buffer))))
  1113. ;;(global-set-key "\C-xk" 'my-query-kill-current-buffer)
  1114. (substitute-key-definition 'kill-buffer
  1115. 'my-query-kill-current-buffer
  1116. global-map)
  1117. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1118. ;; recentf-mode
  1119. (set-variable 'recentf-save-file (expand-file-name (concat user-emacs-directory
  1120. "recentf")))
  1121. (set-variable 'recentf-max-menu-items 20)
  1122. (set-variable 'recentf-max-saved-items 30)
  1123. (set-variable 'recentf-show-file-shortcuts-flag nil)
  1124. (set-variable 'recentf-auto-cleanup 3)
  1125. ;; (safe-require-or-eval 'sync-recentf)
  1126. ;; (when (safe-require-or-eval 'recentf)
  1127. ;; (add-to-list 'recentf-exclude
  1128. ;; (regexp-quote recentf-save-file))
  1129. ;; (add-to-list 'recentf-exclude
  1130. ;; (regexp-quote (expand-file-name user-emacs-directory)))
  1131. ;; (add-to-list 'recentf-exclude
  1132. ;; "/sync-recentf-marker\\'")
  1133. ;; (define-key ctl-x-map (kbd "C-r") 'recentf-open-files)
  1134. ;; (remove-hook 'find-file-hook
  1135. ;; 'recentf-track-opened-file)
  1136. ;; (defun my-recentf-load-track-save-list ()
  1137. ;; "Load current recentf list from file, track current visiting file, then save
  1138. ;; the list."
  1139. ;; (recentf-load-list)
  1140. ;; (recentf-track-opened-file)
  1141. ;; (recentf-save-list))
  1142. ;; (add-hook 'find-file-hook
  1143. ;; 'my-recentf-load-track-save-list)
  1144. ;; (add-hook 'kill-emacs-hook
  1145. ;; 'recentf-load-list)
  1146. ;; ;;(run-with-idle-timer 5 t 'recentf-save-list)
  1147. ;; ;; (add-hook 'find-file-hook
  1148. ;; ;; (lambda ()
  1149. ;; ;; (recentf-add-file default-directory)))
  1150. ;; (when (autoload-eval-lazily 'recentf-show)
  1151. ;; (define-key ctl-x-map (kbd "C-r") 'recentf-show)
  1152. ;; ;; (add-hook 'recentf-show-before-listing-hook
  1153. ;; ;; 'recentf-load-list)
  1154. ;; )
  1155. ;; (recentf-mode 1)
  1156. ;; (define-key recentf-dialog-mode-map (kbd "<up>") 'previous-line)
  1157. ;; (define-key recentf-dialog-mode-map (kbd "<down>") 'next-line)
  1158. ;; (define-key recentf-dialog-mode-map "p" 'previous-line)
  1159. ;; (define-key recentf-dialog-mode-map "n" 'next-line)
  1160. ;; (add-hook 'recentf-dialog-mode-hook
  1161. ;; (lambda ()
  1162. ;; ;; (recentf-save-list)
  1163. ;; ;; (define-key recentf-dialog-mode-map (kbd "C-x C-f")
  1164. ;; ;; 'my-recentf-cd-and-find-file)
  1165. ;; (cd "~/"))))
  1166. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1167. ;; dired
  1168. (defun my-file-head (filename &optional n)
  1169. "Return list of first N lines of file FILENAME."
  1170. ;; TODO: Fix for janapese text
  1171. ;; TODO: Fix for short text
  1172. (let ((num (or n 10))
  1173. (size 100)
  1174. (beg 0)
  1175. (end 0)
  1176. (result '())
  1177. (read -1))
  1178. (with-temp-buffer
  1179. (erase-buffer)
  1180. (while (or (<= (count-lines (point-min)
  1181. (point-max))
  1182. num)
  1183. (eq read 0))
  1184. (setq end (+ beg size))
  1185. (setq read (nth 1 (insert-file-contents-literally filename
  1186. nil
  1187. beg
  1188. end)))
  1189. (goto-char (point-max))
  1190. (setq beg (+ beg size)))
  1191. (goto-char (point-min))
  1192. (while (< (length result) num)
  1193. (let ((start (point)))
  1194. (forward-line 1)
  1195. (setq result
  1196. `(,@result ,(buffer-substring-no-properties start
  1197. (point))))))
  1198. result
  1199. ;; (buffer-substring-no-properties (point-min)
  1200. ;; (progn
  1201. ;; (forward-line num)
  1202. ;; (point)))
  1203. )))
  1204. ;; (apply 'concat (my-file-head "./shrc" 10)
  1205. (defun my-dired-echo-file-head (arg)
  1206. "Echo head of current file.
  1207. ARG is num to show, or defaults to 7."
  1208. (interactive "P")
  1209. (let ((f (dired-get-filename)))
  1210. (message "%s"
  1211. (apply 'concat
  1212. (my-file-head f
  1213. 7)))))
  1214. (defun my-dired-diff ()
  1215. "Show diff of marked file and file of current line."
  1216. (interactive)
  1217. (let ((files (dired-get-marked-files nil nil nil t)))
  1218. (if (eq (car files)
  1219. t)
  1220. (diff (cadr files) (dired-get-filename))
  1221. (message "One file must be marked!"))))
  1222. (defun dired-get-file-info ()
  1223. "Print information of current line file."
  1224. (interactive)
  1225. (let ((f (shell-quote-argument (dired-get-filename t))))
  1226. (if (file-directory-p f)
  1227. (progn
  1228. (message "Calculating disk usage...")
  1229. (shell-command (concat "du -hsD "
  1230. f)))
  1231. (shell-command (concat "file "
  1232. f)))))
  1233. (defun my-dired-scroll-up ()
  1234. "Scroll up."
  1235. (interactive)
  1236. (my-dired-previous-line (- (window-height) 1)))
  1237. (defun my-dired-scroll-down ()
  1238. "Scroll down."
  1239. (interactive)
  1240. (my-dired-next-line (- (window-height) 1)))
  1241. ;; (defun my-dired-forward-line (arg)
  1242. ;; ""
  1243. ;; (interactive "p"))
  1244. (defun my-dired-previous-line (arg)
  1245. "Move ARG lines up."
  1246. (interactive "p")
  1247. (if (> arg 0)
  1248. (progn
  1249. (if (eq (line-number-at-pos)
  1250. 1)
  1251. (goto-char (point-max))
  1252. (forward-line -1))
  1253. (my-dired-previous-line (if (or (dired-get-filename nil t)
  1254. (dired-get-subdir))
  1255. (- arg 1)
  1256. arg)))
  1257. (dired-move-to-filename)))
  1258. (defun my-dired-next-line (arg)
  1259. "Move ARG lines down."
  1260. (interactive "p")
  1261. (if (> arg 0)
  1262. (progn
  1263. (if (eq (point)
  1264. (point-max))
  1265. (goto-char (point-min))
  1266. (forward-line 1))
  1267. (my-dired-next-line (if (or (dired-get-filename nil t)
  1268. (dired-get-subdir))
  1269. (- arg 1)
  1270. arg)))
  1271. (dired-move-to-filename)))
  1272. (defun my-tramp-remote-find-file (f)
  1273. "Open F."
  1274. (interactive (list (read-file-name "My Find File Tramp: "
  1275. "/scp:"
  1276. nil ;; "/scp:"
  1277. (confirm-nonexistent-file-or-buffer))))
  1278. (find-file f))
  1279. ;;http://bach.istc.kobe-u.ac.jp/lect/tamlab/ubuntu/emacs.html
  1280. (if (eq window-system 'mac)
  1281. (setq dired-listing-switches "-lhF")
  1282. (setq dired-listing-switches "-lhF --time-style=long-iso")
  1283. )
  1284. (setq dired-listing-switches "-lhF")
  1285. ;; when using dired-find-alternate-file
  1286. ;; reuse current dired buffer for the file to open
  1287. ;; (put 'dired-find-alternate-file 'disabled nil)
  1288. (set-variable 'dired-ls-F-marks-symlinks t)
  1289. (set-variable 'ls-lisp-use-insert-directory-program nil) ; always use ls-lisp
  1290. (set-variable 'ls-lisp-dirs-first t)
  1291. (set-variable 'ls-lisp-use-localized-time-format t)
  1292. (set-variable 'ls-lisp-format-time-list
  1293. '("%Y-%m-%d %H:%M"
  1294. "%Y-%m-%d "))
  1295. (set-variable 'dired-dwim-target t)
  1296. (set-variable 'dired-isearch-filenames t)
  1297. (set-variable 'dired-hide-details-hide-symlink-targets nil)
  1298. (set-variable 'dired-hide-details-hide-information-lines nil)
  1299. (set-variable 'dired-deletion-confirmer 'y-or-n-p)
  1300. (set-variable 'dired-recursive-deletes 'always)
  1301. ;; (add-hook 'dired-after-readin-hook
  1302. ;; 'my-replace-nasi-none)
  1303. (with-eval-after-load 'dired
  1304. (safe-require-or-eval 'ls-lisp)
  1305. (defvar dired-mode-map (make-sparse-keymap))
  1306. ;; dired-do-chgrp sometimes cause system hung
  1307. (define-key dired-mode-map "G" 'ignore)
  1308. (define-key dired-mode-map "e" 'wdired-change-to-wdired-mode)
  1309. (define-key dired-mode-map "i" 'dired-get-file-info)
  1310. (define-key dired-mode-map "f" 'find-file)
  1311. (define-key dired-mode-map "!" 'shell-command)
  1312. (define-key dired-mode-map "&" 'async-shell-command)
  1313. (define-key dired-mode-map "X" 'dired-do-async-shell-command)
  1314. (define-key dired-mode-map "=" 'my-dired-diff)
  1315. (define-key dired-mode-map "B" 'gtkbm-add-current-dir)
  1316. (define-key dired-mode-map "b" 'gtkbm)
  1317. (define-key dired-mode-map "h" 'my-dired-echo-file-head)
  1318. (define-key dired-mode-map (kbd "TAB") 'other-window)
  1319. ;; (define-key dired-mode-map "P" 'my-dired-do-pack-or-unpack)
  1320. (define-key dired-mode-map "/" 'dired-isearch-filenames)
  1321. (define-key dired-mode-map (kbd "DEL") 'dired-up-directory)
  1322. (define-key dired-mode-map (kbd "C-h") 'dired-up-directory)
  1323. (substitute-key-definition 'dired-next-line
  1324. 'my-dired-next-line
  1325. dired-mode-map)
  1326. (substitute-key-definition 'dired-previous-line
  1327. 'my-dired-previous-line
  1328. dired-mode-map)
  1329. ;; (define-key dired-mode-map (kbd "C-p") 'my-dired-previous-line)
  1330. ;; (define-key dired-mode-map (kbd "p") 'my-dired-previous-line)
  1331. ;; (define-key dired-mode-map (kbd "C-n") 'my-dired-next-line)
  1332. ;; (define-key dired-mode-map (kbd "n") 'my-dired-next-line)
  1333. (define-key dired-mode-map (kbd "<left>") 'my-dired-scroll-up)
  1334. (define-key dired-mode-map (kbd "<right>") 'my-dired-scroll-down)
  1335. (define-key dired-mode-map (kbd "ESC p") 'my-dired-scroll-up)
  1336. (define-key dired-mode-map (kbd "ESC n") 'my-dired-scroll-down)
  1337. (add-hook 'dired-mode-hook
  1338. (lambda ()
  1339. (when (fboundp 'dired-hide-details-mode)
  1340. (dired-hide-details-mode t)
  1341. (local-set-key "l" 'dired-hide-details-mode))
  1342. (let ((file "._Icon\015"))
  1343. (when nil
  1344. '(file-readable-p file)
  1345. (delete-file file)))))
  1346. (when (autoload-eval-lazily 'pack '(dired-do-pack-or-unpack pack-pack))
  1347. (with-eval-after-load 'dired
  1348. (define-key dired-mode-map "P" 'pack-dired-dwim)))
  1349. (when (autoload-eval-lazily 'dired-list-all-mode)
  1350. (setq dired-listing-switches "-lhF")
  1351. (with-eval-after-load 'dired
  1352. (define-key dired-mode-map "a" 'dired-list-all-mode))))
  1353. (when (autoload-eval-lazily 'dired-filter)
  1354. (add-hook 'dired-mode-hook
  1355. 'dired-filter-mode))
  1356. ;; Currently disabled in favor of dired-from-git-ls-files
  1357. ;; (define-key ctl-x-map "f" 'find-dired)
  1358. ;; It works!
  1359. ;; (pop-to-buffer (dired-noselect '("." "shrc" "emacs.el")))
  1360. (defun my-dired-git-ls-files (args)
  1361. "Dired from git ls-files."
  1362. (interactive "sgit ls-files args: ")
  1363. (pop-to-buffer-same-window
  1364. (dired-noselect `(,default-directory
  1365. ,@(split-string (shell-command-to-string (concat "git ls-files -z " args))
  1366. "\0" t))
  1367. ""))
  1368. )
  1369. (define-key ctl-x-map (kbd "f") 'my-dired-git-ls-files)
  1370. (with-eval-after-load 'dired
  1371. (defvar dired-mode-map (make-sparse-keymap))
  1372. (define-key dired-mode-map "f" 'my-dired-git-ls-files))
  1373. ;; (define-minor-mode my-dired-glob-filter)
  1374. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1375. ;; misc funcs
  1376. (defalias 'qcalc 'quick-calc)
  1377. (defun memo (&optional dir)
  1378. "Open memo.txt in DIR."
  1379. (interactive)
  1380. (pop-to-buffer (find-file-noselect (concat (if dir
  1381. (file-name-as-directory dir)
  1382. "")
  1383. "memo.txt"))))
  1384. (set (defvar my-rgrep-alist nil
  1385. "Alist of rgrep command.
  1386. Each element is in the form like (NAME SEXP COMMAND), where SEXP returns the
  1387. condition to choose COMMAND when evaluated.")
  1388. `(
  1389. ;; ripgrep
  1390. ("rg"
  1391. (executable-find "rg")
  1392. "rg --hidden --no-heading --smart-case ")
  1393. ;; git grep
  1394. ("gitgrep"
  1395. (eq 0
  1396. (shell-command "git rev-parse --git-dir"))
  1397. "git --no-pager -c color.grep=always grep -nH -e ")
  1398. ;; sift
  1399. ("sift"
  1400. (executable-find "sift")
  1401. ("sift --binary-skip --filename --line-number --git --smart-case "))
  1402. ;; the silver searcher
  1403. ("ag"
  1404. (executable-find "ag")
  1405. "ag --nogroup --nopager --filename ")
  1406. ;; ack
  1407. ("ack"
  1408. (executable-find "ack")
  1409. "ack --nogroup --nopager --with-filename ")
  1410. ;; gnu global
  1411. ("global"
  1412. (and (require 'ggtags nil t)
  1413. (executable-find "global")
  1414. (ggtags-current-project-root))
  1415. "global --result grep ")
  1416. ;; grep
  1417. ("grep"
  1418. t
  1419. ,(concat "find . "
  1420. "-path '*/.git' -prune -o "
  1421. "-path '*/.svn' -prune -o "
  1422. "-type f -print0 | "
  1423. "xargs -0 grep -nH -e "))
  1424. )
  1425. )
  1426. (defvar my-rgrep-default nil
  1427. "Default command name for my-rgrep.")
  1428. (defun my-rgrep-grep-command (&optional name alist)
  1429. "Return recursive grep command for current directory or nil.
  1430. If NAME is given, use that without testing.
  1431. Commands are searched from ALIST."
  1432. (if alist
  1433. (if name
  1434. ;; if name is given search that from alist and return the command
  1435. (nth 2 (assoc name
  1436. alist))
  1437. ;; if name is not given try test in 1th elem
  1438. (let ((car (car alist))
  1439. (cdr (cdr alist)))
  1440. (if (eval (nth 1 car))
  1441. ;; if the condition is true return the command
  1442. (nth 2 car)
  1443. ;; try next one
  1444. (and cdr
  1445. (my-rgrep-grep-command name cdr)))))
  1446. ;; if alist is not given set default value
  1447. (my-rgrep-grep-command name my-rgrep-alist)))
  1448. (defun my-rgrep (command-args)
  1449. "My recursive grep. Run COMMAND-ARGS.
  1450. If prefix argument is given, use current symbol as default search target
  1451. and search from projectile root (if projectile is available)."
  1452. (interactive (let ((cmd (my-rgrep-grep-command my-rgrep-default
  1453. nil)))
  1454. (if cmd
  1455. (list (read-shell-command "grep command: "
  1456. (concat cmd
  1457. (if current-prefix-arg
  1458. (thing-at-point 'symbol t)
  1459. ""))
  1460. 'grep-find-history))
  1461. (error "My-Rgrep: Command for rgrep not found")
  1462. )))
  1463. (if (and current-prefix-arg
  1464. (safe-require-or-eval 'projectile)
  1465. (projectile-project-p))
  1466. (projectile-with-default-dir (projectile-project-root)
  1467. (compilation-start command-args
  1468. 'grep-mode))
  1469. (compilation-start command-args
  1470. 'grep-mode)))
  1471. (defun my-rgrep-thing-at-point-projectile-root ()
  1472. "My recursive grep to find thing at point from project root."
  1473. (interactive)
  1474. (let* ((cmd (my-rgrep-grep-command my-rgrep-default
  1475. nil))
  1476. (command-args
  1477. (if cmd
  1478. (concat cmd
  1479. (or (thing-at-point 'symbol t)
  1480. (error "No symbol at point")))
  1481. (error "My-Rgrep: Command for rgrep not found"))))
  1482. (if (safe-require-or-eval 'projectile)
  1483. (projectile-with-default-dir (or (projectile-project-root)
  1484. default-directory)
  1485. (compilation-start command-args
  1486. 'grep-mode))
  1487. (compilation-start command-args
  1488. 'grep-mode))))
  1489. (defmacro define-my-rgrep (name)
  1490. "Define rgrep for NAME."
  1491. `(defun ,(intern (concat "my-rgrep-"
  1492. name)) ()
  1493. ,(format "My recursive grep by %s."
  1494. name)
  1495. (interactive)
  1496. (let ((my-rgrep-default ,name))
  1497. (if (called-interactively-p 'any)
  1498. (call-interactively 'my-rgrep)
  1499. (error "Not intended to be called noninteractively. Use `my-rgrep'"))))
  1500. )
  1501. (define-my-rgrep "ack")
  1502. (define-my-rgrep "ag")
  1503. (define-my-rgrep "rg")
  1504. (define-my-rgrep "sift")
  1505. (define-my-rgrep "gitgrep")
  1506. (define-my-rgrep "grep")
  1507. (define-my-rgrep "global")
  1508. (define-key ctl-x-map "s" 'my-rgrep)
  1509. (define-key ctl-x-map "." 'my-rgrep-thing-at-point-projectile-root)
  1510. (defun my-occur (regexp &optional region)
  1511. "My occur command to search REGEXP."
  1512. (interactive (list (read-string "List lines matching regexp: "
  1513. (thing-at-point 'symbol t))))
  1514. (occur regexp nil region))
  1515. (define-key ctl-x-map (kbd "C-o") 'my-occur)
  1516. (set-variable 'dumb-jump-prefer-searcher 'rg)
  1517. (defalias 'make 'compile)
  1518. (define-key ctl-x-map "c" 'compile)
  1519. ;;;;;;;;;;;;;;;;;;;;;;;
  1520. ;; adoc-simple-mode
  1521. (when (safe-require-or-eval 'adoc-mode)
  1522. (defvar adoc-simple-font-lock-keywords
  1523. nil)
  1524. (define-derived-mode adoc-simple-mode adoc-mode
  1525. "Adoc-Simple"
  1526. "Major mode for editing AsciiDoc text files.
  1527. This mode is a simplified version of `adoc-mode'."
  1528. '(set (make-local-variable 'font-lock-defaults)
  1529. '(adoc-simple-font-lock-keywords
  1530. nil nil nil nil
  1531. (font-lock-multiline . t)
  1532. (font-lock-mark-block-function . adoc-font-lock-mark-block-function))))
  1533. (add-to-list 'auto-mode-alist
  1534. '("\\.adoc\\'" . adoc-simple-mode)))
  1535. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1536. ;; awk-preview
  1537. (defgroup awk-preview nil
  1538. "Awk previewer."
  1539. :tag "Awk Preview"
  1540. :prefix "awk-preview-"
  1541. :group 'tools)
  1542. (defcustom awk-preview-program
  1543. (or (executable-find "gawk")
  1544. (executable-find "awk")
  1545. "awk")
  1546. "Awk program to execute."
  1547. :type 'string
  1548. :group 'awk-preview)
  1549. (defcustom awk-preview-switches
  1550. ;; '("--sandbox")
  1551. nil
  1552. "String of awk options appended when running awk preview."
  1553. :type '(repeat string)
  1554. :group 'awk-preview)
  1555. (defcustom awk-preview-default-program
  1556. "# C-c C-l to update preview, C-c C-c to commit, C-c C-k to abort.
  1557. {
  1558. print NR, $0
  1559. }
  1560. "
  1561. "Default awk command."
  1562. :type 'string
  1563. :group 'awk-preview)
  1564. (cl-defstruct awk-preview--env
  1565. (running-p nil)
  1566. ;; Point of beg in source buffer
  1567. (point-beg nil)
  1568. ;; Point of end in source buffer
  1569. (point-end nil)
  1570. ;; Point of beginning
  1571. ;; Used by preview buffer and always same as awk-preview--point-beg
  1572. (preview-point-beg nil)
  1573. ;; Point of beginning
  1574. ;; Used by preview buffer and may defferent from awk-preview--point-end
  1575. (preview-point-end nil)
  1576. ;; Awk preview program file name
  1577. (program-filename nil)
  1578. ;; Source buffer
  1579. (source-buffer nil)
  1580. ;; Preview buffer
  1581. (preview-buffer nil)
  1582. ;; Program buffer
  1583. (program-buffer nil)
  1584. ;; Window configuration when entering awk-review
  1585. (window-configuration nil)
  1586. )
  1587. (defvar awk-preview--env nil
  1588. "`awk-preview--env' struct object of currently running.")
  1589. (make-variable-buffer-local 'awk-preview--env)
  1590. (put 'awk-preview--env
  1591. 'permanent-local
  1592. t)
  1593. (defun awk-preview--invoke-awk (buf beg end progfile output &optional delete display)
  1594. "Execute PROGFILE awk process with BEG and END input and output to OUTPUT buffer.
  1595. When OUTPUT is t the result will be output to BUF.
  1596. Delete the text between BEG and END when DELETE is non-nil.
  1597. DISPLAY non-nil means redisplay buffer as output is inserted."
  1598. (with-current-buffer buf
  1599. (let ((status (apply 'call-process-region
  1600. beg
  1601. end
  1602. awk-preview-program
  1603. delete
  1604. output
  1605. display
  1606. `(,@awk-preview-switches "-f" ,progfile))))
  1607. (unless (eq status
  1608. 0)
  1609. (error "awk-preview: Awk program exited abnormally."))
  1610. output)))
  1611. (defvar awk-preview-program-buffer-name
  1612. "*AWK Preview Program<%s>*"
  1613. "Buffer name of awk preview program.")
  1614. (defvar awk-preview-preview-buffer-name
  1615. "*AWK Preview<%s>*"
  1616. "Buffer name of awk preview.")
  1617. (defun awk-preview--create-program-buffer (e)
  1618. "Create program buffer for `awk-preview--env' object E."
  1619. (let ((source-name (buffer-name (awk-preview--env-source-buffer e))))
  1620. (with-current-buffer (generate-new-buffer (format awk-preview-program-buffer-name
  1621. source-name))
  1622. (erase-buffer)
  1623. (insert awk-preview-default-program)
  1624. (awk-mode)
  1625. (awk-preview-program-mode 1)
  1626. (setf (awk-preview--env-program-buffer e) (current-buffer))
  1627. (setf (awk-preview--env-program-filename e) (make-temp-file "awk-preview-"
  1628. nil
  1629. ".awk"))
  1630. (setq awk-preview--env e)
  1631. (current-buffer))))
  1632. (defun awk-preview-program-buffer-kill-hook ()
  1633. "Cleanup for awk-preview program buffer."
  1634. (when (and awk-preview--env
  1635. (awk-preview--env-program-filename awk-preview--env))
  1636. (delete-file (awk-preview--env-program-filename awk-preview--env))))
  1637. (add-hook 'kill-buffer-hook
  1638. 'awk-preview-program-buffer-kill-hook)
  1639. (defun awk-preview--create-preview-buffer (e)
  1640. "Create preview buffer for `awk-preview--env' object E."
  1641. (with-current-buffer (awk-preview--env-source-buffer e)
  1642. ;; TODO: Do not unset buffer-file-name in let, use unwind-protect
  1643. (let ((buffer-file-name nil)
  1644. (beg (awk-preview--env-point-beg e))
  1645. (end (awk-preview--env-point-end e)))
  1646. (with-current-buffer (clone-buffer (format awk-preview-preview-buffer-name
  1647. (buffer-name)))
  1648. (awk-preview-program-mode 1)
  1649. (setf (awk-preview--env-preview-point-beg e) beg)
  1650. (setf (awk-preview--env-preview-point-end e) end)
  1651. (setf (awk-preview--env-preview-buffer e) (current-buffer))
  1652. (goto-char end)
  1653. (setq buffer-read-only t)
  1654. (setq awk-preview--env e)
  1655. (current-buffer)))))
  1656. (defun awk-preview (beg end)
  1657. "Run awk and preview result."
  1658. (interactive (if (use-region-p)
  1659. (list (region-beginning)
  1660. (region-end))
  1661. (list (point-min)
  1662. (point-max))))
  1663. (when (and awk-preview--env
  1664. (awk-preview--env-running-p awk-preview--env))
  1665. (error "AWK-Preview already running"))
  1666. (let ((e (make-awk-preview--env)))
  1667. (setf (awk-preview--env-point-beg e) beg)
  1668. (setf (awk-preview--env-point-end e) end)
  1669. (setf (awk-preview--env-source-buffer e) (current-buffer))
  1670. (awk-preview--create-preview-buffer e)
  1671. (awk-preview--create-program-buffer e)
  1672. (setf (awk-preview--env-window-configuration e)
  1673. (current-window-configuration))
  1674. (cl-assert (awk-preview--env-point-beg e))
  1675. (cl-assert (awk-preview--env-point-end e))
  1676. (cl-assert (awk-preview--env-preview-point-beg e))
  1677. (cl-assert (awk-preview--env-preview-point-end e))
  1678. (cl-assert (awk-preview--env-program-filename e))
  1679. (cl-assert (awk-preview--env-source-buffer e))
  1680. (cl-assert (awk-preview--env-preview-buffer e))
  1681. (cl-assert (awk-preview--env-program-buffer e))
  1682. (cl-assert (awk-preview--env-window-configuration e))
  1683. (setq awk-preview--env e)
  1684. (set-window-buffer (get-buffer-window (awk-preview--env-source-buffer e))
  1685. (awk-preview--env-preview-buffer e))
  1686. (pop-to-buffer (awk-preview--env-program-buffer e))
  1687. (switch-to-buffer (awk-preview--env-program-buffer e))
  1688. (setf (awk-preview--env-running-p e) t)
  1689. (awk-preview-update-preview)
  1690. ))
  1691. (defun awk-preview-update-preview ()
  1692. "Update awk-preview."
  1693. (interactive)
  1694. (with-current-buffer (awk-preview--env-program-buffer awk-preview--env)
  1695. (write-region (point-min)
  1696. (point-max)
  1697. (awk-preview--env-program-filename awk-preview--env)))
  1698. (let ((output (with-current-buffer (get-buffer-create " *awk-preview output*")
  1699. (erase-buffer)
  1700. (current-buffer)))
  1701. (progfile (awk-preview--env-program-filename awk-preview--env)))
  1702. (awk-preview--invoke-awk (awk-preview--env-source-buffer awk-preview--env)
  1703. (awk-preview--env-point-beg awk-preview--env)
  1704. (awk-preview--env-point-end awk-preview--env)
  1705. progfile
  1706. output)
  1707. (with-current-buffer (awk-preview--env-preview-buffer awk-preview--env)
  1708. (let ((inhibit-read-only t))
  1709. (goto-char (awk-preview--env-preview-point-end awk-preview--env))
  1710. (delete-region (awk-preview--env-preview-point-beg awk-preview--env)
  1711. (point))
  1712. (insert-buffer-substring output)
  1713. (setf (awk-preview--env-preview-point-end awk-preview--env)
  1714. (point)))
  1715. )))
  1716. (defun awk-preview-commit ()
  1717. "Exit awk-preview and update buffer."
  1718. (interactive)
  1719. (cl-assert awk-preview--env)
  1720. (awk-preview--invoke-awk (awk-preview--env-source-buffer awk-preview--env)
  1721. (awk-preview--env-point-beg awk-preview--env)
  1722. (awk-preview--env-point-end awk-preview--env)
  1723. (awk-preview--env-program-filename awk-preview--env)
  1724. t
  1725. t
  1726. t)
  1727. (awk-preview--cleanup))
  1728. (defun awk-preview-abort ()
  1729. "Discard result and exit awk-preview."
  1730. (interactive)
  1731. (awk-preview--cleanup))
  1732. (defun awk-preview--cleanup()
  1733. "Cleanup awk preview buffers and variables."
  1734. (with-current-buffer (awk-preview--env-source-buffer awk-preview--env)
  1735. (cl-assert awk-preview--env)
  1736. (kill-buffer (awk-preview--env-preview-buffer awk-preview--env))
  1737. ;; TODO: Ask if it should be killed
  1738. (kill-buffer (awk-preview--env-program-buffer awk-preview--env))
  1739. (setf (awk-preview--env-running-p awk-preview--env) nil))
  1740. (set-window-configuration (awk-preview--env-window-configuration awk-preview--env)))
  1741. (defvar awk-preview-program-mode-map
  1742. (let ((map (make-sparse-keymap)))
  1743. (define-key map (kbd "C-c C-l") 'awk-preview-update-preview)
  1744. (define-key map (kbd "C-c C-k") 'awk-preview-abort)
  1745. (define-key map (kbd "C-c C-c") 'awk-preview-commit)
  1746. ;; TODO: Implement
  1747. ;; Back preview buffer to original content
  1748. (define-key map (kbd "C-c C-r") 'awk-preview-reset)
  1749. map)
  1750. "Keymap for `awk-preview-program-mode'.")
  1751. (define-minor-mode awk-preview-program-mode
  1752. "Minor mode for awk-preview program buffer."
  1753. :lighter " AWKPrev"
  1754. :keymap awk-preview-program-mode-map)
  1755. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1756. ;; editorconfig-auto-apply
  1757. (define-minor-mode editorconfig-auto-apply-mode
  1758. "When saving .editorconfig file update buffer configs."
  1759. :global t
  1760. :lighter ""
  1761. (if editorconfig-auto-apply-mode
  1762. (add-hook 'after-save-hook
  1763. 'editorconfig-auto-apply-mode--run)
  1764. (remove-hook 'after-save-hook
  1765. 'editorconfig-auto-apply-mode--run)))
  1766. (defun editorconfig-auto-apply-mode--run ()
  1767. "When saving .editorconfig file walk all buffers and update configs."
  1768. (when (eq major-mode
  1769. 'editorconfig-conf-mode)
  1770. (let ((dir (file-name-directory buffer-file-name)))
  1771. (cl-dolist (buf (buffer-list))
  1772. (when (and (buffer-file-name buf)
  1773. (file-in-directory-p (buffer-file-name buf)
  1774. dir))
  1775. (with-current-buffer buf
  1776. (editorconfig-mode-apply)))))))
  1777. (editorconfig-auto-apply-mode 1)
  1778. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1779. ;; recently
  1780. (defgroup recently nil
  1781. "Recently visited files"
  1782. :tag "Recently"
  1783. :prefix "recently-"
  1784. :group 'tools)
  1785. (defcustom recently-file
  1786. (concat user-emacs-directory
  1787. "recently.el")
  1788. "Recently file."
  1789. :type 'string
  1790. :group 'recently)
  1791. (defcustom recently-max
  1792. 100
  1793. "Recently list max length."
  1794. :type 'int
  1795. :group 'recently)
  1796. (defcustom recently-excludes
  1797. '()
  1798. "List of regexps for filenames excluded from the recent list."
  1799. :type '(repeat string)
  1800. :group 'recently)
  1801. (add-to-list 'recently-excludes
  1802. (eval-when-compile (rx "/COMMIT_EDITMSG" eot)))
  1803. (defvar recently-list
  1804. '()
  1805. "Recently list.")
  1806. (defvar recently-file-mtime
  1807. nil
  1808. "Modified time of file when last read file.")
  1809. (defun recently-write ()
  1810. "Write to file."
  1811. (with-temp-buffer
  1812. (prin1 recently-list
  1813. (current-buffer))
  1814. (write-region (point-min)
  1815. (point-max)
  1816. recently-file)))
  1817. (defun recently-read ()
  1818. "Read file."
  1819. (when (file-readable-p recently-file)
  1820. (with-temp-buffer
  1821. (insert-file-contents recently-file)
  1822. (goto-char (point-min))
  1823. (setq recently-list
  1824. (read (current-buffer))))
  1825. (setq recently-file-mtime
  1826. (nth 5
  1827. (file-attributes recently-file)))))
  1828. (defun recently-reload ()
  1829. "Reload file."
  1830. (when (and (file-readable-p recently-file)
  1831. (not (equal recently-file-mtime
  1832. (nth 5
  1833. (file-attributes recently-file)))))
  1834. (recently-read)
  1835. (cl-assert (equal recently-file-mtime
  1836. (nth 5
  1837. (file-attributes recently-file))))))
  1838. (defun recently-add (path)
  1839. "Add PATH to list."
  1840. (cl-assert (string= path
  1841. (expand-file-name path)))
  1842. (when (cl-loop for re in recently-excludes
  1843. if (string-match re path) return nil
  1844. finally return t)
  1845. (recently-reload)
  1846. (let* ((l (copy-list recently-list))
  1847. (l (delete path
  1848. l))
  1849. (l (cl-loop for e in l
  1850. unless (file-in-directory-p path e)
  1851. collect e))
  1852. (l (cons path
  1853. l))
  1854. (l (recently--truncate l
  1855. recently-max)))
  1856. (unless (equal recently-list
  1857. l)
  1858. (setq recently-list l)
  1859. (recently-write)))))
  1860. (defun recently--truncate (list len)
  1861. "Truncate LIST to LEN."
  1862. (if (> (length list)
  1863. len)
  1864. (cl-subseq list
  1865. 0
  1866. len)
  1867. list))
  1868. (defun recently-find-file-hook ()
  1869. "Add current file."
  1870. (when buffer-file-name
  1871. (recently-add buffer-file-name)))
  1872. (defun recently-dired-mode-hook ()
  1873. "Add current directory."
  1874. (recently-add (expand-file-name default-directory)))
  1875. (add-hook 'find-file-hook
  1876. 'recently-find-file-hook)
  1877. (add-hook 'dired-mode-hook
  1878. 'recently-dired-mode-hook)
  1879. ;;;;;;;;;;;;;;;;
  1880. ;; recently-show
  1881. (defvar recently-show-window-height 10
  1882. "Max height of window of `recently-show'")
  1883. ;; (defvar recently-show-mode-map
  1884. ;; (let ((map (make-sparse-keymap)))
  1885. ;; (suppress-keymap map)
  1886. ;; (define-key map "n" 'next-line)
  1887. ;; (define-key map "p" 'previous-line)
  1888. ;; (define-key map (kbd "C-m") 'recently-show-find-file)
  1889. ;; (define-key map (kbd "SPC") 'recently-show-find-file)
  1890. ;; (define-key map "v" 'recently-show-view-file)
  1891. ;; (define-key map "@" 'recently-show-dired)
  1892. ;; (define-key map "q" 'recently-show-close)
  1893. ;; (define-key map (kbd "C-g") 'recently-show-close)
  1894. ;; (define-key map "?" 'describe-mode)
  1895. ;; (define-key map "/" 'isearch-forward)
  1896. ;; map))
  1897. ;; (defvar recently-show-before-listing-hook nil
  1898. ;; "Hook run before creating buffer of `recently-show'.")
  1899. (defvar recently-show-window-configuration nil
  1900. "Used for internal")
  1901. (defvar recently-show-abbreviate t
  1902. "Non-nil means use `abbreviate-file-name' when listing recently opened files.")
  1903. ;; (define-derived-mode recently-show-mode special-mode "recently-show"
  1904. ;; "Major mode for `recently-show'."
  1905. ;; ;; (set (make-local-variable 'scroll-margin)
  1906. ;; ;; 0)
  1907. ;; )
  1908. ;;;###autoload
  1909. (defun recently-show (&optional files buffer-name)
  1910. "Show simplified list of recently opened files.
  1911. If optional argument FILES is non-nil, it is a list of recently-opened
  1912. files to choose from. It defaults to the whole recent list.
  1913. If optional argument BUFFER-NAME is non-nil, it is a buffer name to
  1914. use for the buffer. It defaults to \"*recetf-show*\"."
  1915. (interactive)
  1916. (let ((bf (recently-show--create-buffer-tabulated files buffer-name)))
  1917. (if bf
  1918. (progn
  1919. ;; (recently-save-list)
  1920. (setq recently-show-window-configuration (current-window-configuration))
  1921. ;;(pop-to-buffer bf t t)
  1922. (display-buffer bf)
  1923. ;; (set-window-text-height (selected-window)
  1924. ;; recently-show-window-height)
  1925. ;; (shrink-window-if-larger-than-buffer (selected-window))
  1926. )
  1927. (message "No recent file!"))))
  1928. (defun recently-show--create-buffer-tabulated (&optional files buffer-name)
  1929. "Create buffer listing recently files FILES."
  1930. (let ((bname (or buffer-name
  1931. "*recently-show-tabulated*"))
  1932. (list (or files
  1933. (progn
  1934. (recently-reload)
  1935. recently-list))))
  1936. (when list
  1937. (when (get-buffer bname)
  1938. (kill-buffer bname))
  1939. (with-current-buffer (get-buffer-create bname)
  1940. ;; (setq tabulated-list-sort-key (cons "Name" nil))
  1941. (setq tabulated-list-entries
  1942. (mapcar (lambda (f)
  1943. (list f
  1944. (vector (file-name-nondirectory f)
  1945. (if recently-show-abbreviate
  1946. (abbreviate-file-name f)
  1947. f))))
  1948. ;; list
  1949. recently-list
  1950. ))
  1951. (let ((max
  1952. (apply 'max
  1953. (mapcar (lambda (l)
  1954. (length (elt (cadr l) 0)))
  1955. tabulated-list-entries))))
  1956. (setq tabulated-list-format
  1957. `[("Name"
  1958. ,(min max
  1959. 30)
  1960. t)
  1961. ("Full Path" 0 t)])
  1962. )
  1963. (recently-show-tabulated-mode)
  1964. (current-buffer)))))
  1965. (defun recently-show-tabulated-find-file ()
  1966. "Find-file in `recently-show-tabulated-mode'."
  1967. (interactive)
  1968. (let ((f (tabulated-list-get-id)))
  1969. (when f
  1970. (recently-show-tabulated-close)
  1971. (find-file f))))
  1972. (defvar recently-show-tabulated-mode-map
  1973. (let ((map (make-sparse-keymap)))
  1974. (suppress-keymap map)
  1975. (define-key map (kbd "C-m") 'recently-show-tabulated-find-file)
  1976. ;; TODO: implement
  1977. (define-key map "v" 'recently-show-tabulated-view-file)
  1978. ;; TODO: implement
  1979. (define-key map "@" 'recently-show-tabulated-dired)
  1980. (define-key map (kbd "C-g") 'recently-show-tabulated-close)
  1981. (define-key map "/" 'isearch-forward)
  1982. map))
  1983. (define-derived-mode recently-show-tabulated-mode tabulated-list-mode "Recently Show"
  1984. "Major mode for browsing recently opened files and directories."
  1985. (setq tabulated-list-padding 2)
  1986. ;; TODO: Implement revert
  1987. ;; (add-hook 'tabulated-list-revert-hook 'recently-reload nil t)
  1988. (tabulated-list-init-header)
  1989. (tabulated-list-print nil nil))
  1990. ;; (defun recently-show-create-buffer (&optional files buffer-name)
  1991. ;; "Create buffer listing recently files."
  1992. ;; (let ((bname (or buffer-name
  1993. ;; "*recently-show*"))
  1994. ;; (list (or files
  1995. ;; (progn
  1996. ;; (recently-reload)
  1997. ;; recently-list))))
  1998. ;; (when list
  1999. ;; (and (get-buffer bname)
  2000. ;; (kill-buffer bname))
  2001. ;; (let ((bf (get-buffer-create bname)))
  2002. ;; (with-current-buffer bf
  2003. ;; (recently-show-mode)
  2004. ;; (let ((inhibit-read-only t))
  2005. ;; (mapc (lambda (f)
  2006. ;; (insert (if recently-show-abbreviate
  2007. ;; (abbreviate-file-name f)
  2008. ;; f)
  2009. ;; "\n"))
  2010. ;; list))
  2011. ;; (goto-char (point-min))
  2012. ;; ;; (setq buffer-read-only t)
  2013. ;; )
  2014. ;; bf))))
  2015. ;; (defun recently-show-close ()
  2016. ;; "Close recently-show window."
  2017. ;; (interactive)
  2018. ;; (kill-buffer (current-buffer))
  2019. ;; (set-window-configuration recently-show-window-configuration))
  2020. (defun recently-show-tabulated-close ()
  2021. "Close recently-show window."
  2022. (interactive)
  2023. (kill-buffer (current-buffer))
  2024. (set-window-configuration recently-show-window-configuration))
  2025. ;; (defun recently-show-find-file ()
  2026. ;; "Fine file of current line."
  2027. ;; (interactive)
  2028. ;; (let ((f (recently-show-get-filename)))
  2029. ;; (recently-show-close)
  2030. ;; (find-file f)))
  2031. ;; (defun recently-show-view-file ()
  2032. ;; "view file of current line."
  2033. ;; (interactive)
  2034. ;; (let ((f (recently-show-get-filename)))
  2035. ;; (recently-show-close)
  2036. ;; (view-file f)))
  2037. ;; (defun recently-show-get-filename ()
  2038. ;; "Get filename of current line."
  2039. ;; (buffer-substring-no-properties (point-at-bol)
  2040. ;; (point-at-eol)))
  2041. ;; (defun recently-show-dired()
  2042. ;; "Open dired buffer of directory containing file of current line."
  2043. ;; (interactive)
  2044. ;; (let ((f (recently-show-get-filename)))
  2045. ;; (recently-show-close)
  2046. ;; (dired (if (file-directory-p f)
  2047. ;; f
  2048. ;; (or (file-name-directory f)
  2049. ;; ".")))))
  2050. (define-key ctl-x-map (kbd "C-r") 'recently-show)
  2051. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2052. ;; git walktree
  2053. (defgroup git-walktree nil
  2054. "Git Walktree."
  2055. :tag "GitWalktree"
  2056. :prefix "git-walktree-"
  2057. :group 'tools)
  2058. (defvar git-walktree-current-committish nil
  2059. "Committish name of currently browsing.")
  2060. (make-variable-buffer-local 'git-walktree-current-committish)
  2061. (defvar git-walktree-current-path nil
  2062. "Path name currently visiting without leading and trailing slash.
  2063. This path is always relative to repository root.")
  2064. (make-variable-buffer-local 'git-walktree-current-path)
  2065. (defvar git-walktree-buffer-file-name nil
  2066. "Psudo filename of current buffer.")
  2067. (make-variable-buffer-local 'git-walktree-buffer-file-name)
  2068. (defvar git-walktree-object-full-sha1 nil
  2069. "Object name in full sha1 format of current buffer.")
  2070. (make-variable-buffer-local 'git-walktree-object-full-sha1)
  2071. (defvar git-walktree-repository-root nil
  2072. "Repository root path of current buffer.")
  2073. (make-variable-buffer-local 'git-walktree-repository-root)
  2074. (put 'git-walktree-repository-root
  2075. 'permanent-local
  2076. t)
  2077. (defun git-walktree--committish-fordisplay (committish)
  2078. "Convert COMMITTISH and return is a suitable format for displaying."
  2079. (if (and committish
  2080. (string-match-p "\\`[0-9a-f]+\\'"
  2081. committish)
  2082. (>= (length committish) 32))
  2083. (git-walktree--git-plumbing "rev-parse"
  2084. "--short"
  2085. committish)
  2086. committish))
  2087. (defun git-walktree--create-buffer (committish name type)
  2088. "Create and return buffer for COMMITTISH:NAME.
  2089. TYPE is target object type."
  2090. (let* ((root (git-walktree--git-plumbing "rev-parse"
  2091. "--show-toplevel"))
  2092. (committish-display (git-walktree--committish-fordisplay committish))
  2093. (name (format "%s:%s"
  2094. (or committish-display "")
  2095. name)))
  2096. (if (and git-walktree-reuse-tree-buffer
  2097. (string= type "tree"))
  2098. (with-current-buffer (or git-walktree-tree-buffer-for-reuse
  2099. (setq git-walktree-tree-buffer-for-reuse
  2100. (generate-new-buffer "gitwalktreebuf")))
  2101. (setq git-walktree-repository-root root)
  2102. (rename-buffer name t)
  2103. (current-buffer))
  2104. (with-current-buffer (get-buffer-create name)
  2105. (if git-walktree-repository-root
  2106. (if (string= root
  2107. git-walktree-repository-root)
  2108. (current-buffer)
  2109. ;; If the buffer is for another repository, create new buffer
  2110. (with-current-buffer (generate-new-buffer name)
  2111. (setq git-walktree-repository-root root)
  2112. (current-buffer)))
  2113. ;; New buffer
  2114. (setq git-walktree-repository-root root)
  2115. (current-buffer))))))
  2116. (defun git-walktree--replace-into-buffer (target)
  2117. "Replace TARGET buffer contents with that of current buffer.
  2118. It also copy text overlays."
  2119. (let ((src (current-buffer)))
  2120. (with-current-buffer target
  2121. (replace-buffer-contents src)))
  2122. ;; Copy color overlays
  2123. (let ((overlays (overlays-in (point-min) (point-max))))
  2124. (dolist (o overlays)
  2125. (let ((beg (overlay-start o))
  2126. (end (overlay-end o)))
  2127. (move-overlay (copy-overlay o)
  2128. beg
  2129. end
  2130. target)))))
  2131. (require 'ansi-color)
  2132. (defun git-walktree--open-treeish (committish path treeish)
  2133. "Open git tree buffer of COMMITISH:PATH.
  2134. TREEISH should be a tree-ish object full-sha1 of COMMITISH:PATH."
  2135. (cl-assert path)
  2136. (cl-assert treeish)
  2137. (let* (point-tree-start
  2138. (type (git-walktree--git-plumbing "cat-file"
  2139. "-t"
  2140. treeish))
  2141. (buf (git-walktree--create-buffer committish path type))
  2142. )
  2143. (cl-assert (member type
  2144. '("commit" "tree")))
  2145. (with-current-buffer buf
  2146. (unless (and (string= treeish
  2147. git-walktree-object-full-sha1)
  2148. (or (eq committish
  2149. git-walktree-current-committish)
  2150. (string= committish
  2151. git-walktree-current-committish)))
  2152. (buffer-disable-undo)
  2153. ;; For running git command go back to repository root
  2154. (cd git-walktree-repository-root)
  2155. (save-excursion
  2156. (let ((inhibit-read-only t))
  2157. ;; Remove existing overlays generated by ansi-color-apply-on-region
  2158. (remove-overlays)
  2159. (with-temp-buffer
  2160. (if committish
  2161. (progn (git-walktree--call-process nil
  2162. "show"
  2163. ;; TODO: Make this args configurable
  2164. ;; "--no-patch"
  2165. "--color=always"
  2166. "--pretty=short"
  2167. "--decorate"
  2168. "--stat"
  2169. committish)
  2170. (ansi-color-apply-on-region (point-min)
  2171. (point))
  2172. (insert "\n")
  2173. (insert (format "Contents of '%s:%s':\n"
  2174. (git-walktree--committish-fordisplay committish)
  2175. path)))
  2176. (insert (format "Contents of treeish object '%s:\n"
  2177. treeish)))
  2178. (setq point-tree-start (point))
  2179. (git-walktree--call-process nil
  2180. "ls-tree"
  2181. ;; "-r"
  2182. "--abbrev"
  2183. treeish)
  2184. (git-walktree--replace-into-buffer buf))
  2185. ))
  2186. (git-walktree-mode)
  2187. (set-buffer-modified-p nil)
  2188. (setq git-walktree-current-committish committish)
  2189. (setq git-walktree-current-path path)
  2190. (setq git-walktree-object-full-sha1 treeish)
  2191. (let ((dir (expand-file-name path git-walktree-repository-root)))
  2192. (when (and git-walktree-try-cd
  2193. (file-directory-p dir))
  2194. (cd dir)))
  2195. (when (= (point) (point-min))
  2196. (goto-char point-tree-start)
  2197. (git-walktree-mode--move-to-file)
  2198. )
  2199. ))
  2200. buf))
  2201. (defun git-walktree--call-process (&optional infile &rest args)
  2202. "Call git command with input from INFILE and args ARGS.
  2203. Result will be inserted into current buffer."
  2204. (let ((status (apply 'call-process
  2205. git-walktree-git-executable
  2206. infile
  2207. t
  2208. nil
  2209. args)))
  2210. (unless (eq 0
  2211. status)
  2212. (error "Failed to call git process %S %S"
  2213. infile
  2214. args))))
  2215. ?w
  2216. (defun git-walktree--open-blob (committish path blob)
  2217. "Open blob object of COMMITISH:PATH.
  2218. BLOB should be a object full sha1 of COMMITISH:PATH."
  2219. (cl-assert committish)
  2220. (cl-assert path)
  2221. (cl-assert blob)
  2222. (let* ((type (git-walktree--git-plumbing "cat-file"
  2223. "-t"
  2224. blob))
  2225. (buf (git-walktree--create-buffer committish path type)))
  2226. (cl-assert (string= type "blob"))
  2227. (with-current-buffer buf
  2228. (unless (string= blob
  2229. git-walktree-object-full-sha1)
  2230. ;; For running git command go back to repository root
  2231. (cd git-walktree-repository-root)
  2232. (let ((inhibit-read-only t))
  2233. (with-temp-buffer
  2234. (git-walktree--call-process nil
  2235. "cat-file"
  2236. "-p"
  2237. blob)
  2238. (git-walktree--replace-into-buffer buf)))
  2239. (setq git-walktree-buffer-file-name
  2240. (concat git-walktree-repository-root "/git@" committish ":" path))
  2241. (setq buffer-file-name
  2242. (concat git-walktree-repository-root "/" path))
  2243. (normal-mode t)
  2244. ;; For asking filename when C-xC-s
  2245. (setq buffer-file-name nil)
  2246. (set-buffer-modified-p t)
  2247. (setq git-walktree-current-committish committish)
  2248. (setq git-walktree-current-path path)
  2249. (setq git-walktree-object-full-sha1 blob)
  2250. (let ((dir (expand-file-name (or (file-name-directory path)
  2251. ".")
  2252. git-walktree-repository-root)))
  2253. (when (and git-walktree-try-cd
  2254. (file-directory-p dir))
  2255. (cd dir)))
  2256. (view-mode 1)
  2257. ))
  2258. buf))
  2259. (defun git-walktree--open-noselect-safe-path (committish &optional path)
  2260. "Open git object of COMMITTISH:PATH.
  2261. If PATH not found in COMMITTISH tree, go up path and try again until found.
  2262. When PATH is omitted or nil, it is calculated from current file or directory."
  2263. (cl-assert committish)
  2264. (let ((type (git-walktree--git-plumbing "cat-file"
  2265. "-t"
  2266. committish)))
  2267. (cl-assert (string= type "commit")))
  2268. (setq path
  2269. (or path
  2270. (git-walktree--path-in-repository (or buffer-file-name
  2271. default-directory))))
  2272. ;; PATH must not start with and end with slashes
  2273. (cl-assert (not (string-match-p "\\`/" path)))
  2274. (cl-assert (not (string-match-p "/\\'" path)))
  2275. (let ((obj (git-walktree--resolve-object committish path)))
  2276. (while (not obj)
  2277. (setq path
  2278. (git-walktree--parent-directory path))
  2279. (setq obj
  2280. (git-walktree--resolve-object committish path)))
  2281. (git-walktree--open-noselect committish
  2282. path
  2283. obj)))
  2284. ;; TODO: Store view history
  2285. (defun git-walktree--open-noselect (committish path object)
  2286. "Open buffer to view git object of COMMITTISH:PATH.
  2287. When PATH was given and non-nil open that, otherwise open root tree.
  2288. When OBJECT was given and non-nil, assume that is the object full sha1 of
  2289. COMMITTISH:PATH without checking it."
  2290. (cl-assert committish)
  2291. (let ((type (git-walktree--git-plumbing "cat-file"
  2292. "-t"
  2293. committish)))
  2294. (cl-assert (string= type "commit")))
  2295. (setq path (or path
  2296. "."))
  2297. ;; PATH must not start with and end with slashes
  2298. (cl-assert (not (string-match-p "\\`/" path)))
  2299. (cl-assert (not (string-match-p "/\\'" path)))
  2300. (setq object (or object
  2301. (git-walktree--resolve-object committish path)))
  2302. (setq object (git-walktree--git-plumbing "rev-parse"
  2303. object))
  2304. (cl-assert object)
  2305. (let ((type (git-walktree--git-plumbing "cat-file"
  2306. "-t"
  2307. object)))
  2308. (pcase type
  2309. ((or "commit" "tree")
  2310. (git-walktree--open-treeish committish path object))
  2311. ("blob"
  2312. (git-walktree--open-blob committish path object))
  2313. (_
  2314. (error "Type cannot handle: %s" type)))))
  2315. (defun git-walktree--resolve-object (committish path)
  2316. "Return object full sha1 name of COMMITISIH:PATH.
  2317. If path is equal to \".\" return COMMITTISH's root tree object.
  2318. PATH will be always treated as relative to repository root."
  2319. (cl-assert committish)
  2320. (cl-assert path)
  2321. (cl-assert (not (string-match-p "\\`/" path)))
  2322. (cl-assert (not (string-match-p "/\\'" path)))
  2323. (if (string= path ".")
  2324. (git-walktree--git-plumbing "show"
  2325. "--no-patch"
  2326. "--pretty=format:%T"
  2327. committish)
  2328. (let ((info (git-walktree--parse-lstree-line (git-walktree--git-plumbing "ls-tree"
  2329. "--full-tree"
  2330. committish
  2331. path))))
  2332. (plist-get info :object))))
  2333. (defun git-walktree-open (committish &optional path)
  2334. "Open git tree buffer of COMMITTISH.
  2335. When PATH was given and non-nil open that, otherwise try to open current path.
  2336. If target path is not found in COMMITISH tree, go up path and try again until found."
  2337. ;; TODO: Add fallback method for cases where magit is not available
  2338. (interactive (list (magit-read-branch-or-commit "Revision: ")))
  2339. (switch-to-buffer (git-walktree--open-noselect-safe-path committish path)))
  2340. (defalias 'git-walktree 'git-walktree-open)
  2341. (defun git-walktree--path-in-repository (path)
  2342. "Convert PATH into relative path to repository root.
  2343. Result will not have leading and trailing slashes."
  2344. (with-temp-buffer
  2345. (cd (if (file-directory-p path)
  2346. path
  2347. (file-name-directory path)))
  2348. (let ((root (git-walktree--git-plumbing "rev-parse"
  2349. "--show-toplevel")))
  2350. (file-relative-name (directory-file-name path)
  2351. root))))
  2352. (defcustom git-walktree-git-executable "git"
  2353. "Git executable."
  2354. :type 'string
  2355. :group 'git-walktree)
  2356. (defcustom git-walktree-try-cd t
  2357. "Try to cd if directory exists in current working directory if non-nil.
  2358. Otherwise use repository root for gitwalktree buffer's `default-directory'."
  2359. :type 'boolean
  2360. :group 'git-walktree)
  2361. (defcustom git-walktree-reuse-tree-buffer t
  2362. "Non-nil to reuse buffer for treeish object."
  2363. :type 'boolean
  2364. :group 'git-walktree)
  2365. (defvar git-walktree-tree-buffer-for-reuse nil
  2366. "Buffer to use when `git-walktree-reuse-tree-buffer' is non-nil.")
  2367. (defun git-walktree--git-plumbing (&rest args)
  2368. "Run git plubming command with ARGS.
  2369. Returns first line of output without newline."
  2370. (with-temp-buffer
  2371. (let ((status (apply 'call-process
  2372. git-walktree-git-executable
  2373. nil
  2374. t
  2375. nil
  2376. args)))
  2377. (unless (eq 0
  2378. status)
  2379. (error "Faild to run git %S:\n%s"
  2380. args
  2381. (buffer-substring-no-properties (point-min)
  2382. (point-max))))
  2383. (buffer-substring-no-properties (point-min)
  2384. (progn
  2385. (goto-char (point-min))
  2386. (point-at-eol))))))
  2387. (defconst git-walktree-ls-tree-line-regexp
  2388. "^\\([0-9]\\{6\\}\\) \\(\\w+\\) \\([0-9a-f]+\\)\t\\(.*\\)$"
  2389. "Regexp for one line of output of git ls-tree.")
  2390. (defconst git-walktree-ls-tree-line-tree-regexp
  2391. "^\\([0-9]\\{6\\}\\) \\(tree\\) \\([0-9a-f]+\\)\t\\(.*\\)$"
  2392. "Regexp for tree line of output of git ls-tree.")
  2393. (defconst git-walktree-ls-tree-line-commit-regexp
  2394. "^\\([0-9]\\{6\\}\\) \\(commit\\) \\([0-9a-f]+\\)\t\\(.*\\)$"
  2395. "Regexp for commit line of output of git ls-tree.")
  2396. (defun git-walktree--parse-lstree-line (str)
  2397. "Extract object info from STR.
  2398. STR should be a string like following without newline.:
  2399. 100644 blob 6fd4d58202d0b46547c6fe43de0f8c878456f966 .editorconfig
  2400. Returns property list like (:mode MODE :type TYPE :object OBJECT :file FILE)."
  2401. (let (result mode type object file)
  2402. (save-match-data
  2403. (with-temp-buffer
  2404. (insert str)
  2405. (goto-char (point-min))
  2406. (and (re-search-forward git-walktree-ls-tree-line-regexp
  2407. nil
  2408. t)
  2409. (list :mode (match-string 1)
  2410. :type (match-string 2)
  2411. :object (match-string 3)
  2412. :file (match-string 4)))))))
  2413. (defun git-walktree-mode-open-this ()
  2414. "Open git object of current line."
  2415. (interactive)
  2416. (let ((info (git-walktree--parse-lstree-line (buffer-substring-no-properties (point-at-bol)
  2417. (point-at-eol)))))
  2418. (if info
  2419. (switch-to-buffer
  2420. (if (string= (plist-get info
  2421. :type)
  2422. "commit")
  2423. ;; For submodule cd to that directory and intialize
  2424. ;; TODO: Provide way to go back to known "parent" repository
  2425. (with-temp-buffer
  2426. (cd (plist-get info :file))
  2427. (git-walktree--open-noselect (plist-get info
  2428. :object)
  2429. nil
  2430. (plist-get info
  2431. :object)))
  2432. (git-walktree--open-noselect git-walktree-current-committish
  2433. (git-walktree--join-path (plist-get info
  2434. :file))
  2435. (plist-get info
  2436. :object))))
  2437. (message "No object on current line."))))
  2438. (defun git-walktree--join-path (name &optional base)
  2439. "Make path from NAME and BASE.
  2440. If base is omitted or nil use value of `git-walktree-current-path'."
  2441. (setq base (or base
  2442. git-walktree-current-path))
  2443. (cl-assert base)
  2444. (if (string= base ".")
  2445. name
  2446. (concat base "/" name)))
  2447. (defun git-walktree--parent-directory (path)
  2448. "Return parent directory of PATH without trailing slash.
  2449. For root directory return \".\".
  2450. If PATH is equal to \".\", return nil."
  2451. (if (string-match-p "/" path)
  2452. (directory-file-name (file-name-directory path))
  2453. (if (string= "." path)
  2454. nil
  2455. ".")))
  2456. (defun git-walktree-up (&optional committish path)
  2457. "Open parent directory of COMMITTISH and PATH.
  2458. If not given, value of current buffer will be used."
  2459. (interactive)
  2460. (setq committish
  2461. (or committish git-walktree-current-committish))
  2462. (setq path
  2463. (or path git-walktree-current-path))
  2464. (let ((parent (git-walktree--parent-directory path)))
  2465. (if parent
  2466. (switch-to-buffer (git-walktree--open-noselect committish
  2467. parent
  2468. nil))
  2469. (message "Cannot find parent directory for current tree."))))
  2470. (defun git-walktree-mode--move-to-file ()
  2471. "Move point to file field of ls-tree output in current line.
  2472. This function do nothing when current line is not ls-tree output."
  2473. (interactive)
  2474. (save-match-data
  2475. (when (save-excursion
  2476. (goto-char (point-at-bol))
  2477. (re-search-forward git-walktree-ls-tree-line-regexp
  2478. (point-at-eol) t))
  2479. (goto-char (match-beginning 4)))))
  2480. (defun git-walktree-mode-next-line (&optional arg try-vscroll)
  2481. "Move cursor vertically down ARG lines and move to file field if found."
  2482. (interactive "^p\np")
  2483. (or arg (setq arg 1))
  2484. (line-move arg nil nil try-vscroll)
  2485. (git-walktree-mode--move-to-file)
  2486. )
  2487. (defun git-walktree-mode-previous-line (&optional arg try-vscroll)
  2488. "Move cursor vertically up ARG lines and move to file field if found."
  2489. (interactive "^p\np")
  2490. (or arg (setq arg 1))
  2491. (line-move (- arg) nil nil try-vscroll)
  2492. (git-walktree-mode--move-to-file)
  2493. )
  2494. (defgroup git-walktree-faces nil
  2495. "Faces used by git-walktree."
  2496. :group 'git-walktree
  2497. :group 'faces)
  2498. (defface git-walktree-tree-face
  2499. ;; Same as dired-directory
  2500. '((t (:inherit font-lock-function-name-face)))
  2501. "Face used for tree objects."
  2502. :group 'git-walktree-faces)
  2503. (defface git-walktree-commit-face
  2504. ;; Same as dired-symlink face
  2505. '((t (:inherit font-lock-keyword-face)))
  2506. "Face used for commit objects."
  2507. :group 'git-walktree-faces)
  2508. (defvar git-walktree-known-child-revisions (make-hash-table :test 'equal)
  2509. "Hash of already known pair of commitid -> list of child commitid.
  2510. Both values should be object full sha1 names.")
  2511. (defun git-walktree--put-child (parent child)
  2512. "Register PARENT and CHILD relationship.
  2513. PARENT should be a full sha1 object name."
  2514. ;; Any way to check if PARENT is a full SHA-1 object name?
  2515. (let ((current (gethash parent git-walktree-known-child-revisions)))
  2516. (unless (member child current)
  2517. (puthash parent
  2518. (cons child
  2519. current)
  2520. git-walktree-known-child-revisions))))
  2521. ;; TODO: Add aggressive search mode
  2522. ;; https://stackoverflow.com/a/9870218
  2523. ;; git log --reverse --pretty=format:%H -n 1 --ancestry-path <PARENT>..HEAD
  2524. (defun git-walktree--get-children (parent)
  2525. "Get known children list of PARENT commit.
  2526. PARENT should be a full sha1 object name."
  2527. (gethash parent git-walktree-known-child-revisions))
  2528. (defun git-walktree--choose-committish (prompt-format collection)
  2529. "Emit PROMPT-FORMAT and ask user to which committish of COLLECTION to use.
  2530. When collection has just one element, return the first element without asking."
  2531. (cl-assert collection)
  2532. (if (< (length collection) 2)
  2533. (car collection)
  2534. (completing-read (format prompt-format
  2535. (mapconcat 'git-walktree--committish-fordisplay
  2536. collection
  2537. " "))
  2538. collection
  2539. nil
  2540. t)))
  2541. (defun git-walktree-parent-revision ()
  2542. "Open parent revision of current path.
  2543. If current path was not found in the parent revision try to go up path."
  2544. (interactive)
  2545. (cl-assert git-walktree-current-committish)
  2546. (let* ((commit-full-sha1 (git-walktree--git-plumbing "rev-parse"
  2547. git-walktree-current-committish))
  2548. (parents (git-walktree--parent-full-sha1 commit-full-sha1)))
  2549. (dolist (parent parents)
  2550. (git-walktree--put-child parent
  2551. commit-full-sha1))
  2552. (if (< (length parents)
  2553. 1)
  2554. (message "This revision has no parent revision")
  2555. (let* ((parent (git-walktree--choose-committish "This revision has multiple parents. Which to open? (%s) "
  2556. parents))
  2557. (path git-walktree-current-path))
  2558. (cl-assert path)
  2559. (switch-to-buffer (git-walktree--open-noselect-safe-path parent
  2560. path))))))
  2561. (defun git-walktree--parent-full-sha1 (committish)
  2562. "Return list of parent commits of COMMITTISH in sha1 string."
  2563. (let ((type (git-walktree--git-plumbing "cat-file"
  2564. "-t"
  2565. committish)))
  2566. (cl-assert (string= type "commit")))
  2567. (let ((parents (git-walktree--git-plumbing "show"
  2568. "--no-patch"
  2569. "--pretty=format:%P"
  2570. committish)))
  2571. (split-string parents)))
  2572. (defun git-walktree-known-child-revision ()
  2573. "Open known revision of current path."
  2574. (interactive)
  2575. (let* ((commit-full-sha1 (git-walktree--git-plumbing "rev-parse"
  2576. git-walktree-current-committish))
  2577. (children (git-walktree--get-children commit-full-sha1)))
  2578. (if (< (length children)
  2579. 1)
  2580. (message "There are no known child revision")
  2581. (let* ((child (git-walktree--choose-committish "There are multiple known childrens. Which to open? (%s)"
  2582. children))
  2583. (path git-walktree-current-path))
  2584. (cl-assert path)
  2585. (switch-to-buffer (git-walktree--open-noselect-safe-path child
  2586. path))))))
  2587. (defvar git-walktree-mode-map
  2588. (let ((map (make-sparse-keymap)))
  2589. (define-key map "n" 'git-walktree-mode-next-line)
  2590. (define-key map "p" 'git-walktree-mode-previous-line)
  2591. (define-key map (kbd "C-n") 'git-walktree-mode-next-line)
  2592. (define-key map (kbd "C-p") 'git-walktree-mode-previous-line)
  2593. ;; TODO: Review keybind
  2594. (define-key map "P" 'git-walktree-parent-revision)
  2595. (define-key map "N" 'git-walktree-known-child-revision)
  2596. (define-key map "^" 'git-walktree-up)
  2597. ;; TODO: implement
  2598. (define-key map (kbd "DEL") 'git-walktree-back)
  2599. (define-key map (kbd "C-m") 'git-walktree-mode-open-this)
  2600. map))
  2601. (defvar git-walktree-mode-font-lock-keywords
  2602. `(
  2603. (,git-walktree-ls-tree-line-regexp . (
  2604. (1 'shadow)
  2605. (3 'shadow)
  2606. ))
  2607. (,git-walktree-ls-tree-line-tree-regexp . (
  2608. (2 'git-walktree-tree-face)
  2609. (4 'git-walktree-tree-face)
  2610. ))
  2611. (,git-walktree-ls-tree-line-commit-regexp . (
  2612. (2 'git-walktree-commit-face)
  2613. (4 'git-walktree-commit-face)
  2614. ))
  2615. )
  2616. "Syntax highlighting for git-walktree mode.")
  2617. (define-derived-mode git-walktree-mode special-mode "GitWalktree"
  2618. "Major-mode for `git-walktree-open'."
  2619. (set (make-local-variable 'font-lock-defaults)
  2620. '(git-walktree-mode-font-lock-keywords
  2621. nil nil nil nil
  2622. ))
  2623. )
  2624. (require 'magit nil t)
  2625. ;; (git-revision--git-plumbing "cat-file" "-t" "HEAD")
  2626. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2627. ;; git-worktree
  2628. (defun git-worktree-get-current-list ()
  2629. "Get current worktree list."
  2630. (with-temp-buffer
  2631. (let ((trees nil)
  2632. (status (call-process "git"
  2633. nil
  2634. t
  2635. nil
  2636. "worktree" "list" "--porcelain")))
  2637. (cl-assert (eq status 0))
  2638. (goto-char (point-min))
  2639. (save-match-data
  2640. (while (not (eq (point) (point-max)))
  2641. (let ((worktree nil)
  2642. (head nil)
  2643. (branch nil))
  2644. (while (re-search-forward "^\\([^ ]+\\) \\(.*\\)$" (point-at-eol) t)
  2645. (pcase (match-string 1)
  2646. ("worktree" (setq worktree (match-string 2)))
  2647. ("HEAD" (setq head (match-string 2)))
  2648. ("branch" (setq branch (match-string 2))))
  2649. (forward-line 1)
  2650. (goto-char (point-at-bol)))
  2651. (setq trees `(,@trees
  2652. (:worktree ,worktree :head ,head :branch ,branch)))
  2653. (forward-line 1)
  2654. (goto-char (point-at-bol)))
  2655. ))
  2656. trees)))
  2657. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2658. ;; j2-mode jinja2-mmm-mode?
  2659. (define-derived-mode jinja2-mmm-mode prog-mode
  2660. "Jinja2 MMM"
  2661. "Major mode to setup mmm-mode for jinja2 files."
  2662. (require 'mmm-mode)
  2663. (require 'mmm-jinja2)
  2664. (let* ((origname buffer-file-name)
  2665. (withoutj2 buffer-file-name)
  2666. (withoutj2 (replace-regexp-in-string "\\.j2\\'"
  2667. ""
  2668. withoutj2))
  2669. (withoutj2 (replace-regexp-in-string "\\.j2\\'"
  2670. ""
  2671. withoutj2)))
  2672. (unwind-protect
  2673. (progn
  2674. (let ((mode (assoc-default withoutj2
  2675. auto-mode-alist
  2676. 'string-match)))
  2677. (when mode
  2678. (funcall mode)))
  2679. (add-to-list 'mmm-classes
  2680. 'jinja2)
  2681. (mmm-mode-on))
  2682. (setq buffer-file-name origname))))
  2683. ;; (add-to-list 'auto-mode-alist
  2684. ;; '("\\.j2\\'" . jinja2-mmm-mode))
  2685. ;; Local Variables:
  2686. ;; flycheck-disabled-checkers: (emacs-lisp-checkdoc)
  2687. ;; flycheck-checker: emacs-lisp
  2688. ;; End:
  2689. ;;; emancs.el ends here