[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

general-purpose.el - a general-forms-resource-utility

From: Andreas Roehler
Subject: general-purpose.el - a general-forms-resource-utility
Date: Thu, 06 Jul 2006 12:11:01 +0200
User-agent: Thunderbird (X11/20060516)

There are general usable functions scattered in the
source files, which are useful in a lot of
circumstances and could be callable in a given context.

Roman-to-latin- and Latin-to-roman-numbers for example.

Just today I saw

(defun region-around-match (&optional n)
 (set-mark (match-beginning n))
 (goto-char (match-end n)))

(defun region-to-string ()
 (buffer-substring (min (point) (mark)) (max (point) (mark))))

in mlsupport.el.

To have an (indexed) collection of all these goodies,
not only developers would gain an additional - sorted -
resource: This would also be useful for beginners to
learn Elisp - as function given there will be rather
basic ones, avoiding complexity. Also the Elisp-Manual
could refer to in order to give more examples, to
deepen understanding.

To show up such basic utilities one by one and in
relation might help finding errors and/or to optimize

Already started to collect some forms (example below)
but realize, this should not be done by hand.

Post this here, as there are probably more ideas to
consider before a major writing effort starts.

At the moment I see the following requirements at such
a general-resource-utility:

- take a given function or form where the point is in
 and prompt the user where to sort it (as groups in
 customize deliver a hierarchy and system)

- display a list of clickable functions or forms the
 way `apropos' does; i.e. M-x `general-resource-utility'
 RET `string-strip' should display several
 string-strip-utilities to examine, insert at point etc.

- allow users to edit the collection via
 copy-and-paste; provide some automated indexing

- display related topics as customize does with
 parent groups

- collect resources from inside Emacs and third party
 stuff separately. (As a lot of users and me too use
 such stuff, it should not be impossible to collect
 and use it, provided it doesn't not disturb
 distribution. There might be - and should be probably
 - a warning at least once in that case.)


Thought to use/adapt cus-edit.el as a starting

Please send your suggestions and/or objections.

Andreas Roehler


;; Please look with patience at this decent
  beginning :)

;;;_. * move related functions

(defun skip-blank-lines-backward ()
 " "
 (while (looking-at "[ \t]*$")
     (forward-line -1))
 (forward-line 1))

(defun skip-blank-lines-forward ()
 " "
 (while (looking-at "[ \t\n]")
   (forward-line 1))
 (forward-line -1))

;;;_. * string related functions
;;;_. ** strip-whitespace
;;;_. *** string-strip

(defun string-strip (str beforep afterp)
 "Strip STR of any leading (if BEFOREP) and/or trailing (if AFTERP) space.
 (string-match (concat "\\`" (if beforep "\\s-*")
           "\\(.*?\\)" (if afterp "\\s-*\n?")
           "\\'") str)
 (match-string 1 str))

;; Source: comment-string-strip, newcomment.el, GNU Emacs  ;;

;;;_. *** truncate-string-left

(defun concat-and-truncate-string-left (str prefix newlen)
 ;; leave space for ... on the left
 (let ((len (length str))
   (lenprefix (length prefix))
   (if (<= len newlen)
     (setq newlen (max 0 (- newlen lenprefix)))
     (setq substr (substring str (max 0 (- len 1 newlen))))
     (concat prefix substr))))

;; Example:
;; (concat-and-truncate-string-left "dasddddd" "+++" 4)
;; -> "+++dd"

;; following ediff-truncate-string-left from ediff-init.el

;;;_. *** nonempty-string-p

(defsubst nonempty-string-p (string)
 (and (stringp string) (not (string= string ""))))

;; Source: ediff-nonempty-string-p; ediff-init.el ---
;; Macros, variables, and defsubsts used by Ediff
;; Author: Michael Kifer <address@hidden> ;;

;;;_. *** kill-trailing-spaces

;;;_. *** clean-out-spaces

(defun string-reverse (s)
 "Return the mirror image of string S, without any trailing space."
 (comment-string-strip (concat (nreverse (string-to-list s))) nil t))

;; Source: comment-string-reverse, newcomment.el, GNU Emacs ;;

;;;_. ** numeral-string-conversions
;;;_. *** decimal-to-roman

(defvar w3-roman-characters "ivxLCDMVX" "Roman numerals.")

(defun w3-decimal-to-roman (n)
 "Convert from decimal to roman numerals"

 (let ((curmod 1000)
   (str "")
   (j 7)
   i2 k curcnt)
   (while (>= curmod 1)
     (if (>= n curmod)
       (setq curcnt (/ n curmod)
         n (- n (* curcnt curmod)))
       (if (= 4 (% curcnt 5))
       (setq i2 (+ j (if (> curcnt 5) 1 0))
             str (format "%s%c%c" str
                 (aref w3-roman-characters (1- j))
                 (aref w3-roman-characters i2)))
       (if (>= curcnt 5)
           (setq str (format "%s%c" str (aref w3-roman-characters j))
             curcnt (- curcnt 5)))
       (setq k 0)
       (while (< k curcnt)
         (setq str (format "%s%c" str
                   (aref w3-roman-characters (1- j)))
           k (1+ k)))))))
     (setq curmod (/ curmod 10)
       j (- j 2)))

;; Source: w3-display.el --- W3 display engine. Author: William M. Perry <address@hidden" ;;

;;;_. *** decimal-to-alpha

(defun w3-decimal-to-alpha (n)
 "Convert from decimal to alphabetical (a, b, c, ..., aa, ab,...)"
  ((< n 1) (char-to-string ?Z))
  ((<= n 26) (char-to-string (+ ?A (1- n))))
  (t (concat (w3-decimal-to-alpha (/ n 26))
         (w3-decimal-to-alpha (% n 26))))))

;; Source: w3-display.el --- W3 display engine. Author: William M. Perry <address@hidden" ;;

;;;_. * clipboard related functions
;;;_. ** clipboard and x-select-enable-clipboard

(defun clipboard-yank ()
 "Insert the clipboard contents, or the last stretch of killed text."
 (let ((x-select-enable-clipboard t))

(defun clipboard-kill-ring-save (beg end)
 "Copy region to kill ring, and save in the X clipboard."
 (interactive "r")
 (let ((x-select-enable-clipboard t))
   (kill-ring-save beg end)))

(defun clipboard-kill-region (beg end)
 "Kill the region, and save it in the X clipboard."
 (interactive "r")
 (let ((x-select-enable-clipboard t))
   (kill-region beg end)))

;; Source: menu-bar.el ;;

;;;;; End

reply via email to

[Prev in Thread] Current Thread [Next in Thread]