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

emacs.el 71 KiB

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