You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

emacs.el 71 KiB

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