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 73 KiB

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