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

emacs.el 69 KiB

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