Write modules + defuns

This commit is contained in:
Henrik Lissner 2015-06-15 09:06:10 +02:00
parent b998f4ab08
commit 3472a1631f
31 changed files with 1418 additions and 32 deletions

79
modules/lib/defuns-cc.el Normal file
View file

@ -0,0 +1,79 @@
;;; defuns-cc.el --- for module-cc.el
(defun narf--c-lineup-inclass (langelem)
(let ((inclass (assoc 'inclass c-syntactic-context)))
(save-excursion
(goto-char (c-langelem-pos inclass))
(if (or (looking-at "struct")
(looking-at "typedef struct"))
'+
'++))))
;;;###autoload
(defun narf|init-c/c++-settings ()
(c-toggle-electric-state -1)
(c-toggle-auto-newline -1)
(c-set-offset 'substatement-open '0) ; brackets should be at same indentation level as the statements they open
(c-set-offset 'inline-open '+)
(c-set-offset 'block-open '+)
(c-set-offset 'brace-list-open '+) ; all "opens" should be indented by the c-indent-level
(c-set-offset 'case-label '+) ; indent case labels by c-indent-level, too
(c-set-offset 'access-label '-)
(c-set-offset 'inclass 'narf--c-lineup-inclass)
;; DEL mapping interferes with smartparens and my custom DEL binding
(define-key c-mode-map (kbd "DEL") nil))
;;;###autoload
(defun narf*c-lineup-arglist ()
"Improve indentation of continued C++11 lambda function opened as argument."
(setq ad-return-value
(if (and (equal major-mode 'c++-mode)
(ignore-errors
(save-excursion
(goto-char (c-langelem-pos langelem))
;; Detect "[...](" or "[...]{". preceded by "," or "(",
;; and with unclosed brace.
(looking-at ".*[(,][ \t]*\\[[^]]*\\][ \t]*[({][^}]*$"))))
0 ; no additional indent
ad-do-it)))
;;;###autoload
(defun narf|init-c++-C11-highlights ()
;; We could place some regexes into `c-mode-common-hook', but
;; note that their evaluation order matters.
(font-lock-add-keywords
nil '(;; complete some fundamental keywords
("\\<\\(void\\|unsigned\\|signed\\|char\\|short\\|bool\\|int\\|long\\|float\\|double\\)\\>" . font-lock-keyword-face)
;; namespace names and tags - these are rendered as constants by cc-mode
("\\<\\(\\w+::\\)" . font-lock-function-name-face)
;; new C++11 keywords
("\\<\\(alignof\\|alignas\\|constexpr\\|decltype\\|noexcept\\|nullptr\\|static_assert\\|thread_local\\|override\\|final\\)\\>" . font-lock-keyword-face)
("\\<\\(char16_t\\|char32_t\\)\\>" . font-lock-keyword-face)
;; PREPROCESSOR_CONSTANT, PREPROCESSORCONSTANT
("\\<[A-Z]*_[A-Z_]+\\>" . font-lock-constant-face)
("\\<[A-Z]\\{3,\\}\\>" . font-lock-constant-face)
;; hexadecimal numbers
("\\<0[xX][0-9A-Fa-f]+\\>" . font-lock-constant-face)
;; integer/float/scientific numbers
("\\<[\\-+]*[0-9]*\\.?[0-9]+\\([ulUL]+\\|[eE][\\-+]?[0-9]+\\)?\\>" . font-lock-constant-face)
;; c++11 string literals
;; L"wide string"
;; L"wide string with UNICODE codepoint: \u2018"
;; u8"UTF-8 string", u"UTF-16 string", U"UTF-32 string"
("\\<\\([LuU8]+\\)\".*?\"" 1 font-lock-keyword-face)
;; R"(user-defined literal)"
;; R"( a "quot'd" string )"
;; R"delimiter(The String Data" )delimiter"
;; R"delimiter((a-z))delimiter" is equivalent to "(a-z)"
("\\(\\<[uU8]*R\"[^\\s-\\\\()]\\{0,16\\}(\\)" 1 font-lock-keyword-face t) ; start delimiter
( "\\<[uU8]*R\"[^\\s-\\\\()]\\{0,16\\}(\\(.*?\\))[^\\s-\\\\()]\\{0,16\\}\"" 1 font-lock-string-face t) ; actual string
( "\\<[uU8]*R\"[^\\s-\\\\()]\\{0,16\\}(.*?\\()[^\\s-\\\\()]\\{0,16\\}\"\\)" 1 font-lock-keyword-face t) ; end delimiter
;; user-defined types (rather project-specific)
("\\<[A-Za-z_]+[A-Za-z_0-9]*_\\(type\\|ptr\\)\\>" . font-lock-type-face)
("\\<\\(xstring\\|xchar\\)\\>" . font-lock-type-face)
) t))
(provide 'defuns-cc)
;;; defuns-cc.el ends here

View file

@ -0,0 +1,16 @@
;;; defuns-elisp.el
;;;###autoload
(defun narf/elisp-find-function-at-pt ()
(interactive)
(let ((func (function-called-at-point)))
(if func (find-function func))))
;;;###autoload
(defun narf/elisp-find-function-at-pt-other-window ()
(interactive)
(let ((func (function-called-at-point)))
(if func (find-function-other-window func))))
(provide 'defuns-elisp)
;;; defuns-elisp.el ends here

View file

@ -0,0 +1,61 @@
;;; defuns-eshell.el ---
(defun narf--eshell-in-prompt-p (&optional offset)
(>= (- (point) (or offset 0)) (save-excursion (eshell-bol) (point))))
(defun narf--eshell-current-git-branch ()
(let ((branch (car (loop for match in (split-string (shell-command-to-string "git branch") "\n")
when (string-match "^\*" match)
collect match))))
(if (not (eq branch nil))
(concat " [" (substring branch 2) "]")
"")))
;;;###autoload
(defun narf/eshell-prompt ()
(concat (propertize (abbreviate-file-name (eshell/pwd)) 'face 'eshell-prompt)
(propertize (narf--eshell-current-git-branch) 'face 'font-lock-function-name-face)
(propertize " $ " 'face 'font-lock-constant-face)))
;;;###autoload
(defun narf/eshell-evil-append ()
(interactive)
(goto-char (point-max))
(call-interactively 'evil-append))
;;;###autoload
(defun narf/eshell-evil-append-maybe ()
(interactive)
(if (narf--eshell-in-prompt-p)
(call-interactively 'evil-insert)
(narf/eshell-append)))
;;;###autoload
(defun narf/eshell-evil-prepend ()
(interactive)
(eshell-bol)
(call-interactively 'evil-insert))
;;;###autoload
(defun narf/eshell-evil-prepend-maybe ()
(interactive)
(if (narf--eshell-in-prompt-p)
(call-interactively 'evil-insert)
(narf/eshell-prepend)))
;;;###autoload
(defun narf/eshell-evil-replace-maybe ()
(interactive)
(if (narf--eshell-in-prompt-p)
(call-interactively 'evil-replace)
(user-error "Cannot edit read-only region")))
;;;###autoload
(defun narf/eshell-evil-replace-state-maybe ()
(interactive)
(if (narf--eshell-in-prompt-p)
(call-interactively 'evil-replace-state)
(user-error "Cannot edit read-only region")))
(provide 'defuns-eshell)
;;; defuns-eshell.el ends here

View file

@ -0,0 +1,48 @@
;;; defuns-java.el ---
;;;###autoload
(defun narf/java-project-package ()
(if (eq major-mode 'java-mode)
(s-chop-suffix "." (s-replace "/" "." (f-dirname (f-relative (buffer-file-name)
(concat (narf/project-root) "/src/")))))
""))
;;;###autoload
(defun narf/java-class-name ()
(if (eq major-mode 'java-mode)
(f-no-ext (f-base (buffer-file-name)))
""))
;; yasnippet defuns
;;;###autoload
(defun narf/java-android-mode-is-layout-file ()
(and android-mode
(eq major-mode 'nxml-mode)
(string-equal (file-name-base (directory-file-name default-directory)) "layout")))
;;;###autoload
(defun narf/java-android-mode-in-tags (&rest tags)
(-contains? tags (android-mode-tag-name)))
;;;###autoload
(defun narf/java-android-mode-tag-name ()
(save-excursion
(let (beg end)
(nxml-backward-up-element)
(evil-forward-word-begin)
(setq beg (point))
(evil-forward-WORD-end)
(setq end (1+ (point)))
(buffer-substring-no-properties beg end))))
;;;###autoload
(defun narf|android-mode-enable-maybe ()
(let ((root (narf/project-root)))
(when (or (narf/project-has-files "local.properties" root)
(narf/project-has-files "AndroidManifest.xml" root)
(narf/project-has-files "src/main/AndroidManifest.xml" root))
(android-mode +1)
(narf/set-build-command "./gradlew %s" "build.gradle"))))
(provide 'defuns-java)
;;; defuns-java.el ends here

View file

@ -0,0 +1,25 @@
;;; defuns-markdown.el --- for module-markdown.el
;; Implement strike-through formatting
(defvar narf--markdown-regex-del
"\\(^\\|[^\\]\\)\\(\\(~\\{2\\}\\)\\([^ \n \\]\\|[^ \n ]\\(?:.\\|\n[^\n]\\)*?[^\\ ]\\)\\(\\3\\)\\)")
;;;###autoload
(defun narf/markdown-insert-del ()
"Surround region in github strike-through delimiters."
(interactive)
(let ((delim "~~"))
(if (markdown-use-region-p)
;; Active region
(let ((bounds (markdown-unwrap-things-in-region
(region-beginning) (region-end)
narf--markdown-regex-del 2 4)))
(markdown-wrap-or-insert delim delim nil (car bounds) (cdr bounds)))
;; Bold markup removal, bold word at point, or empty markup insertion
(if (thing-at-point-looking-at narf--markdown-regex-del)
(markdown-unwrap-thing-at-point nil 2 4)
(markdown-wrap-or-insert delim delim 'word nil nil)))))
(provide 'defuns-markdown)
;;; defuns-markdown.el ends here

124
modules/lib/defuns-org.el Normal file
View file

@ -0,0 +1,124 @@
;;; defuns-org.el
;;;###autoload
(defun narf/org-cycle-hide-drawers (state)
"Re-hide all drawers after a visibility state change. Hides properties permanently."
(when (and (derived-mode-p 'org-mode)
(not (memq state '(overview folded contents))))
(save-excursion
(let* ((globalp (memq state '(contents all)))
(beg (if globalp (point-min) (point)))
(end (if globalp (point-max)
(if (eq state 'children)
(save-excursion (outline-next-heading) (point))
(org-end-of-subtree t)))))
(goto-char beg)
(while (re-search-forward org-drawer-regexp end t)
(save-excursion
(beginning-of-line 1)
(backward-char 1)
(let ((b (point)))
(if (re-search-forward
"^[ \t]*:END:"
(save-excursion (outline-next-heading) (point)) t)
(outline-flag-region b (point-at-eol) t)
(user-error ":END: line missing at position %s" b)))))))))
(defun narf--org-in-list-p ()
(and (save-excursion (search-backward-regexp "^ *\\([0-9]+[\.)]\\|[-*+]\\) "
(line-beginning-position) t))
(org-in-item-p)))
;;;###autoload
(defun narf/project-org-filename (cat)
(interactive (list (completing-read "Choose category:"
(mapcar 'f-filename (f-directories org-project-directory)))))
(expand-file-name (concat (file-name-nondirectory (directory-file-name (narf/project-root))) ".org")
(expand-file-name cat org-project-directory)))
;;;###autoload
(defun narf/org-insert-item-after ()
"Inserts a new heading or item, depending on the context."
(interactive)
(org-end-of-line)
(cond ((org-at-item-checkbox-p)
(org-insert-heading)
(insert "[ ] "))
((narf--org-in-list-p)
(org-insert-heading))
((org-on-heading-p)
(org-insert-heading-after-current))
(t
(org-insert-heading-after-current)
(delete-char 1)))
(evil-insert-state))
;; TODO Check if this and -forward can be combined
;;;###autoload
(defun narf/org-insert-item-before ()
"Inserts a new heading or item, depending on the context."
(interactive)
(evil-first-non-blank)
(cond ((org-at-item-checkbox-p)
(org-insert-heading)
(insert "[ ] "))
((narf--org-in-list-p)
(org-insert-heading))
(t (org-insert-heading)))
(evil-insert-state))
;;;###autoload
(defun narf/org-toggle-checkbox ()
(interactive)
(save-excursion
(org-end-of-line)
(cond ((org-in-item-p)
(if (search-backward-regexp "\\[[ +-]\\]" (line-beginning-position) t)
(delete-char 4)
(org-beginning-of-line)))
(t (org-insert-heading)))
(insert "[ ] ")))
;; Formatting shortcuts
;;;###autoload
(defun narf/org-surround (delim)
(insert delim) (save-excursion (insert delim)))
;;;###autoload (autoload 'narf:org-insert-image-url "defuns-org" nil t)
(evil-define-command narf:org-insert-image-url (&optional image-url)
:repeat nil
(interactive "<f>")
(unless image-url
(user-error "You must specify an image URL to insert"))
(let ((dest (f-join org-directory "images/" (concat (format-time-string "%Y%m%d-") (f-filename image-url)))))
(shell-command (format "wget '%s' -O '%s'" image-url dest))
(insert (format "<%s>" (f-relative dest (f-dirname (buffer-file-name)))))
(indent-according-to-mode)))
;;;###autoload (autoload 'narf:org-insert-image "defuns-org" nil t)
(evil-define-command narf:org-insert-image (&optional filename bang)
:repeat nil
(interactive "<f><!>")
(if bang
(narf:org-insert-image-url filename)
(unless filename
(user-error "You must specify a file to attach"))
(unless (file-exists-p filename)
(user-error "File %s does not exist" filename))
(let ((dest (f-join org-directory "images/" (concat (format-time-string "%Y%m%d-") (f-filename filename)))))
(when (file-exists-p dest)
(user-error "File %s already exists at destination!"))
(copy-file filename dest)
(insert (format "<file:%s>" (f-relative dest (f-dirname (buffer-file-name)))))
(indent-according-to-mode))))
;;;###autoload (autoload 'narf:org-search-files-or-headers "defuns-org" nil t)
(evil-define-command narf:org-search-files-or-headers (&optional bang)
(interactive "<!>")
(require 'org)
(if bang
(ido-find-file-in-dir org-directory)
(call-interactively 'helm-org-agenda-files-headings)))
(provide 'defuns-org)
;;; defuns-org.el ends here

View file

@ -0,0 +1,11 @@
;;; defuns-python.el
;;;###autoload
(defun narf*anaconda-mode-doc-buffer ()
"Delete the window on escape or C-g."
(with-current-buffer (get-buffer "*anaconda-doc*")
(local-set-key [escape] 'anaconda-nav-quit)
(local-set-key [?\C-g] 'anaconda-nav-quit)))
(provide 'defuns-python)
;;; defuns-python.el ends here

View file

@ -0,0 +1,40 @@
;;; defuns-regex.el
;;;###autoload
(defun narf|reb-cleanup ()
(replace-regexp "^[ \n]*" "" nil (point-min) (point-max))
(text-scale-set 1.5)
(goto-char 2))
;;;###autoload (autoload 'narf:regex "defuns-regex" nil t)
(evil-define-operator narf:regex (beg end type &optional regexstr bang)
"Either a) converts selected (or entered-in) pcre regex into elisp
regex, OR b) opens up re-builder."
:move-point nil
:type inclusive
:repeat nil
(interactive "<R><a><!>")
(if (reb-mode-buffer-p)
(if regexstr
(let ((regexstr (s-trim (buffer-string))))
(if bang
(rxt-explain-pcre regexstr)
(rxt-pcre-to-elisp (s-trim (buffer-string)))))
(erase-buffer)
(insert (concat "/" regexstr "/")))
(cond ((and beg end (/= beg (1- end))) ; Convert selection from pcre regex to elisp
(let ((regexstr (buffer-substring-no-properties beg end)))
(if bang
(rxt-explain-pcre (concat "/" regexstr "/"))
(delete-region beg end)
(insert (rxt-pcre-to-elisp regexstr)))))
(regexstr ; Convert input regex into elisp regex
(let ((newregex (rxt-pcre-to-elisp regexstr)))
(when bang
(setq newregex (s-replace "\\" "\\\\" newregex)))
(kill-new newregex)
(message "Copied regex to kill ring: %s" newregex)))
(t (re-builder)))))
(provide 'defuns-regex)
;;; defuns-regex.el ends here

View file

@ -0,0 +1,25 @@
;;; defuns-ruby.el
;;;###autoload
(defun narf|enable-robe-maybe ()
(let ((file (buffer-file-name)))
;; Don't run in gemfiles, capfiles or vagrantfiles
(unless (or (string-equal (f-filename file) "Gemfile")
(string-equal (f-filename file) "Capfile")
(string-equal (f-filename file) "Vagrantfile")
(f-ext? file "org")) ;; or org-mode
(robe-mode 1)
(narf|ruby-load-file file))))
;;;###autoload
(defun narf|ruby-load-file (&optional file)
(let ((file (or file buffer-file-name)))
(when (and (eq major-mode 'enh-ruby-mode)
(featurep 'robe)
(not (string= (f-base file) "Gemfile"))
(file-exists-p buffer-file-name))
(unless robe-running (robe-start 1))
(when robe-running (ruby-load-file file)))))
(provide 'defuns-ruby)
;;; defuns-ruby.el ends here