merged.

Pascal J. Bourguignon [2018-07-25 21:54]
merged.
Filename
android-classes.el
freerdp-c-style.el
pjb-banks.el
pjb-c.el
pjb-cl.el
pjb-erc.el
pjb-objc-ide.el
pjb-searches.el
pl1-keywords.lisp
diff --git a/android-classes.el b/android-classes.el
index b8bed19..add7e04 100644
--- a/android-classes.el
+++ b/android-classes.el
@@ -21,7 +21,7 @@
         (packages (mapcar (lambda (li)
                             (let* ((a (get-first-child-tagged li 'a))
                                    (href (attribute-value (get-attribute-named a 'href )))
-                                   (name (string-trim " \n\t" (first (element-children a)))))
+                                   (name (cl:string-trim " \n\t" (first (element-children a)))))
                               (make-android-package-info name href)))
                           lis))
         (table (get-first-child-tagged body 'table))
@@ -30,7 +30,7 @@
                            (let* ((linkcol (get-first-child-valued tr 'class "jd-linkcol"))
                                   (a (get-first-child-tagged linkcol 'a))
                                   (url  (attribute-value (get-attribute-named a 'href)))
-                                  (name (string-trim " \n\t" (first (element-children a))))
+                                  (name (cl:string-trim " \n\t" (first (element-children a))))
                                   (package (if (and (prefixp root url)
                                                     (suffixp ".html" url))
                                                (substitute (character ".") (character "/")
diff --git a/freerdp-c-style.el b/freerdp-c-style.el
index 8befdd0..7956384 100644
--- a/freerdp-c-style.el
+++ b/freerdp-c-style.el
@@ -219,7 +219,7 @@
        (substatement-label    . /)
        ;; Labelled line after an if/while/for/do/else.

-       (case-label            . *)
+       (case-label            . +)
        ;; A "case" or "default" label.

        (access-label          . -)
@@ -408,9 +408,15 @@
 ;;         then remove previous spaces; add following space;
 ;;         else add following space;

-(defun freerdp-remove-previous-spaces ()
-  (while (= 32 (char-before))
-         (kill-backward-chars 1)))
+(defun spacep (ch)
+  (string-match "[[:blank:]]"  (string ch)))
+
+(defun* freerdp-remove-previous-spaces (&optional (from (point)))
+  (save-excursion
+   (goto-char from)
+   (while (spacep (char-before))
+          (delete-region (1- (point)) (point)))))
+

 (defun freerdp-electric-space-after (repeat)
   (interactive "p")
@@ -467,21 +473,57 @@
          (insert " "))
        (self-insert-command repeat)))

+(defun* freerdp-reverse-skip-spaces (&optional (from (point)))
+  (while (spacep (char-before from))
+         (decf from))
+  from)
+
+
+(defun freerdp-electric-space-before-after-> (repeat)
+  "Same as freerdp-electric-space-before-after-double
+unless -> in which case we remove the spaces."
+  (interactive "p")
+  (if (= 1 repeat)
+      (if (save-excursion (beginning-of-line)
+                          (thing-at-point-looking-at "[\t ]*#"))
+          (self-insert-command repeat)
+          (let ((space-after t))
+            ;;  x - - | x--  | x-- > | x-- >
+            ;;  x - > | x->a |
+            ;;  x>    | x >  |
+            (freerdp-remove-previous-spaces)
+            (if (= ?- (char-before))
+                (let ((pos (freerdp-reverse-skip-spaces (1- (point)))))
+                  (if (= ?- (char-before pos))
+                      (insert " ")
+                      (progn
+                        (freerdp-remove-previous-spaces (- (point) 1))
+                        (freerdp-remove-previous-spaces (- (point) 2))
+                        (setf space-after nil))))
+                (insert " "))
+            (self-insert-command 1)
+            (when space-after (insert " "))))
+      (self-insert-command repeat)))
+
 (defun freerdp-electric-space-before-after-*/ (repeat)
   (interactive "p")
   (if (= 1 repeat)
        (let ((ch (this-command-keys)))
          (freerdp-remove-previous-spaces)
          (when (cond
-               ((string= "*" ch) (/= ?/ (char-before)))
-               ((string= "/" ch) (/= ?* (char-before)))
-               (t                t))
-             (insert " "))
+                 ((string= "*" ch) (and (/= ?/  (char-before))
+                                        (/= ?\( (char-before))))
+                 ((string= "/" ch) (and (/= ?*  (char-before))
+                                        (/= ?/  (char-before))))
+                 (t                t))
+           (insert " "))
          (self-insert-command 1)
-         (insert " "))
+         (unless (and (string= "*" ch) (looking-at "[()]"))
+           (insert " ")))
        (self-insert-command repeat)))


+
 (defun freerdp-electric-brace-open (repeat)
   (interactive "p")
   (if (= 1 repeat)
@@ -513,17 +555,32 @@
                     (character (string string-designator)))
                 c-keywords-obarray))

+(defun freerdp-electric-special-character-p (ch)
+  (string-match "[[:punct:]]" (string ch)))
+
+(defvar freerdp-electric-identifier-character-regexp "[[:alnum:]_]")
+
+
 (defun freerdp-electric-paren-open (repeat)
   (interactive "p")
   (if (= 1 repeat)
       (progn
-        (freerdp-remove-previous-spaces)
-        (let ((current (point)))
-          (backward-sexp)
-          (if (prog1 (freerdp-c-keyword-p (symbol-at-point))
-                (forward-sexp))
-              (insert " ()")
-              (insert "()")))
+        (if (and (looking-at freerdp-electric-identifier-character-regexp)
+                 ;; (looking-back freerdp-electric-identifier-character-regexp (- (point) 1))
+                 )
+            (insert "()")
+            (progn
+              (freerdp-remove-previous-spaces)
+              (let ((current (point)))
+                (cond
+                  ((bolp)
+                   (insert "()"))
+                  (t
+                   (backward-sexp)
+                   (if (prog1 (freerdp-c-keyword-p (symbol-at-point))
+                         (forward-sexp))
+                       (insert " ()")
+                       (insert "()")))))))
         (backward-char))
       (self-insert-command repeat)))

@@ -536,5 +593,24 @@
             (insert ")")))
        (self-insert-command repeat)))

+
+(defun freerdp-style-set-local-bindings ()
+  (interactive)
+  (setf indent-tabs-mode t
+        tab-width        8)
+  (local-set-key "," 'freerdp-electric-space-after)
+  (local-set-key "=" 'freerdp-electric-space-before-after-=)
+  (local-set-key ">" 'freerdp-electric-space-before-after->)
+  (dolist (key '("<" "+" "-" "&" "|"))
+    (local-set-key key 'freerdp-electric-space-before-after-double))
+  (dolist (key '("*" "/"))
+    (local-set-key key 'freerdp-electric-space-before-after-*/))
+  (dolist (key '("%" "^"))
+    (local-set-key key 'freerdp-electric-space-before-after))
+  (local-set-key "{" 'freerdp-electric-brace-open)
+  (local-set-key "}" 'freerdp-electric-brace-close)
+  (local-set-key "(" 'freerdp-electric-paren-open)
+  (local-set-key ")" 'freerdp-electric-paren-close))
+
 (provide 'freerdp-c-style)
 ;;;; THE END ;;;;
diff --git a/pjb-banks.el b/pjb-banks.el
index 1a17320..0fff767 100644
--- a/pjb-banks.el
+++ b/pjb-banks.el
@@ -72,7 +72,7 @@ RETURN: A cleaned string where the comas are replaced by dots,
         (i 0)
         (result '()))
       ((null items) (apply (function concatenate) 'string (nreverse result)))
-    (unless (zerop (length (string-trim " " item)))
+    (unless (zerop (length (cl:string-trim " " item)))
       (incf i)
       (push (format "%10s" item) result)
       (when (zerop (mod i 6))
@@ -131,8 +131,8 @@ Use Mozilla, and copy-and-paste to emacs."
                (sol (match-string-no-properties 7 line))
                ;; (mat (match-lines))
                (lin) (ori))
-          (setf deb (string-trim " " deb)
-                cre (string-trim " " cre)
+          (setf deb (cl:string-trim " " deb)
+                cre (cl:string-trim " " cre)
                 y (+ y (cond ((and (= mv 12) (= m 1)) 1999)
                              ((and (= mv 1) (= m 12)) 2001)
                              (t                       2000)))
@@ -162,7 +162,7 @@ Use Mozilla, and copy-and-paste to emacs."
                      (if (< (length lab) 48) lab
                          (replace-regexp-in-string
                           "\n" "\n                "
-                          (string-trim " " (string-justify-left lab 48)))))
+                          (cl:string-trim " " (string-justify-left lab 48)))))
                   ,(cond
                     ((string-match "HACIENDA\\|SEGURIDAD" lab)
                      ":IMPUESTO")
@@ -222,8 +222,8 @@ Use Mozilla, and copy-and-paste to emacs."
                ;; (mat (match-lines))
                (lin) (ori))
           (message "%S %S %S" deb cre sol)
-          (setf deb (string-trim " " deb)
-                cre (string-trim " " cre)
+          (setf deb (cl:string-trim " " deb)
+                cre (cl:string-trim " " cre)
                 y (+ y (cond ((and (= mv 12) (= m 1)) 1999)
                              ((and (= mv 1) (= m 12)) 2001)
                              (t                       2000)))
diff --git a/pjb-c.el b/pjb-c.el
index df511fc..5a12d94 100644
--- a/pjb-c.el
+++ b/pjb-c.el
@@ -319,7 +319,45 @@ SEE ALSO:       c-indent-line
 ;;;                                         ;  is when the TAB command is used.


-
-
-
-;;;; pjb-c.el                         -- 2003-10-10 23:50:40 -- pascal   ;;;;
+(defvar *pjb-c-word-size* 64 "Number of bits in a unsigned long long.")
+(defun pjb-c-label-function (name enum-type enum-constants)
+  (let* ((word-size   64)
+         (max-width   (loop for constant in enum-constants
+                            maximize (length (prin1-to-string constant))))
+         (case-format (format "    case %%-%ds: return %%S;\n" max-width))
+         (buffer-size (+ (length "Invalid  value: ")
+                         (length (prin1-to-string enum-type))
+                         (ceiling (* *pjb-c-word-size* (log 2 10)))
+                         1 ; terminating null
+                         16 ; safety.
+                         )))
+    (concat (format "const char* %s(%s value){
+    switch(value){
+" name enum-type)
+            (mapconcat (lambda (constant) (format case-format constant (prin1-to-string constant)))
+                       enum-constants "")
+            (format "    default: {
+        static char buffer[%d];
+        sprintf(buffer,\"Invalid %s value: %%d\",value);
+        return buffer;
+    }}
+}
+" buffer-size enum-type))))
+
+
+(assert (string= (pjb-c-label-function 'get_label 'foo '(foo_foo foo_bar foo_chiang))
+         "const char* get_label(foo value){
+    switch(value){
+    case foo_foo   : return \"foo_foo\";
+    case foo_bar   : return \"foo_bar\";
+    case foo_chiang: return \"foo_chiang\";
+    default: {
+        static char buffer[56];
+        sprintf(buffer,\"Invalid foo value: %d\",value);
+        return buffer;
+    }}
+}
+"))
+
+
+;;;; THE END ;;;;
diff --git a/pjb-cl.el b/pjb-cl.el
index 5d63589..54b7315 100644
--- a/pjb-cl.el
+++ b/pjb-cl.el
@@ -1127,7 +1127,7 @@ DO:     [cltl2] string= compares two strings and is true if they are
   (make-string size initial-element))


-(defun string-trim (character-bag string-designator)
+(defun cl:string-trim (character-bag string-designator)
   "Common-Lisp: returns a substring of string, with all characters in \
 character-bag stripped off the beginning and end.
 "
diff --git a/pjb-erc.el b/pjb-erc.el
index 2fdef8b..eaa510c 100644
--- a/pjb-erc.el
+++ b/pjb-erc.el
@@ -255,16 +255,16 @@
   (let* ((header-end (or (search "\r\n\r\n" http-output)
                          (search "\n\n" http-output)))
          (headers (mapcar
-                   (lambda (line) (string-trim "\r" line))
+                   (lambda (line) (cl:string-trim "\r" line))
                    (split-string (subseq http-output 0 header-end) "\n"))))
     (values
      (first headers)
      (mapcar (lambda (header)
                (let ((colon (position (character ":") header)))
-                 (cons (string-trim " " (subseq header 0 colon))
-                       (string-trim " " (subseq header (1+ colon))))))
+                 (cons (cl:string-trim " " (subseq header 0 colon))
+                       (cl:string-trim " " (subseq header (1+ colon))))))
              (rest headers))
-     (string-trim "\r\n" (subseq http-output  header-end)))))
+     (cl:string-trim "\r\n" (subseq http-output  header-end)))))

 (defun pjb-http-session (method url headers data)
   (with-temp-buffer
@@ -596,7 +596,7 @@ displaying erc buffers."
 (defun pjb-erc-user-input ()
   "Return the input of the user in the current buffer."
   ;; We use erc-bol to ignore the prompt.
-  (string-trim "\n" (buffer-substring
+  (cl:string-trim "\n" (buffer-substring
                      (progn
                        (goto-char (erc-beg-of-input-line))
                        (erc-bol))
diff --git a/pjb-objc-ide.el b/pjb-objc-ide.el
index a51d8fa..e8ca6c9 100644
--- a/pjb-objc-ide.el
+++ b/pjb-objc-ide.el
@@ -375,7 +375,7 @@ This command assumes point is not in a string or comment."
 (defun pjb-ide-insert-tag-comment (&optional tag)
   (interactive "p")
   (let ((tag   (or tag 0)))
-    (unless (string= "" (string-trim " \t" (pjb-ide-beginning-of-line-to-point)))
+    (unless (string= "" (cl:string-trim " \t" (pjb-ide-beginning-of-line-to-point)))
       (insert "\n"))
     (if (region-active-p)
         (let ((start (region-beginning))
diff --git a/pjb-searches.el b/pjb-searches.el
index 05b0021..cd83370 100644
--- a/pjb-searches.el
+++ b/pjb-searches.el
@@ -73,7 +73,7 @@
    (browse-url
     (format "https://developer.apple.com/library/mac/search/?q=%s"
             (browse-url-url-encode-chars
-             (string-trim *whitespaces* search-string)
+             (cl:string-trim *whitespaces* search-string)
              "[^A-Za-z0-9]")))))

 (defun osx-search-region (start end)
@@ -89,7 +89,7 @@
       (browse-url
        (format "https://developer.apple.com/library/ios/search/?q=%s"
                (browse-url-url-encode-chars
-                (string-trim *whitespaces* search-string)
+                (cl:string-trim *whitespaces* search-string)
                 "[^A-Za-z0-9]")))))

 (defun ios-search-region (start end)
@@ -113,7 +113,7 @@
                                 (mapconcat (function identity) words "/")))))
                   (format "http://developer.android.com/reference/index.html?q=%s"
                           (browse-url-url-encode-chars
-                           (string-trim *whitespaces* search-string)
+                           (cl:string-trim *whitespaces* search-string)
                            "[^A-Za-z0-9]")))))

 (defun android-search-region (start end)
@@ -121,6 +121,40 @@
   (interactive "r")
   (%search-region start end 'symbol 'android-search))

+
+(defvar *pjb-search-exclude*            '("debug" "release" ".svn" ".git" ".hg" ".cvs"))
+(defvar *pjb-search-include-extensions* '("xib" "h" "c" "m" "hh" "cc" "mm" "hxx" "cxx"
+                                          "lisp" "asd" "cl" "el"
+                                          "rb" "java" "xml"
+                                          "logs" "txt" "html" "iml"
+                                          "json" "md" "prefs"
+                                          "project" "properties"
+                                          "sh" "bash"))
+(defun git-search (search-string)
+  "Search a regex in the current git repository (with `find-grep' and `grep-find-command')."
+  (interactive "sSearch Git Regexp: ")
+  (let ((exclude *pjb-search-exclude*)
+        (include (mapcar (lambda (extension) (format "\\*.%s" extension))
+                         *pjb-search-include-extensions*)))
+    (find-grep (format "find %S \\( \\( %s \\) -prune \\) -o -type f %s -print0 | xargs -0 grep -nHi -e %s"
+
+                       (expand-file-name (vc-git-root (or (buffer-file-name) default-directory)))
+                       (mapconcat (lambda (name) (format "-name %s" name)) exclude " -o ")
+                       (if include
+                           (format "\\( %s \\)" (mapconcat (lambda (name) (format "-name %s" name)) include " -o "))
+                           "")
+                       (shell-quote-argument search-string)))))
+
+(defun git-search-region (start end)
+  "Search the text in the region in the current git repository."
+  (interactive "r")
+  (%search-region start end 'symbol 'git-search))
+
+(defun git-search-symbol-at-point ()
+  "Search the symbol at point in the current git repository."
+  (interactive)
+  (git-search (symbol-name (symbol-at-point))))
+
 (defun project-search (search-string)
   "Search a regex in the current project (with `find-grep' and `grep-find-command')."
   (interactive "sSearch Project Regexp: ")
@@ -139,7 +173,7 @@
    (format "http://www.google.com/search?as_q=%s&num=50&hl=en&ie=ISO8869-1&btnG=Google+Search&as_epq=&as_oq=&as_eq=&lr=&as_ft=i&as_filetype=&as_qdr=all&as_nlo=&as_nhi=&as_occt=any&as_dt=i&as_s
 itesearch=&safe=images"
            (browse-url-url-encode-chars
-            (string-trim *whitespaces* search-string)
+            (cl:string-trim *whitespaces* search-string)
             "[^A-Za-z0-9]"))))

 (defun google-search-region (start end)
@@ -206,6 +240,7 @@ itesearch=&safe=images"

 ;;(global-set-key (kbd "C-h 1") 'android-search-region)
 ;;(global-set-key (kbd "C-h 1") 'osx-search-region)
+
 (global-set-key (kbd "C-h 1") 'ios-search-region)
 (global-set-key (kbd "C-h 2") 'google-search-region)
 (global-set-key (kbd "C-h 3") 'acronym-search-region)
@@ -213,7 +248,12 @@ itesearch=&safe=images"
 (global-set-key (kbd "C-h 5") 'includes-search-region)
 (global-set-key (kbd "C-h 6") 'hyperspec-search-region)
 (global-set-key (kbd "C-h 7") 'here-search-region)
-(global-set-key (kbd "C-h 0") 'android-browse-documentation-of-class-at-point)
+(global-set-key (kbd "C-h 8") 'git-search-region)
+
+(global-set-key (kbd "H-s")   'git-search-symbol-at-point)
+(global-set-key (kbd "C-H-s") 'git-search)
+(global-set-key (kbd "M-H-s") 'git-search-region)
+

 (defun set-osx-search-region-function ()
   (interactive)
diff --git a/pl1-keywords.lisp b/pl1-keywords.lisp
index 7570ebe..c195b40 100644
--- a/pl1-keywords.lisp
+++ b/pl1-keywords.lisp
@@ -373,7 +373,7 @@ zerodivide|zdiv


 (defparameter *expanded-keywords*
-  (mapcar (lambda (regexp) (parse-lex-regexp (string-trim "     " regexp)))
+  (mapcar (lambda (regexp) (parse-lex-regexp (cl:string-trim "     " regexp)))
          (split-sequence:split-sequence  #\newline  *pl1-keywords* :remove-empty-subseqs t)))

 ;; (reduce (function append) *expanded-keywords*)
ViewGit