diff --git a/modules/contrib/helm-deft.el b/modules/contrib/helm-deft.el new file mode 100644 index 000000000..b4ac332de --- /dev/null +++ b/modules/contrib/helm-deft.el @@ -0,0 +1,162 @@ +;;; helm-deft.el --- helm module for grepping note files over directories + +;; Copyright (C) 2014 Derek Feichtinger + +;; Author: Derek Feichtinger +;; Keywords: convenience +;; Homepage: https://github.com/dfeich/helm-deft +;; Version: TODO +;; Package-Requires: ((helm "1.7.7") (f "0.17.0") (cl-lib "0.5")) + +;; This file is not part of GNU Emacs. + +;; This program is free software; you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation; either version 3, or (at your option) +;; any later version. +;; +;; This program is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. +;; +;; You should have received a copy of the GNU General Public License +;; along with GNU Emacs. If not, see . + +;;; Code: + +(require 'helm) +(require 'helm-grep) +(require 'helm-files) +(require 'f) +(require 'cl-lib) + +(defgroup helm-deft nil + "customization group for the helm-deft utility" :group 'helm :version 24.3) + +(defcustom helm-deft-dir-list + '("~/Documents") + "list of directories in which to search recursively for candidate files" + :group 'helm-deft + ) + +(defcustom helm-deft-extension "org" + "defines file extension for identifying candidate files to be searched for") + +(defvar helm-deft-file-list "" + "variable to store the list of candidate files") + +(defvar helm-source-deft-fn + '((name . "File Names") + (init . (lambda () + (progn (setq helm-deft-file-list (helm-deft-fname-search)) + (with-current-buffer (helm-candidate-buffer 'local) + (insert (mapconcat 'identity + helm-deft-file-list "\n")))))) + (candidates-in-buffer) + ;; matching is done in the buffer when candidates-in-buffer is used + ;; We only want against the basename and not the full path + (match-part . (lambda (c) (helm-basename c))) + (type . file) + ;; Note: We override the transformer that the file type brings. We + ;; want the file list sorted + (candidate-transformer . (lambda (c) (sort (helm-highlight-files c) + (lambda (a b) + (string< (downcase (car a)) + (downcase (car b))))))) + ;; (action . (("open file" . (lambda (candidate) + ;; (find-file candidate))))) + ;;(persistent-help . "show name") + ) + "Source definition for matching filenames of the `helm-deft' utility") + +(defvar helm-source-deft-filegrep + '((name . "File Contents") + (candidates-process . helm-deft-fgrep-search) + ;; We use the action from the helm-grep module + (action . helm-grep-action) + (requires-pattern) + (filter-one-by-one . helm-grep-filter-one-by-one) + (cleanup . (lambda () (when (get-buffer "*helm-deft-proc*") + (let ((kill-buffer-query-functions nil)) + (kill-buffer "*helm-deft-proc*"))))) + ) + "Source definition for matching against file contents for the + `helm-deft' utility") + +(defun helm-deft-rotate-searchkeys () + "rotate the words of the search pattern in the helm minibuffer" + (interactive) + (helm-log "Executing helm-deft-rotate-searchkeys") + (let ((patlst (split-string helm-pattern " *"))) + (when (and (>= (length patlst) 1) + (> (length (car patlst)) 0)) + (delete-minibuffer-contents) + (insert (mapconcat #'identity + (append (cdr patlst) (list (car patlst))) + " ")) + (helm-update))) + ) + +(defvar helm-deft-map + (let ((map (make-sparse-keymap))) + (set-keymap-parent map helm-map) + (define-key map (kbd "C-r") 'helm-deft-rotate-searchkeys) + (delq nil map)) + "helm keymap used for helm deft sources") + +(defun helm-deft-fname-search () + "search all preconfigured directories for matching files and return the +filenames as a list" + (cl-assert helm-deft-extension nil "No file extension defined for helm-deft") + (cl-assert helm-deft-dir-list nil "No directories defined for helm-deft") + (cl-loop for dir in helm-deft-dir-list + do (cl-assert (file-exists-p dir) nil + (format "Directory %s does not exist. Check helm-deft-dir-list" dir)) + collect (f--files dir (equal (f-ext it) helm-deft-extension) t) + into reslst + finally (return (apply #'append reslst))) + ) + +(defun helm-deft-build-cmd (ptrnstr filelst) + "Builds a grep command where PTRNSTR may contain multiple search patterns +separated by spaces. The first pattern will be used to retrieve matching lines. +All other patterns will be used to pre-select files with matching lines. +FILELST is a list of file paths" + (let* ((ptrnlst (remove "" (reverse (split-string ptrnstr " *")))) + (firstp (pop ptrnlst)) + (filelst (mapconcat 'identity filelst " ")) + (innercmd (if ptrnlst + (cl-labels ((build-inner-cmd + (ptrnlst filelst) + (let ((pattern (pop ptrnlst))) + (if ptrnlst + (format "$(grep -Elie \"%s\" %s)" pattern + (build-inner-cmd ptrnlst filelst)) + (format "$(grep -Elie \"%s\" %s)" + pattern filelst))))) + (build-inner-cmd ptrnlst filelst)) + filelst))) + (format "grep -EHine \"%s\" %s" firstp innercmd)) + ) + +(defun helm-deft-fgrep-search () + "greps for the helm search pattern in the configuration defined +file list" + (let* ((shcmd (helm-deft-build-cmd helm-pattern helm-deft-file-list))) + (helm-log "grep command: %s" shcmd) + (start-process-shell-command "helm-deft-proc" "*helm-deft-proc*" + shcmd)) + ) + +;;;###autoload +(defun helm-deft () + "Preconfigured `helm' module for locating note files where either the +filename or the file contents match the query string. Inspired by the +emacs `deft' extension" + (interactive) + (helm :sources '(helm-source-deft-fn helm-source-deft-filegrep) + :keymap helm-deft-map)) + +(provide 'helm-deft) +;;; helm-deft.el ends here diff --git a/modules/contrib/ob-rust.el b/modules/contrib/ob-rust.el new file mode 100644 index 000000000..d0bad3e07 --- /dev/null +++ b/modules/contrib/ob-rust.el @@ -0,0 +1,207 @@ +;;; ob-rust.el --- org-babel functions for rust + +;; Author: Philip Munksgaard +;; Keywords: literate programming, reproducible research +;; Homepage: http://orgmode.org + +;;; Commentary: + +;; Org-Babel support for evaluating rust code. +;; +;; This is simply an adaptation of ob-C.el that instead targets rust code. +;; Thanks to the original creators Eric Schulte and Thierry Banel +;; +;; Very limited implementation: +;; - currently only support :results output +;; - not much in the way of error feedback + +;;; Code: +(eval-when-compile + (require 'cl)) +(require 'ob) +(require 'cc-mode) + +(declare-function org-entry-get "org" + (pom property &optional inherit literal-nil)) + + +(defvar org-babel-tangle-lang-exts) +(add-to-list 'org-babel-tangle-lang-exts '("rust")) + +(defvar org-babel-default-header-args:rust '()) + +(defvar org-babel-rust-compiler "rustc" + "Command used to compile a rust source code file into an +executable.") + +(defun org-babel-expand-body:rust (body params) + (org-babel-rust-expand body params)) + +(defun org-babel-execute:rust (body params) + "Execute a block of rust code with org-babel. +This function is called by `org-babel-execute-src-block'." + (print body) + (princ params) + (let* ((tmp-src-file (org-babel-temp-file + "rust-src-" + ".rs")) + (tmp-bin-file (org-babel-temp-file "rust-bin-" org-babel-exeext)) + (cmdline (cdr (assoc :cmdline params))) + (flags (cdr (assoc :flags params))) + (full-body (org-babel-rust-expand body params)) + (compile + (progn + (with-temp-file tmp-src-file (insert full-body)) + (org-babel-eval + (format "%s -o %s %s %s" + org-babel-rust-compiler + (org-babel-process-file-name tmp-bin-file) + (mapconcat 'identity + (if (listp flags) flags (list flags)) " ") + (org-babel-process-file-name tmp-src-file)) "")))) + (let ((results + (org-babel-trim + (org-babel-eval + (concat tmp-bin-file (if cmdline (concat " " cmdline) "")) "")))) + (org-babel-reassemble-table + (org-babel-result-cond (cdr (assoc :result-params params)) + (org-babel-read results) + (let ((tmp-file (org-babel-temp-file "rust-"))) + (with-temp-file tmp-file (insert results)) + (org-babel-import-elisp-from-file tmp-file))) + (org-babel-pick-name + (cdr (assoc :colname-names params)) (cdr (assoc :colnames params))) + (org-babel-pick-name + (cdr (assoc :rowname-names params)) (cdr (assoc :rownames params))))) + )) + +(defun org-babel-rust-expand (body params) + "Expand a block of rust code with org-babel." + (print params) + (print body) + (let ((vars (mapcar #'cdr (org-babel-get-header params :var))) + (main-p (not (string= (cdr (assoc :main params)) "no"))) + (uses (or (cdr (assoc :uses params)) + (org-babel-read (org-entry-get nil "uses" t)))) + (externs (org-babel-read + (or (cdr (assoc :externs params)) + (org-babel-read (org-entry-get nil "externs" t))))) + (attributes (org-babel-read + (or (cdr (assoc :attributes params)) + (org-babel-read (org-entry-get nil "attributes" t)))))) + (mapconcat 'identity + (list + ;; attributes + (mapconcat + (lambda (attr) (format "#[%s]" attr)) + (if (listp attributes) attributes (list attributes)) "\n") + ;; externs + (mapconcat + (lambda (ext) (format "extern crate %s;" ext)) + (if (listp externs) externs (list externs)) "\n") + ;; uses + (mapconcat + (lambda (use) (format "use %s;" use)) + (if (listp uses) uses (list uses)) "\n") + ;; variables + (mapconcat 'org-babel-rust-var-to-rust vars "\n") + ;; body + (if main-p + (org-babel-rust-ensure-main-wrap body) + body) "\n") "\n"))) + +(defun org-babel-rust-ensure-main-wrap (body) + "Wrap BODY in a \"main\" function call if none exists." + (if (string-match "[ \t]*fn+[ \t\n\r]*main[ \t]*(.*)" body) + body + (format "fn main() {\n%s\n}\n" body))) + +(defun org-babel-prep-session:rust (session params) + "This function does nothing as rust is a compiled language with no +support for sessions" + (error "rust is a compiled languages -- no support for sessions")) + +(defun org-babel-load-session:rust (session body params) + "This function does nothing as rust is a compiled language with no +support for sessions" + (error "rust is a compiled languages -- no support for sessions")) + +;; helper functions + +(defun org-babel-rust-format-val (type val) + "Handle the FORMAT part of TYPE with the data from VAL." + (let ((format-data (cadr type))) + (if (stringp format-data) + (cons "" (format format-data val)) + (funcall format-data val)))) + +(defun org-babel-rust-val-to-rust-type (val) + "Determine the type of VAL. +Return a list (TYPE-NAME FORMAT). TYPE-NAME should be the name of the type. +FORMAT can be either a format string or a function which is called with VAL." + (cond + ((integerp val) '("int" "%d")) + ((floatp val) '("f64" "%f")) + ((or (listp val) (vectorp val)) + (lexical-let ((type (org-babel-rust-val-to-rust-list-type val))) + (list (concat "&'static [" (car type) "]") + (lambda (val) + (cons + (format "[%d]%s" + (length val) + (car (org-babel-rust-format-val type (elt val 0)))) + (concat "&[" + (mapconcat (lambda (v) + (cdr (org-babel-rust-format-val type v))) + val + ", ") + "]")))))) + (t ;; treat unknown types as string + '("&'static str" (lambda (val) + (let ((s (format "%s" val))) ;; convert to string for unknown types + (cons (format "[%d]" (1+ (length s))) + (concat "\"" s "\"")))))))) + +(defun org-babel-rust-val-to-rust-list-type (val) + "Determine the rust array type of a VAL." + (let (type) + (mapc + #'(lambda (i) + (let* ((tmp-type (org-babel-rust-val-to-rust-type i)) + (type-name (car type)) + (tmp-type-name (car tmp-type))) + (when (and type (not (string= type-name tmp-type-name))) + (if (and (member type-name '("int" "f64")) + (member tmp-type-name '("int" "f64"))) + (setq tmp-type '("f64" "" "%f")) + (error "Only homogeneous lists are supported by rust. You can not mix %s and %s" + type-name + tmp-type-name))) + (setq type tmp-type))) + val) + type)) + +(defun org-babel-rust-var-to-rust (pair) + "Convert an elisp val into a string of rust code specifying a var +of the same value." + ;; TODO list support + (let ((var (car pair)) + (val (cdr pair))) + (when (symbolp val) + (setq val (symbol-name val)) + (when (= (length val) 1) + (setq val (string-to-char val)))) + (let* ((type-data (org-babel-rust-val-to-rust-type val)) + (type (car type-data)) + (formated (org-babel-rust-format-val type-data val)) + ;; (suffix (car formated)) + (data (cdr formated))) + (format "static %s: %s = %s;" + var + type + ;suffix + data)))) + +(provide 'ob-rust) + +;;; ob-rust.el ends here