Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

3036 строки
104 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. (when (autoload-eval-lazily 'json-mode)
  1057. (add-to-list 'auto-mode-alist
  1058. '("/Pipfile\\.lock\\'" . json-mode)))
  1059. (add-hook 'go-mode-hook
  1060. (lambda()
  1061. (defvar go-mode-map)
  1062. (add-hook 'before-save-hook' 'gofmt-before-save)
  1063. (define-key go-mode-map (kbd "M-.") 'godef-jump)))
  1064. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1065. ;; buffers
  1066. (defvar bs-configurations)
  1067. (when (autoload-eval-lazily 'bs '(bs-show)
  1068. (add-to-list 'bs-configurations
  1069. '("specials" "^\\*" nil ".*" nil nil))
  1070. (add-to-list 'bs-configurations
  1071. '("files-and-specials" "^\\*" buffer-file-name ".*" nil nil))
  1072. (defvar bs-mode-map)
  1073. (defvar bs-current-configuration)
  1074. (define-key bs-mode-map (kbd "t")
  1075. ;; TODO: fix toggle feature
  1076. (lambda ()
  1077. (interactive)
  1078. (if (string= "specials"
  1079. bs-current-configuration)
  1080. (bs-set-configuration "files")
  1081. (bs-set-configuration "specials"))
  1082. (bs-refresh)
  1083. (bs-message-without-log "%s"
  1084. (bs--current-config-message))))
  1085. ;; (setq bs-configurations (list
  1086. ;; '("processes" nil get-buffer-process ".*" nil nil)
  1087. ;; '("files-and-scratch" "^\\*scratch\\*$" nil nil
  1088. ;; bs-visits-non-file bs-sort-buffer-interns-are-last)))
  1089. )
  1090. (defalias 'list-buffers 'bs-show)
  1091. (set-variable 'bs-default-configuration "files-and-specials")
  1092. (set-variable 'bs-default-sort-name "by nothing")
  1093. (add-hook 'bs-mode-hook
  1094. (lambda ()
  1095. (set (make-local-variable 'scroll-margin) 0))))
  1096. ;;(iswitchb-mode 1)
  1097. (icomplete-mode)
  1098. (defun iswitchb-buffer-display-other-window ()
  1099. "Do iswitchb in other window."
  1100. (interactive)
  1101. (let ((iswitchb-default-method 'display))
  1102. (call-interactively 'iswitchb-buffer)))
  1103. ;; buffer killing
  1104. ;; (defun my-delete-window-killing-buffer () nil)
  1105. (defun my-query-kill-current-buffer ()
  1106. "Interactively kill current buffer."
  1107. (interactive)
  1108. (if (y-or-n-p (concat "kill current buffer? :"))
  1109. (kill-buffer (current-buffer))))
  1110. ;;(global-set-key "\C-xk" 'my-query-kill-current-buffer)
  1111. (substitute-key-definition 'kill-buffer
  1112. 'my-query-kill-current-buffer
  1113. global-map)
  1114. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1115. ;; recentf-mode
  1116. (set-variable 'recentf-save-file (expand-file-name (concat user-emacs-directory
  1117. "recentf")))
  1118. (set-variable 'recentf-max-menu-items 20)
  1119. (set-variable 'recentf-max-saved-items 30)
  1120. (set-variable 'recentf-show-file-shortcuts-flag nil)
  1121. (set-variable 'recentf-auto-cleanup 3)
  1122. ;; (safe-require-or-eval 'sync-recentf)
  1123. ;; (when (safe-require-or-eval 'recentf)
  1124. ;; (add-to-list 'recentf-exclude
  1125. ;; (regexp-quote recentf-save-file))
  1126. ;; (add-to-list 'recentf-exclude
  1127. ;; (regexp-quote (expand-file-name user-emacs-directory)))
  1128. ;; (add-to-list 'recentf-exclude
  1129. ;; "/sync-recentf-marker\\'")
  1130. ;; (define-key ctl-x-map (kbd "C-r") 'recentf-open-files)
  1131. ;; (remove-hook 'find-file-hook
  1132. ;; 'recentf-track-opened-file)
  1133. ;; (defun my-recentf-load-track-save-list ()
  1134. ;; "Load current recentf list from file, track current visiting file, then save
  1135. ;; the list."
  1136. ;; (recentf-load-list)
  1137. ;; (recentf-track-opened-file)
  1138. ;; (recentf-save-list))
  1139. ;; (add-hook 'find-file-hook
  1140. ;; 'my-recentf-load-track-save-list)
  1141. ;; (add-hook 'kill-emacs-hook
  1142. ;; 'recentf-load-list)
  1143. ;; ;;(run-with-idle-timer 5 t 'recentf-save-list)
  1144. ;; ;; (add-hook 'find-file-hook
  1145. ;; ;; (lambda ()
  1146. ;; ;; (recentf-add-file default-directory)))
  1147. ;; (when (autoload-eval-lazily 'recentf-show)
  1148. ;; (define-key ctl-x-map (kbd "C-r") 'recentf-show)
  1149. ;; ;; (add-hook 'recentf-show-before-listing-hook
  1150. ;; ;; 'recentf-load-list)
  1151. ;; )
  1152. ;; (recentf-mode 1)
  1153. ;; (define-key recentf-dialog-mode-map (kbd "<up>") 'previous-line)
  1154. ;; (define-key recentf-dialog-mode-map (kbd "<down>") 'next-line)
  1155. ;; (define-key recentf-dialog-mode-map "p" 'previous-line)
  1156. ;; (define-key recentf-dialog-mode-map "n" 'next-line)
  1157. ;; (add-hook 'recentf-dialog-mode-hook
  1158. ;; (lambda ()
  1159. ;; ;; (recentf-save-list)
  1160. ;; ;; (define-key recentf-dialog-mode-map (kbd "C-x C-f")
  1161. ;; ;; 'my-recentf-cd-and-find-file)
  1162. ;; (cd "~/"))))
  1163. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1164. ;; dired
  1165. (defun my-file-head (filename &optional n)
  1166. "Return list of first N lines of file FILENAME."
  1167. ;; TODO: Fix for janapese text
  1168. ;; TODO: Fix for short text
  1169. (let ((num (or n 10))
  1170. (size 100)
  1171. (beg 0)
  1172. (end 0)
  1173. (result '())
  1174. (read -1))
  1175. (with-temp-buffer
  1176. (erase-buffer)
  1177. (while (or (<= (count-lines (point-min)
  1178. (point-max))
  1179. num)
  1180. (eq read 0))
  1181. (setq end (+ beg size))
  1182. (setq read (nth 1 (insert-file-contents-literally filename
  1183. nil
  1184. beg
  1185. end)))
  1186. (goto-char (point-max))
  1187. (setq beg (+ beg size)))
  1188. (goto-char (point-min))
  1189. (while (< (length result) num)
  1190. (let ((start (point)))
  1191. (forward-line 1)
  1192. (setq result
  1193. `(,@result ,(buffer-substring-no-properties start
  1194. (point))))))
  1195. result
  1196. ;; (buffer-substring-no-properties (point-min)
  1197. ;; (progn
  1198. ;; (forward-line num)
  1199. ;; (point)))
  1200. )))
  1201. ;; (apply 'concat (my-file-head "./shrc" 10)
  1202. (defun my-dired-echo-file-head (arg)
  1203. "Echo head of current file.
  1204. ARG is num to show, or defaults to 7."
  1205. (interactive "P")
  1206. (let ((f (dired-get-filename)))
  1207. (message "%s"
  1208. (apply 'concat
  1209. (my-file-head f
  1210. 7)))))
  1211. (defun my-dired-diff ()
  1212. "Show diff of marked file and file of current line."
  1213. (interactive)
  1214. (let ((files (dired-get-marked-files nil nil nil t)))
  1215. (if (eq (car files)
  1216. t)
  1217. (diff (cadr files) (dired-get-filename))
  1218. (message "One file must be marked!"))))
  1219. (defun dired-get-file-info ()
  1220. "Print information of current line file."
  1221. (interactive)
  1222. (let ((f (shell-quote-argument (dired-get-filename t))))
  1223. (if (file-directory-p f)
  1224. (progn
  1225. (message "Calculating disk usage...")
  1226. (shell-command (concat "du -hsD "
  1227. f)))
  1228. (shell-command (concat "file "
  1229. f)))))
  1230. (defun my-dired-scroll-up ()
  1231. "Scroll up."
  1232. (interactive)
  1233. (my-dired-previous-line (- (window-height) 1)))
  1234. (defun my-dired-scroll-down ()
  1235. "Scroll down."
  1236. (interactive)
  1237. (my-dired-next-line (- (window-height) 1)))
  1238. ;; (defun my-dired-forward-line (arg)
  1239. ;; ""
  1240. ;; (interactive "p"))
  1241. (defun my-dired-previous-line (arg)
  1242. "Move ARG lines up."
  1243. (interactive "p")
  1244. (if (> arg 0)
  1245. (progn
  1246. (if (eq (line-number-at-pos)
  1247. 1)
  1248. (goto-char (point-max))
  1249. (forward-line -1))
  1250. (my-dired-previous-line (if (or (dired-get-filename nil t)
  1251. (dired-get-subdir))
  1252. (- arg 1)
  1253. arg)))
  1254. (dired-move-to-filename)))
  1255. (defun my-dired-next-line (arg)
  1256. "Move ARG lines down."
  1257. (interactive "p")
  1258. (if (> arg 0)
  1259. (progn
  1260. (if (eq (point)
  1261. (point-max))
  1262. (goto-char (point-min))
  1263. (forward-line 1))
  1264. (my-dired-next-line (if (or (dired-get-filename nil t)
  1265. (dired-get-subdir))
  1266. (- arg 1)
  1267. arg)))
  1268. (dired-move-to-filename)))
  1269. (defun my-tramp-remote-find-file (f)
  1270. "Open F."
  1271. (interactive (list (read-file-name "My Find File Tramp: "
  1272. "/scp:"
  1273. nil ;; "/scp:"
  1274. (confirm-nonexistent-file-or-buffer))))
  1275. (find-file f))
  1276. ;;http://bach.istc.kobe-u.ac.jp/lect/tamlab/ubuntu/emacs.html
  1277. (if (eq window-system 'mac)
  1278. (setq dired-listing-switches "-lhF")
  1279. (setq dired-listing-switches "-lhF --time-style=long-iso")
  1280. )
  1281. (setq dired-listing-switches "-lhF")
  1282. ;; when using dired-find-alternate-file
  1283. ;; reuse current dired buffer for the file to open
  1284. ;; (put 'dired-find-alternate-file 'disabled nil)
  1285. (set-variable 'dired-ls-F-marks-symlinks t)
  1286. (set-variable 'ls-lisp-use-insert-directory-program nil) ; always use ls-lisp
  1287. (set-variable 'ls-lisp-dirs-first t)
  1288. (set-variable 'ls-lisp-use-localized-time-format t)
  1289. (set-variable 'ls-lisp-format-time-list
  1290. '("%Y-%m-%d %H:%M"
  1291. "%Y-%m-%d "))
  1292. (set-variable 'dired-dwim-target t)
  1293. (set-variable 'dired-isearch-filenames t)
  1294. (set-variable 'dired-hide-details-hide-symlink-targets nil)
  1295. (set-variable 'dired-hide-details-hide-information-lines nil)
  1296. (set-variable 'dired-deletion-confirmer 'y-or-n-p)
  1297. (set-variable 'dired-recursive-deletes 'always)
  1298. ;; (add-hook 'dired-after-readin-hook
  1299. ;; 'my-replace-nasi-none)
  1300. (with-eval-after-load 'dired
  1301. (safe-require-or-eval 'ls-lisp)
  1302. (defvar dired-mode-map (make-sparse-keymap))
  1303. ;; dired-do-chgrp sometimes cause system hung
  1304. (define-key dired-mode-map "G" 'ignore)
  1305. (define-key dired-mode-map "e" 'wdired-change-to-wdired-mode)
  1306. (define-key dired-mode-map "i" 'dired-get-file-info)
  1307. (define-key dired-mode-map "f" 'find-file)
  1308. (define-key dired-mode-map "!" 'shell-command)
  1309. (define-key dired-mode-map "&" 'async-shell-command)
  1310. (define-key dired-mode-map "X" 'dired-do-async-shell-command)
  1311. (define-key dired-mode-map "=" 'my-dired-diff)
  1312. (define-key dired-mode-map "B" 'gtkbm-add-current-dir)
  1313. (define-key dired-mode-map "b" 'gtkbm)
  1314. (define-key dired-mode-map "h" 'my-dired-echo-file-head)
  1315. (define-key dired-mode-map (kbd "TAB") 'other-window)
  1316. ;; (define-key dired-mode-map "P" 'my-dired-do-pack-or-unpack)
  1317. (define-key dired-mode-map "/" 'dired-isearch-filenames)
  1318. (define-key dired-mode-map (kbd "DEL") 'dired-up-directory)
  1319. (define-key dired-mode-map (kbd "C-h") 'dired-up-directory)
  1320. (substitute-key-definition 'dired-next-line
  1321. 'my-dired-next-line
  1322. dired-mode-map)
  1323. (substitute-key-definition 'dired-previous-line
  1324. 'my-dired-previous-line
  1325. dired-mode-map)
  1326. ;; (define-key dired-mode-map (kbd "C-p") 'my-dired-previous-line)
  1327. ;; (define-key dired-mode-map (kbd "p") 'my-dired-previous-line)
  1328. ;; (define-key dired-mode-map (kbd "C-n") 'my-dired-next-line)
  1329. ;; (define-key dired-mode-map (kbd "n") 'my-dired-next-line)
  1330. (define-key dired-mode-map (kbd "<left>") 'my-dired-scroll-up)
  1331. (define-key dired-mode-map (kbd "<right>") 'my-dired-scroll-down)
  1332. (define-key dired-mode-map (kbd "ESC p") 'my-dired-scroll-up)
  1333. (define-key dired-mode-map (kbd "ESC n") 'my-dired-scroll-down)
  1334. (add-hook 'dired-mode-hook
  1335. (lambda ()
  1336. (when (fboundp 'dired-hide-details-mode)
  1337. (dired-hide-details-mode t)
  1338. (local-set-key "l" 'dired-hide-details-mode))
  1339. (let ((file "._Icon\015"))
  1340. (when nil
  1341. '(file-readable-p file)
  1342. (delete-file file)))))
  1343. (when (autoload-eval-lazily 'pack '(dired-do-pack-or-unpack pack-pack))
  1344. (with-eval-after-load 'dired
  1345. (define-key dired-mode-map "P" 'pack-dired-dwim)))
  1346. (when (autoload-eval-lazily 'dired-list-all-mode)
  1347. (setq dired-listing-switches "-lhF")
  1348. (with-eval-after-load 'dired
  1349. (define-key dired-mode-map "a" 'dired-list-all-mode))))
  1350. (when (autoload-eval-lazily 'dired-filter)
  1351. (add-hook 'dired-mode-hook
  1352. 'dired-filter-mode))
  1353. ;; Currently disabled in favor of dired-from-git-ls-files
  1354. ;; (define-key ctl-x-map "f" 'find-dired)
  1355. ;; It works!
  1356. ;; (pop-to-buffer (dired-noselect '("." "shrc" "emacs.el")))
  1357. (defun my-dired-git-ls-files (args)
  1358. "Dired from git ls-files."
  1359. (interactive "sgit ls-files args: ")
  1360. (pop-to-buffer-same-window
  1361. (dired-noselect `(,default-directory
  1362. ,@(split-string (shell-command-to-string (concat "git ls-files -z " args))
  1363. "\0" t))
  1364. ""))
  1365. )
  1366. (define-key ctl-x-map (kbd "f") 'my-dired-git-ls-files)
  1367. (with-eval-after-load 'dired
  1368. (defvar dired-mode-map (make-sparse-keymap))
  1369. (define-key dired-mode-map "f" 'my-dired-git-ls-files))
  1370. ;; (define-minor-mode my-dired-glob-filter)
  1371. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1372. ;; misc funcs
  1373. (defalias 'qcalc 'quick-calc)
  1374. (defun memo (&optional dir)
  1375. "Open memo.txt in DIR."
  1376. (interactive)
  1377. (pop-to-buffer (find-file-noselect (concat (if dir
  1378. (file-name-as-directory dir)
  1379. "")
  1380. "memo.txt"))))
  1381. (set (defvar my-rgrep-alist nil
  1382. "Alist of rgrep command.
  1383. Each element is in the form like (NAME SEXP COMMAND), where SEXP returns the
  1384. condition to choose COMMAND when evaluated.")
  1385. `(
  1386. ;; ripgrep
  1387. ("rg"
  1388. (executable-find "rg")
  1389. "rg --hidden --no-heading --smart-case ")
  1390. ;; git grep
  1391. ("gitgrep"
  1392. (eq 0
  1393. (shell-command "git rev-parse --git-dir"))
  1394. "git --no-pager -c color.grep=always grep -nH -e ")
  1395. ;; sift
  1396. ("sift"
  1397. (executable-find "sift")
  1398. ("sift --binary-skip --filename --line-number --git --smart-case "))
  1399. ;; the silver searcher
  1400. ("ag"
  1401. (executable-find "ag")
  1402. "ag --nogroup --nopager --filename ")
  1403. ;; ack
  1404. ("ack"
  1405. (executable-find "ack")
  1406. "ack --nogroup --nopager --with-filename ")
  1407. ;; gnu global
  1408. ("global"
  1409. (and (require 'ggtags nil t)
  1410. (executable-find "global")
  1411. (ggtags-current-project-root))
  1412. "global --result grep ")
  1413. ;; grep
  1414. ("grep"
  1415. t
  1416. ,(concat "find . "
  1417. "-path '*/.git' -prune -o "
  1418. "-path '*/.svn' -prune -o "
  1419. "-type f -print0 | "
  1420. "xargs -0 grep -nH -e "))
  1421. )
  1422. )
  1423. (defvar my-rgrep-default nil
  1424. "Default command name for my-rgrep.")
  1425. (defun my-rgrep-grep-command (&optional name alist)
  1426. "Return recursive grep command for current directory or nil.
  1427. If NAME is given, use that without testing.
  1428. Commands are searched from ALIST."
  1429. (if alist
  1430. (if name
  1431. ;; if name is given search that from alist and return the command
  1432. (nth 2 (assoc name
  1433. alist))
  1434. ;; if name is not given try test in 1th elem
  1435. (let ((car (car alist))
  1436. (cdr (cdr alist)))
  1437. (if (eval (nth 1 car))
  1438. ;; if the condition is true return the command
  1439. (nth 2 car)
  1440. ;; try next one
  1441. (and cdr
  1442. (my-rgrep-grep-command name cdr)))))
  1443. ;; if alist is not given set default value
  1444. (my-rgrep-grep-command name my-rgrep-alist)))
  1445. (defun my-rgrep (command-args)
  1446. "My recursive grep. Run COMMAND-ARGS.
  1447. If prefix argument is given, use current symbol as default search target
  1448. and search from projectile root (if projectile is available)."
  1449. (interactive (let ((cmd (my-rgrep-grep-command my-rgrep-default
  1450. nil)))
  1451. (if cmd
  1452. (list (read-shell-command "grep command: "
  1453. (concat cmd
  1454. (if current-prefix-arg
  1455. (thing-at-point 'symbol t)
  1456. ""))
  1457. 'grep-find-history))
  1458. (error "My-Rgrep: Command for rgrep not found")
  1459. )))
  1460. (if (and current-prefix-arg
  1461. (safe-require-or-eval 'projectile)
  1462. (projectile-project-p))
  1463. (projectile-with-default-dir (projectile-project-root)
  1464. (compilation-start command-args
  1465. 'grep-mode))
  1466. (compilation-start command-args
  1467. 'grep-mode)))
  1468. (defun my-rgrep-thing-at-point-projectile-root ()
  1469. "My recursive grep to find thing at point from project root."
  1470. (interactive)
  1471. (let* ((cmd (my-rgrep-grep-command my-rgrep-default
  1472. nil))
  1473. (command-args
  1474. (if cmd
  1475. (concat cmd
  1476. (or (thing-at-point 'symbol t)
  1477. (error "No symbol at point")))
  1478. (error "My-Rgrep: Command for rgrep not found"))))
  1479. (if (safe-require-or-eval 'projectile)
  1480. (projectile-with-default-dir (or (projectile-project-root)
  1481. default-directory)
  1482. (compilation-start command-args
  1483. 'grep-mode))
  1484. (compilation-start command-args
  1485. 'grep-mode))))
  1486. (defmacro define-my-rgrep (name)
  1487. "Define rgrep for NAME."
  1488. `(defun ,(intern (concat "my-rgrep-"
  1489. name)) ()
  1490. ,(format "My recursive grep by %s."
  1491. name)
  1492. (interactive)
  1493. (let ((my-rgrep-default ,name))
  1494. (if (called-interactively-p 'any)
  1495. (call-interactively 'my-rgrep)
  1496. (error "Not intended to be called noninteractively. Use `my-rgrep'"))))
  1497. )
  1498. (define-my-rgrep "ack")
  1499. (define-my-rgrep "ag")
  1500. (define-my-rgrep "rg")
  1501. (define-my-rgrep "sift")
  1502. (define-my-rgrep "gitgrep")
  1503. (define-my-rgrep "grep")
  1504. (define-my-rgrep "global")
  1505. (define-key ctl-x-map "s" 'my-rgrep)
  1506. (define-key ctl-x-map "." 'my-rgrep-thing-at-point-projectile-root)
  1507. (defun my-occur (regexp &optional region)
  1508. "My occur command to search REGEXP."
  1509. (interactive (list (read-string "List lines matching regexp: "
  1510. (thing-at-point 'symbol t))))
  1511. (occur regexp nil region))
  1512. (define-key ctl-x-map (kbd "C-o") 'my-occur)
  1513. (set-variable 'dumb-jump-prefer-searcher 'rg)
  1514. (defalias 'make 'compile)
  1515. (define-key ctl-x-map "c" 'compile)
  1516. ;;;;;;;;;;;;;;;;;;;;;;;
  1517. ;; adoc-simple-mode
  1518. (when (safe-require-or-eval 'adoc-mode)
  1519. (defvar adoc-simple-font-lock-keywords
  1520. nil)
  1521. (define-derived-mode adoc-simple-mode adoc-mode
  1522. "Adoc-Simple"
  1523. "Major mode for editing AsciiDoc text files.
  1524. This mode is a simplified version of `adoc-mode'."
  1525. '(set (make-local-variable 'font-lock-defaults)
  1526. '(adoc-simple-font-lock-keywords
  1527. nil nil nil nil
  1528. (font-lock-multiline . t)
  1529. (font-lock-mark-block-function . adoc-font-lock-mark-block-function))))
  1530. (add-to-list 'auto-mode-alist
  1531. '("\\.adoc\\'" . adoc-simple-mode)))
  1532. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1533. ;; awk-preview
  1534. (defgroup awk-preview nil
  1535. "Awk previewer."
  1536. :tag "Awk Preview"
  1537. :prefix "awk-preview-"
  1538. :group 'tools)
  1539. (defcustom awk-preview-program
  1540. (or (executable-find "gawk")
  1541. (executable-find "awk")
  1542. "awk")
  1543. "Awk program to execute."
  1544. :type 'string
  1545. :group 'awk-preview)
  1546. (defcustom awk-preview-switches
  1547. ;; '("--sandbox")
  1548. nil
  1549. "String of awk options appended when running awk preview."
  1550. :type '(repeat string)
  1551. :group 'awk-preview)
  1552. (defcustom awk-preview-default-program
  1553. "# C-c C-l to update preview, C-c C-c to commit, C-c C-k to abort.
  1554. {
  1555. print NR, $0
  1556. }
  1557. "
  1558. "Default awk command."
  1559. :type 'string
  1560. :group 'awk-preview)
  1561. (cl-defstruct awk-preview--env
  1562. (running-p nil)
  1563. ;; Point of beg in source buffer
  1564. (point-beg nil)
  1565. ;; Point of end in source buffer
  1566. (point-end nil)
  1567. ;; Point of beginning
  1568. ;; Used by preview buffer and always same as awk-preview--point-beg
  1569. (preview-point-beg nil)
  1570. ;; Point of beginning
  1571. ;; Used by preview buffer and may defferent from awk-preview--point-end
  1572. (preview-point-end nil)
  1573. ;; Awk preview program file name
  1574. (program-filename nil)
  1575. ;; Source buffer
  1576. (source-buffer nil)
  1577. ;; Preview buffer
  1578. (preview-buffer nil)
  1579. ;; Program buffer
  1580. (program-buffer nil)
  1581. ;; Window configuration when entering awk-review
  1582. (window-configuration nil)
  1583. )
  1584. (defvar awk-preview--env nil
  1585. "`awk-preview--env' struct object of currently running.")
  1586. (make-variable-buffer-local 'awk-preview--env)
  1587. (put 'awk-preview--env
  1588. 'permanent-local
  1589. t)
  1590. (defun awk-preview--invoke-awk (buf beg end progfile output &optional delete)
  1591. "Execute PROGFILE awk process with BEG and END input and output to OUTPUT buffer.
  1592. When OUTPUT is t the result will be output to BUF.
  1593. Delete the text between BEG and END when DELETE is non-nil."
  1594. (with-current-buffer buf
  1595. (let ((status (apply 'call-process-region
  1596. beg
  1597. end
  1598. awk-preview-program
  1599. delete
  1600. output
  1601. nil
  1602. `(,@awk-preview-switches "-f" ,progfile))))
  1603. (unless (eq status
  1604. 0)
  1605. (error "awk-preview: Awk program exited abnormally."))
  1606. output)))
  1607. (defvar awk-preview-program-buffer-name
  1608. "*AWK Preview Program<%s>*"
  1609. "Buffer name of awk preview program.")
  1610. (defvar awk-preview-preview-buffer-name
  1611. "*AWK Preview<%s>*"
  1612. "Buffer name of awk preview.")
  1613. (defun awk-preview--create-program-buffer (e)
  1614. "Create program buffer for `awk-preview--env' object E."
  1615. (let ((source-name (buffer-name (awk-preview--env-source-buffer e))))
  1616. (with-current-buffer (generate-new-buffer (format awk-preview-program-buffer-name
  1617. source-name))
  1618. (erase-buffer)
  1619. (insert awk-preview-default-program)
  1620. (awk-mode)
  1621. (awk-preview-program-mode 1)
  1622. (setf (awk-preview--env-program-buffer e) (current-buffer))
  1623. (setf (awk-preview--env-program-filename e) (make-temp-file "awk-preview-"
  1624. nil
  1625. ".awk"))
  1626. (setq awk-preview--env e)
  1627. (current-buffer))))
  1628. (defun awk-preview-program-buffer-kill-hook ()
  1629. "Cleanup for awk-preview program buffer."
  1630. (when (and awk-preview--env
  1631. (awk-preview--env-program-filename awk-preview--env))
  1632. (delete-file (awk-preview--env-program-filename awk-preview--env))))
  1633. (add-hook 'kill-buffer-hook
  1634. 'awk-preview-program-buffer-kill-hook)
  1635. (defun awk-preview--create-preview-buffer (e)
  1636. "Create preview buffer for `awk-preview--env' object E."
  1637. (with-current-buffer (awk-preview--env-source-buffer e)
  1638. ;; TODO: Do not unset buffer-file-name in let
  1639. (let ((buffer-file-name nil)
  1640. (beg (awk-preview--env-point-beg e))
  1641. (end (awk-preview--env-point-end e)))
  1642. (with-current-buffer (clone-buffer (format awk-preview-preview-buffer-name
  1643. (buffer-name)))
  1644. (awk-preview-program-mode 1)
  1645. (setf (awk-preview--env-preview-point-beg e) beg)
  1646. (setf (awk-preview--env-preview-point-end e) end)
  1647. (setf (awk-preview--env-preview-buffer e) (current-buffer))
  1648. (goto-char end)
  1649. (setq buffer-read-only t)
  1650. (setq awk-preview--env e)
  1651. (current-buffer)))))
  1652. (defun awk-preview (beg end)
  1653. "Run awk and preview result."
  1654. (interactive "r")
  1655. (let ((e (make-awk-preview--env)))
  1656. (setf (awk-preview--env-point-beg e) beg)
  1657. (setf (awk-preview--env-point-end e) end)
  1658. (setf (awk-preview--env-source-buffer e) (current-buffer))
  1659. (awk-preview--create-preview-buffer e)
  1660. (awk-preview--create-program-buffer e)
  1661. (setf (awk-preview--env-window-configuration e)
  1662. (current-window-configuration))
  1663. (cl-assert (awk-preview--env-point-beg e))
  1664. (cl-assert (awk-preview--env-point-end e))
  1665. (cl-assert (awk-preview--env-preview-point-beg e))
  1666. (cl-assert (awk-preview--env-preview-point-end e))
  1667. (cl-assert (awk-preview--env-program-filename e))
  1668. (cl-assert (awk-preview--env-source-buffer e))
  1669. (cl-assert (awk-preview--env-preview-buffer e))
  1670. (cl-assert (awk-preview--env-program-buffer e))
  1671. (cl-assert (awk-preview--env-window-configuration e))
  1672. (setq awk-preview--env e)
  1673. (set-window-buffer (get-buffer-window (awk-preview--env-source-buffer e))
  1674. (awk-preview--env-preview-buffer e))
  1675. (pop-to-buffer (awk-preview--env-program-buffer e))
  1676. (switch-to-buffer (awk-preview--env-program-buffer e))
  1677. (setf (awk-preview--env-running-p e) t)
  1678. (awk-preview-update-preview)
  1679. ))
  1680. (defun awk-preview-update-preview ()
  1681. "Update awk-preview."
  1682. (interactive)
  1683. (with-current-buffer (awk-preview--env-program-buffer awk-preview--env)
  1684. (write-region (point-min)
  1685. (point-max)
  1686. (awk-preview--env-program-filename awk-preview--env)))
  1687. (let ((output (with-current-buffer (get-buffer-create " *awk-preview output*")
  1688. (erase-buffer)
  1689. (current-buffer)))
  1690. (progfile (awk-preview--env-program-filename awk-preview--env)))
  1691. (awk-preview--invoke-awk (awk-preview--env-source-buffer awk-preview--env)
  1692. (awk-preview--env-point-beg awk-preview--env)
  1693. (awk-preview--env-point-end awk-preview--env)
  1694. progfile
  1695. output)
  1696. (with-current-buffer (awk-preview--env-preview-buffer awk-preview--env)
  1697. (let ((inhibit-read-only t))
  1698. (goto-char (awk-preview--env-preview-point-end awk-preview--env))
  1699. (delete-region (awk-preview--env-preview-point-beg awk-preview--env)
  1700. (point))
  1701. (insert-buffer-substring output)
  1702. (setf (awk-preview--env-preview-point-end awk-preview--env)
  1703. (point)))
  1704. )))
  1705. (defun awk-preview-commit ()
  1706. "Exit awk-preview and update buffer."
  1707. (interactive)
  1708. ;; (with-current-buffer awk-preview--source-buffer
  1709. ;; (awk-preview--invoke-awk (current-buffer)
  1710. ;; awk-preview--point-beg
  1711. ;; awk-preview--point-end
  1712. ;; progfile
  1713. ;; t
  1714. ;; t))
  1715. (awk-preview--cleanup))
  1716. (defun awk-preview-abort ()
  1717. "Discard result and exit awk-preview."
  1718. (interactive)
  1719. (awk-preview--cleanup))
  1720. (defun awk-preview--cleanup()
  1721. "Cleanup awk preview buffers and variables.")
  1722. (defvar awk-preview-program-mode-map
  1723. (let ((map (make-sparse-keymap)))
  1724. (define-key map (kbd "C-c C-l") 'awk-preview-update-preview)
  1725. (define-key map (kbd "C-c C-k") 'awk-preview-abort)
  1726. (define-key map (kbd "C-c C-c") 'awk-preview-commit)
  1727. map)
  1728. "Keymap for `awk-preview-program-mode'.")
  1729. (define-minor-mode awk-preview-program-mode
  1730. "Minor mode for awk-preview program buffer."
  1731. :lighter " AWKPrev"
  1732. :keymap awk-preview-program-mode-map)
  1733. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1734. ;; editorconfig-auto-apply
  1735. (define-minor-mode editorconfig-auto-apply-mode
  1736. "When saving .editorconfig file update buffer configs."
  1737. :global t
  1738. :lighter ""
  1739. (if editorconfig-auto-apply-mode
  1740. (add-hook 'after-save-hook
  1741. 'editorconfig-auto-apply-mode--run)
  1742. (remove-hook 'after-save-hook
  1743. 'editorconfig-auto-apply-mode--run)))
  1744. (defun editorconfig-auto-apply-mode--run ()
  1745. "When saving .editorconfig file walk all buffers and update configs."
  1746. (when (eq major-mode
  1747. 'editorconfig-conf-mode)
  1748. (let ((dir (file-name-directory buffer-file-name)))
  1749. (cl-dolist (buf (buffer-list))
  1750. (when (and (buffer-file-name buf)
  1751. (file-in-directory-p (buffer-file-name buf)
  1752. dir))
  1753. (with-current-buffer buf
  1754. (editorconfig-mode-apply)))))))
  1755. (editorconfig-auto-apply-mode 1)
  1756. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1757. ;; recently
  1758. (defgroup recently nil
  1759. "Recently visited files"
  1760. :tag "Recently"
  1761. :prefix "recently-"
  1762. :group 'tools)
  1763. (defcustom recently-file
  1764. (concat user-emacs-directory
  1765. "recently.el")
  1766. "Recently file."
  1767. :type 'string
  1768. :group 'recently)
  1769. (defcustom recently-max
  1770. 100
  1771. "Recently list max length."
  1772. :type 'int
  1773. :group 'recently)
  1774. (defcustom recently-excludes
  1775. '()
  1776. "List of regexps for filenames excluded from the recent list."
  1777. :type '(repeat string)
  1778. :group 'recently)
  1779. (add-to-list 'recently-excludes
  1780. (eval-when-compile (rx "/COMMIT_EDITMSG" eot)))
  1781. (defvar recently-list
  1782. '()
  1783. "Recently list.")
  1784. (defvar recently-file-mtime
  1785. nil
  1786. "Modified time of file when last read file.")
  1787. (defun recently-write ()
  1788. "Write to file."
  1789. (with-temp-buffer
  1790. (prin1 recently-list
  1791. (current-buffer))
  1792. (write-region (point-min)
  1793. (point-max)
  1794. recently-file)))
  1795. (defun recently-read ()
  1796. "Read file."
  1797. (when (file-readable-p recently-file)
  1798. (with-temp-buffer
  1799. (insert-file-contents recently-file)
  1800. (goto-char (point-min))
  1801. (setq recently-list
  1802. (read (current-buffer))))
  1803. (setq recently-file-mtime
  1804. (nth 5
  1805. (file-attributes recently-file)))))
  1806. (defun recently-reload ()
  1807. "Reload file."
  1808. (when (and (file-readable-p recently-file)
  1809. (not (equal recently-file-mtime
  1810. (nth 5
  1811. (file-attributes recently-file)))))
  1812. (recently-read)
  1813. (cl-assert (equal recently-file-mtime
  1814. (nth 5
  1815. (file-attributes recently-file))))))
  1816. (defun recently-add (path)
  1817. "Add PATH to list."
  1818. (cl-assert (string= path
  1819. (expand-file-name path)))
  1820. (when (cl-loop for re in recently-excludes
  1821. if (string-match re path) return nil
  1822. finally return t)
  1823. (recently-reload)
  1824. (let* ((l (copy-list recently-list))
  1825. (l (delete path
  1826. l))
  1827. (l (cl-loop for e in l
  1828. unless (file-in-directory-p path e)
  1829. collect e))
  1830. (l (cons path
  1831. l))
  1832. (l (recently--truncate l
  1833. recently-max)))
  1834. (unless (equal recently-list
  1835. l)
  1836. (setq recently-list l)
  1837. (recently-write)))))
  1838. (defun recently--truncate (list len)
  1839. "Truncate LIST to LEN."
  1840. (if (> (length list)
  1841. len)
  1842. (cl-subseq list
  1843. 0
  1844. len)
  1845. list))
  1846. (defun recently-find-file-hook ()
  1847. "Add current file."
  1848. (when buffer-file-name
  1849. (recently-add buffer-file-name)))
  1850. (defun recently-dired-mode-hook ()
  1851. "Add current directory."
  1852. (recently-add (expand-file-name default-directory)))
  1853. (add-hook 'find-file-hook
  1854. 'recently-find-file-hook)
  1855. (add-hook 'dired-mode-hook
  1856. 'recently-dired-mode-hook)
  1857. ;;;;;;;;;;;;;;;;
  1858. ;; recently-show
  1859. (defvar recently-show-window-height 10
  1860. "Max height of window of `recently-show'")
  1861. ;; (defvar recently-show-mode-map
  1862. ;; (let ((map (make-sparse-keymap)))
  1863. ;; (suppress-keymap map)
  1864. ;; (define-key map "n" 'next-line)
  1865. ;; (define-key map "p" 'previous-line)
  1866. ;; (define-key map (kbd "C-m") 'recently-show-find-file)
  1867. ;; (define-key map (kbd "SPC") 'recently-show-find-file)
  1868. ;; (define-key map "v" 'recently-show-view-file)
  1869. ;; (define-key map "@" 'recently-show-dired)
  1870. ;; (define-key map "q" 'recently-show-close)
  1871. ;; (define-key map (kbd "C-g") 'recently-show-close)
  1872. ;; (define-key map "?" 'describe-mode)
  1873. ;; (define-key map "/" 'isearch-forward)
  1874. ;; map))
  1875. ;; (defvar recently-show-before-listing-hook nil
  1876. ;; "Hook run before creating buffer of `recently-show'.")
  1877. (defvar recently-show-window-configuration nil
  1878. "Used for internal")
  1879. (defvar recently-show-abbreviate t
  1880. "Non-nil means use `abbreviate-file-name' when listing recently opened files.")
  1881. ;; (define-derived-mode recently-show-mode special-mode "recently-show"
  1882. ;; "Major mode for `recently-show'."
  1883. ;; ;; (set (make-local-variable 'scroll-margin)
  1884. ;; ;; 0)
  1885. ;; )
  1886. ;;;###autoload
  1887. (defun recently-show (&optional files buffer-name)
  1888. "Show simplified list of recently opened files.
  1889. If optional argument FILES is non-nil, it is a list of recently-opened
  1890. files to choose from. It defaults to the whole recent list.
  1891. If optional argument BUFFER-NAME is non-nil, it is a buffer name to
  1892. use for the buffer. It defaults to \"*recetf-show*\"."
  1893. (interactive)
  1894. (let ((bf (recently-show--create-buffer-tabulated files buffer-name)))
  1895. (if bf
  1896. (progn
  1897. ;; (recently-save-list)
  1898. (setq recently-show-window-configuration (current-window-configuration))
  1899. ;;(pop-to-buffer bf t t)
  1900. (display-buffer bf)
  1901. ;; (set-window-text-height (selected-window)
  1902. ;; recently-show-window-height)
  1903. ;; (shrink-window-if-larger-than-buffer (selected-window))
  1904. )
  1905. (message "No recent file!"))))
  1906. (defun recently-show--create-buffer-tabulated (&optional files buffer-name)
  1907. "Create buffer listing recently files FILES."
  1908. (let ((bname (or buffer-name
  1909. "*recently-show-tabulated*"))
  1910. (list (or files
  1911. (progn
  1912. (recently-reload)
  1913. recently-list))))
  1914. (when list
  1915. (when (get-buffer bname)
  1916. (kill-buffer bname))
  1917. (with-current-buffer (get-buffer-create bname)
  1918. ;; (setq tabulated-list-sort-key (cons "Name" nil))
  1919. (setq tabulated-list-entries
  1920. (mapcar (lambda (f)
  1921. (list f
  1922. (vector (file-name-nondirectory f)
  1923. (if recently-show-abbreviate
  1924. (abbreviate-file-name f)
  1925. f))))
  1926. ;; list
  1927. recently-list
  1928. ))
  1929. (let ((max
  1930. (apply 'max
  1931. (mapcar (lambda (l)
  1932. (length (elt (cadr l) 0)))
  1933. tabulated-list-entries))))
  1934. (setq tabulated-list-format
  1935. `[("Name"
  1936. ,(min max
  1937. 30)
  1938. t)
  1939. ("Full Path" 0 t)])
  1940. )
  1941. (recently-show-tabulated-mode)
  1942. (current-buffer)))))
  1943. (defun recently-show-tabulated-find-file ()
  1944. "Find-file in `recently-show-tabulated-mode'."
  1945. (interactive)
  1946. (let ((f (tabulated-list-get-id)))
  1947. (when f
  1948. (recently-show-tabulated-close)
  1949. (find-file f))))
  1950. (defvar recently-show-tabulated-mode-map
  1951. (let ((map (make-sparse-keymap)))
  1952. (suppress-keymap map)
  1953. (define-key map (kbd "C-m") 'recently-show-tabulated-find-file)
  1954. ;; TODO: implement
  1955. (define-key map "v" 'recently-show-tabulated-view-file)
  1956. ;; TODO: implement
  1957. (define-key map "@" 'recently-show-tabulated-dired)
  1958. (define-key map (kbd "C-g") 'recently-show-tabulated-close)
  1959. (define-key map "/" 'isearch-forward)
  1960. map))
  1961. (define-derived-mode recently-show-tabulated-mode tabulated-list-mode "Recently Show"
  1962. "Major mode for browsing recently opened files and directories."
  1963. (setq tabulated-list-padding 2)
  1964. ;; TODO: Implement revert
  1965. ;; (add-hook 'tabulated-list-revert-hook 'recently-reload nil t)
  1966. (tabulated-list-init-header)
  1967. (tabulated-list-print nil nil))
  1968. ;; (defun recently-show-create-buffer (&optional files buffer-name)
  1969. ;; "Create buffer listing recently files."
  1970. ;; (let ((bname (or buffer-name
  1971. ;; "*recently-show*"))
  1972. ;; (list (or files
  1973. ;; (progn
  1974. ;; (recently-reload)
  1975. ;; recently-list))))
  1976. ;; (when list
  1977. ;; (and (get-buffer bname)
  1978. ;; (kill-buffer bname))
  1979. ;; (let ((bf (get-buffer-create bname)))
  1980. ;; (with-current-buffer bf
  1981. ;; (recently-show-mode)
  1982. ;; (let ((inhibit-read-only t))
  1983. ;; (mapc (lambda (f)
  1984. ;; (insert (if recently-show-abbreviate
  1985. ;; (abbreviate-file-name f)
  1986. ;; f)
  1987. ;; "\n"))
  1988. ;; list))
  1989. ;; (goto-char (point-min))
  1990. ;; ;; (setq buffer-read-only t)
  1991. ;; )
  1992. ;; bf))))
  1993. ;; (defun recently-show-close ()
  1994. ;; "Close recently-show window."
  1995. ;; (interactive)
  1996. ;; (kill-buffer (current-buffer))
  1997. ;; (set-window-configuration recently-show-window-configuration))
  1998. (defun recently-show-tabulated-close ()
  1999. "Close recently-show window."
  2000. (interactive)
  2001. (kill-buffer (current-buffer))
  2002. (set-window-configuration recently-show-window-configuration))
  2003. ;; (defun recently-show-find-file ()
  2004. ;; "Fine file of current line."
  2005. ;; (interactive)
  2006. ;; (let ((f (recently-show-get-filename)))
  2007. ;; (recently-show-close)
  2008. ;; (find-file f)))
  2009. ;; (defun recently-show-view-file ()
  2010. ;; "view file of current line."
  2011. ;; (interactive)
  2012. ;; (let ((f (recently-show-get-filename)))
  2013. ;; (recently-show-close)
  2014. ;; (view-file f)))
  2015. ;; (defun recently-show-get-filename ()
  2016. ;; "Get filename of current line."
  2017. ;; (buffer-substring-no-properties (point-at-bol)
  2018. ;; (point-at-eol)))
  2019. ;; (defun recently-show-dired()
  2020. ;; "Open dired buffer of directory containing file of current line."
  2021. ;; (interactive)
  2022. ;; (let ((f (recently-show-get-filename)))
  2023. ;; (recently-show-close)
  2024. ;; (dired (if (file-directory-p f)
  2025. ;; f
  2026. ;; (or (file-name-directory f)
  2027. ;; ".")))))
  2028. (define-key ctl-x-map (kbd "C-r") 'recently-show)
  2029. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2030. ;; git walktree
  2031. (defgroup git-walktree nil
  2032. "Git Walktree."
  2033. :tag "GitWalktree"
  2034. :prefix "git-walktree-"
  2035. :group 'tools)
  2036. (defvar git-walktree-current-committish nil
  2037. "Committish name of currently browsing.")
  2038. (make-variable-buffer-local 'git-walktree-current-committish)
  2039. (defvar git-walktree-current-path nil
  2040. "Path name currently visiting without leading and trailing slash.
  2041. This path is always relative to repository root.")
  2042. (make-variable-buffer-local 'git-walktree-current-path)
  2043. (defvar git-walktree-buffer-file-name nil
  2044. "Psudo filename of current buffer.")
  2045. (make-variable-buffer-local 'git-walktree-buffer-file-name)
  2046. (defvar git-walktree-object-full-sha1 nil
  2047. "Object name in full sha1 format of current buffer.")
  2048. (make-variable-buffer-local 'git-walktree-object-full-sha1)
  2049. (defvar git-walktree-repository-root nil
  2050. "Repository root path of current buffer.")
  2051. (make-variable-buffer-local 'git-walktree-repository-root)
  2052. (put 'git-walktree-repository-root
  2053. 'permanent-local
  2054. t)
  2055. (defun git-walktree--committish-fordisplay (committish)
  2056. "Convert COMMITTISH and return is a suitable format for displaying."
  2057. (if (and committish
  2058. (string-match-p "\\`[0-9a-f]+\\'"
  2059. committish)
  2060. (>= (length committish) 32))
  2061. (git-walktree--git-plumbing "rev-parse"
  2062. "--short"
  2063. committish)
  2064. committish))
  2065. (defun git-walktree--create-buffer (committish name type)
  2066. "Create and return buffer for COMMITTISH:NAME.
  2067. TYPE is target object type."
  2068. (let* ((root (git-walktree--git-plumbing "rev-parse"
  2069. "--show-toplevel"))
  2070. (committish-display (git-walktree--committish-fordisplay committish))
  2071. (name (format "%s:%s"
  2072. (or committish-display "")
  2073. name)))
  2074. (if (and git-walktree-reuse-tree-buffer
  2075. (string= type "tree"))
  2076. (with-current-buffer (or git-walktree-tree-buffer-for-reuse
  2077. (setq git-walktree-tree-buffer-for-reuse
  2078. (generate-new-buffer "gitwalktreebuf")))
  2079. (setq git-walktree-repository-root root)
  2080. (rename-buffer name t)
  2081. (current-buffer))
  2082. (with-current-buffer (get-buffer-create name)
  2083. (if git-walktree-repository-root
  2084. (if (string= root
  2085. git-walktree-repository-root)
  2086. (current-buffer)
  2087. ;; If the buffer is for another repository, create new buffer
  2088. (with-current-buffer (generate-new-buffer name)
  2089. (setq git-walktree-repository-root root)
  2090. (current-buffer)))
  2091. ;; New buffer
  2092. (setq git-walktree-repository-root root)
  2093. (current-buffer))))))
  2094. (defun git-walktree--replace-into-buffer (target)
  2095. "Replace TARGET buffer contents with that of current buffer.
  2096. It also copy text overlays."
  2097. (let ((src (current-buffer)))
  2098. (with-current-buffer target
  2099. (replace-buffer-contents src)))
  2100. ;; Copy color overlays
  2101. (let ((overlays (overlays-in (point-min) (point-max))))
  2102. (dolist (o overlays)
  2103. (let ((beg (overlay-start o))
  2104. (end (overlay-end o)))
  2105. (move-overlay (copy-overlay o)
  2106. beg
  2107. end
  2108. target)))))
  2109. (require 'ansi-color)
  2110. (defun git-walktree--open-treeish (committish path treeish)
  2111. "Open git tree buffer of COMMITISH:PATH.
  2112. TREEISH should be a tree-ish object full-sha1 of COMMITISH:PATH."
  2113. (cl-assert path)
  2114. (cl-assert treeish)
  2115. (let* (point-tree-start
  2116. (type (git-walktree--git-plumbing "cat-file"
  2117. "-t"
  2118. treeish))
  2119. (buf (git-walktree--create-buffer committish path type))
  2120. )
  2121. (cl-assert (member type
  2122. '("commit" "tree")))
  2123. (with-current-buffer buf
  2124. (unless (and (string= treeish
  2125. git-walktree-object-full-sha1)
  2126. (or (eq committish
  2127. git-walktree-current-committish)
  2128. (string= committish
  2129. git-walktree-current-committish)))
  2130. (buffer-disable-undo)
  2131. ;; For running git command go back to repository root
  2132. (cd git-walktree-repository-root)
  2133. (save-excursion
  2134. (let ((inhibit-read-only t))
  2135. ;; Remove existing overlays generated by ansi-color-apply-on-region
  2136. (remove-overlays)
  2137. (with-temp-buffer
  2138. (if committish
  2139. (progn (git-walktree--call-process nil
  2140. "show"
  2141. ;; TODO: Make this args configurable
  2142. ;; "--no-patch"
  2143. "--color=always"
  2144. "--pretty=short"
  2145. "--decorate"
  2146. "--stat"
  2147. committish)
  2148. (ansi-color-apply-on-region (point-min)
  2149. (point))
  2150. (insert "\n")
  2151. (insert (format "Contents of '%s:%s':\n"
  2152. (git-walktree--committish-fordisplay committish)
  2153. path)))
  2154. (insert (format "Contents of treeish object '%s:\n"
  2155. treeish)))
  2156. (setq point-tree-start (point))
  2157. (git-walktree--call-process nil
  2158. "ls-tree"
  2159. ;; "-r"
  2160. "--abbrev"
  2161. treeish)
  2162. (git-walktree--replace-into-buffer buf))
  2163. ))
  2164. (git-walktree-mode)
  2165. (set-buffer-modified-p nil)
  2166. (setq git-walktree-current-committish committish)
  2167. (setq git-walktree-current-path path)
  2168. (setq git-walktree-object-full-sha1 treeish)
  2169. (let ((dir (expand-file-name path git-walktree-repository-root)))
  2170. (when (and git-walktree-try-cd
  2171. (file-directory-p dir))
  2172. (cd dir)))
  2173. (when (= (point) (point-min))
  2174. (goto-char point-tree-start)
  2175. (git-walktree-mode--move-to-file)
  2176. )
  2177. ))
  2178. buf))
  2179. (defun git-walktree--call-process (&optional infile &rest args)
  2180. "Call git command with input from INFILE and args ARGS.
  2181. Result will be inserted into current buffer."
  2182. (let ((status (apply 'call-process
  2183. git-walktree-git-executable
  2184. infile
  2185. t
  2186. nil
  2187. args)))
  2188. (unless (eq 0
  2189. status)
  2190. (error "Failed to call git process %S %S"
  2191. infile
  2192. args))))
  2193. ?w
  2194. (defun git-walktree--open-blob (committish path blob)
  2195. "Open blob object of COMMITISH:PATH.
  2196. BLOB should be a object full sha1 of COMMITISH:PATH."
  2197. (cl-assert committish)
  2198. (cl-assert path)
  2199. (cl-assert blob)
  2200. (let* ((type (git-walktree--git-plumbing "cat-file"
  2201. "-t"
  2202. blob))
  2203. (buf (git-walktree--create-buffer committish path type)))
  2204. (cl-assert (string= type "blob"))
  2205. (with-current-buffer buf
  2206. (unless (string= blob
  2207. git-walktree-object-full-sha1)
  2208. ;; For running git command go back to repository root
  2209. (cd git-walktree-repository-root)
  2210. (let ((inhibit-read-only t))
  2211. (with-temp-buffer
  2212. (git-walktree--call-process nil
  2213. "cat-file"
  2214. "-p"
  2215. blob)
  2216. (git-walktree--replace-into-buffer buf)))
  2217. (setq git-walktree-buffer-file-name
  2218. (concat git-walktree-repository-root "/git@" committish ":" path))
  2219. (setq buffer-file-name
  2220. (concat git-walktree-repository-root "/" path))
  2221. (normal-mode t)
  2222. ;; For asking filename when C-xC-s
  2223. (setq buffer-file-name nil)
  2224. (set-buffer-modified-p t)
  2225. (setq git-walktree-current-committish committish)
  2226. (setq git-walktree-current-path path)
  2227. (setq git-walktree-object-full-sha1 blob)
  2228. (let ((dir (expand-file-name (or (file-name-directory path)
  2229. ".")
  2230. git-walktree-repository-root)))
  2231. (when (and git-walktree-try-cd
  2232. (file-directory-p dir))
  2233. (cd dir)))
  2234. (view-mode 1)
  2235. ))
  2236. buf))
  2237. (defun git-walktree--open-noselect-safe-path (committish &optional path)
  2238. "Open git object of COMMITTISH:PATH.
  2239. If PATH not found in COMMITTISH tree, go up path and try again until found.
  2240. When PATH is omitted or nil, it is calculated from current file or directory."
  2241. (cl-assert committish)
  2242. (let ((type (git-walktree--git-plumbing "cat-file"
  2243. "-t"
  2244. committish)))
  2245. (cl-assert (string= type "commit")))
  2246. (setq path
  2247. (or path
  2248. (git-walktree--path-in-repository (or buffer-file-name
  2249. default-directory))))
  2250. ;; PATH must not start with and end with slashes
  2251. (cl-assert (not (string-match-p "\\`/" path)))
  2252. (cl-assert (not (string-match-p "/\\'" path)))
  2253. (let ((obj (git-walktree--resolve-object committish path)))
  2254. (while (not obj)
  2255. (setq path
  2256. (git-walktree--parent-directory path))
  2257. (setq obj
  2258. (git-walktree--resolve-object committish path)))
  2259. (git-walktree--open-noselect committish
  2260. path
  2261. obj)))
  2262. ;; TODO: Store view history
  2263. (defun git-walktree--open-noselect (committish path object)
  2264. "Open buffer to view git object of COMMITTISH:PATH.
  2265. When PATH was given and non-nil open that, otherwise open root tree.
  2266. When OBJECT was given and non-nil, assume that is the object full sha1 of
  2267. COMMITTISH:PATH without checking it."
  2268. (cl-assert committish)
  2269. (let ((type (git-walktree--git-plumbing "cat-file"
  2270. "-t"
  2271. committish)))
  2272. (cl-assert (string= type "commit")))
  2273. (setq path (or path
  2274. "."))
  2275. ;; PATH must not start with and end with slashes
  2276. (cl-assert (not (string-match-p "\\`/" path)))
  2277. (cl-assert (not (string-match-p "/\\'" path)))
  2278. (setq object (or object
  2279. (git-walktree--resolve-object committish path)))
  2280. (setq object (git-walktree--git-plumbing "rev-parse"
  2281. object))
  2282. (cl-assert object)
  2283. (let ((type (git-walktree--git-plumbing "cat-file"
  2284. "-t"
  2285. object)))
  2286. (pcase type
  2287. ((or "commit" "tree")
  2288. (git-walktree--open-treeish committish path object))
  2289. ("blob"
  2290. (git-walktree--open-blob committish path object))
  2291. (_
  2292. (error "Type cannot handle: %s" type)))))
  2293. (defun git-walktree--resolve-object (committish path)
  2294. "Return object full sha1 name of COMMITISIH:PATH.
  2295. If path is equal to \".\" return COMMITTISH's root tree object.
  2296. PATH will be always treated as relative to repository root."
  2297. (cl-assert committish)
  2298. (cl-assert path)
  2299. (cl-assert (not (string-match-p "\\`/" path)))
  2300. (cl-assert (not (string-match-p "/\\'" path)))
  2301. (if (string= path ".")
  2302. (git-walktree--git-plumbing "show"
  2303. "--no-patch"
  2304. "--pretty=format:%T"
  2305. committish)
  2306. (let ((info (git-walktree--parse-lstree-line (git-walktree--git-plumbing "ls-tree"
  2307. "--full-tree"
  2308. committish
  2309. path))))
  2310. (plist-get info :object))))
  2311. (defun git-walktree-open (committish &optional path)
  2312. "Open git tree buffer of COMMITTISH.
  2313. When PATH was given and non-nil open that, otherwise try to open current path.
  2314. If target path is not found in COMMITISH tree, go up path and try again until found."
  2315. ;; TODO: Add fallback method for cases where magit is not available
  2316. (interactive (list (magit-read-branch-or-commit "Revision: ")))
  2317. (switch-to-buffer (git-walktree--open-noselect-safe-path committish path)))
  2318. (defalias 'git-walktree 'git-walktree-open)
  2319. (defun git-walktree--path-in-repository (path)
  2320. "Convert PATH into relative path to repository root.
  2321. Result will not have leading and trailing slashes."
  2322. (with-temp-buffer
  2323. (cd (if (file-directory-p path)
  2324. path
  2325. (file-name-directory path)))
  2326. (let ((root (git-walktree--git-plumbing "rev-parse"
  2327. "--show-toplevel")))
  2328. (file-relative-name (directory-file-name path)
  2329. root))))
  2330. (defcustom git-walktree-git-executable "git"
  2331. "Git executable."
  2332. :type 'string
  2333. :group 'git-walktree)
  2334. (defcustom git-walktree-try-cd t
  2335. "Try to cd if directory exists in current working directory if non-nil.
  2336. Otherwise use repository root for gitwalktree buffer's `default-directory'."
  2337. :type 'boolean
  2338. :group 'git-walktree)
  2339. (defcustom git-walktree-reuse-tree-buffer t
  2340. "Non-nil to reuse buffer for treeish object."
  2341. :type 'boolean
  2342. :group 'git-walktree)
  2343. (defvar git-walktree-tree-buffer-for-reuse nil
  2344. "Buffer to use when `git-walktree-reuse-tree-buffer' is non-nil.")
  2345. (defun git-walktree--git-plumbing (&rest args)
  2346. "Run git plubming command with ARGS.
  2347. Returns first line of output without newline."
  2348. (with-temp-buffer
  2349. (let ((status (apply 'call-process
  2350. git-walktree-git-executable
  2351. nil
  2352. t
  2353. nil
  2354. args)))
  2355. (unless (eq 0
  2356. status)
  2357. (error "Faild to run git %S:\n%s"
  2358. args
  2359. (buffer-substring-no-properties (point-min)
  2360. (point-max))))
  2361. (buffer-substring-no-properties (point-min)
  2362. (progn
  2363. (goto-char (point-min))
  2364. (point-at-eol))))))
  2365. (defconst git-walktree-ls-tree-line-regexp
  2366. "^\\([0-9]\\{6\\}\\) \\(\\w+\\) \\([0-9a-f]+\\)\t\\(.*\\)$"
  2367. "Regexp for one line of output of git ls-tree.")
  2368. (defconst git-walktree-ls-tree-line-tree-regexp
  2369. "^\\([0-9]\\{6\\}\\) \\(tree\\) \\([0-9a-f]+\\)\t\\(.*\\)$"
  2370. "Regexp for tree line of output of git ls-tree.")
  2371. (defconst git-walktree-ls-tree-line-commit-regexp
  2372. "^\\([0-9]\\{6\\}\\) \\(commit\\) \\([0-9a-f]+\\)\t\\(.*\\)$"
  2373. "Regexp for commit line of output of git ls-tree.")
  2374. (defun git-walktree--parse-lstree-line (str)
  2375. "Extract object info from STR.
  2376. STR should be a string like following without newline.:
  2377. 100644 blob 6fd4d58202d0b46547c6fe43de0f8c878456f966 .editorconfig
  2378. Returns property list like (:mode MODE :type TYPE :object OBJECT :file FILE)."
  2379. (let (result mode type object file)
  2380. (save-match-data
  2381. (with-temp-buffer
  2382. (insert str)
  2383. (goto-char (point-min))
  2384. (and (re-search-forward git-walktree-ls-tree-line-regexp
  2385. nil
  2386. t)
  2387. (list :mode (match-string 1)
  2388. :type (match-string 2)
  2389. :object (match-string 3)
  2390. :file (match-string 4)))))))
  2391. (defun git-walktree-mode-open-this ()
  2392. "Open git object of current line."
  2393. (interactive)
  2394. (let ((info (git-walktree--parse-lstree-line (buffer-substring-no-properties (point-at-bol)
  2395. (point-at-eol)))))
  2396. (if info
  2397. (switch-to-buffer
  2398. (if (string= (plist-get info
  2399. :type)
  2400. "commit")
  2401. ;; For submodule cd to that directory and intialize
  2402. ;; TODO: Provide way to go back to known "parent" repository
  2403. (with-temp-buffer
  2404. (cd (plist-get info :file))
  2405. (git-walktree--open-noselect (plist-get info
  2406. :object)
  2407. nil
  2408. (plist-get info
  2409. :object)))
  2410. (git-walktree--open-noselect git-walktree-current-committish
  2411. (git-walktree--join-path (plist-get info
  2412. :file))
  2413. (plist-get info
  2414. :object))))
  2415. (message "No object on current line."))))
  2416. (defun git-walktree--join-path (name &optional base)
  2417. "Make path from NAME and BASE.
  2418. If base is omitted or nil use value of `git-walktree-current-path'."
  2419. (setq base (or base
  2420. git-walktree-current-path))
  2421. (cl-assert base)
  2422. (if (string= base ".")
  2423. name
  2424. (concat base "/" name)))
  2425. (defun git-walktree--parent-directory (path)
  2426. "Return parent directory of PATH without trailing slash.
  2427. For root directory return \".\".
  2428. If PATH is equal to \".\", return nil."
  2429. (if (string-match-p "/" path)
  2430. (directory-file-name (file-name-directory path))
  2431. (if (string= "." path)
  2432. nil
  2433. ".")))
  2434. (defun git-walktree-up (&optional committish path)
  2435. "Open parent directory of COMMITTISH and PATH.
  2436. If not given, value of current buffer will be used."
  2437. (interactive)
  2438. (setq committish
  2439. (or committish git-walktree-current-committish))
  2440. (setq path
  2441. (or path git-walktree-current-path))
  2442. (let ((parent (git-walktree--parent-directory path)))
  2443. (if parent
  2444. (switch-to-buffer (git-walktree--open-noselect committish
  2445. parent
  2446. nil))
  2447. (message "Cannot find parent directory for current tree."))))
  2448. (defun git-walktree-mode--move-to-file ()
  2449. "Move point to file field of ls-tree output in current line.
  2450. This function do nothing when current line is not ls-tree output."
  2451. (interactive)
  2452. (save-match-data
  2453. (when (save-excursion
  2454. (goto-char (point-at-bol))
  2455. (re-search-forward git-walktree-ls-tree-line-regexp
  2456. (point-at-eol) t))
  2457. (goto-char (match-beginning 4)))))
  2458. (defun git-walktree-mode-next-line (&optional arg try-vscroll)
  2459. "Move cursor vertically down ARG lines and move to file field if found."
  2460. (interactive "^p\np")
  2461. (or arg (setq arg 1))
  2462. (line-move arg nil nil try-vscroll)
  2463. (git-walktree-mode--move-to-file)
  2464. )
  2465. (defun git-walktree-mode-previous-line (&optional arg try-vscroll)
  2466. "Move cursor vertically up ARG lines and move to file field if found."
  2467. (interactive "^p\np")
  2468. (or arg (setq arg 1))
  2469. (line-move (- arg) nil nil try-vscroll)
  2470. (git-walktree-mode--move-to-file)
  2471. )
  2472. (defgroup git-walktree-faces nil
  2473. "Faces used by git-walktree."
  2474. :group 'git-walktree
  2475. :group 'faces)
  2476. (defface git-walktree-tree-face
  2477. ;; Same as dired-directory
  2478. '((t (:inherit font-lock-function-name-face)))
  2479. "Face used for tree objects."
  2480. :group 'git-walktree-faces)
  2481. (defface git-walktree-commit-face
  2482. ;; Same as dired-symlink face
  2483. '((t (:inherit font-lock-keyword-face)))
  2484. "Face used for commit objects."
  2485. :group 'git-walktree-faces)
  2486. (defvar git-walktree-known-child-revisions (make-hash-table :test 'equal)
  2487. "Hash of already known pair of commitid -> list of child commitid.
  2488. Both values should be object full sha1 names.")
  2489. (defun git-walktree--put-child (parent child)
  2490. "Register PARENT and CHILD relationship.
  2491. PARENT should be a full sha1 object name."
  2492. ;; Any way to check if PARENT is a full SHA-1 object name?
  2493. (let ((current (gethash parent git-walktree-known-child-revisions)))
  2494. (unless (member child current)
  2495. (puthash parent
  2496. (cons child
  2497. current)
  2498. git-walktree-known-child-revisions))))
  2499. ;; TODO: Add aggressive search mode
  2500. ;; https://stackoverflow.com/a/9870218
  2501. ;; git log --reverse --pretty=format:%H -n 1 --ancestry-path <PARENT>..HEAD
  2502. (defun git-walktree--get-children (parent)
  2503. "Get known children list of PARENT commit.
  2504. PARENT should be a full sha1 object name."
  2505. (gethash parent git-walktree-known-child-revisions))
  2506. (defun git-walktree--choose-committish (prompt-format collection)
  2507. "Emit PROMPT-FORMAT and ask user to which committish of COLLECTION to use.
  2508. When collection has just one element, return the first element without asking."
  2509. (cl-assert collection)
  2510. (if (< (length collection) 2)
  2511. (car collection)
  2512. (completing-read (format prompt-format
  2513. (mapconcat 'git-walktree--committish-fordisplay
  2514. collection
  2515. " "))
  2516. collection
  2517. nil
  2518. t)))
  2519. (defun git-walktree-parent-revision ()
  2520. "Open parent revision of current path.
  2521. If current path was not found in the parent revision try to go up path."
  2522. (interactive)
  2523. (cl-assert git-walktree-current-committish)
  2524. (let* ((commit-full-sha1 (git-walktree--git-plumbing "rev-parse"
  2525. git-walktree-current-committish))
  2526. (parents (git-walktree--parent-full-sha1 commit-full-sha1)))
  2527. (dolist (parent parents)
  2528. (git-walktree--put-child parent
  2529. commit-full-sha1))
  2530. (if (< (length parents)
  2531. 1)
  2532. (message "This revision has no parent revision")
  2533. (let* ((parent (git-walktree--choose-committish "This revision has multiple parents. Which to open? (%s) "
  2534. parents))
  2535. (path git-walktree-current-path))
  2536. (cl-assert path)
  2537. (switch-to-buffer (git-walktree--open-noselect-safe-path parent
  2538. path))))))
  2539. (defun git-walktree--parent-full-sha1 (committish)
  2540. "Return list of parent commits of COMMITTISH in sha1 string."
  2541. (let ((type (git-walktree--git-plumbing "cat-file"
  2542. "-t"
  2543. committish)))
  2544. (cl-assert (string= type "commit")))
  2545. (let ((parents (git-walktree--git-plumbing "show"
  2546. "--no-patch"
  2547. "--pretty=format:%P"
  2548. committish)))
  2549. (split-string parents)))
  2550. (defun git-walktree-known-child-revision ()
  2551. "Open known revision of current path."
  2552. (interactive)
  2553. (let* ((commit-full-sha1 (git-walktree--git-plumbing "rev-parse"
  2554. git-walktree-current-committish))
  2555. (children (git-walktree--get-children commit-full-sha1)))
  2556. (if (< (length children)
  2557. 1)
  2558. (message "There are no known child revision")
  2559. (let* ((child (git-walktree--choose-committish "There are multiple known childrens. Which to open? (%s)"
  2560. children))
  2561. (path git-walktree-current-path))
  2562. (cl-assert path)
  2563. (switch-to-buffer (git-walktree--open-noselect-safe-path child
  2564. path))))))
  2565. (defvar git-walktree-mode-map
  2566. (let ((map (make-sparse-keymap)))
  2567. (define-key map "n" 'git-walktree-mode-next-line)
  2568. (define-key map "p" 'git-walktree-mode-previous-line)
  2569. (define-key map (kbd "C-n") 'git-walktree-mode-next-line)
  2570. (define-key map (kbd "C-p") 'git-walktree-mode-previous-line)
  2571. ;; TODO: Review keybind
  2572. (define-key map "P" 'git-walktree-parent-revision)
  2573. (define-key map "N" 'git-walktree-known-child-revision)
  2574. (define-key map "^" 'git-walktree-up)
  2575. ;; TODO: implement
  2576. (define-key map (kbd "DEL") 'git-walktree-back)
  2577. (define-key map (kbd "C-m") 'git-walktree-mode-open-this)
  2578. map))
  2579. (defvar git-walktree-mode-font-lock-keywords
  2580. `(
  2581. (,git-walktree-ls-tree-line-regexp . (
  2582. (1 'shadow)
  2583. (3 'shadow)
  2584. ))
  2585. (,git-walktree-ls-tree-line-tree-regexp . (
  2586. (2 'git-walktree-tree-face)
  2587. (4 'git-walktree-tree-face)
  2588. ))
  2589. (,git-walktree-ls-tree-line-commit-regexp . (
  2590. (2 'git-walktree-commit-face)
  2591. (4 'git-walktree-commit-face)
  2592. ))
  2593. )
  2594. "Syntax highlighting for git-walktree mode.")
  2595. (define-derived-mode git-walktree-mode special-mode "GitWalktree"
  2596. "Major-mode for `git-walktree-open'."
  2597. (set (make-local-variable 'font-lock-defaults)
  2598. '(git-walktree-mode-font-lock-keywords
  2599. nil nil nil nil
  2600. ))
  2601. )
  2602. (require 'magit nil t)
  2603. ;; (git-revision--git-plumbing "cat-file" "-t" "HEAD")
  2604. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2605. ;; git-worktree
  2606. (defun git-worktree-get-current-list ()
  2607. "Get current worktree list."
  2608. (with-temp-buffer
  2609. (let ((trees nil)
  2610. (status (call-process "git"
  2611. nil
  2612. t
  2613. nil
  2614. "worktree" "list" "--porcelain")))
  2615. (cl-assert (eq status 0))
  2616. (goto-char (point-min))
  2617. (save-match-data
  2618. (while (not (eq (point) (point-max)))
  2619. (let ((worktree nil)
  2620. (head nil)
  2621. (branch nil))
  2622. (while (re-search-forward "^\\([^ ]+\\) \\(.*\\)$" (point-at-eol) t)
  2623. (pcase (match-string 1)
  2624. ("worktree" (setq worktree (match-string 2)))
  2625. ("HEAD" (setq head (match-string 2)))
  2626. ("branch" (setq branch (match-string 2))))
  2627. (forward-line 1)
  2628. (goto-char (point-at-bol)))
  2629. (setq trees `(,@trees
  2630. (:worktree ,worktree :head ,head :branch ,branch)))
  2631. (forward-line 1)
  2632. (goto-char (point-at-bol)))
  2633. ))
  2634. trees)))
  2635. ;; Local Variables:
  2636. ;; flycheck-disabled-checkers: (emacs-lisp-checkdoc)
  2637. ;; flycheck-checker: emacs-lisp
  2638. ;; End:
  2639. ;;; emancs.el ends here