I am trying following code:
(require-extension srfi-13)
(require-extension regex)
(print (string-substitute* "This is a test" '(("a test" . "NO TESTING ZONE" ) ) ) )
It works, with following output:
This is NO TESTING ZONE
But following does not work:
(print (string-substitute* "This is a test" '(("a test" . (string-append "NO " "TESTING") ) ) ) )
Following is the error:
Error: (string-substitute) bad argument type - not a string: (string-append "NO " "TESTING")
Even though, following shows that the output is indeed a string:
(print (string? (string-append "NO " "TESTING")))
#t
Where is the problem and how can this be solved?
This has nothing to do with string-substitute*.
You're quoting the list, so (string-append "NO " "TESTING") is not evaluated:
> '(("a test" . (string-append "NO " "TESTING")))
'(("a test" string-append "NO " "TESTING"))
Use quasiquote:
`(("a test" . ,(string-append "NO " "TESTING")
or don't quote at all:
(list (cons "a test" (string-append "NO " "TESTING"))
Related
How can I create map, a higher order function, in Red language. It should take a block and a function as arguments and apply the sent function to each member of block. I tried following code:
Red []
mapfn: function[blk sfn][
outblk: copy []
foreach i blk[
append outblk (sfn i) ]
outblk ]
; to test:
myblk: [" this " " is " " a " " line " "for" " testing " " only "]
probe mapfn myblk 'reverse
probe mapfn myblk 'trim
But it is not working - it simply sends back the original block without changing it or giving any error message. How can this be corrected?
In Rebol you have the mezzanine function apply
>> help apply
USAGE:
APPLY func block /only
DESCRIPTION:
Apply a function to a reduced block of arguments.
APPLY is a function value.
ARGUMENTS:
func -- Function value to apply (Type: any-function)
block -- Block of args, reduced first (unless /only) (Type: block)
REFINEMENTS:
/only -- Use arg values as-is, do not reduce the block
(SPECIAL ATTRIBUTES)
throw
See source apply.
As long as Red has no native apply you can write your own mapping function e.g
mapfn: function[blk sfn][
outblk: copy []
foreach i blk[
append outblk sfn copy i
]
outblk
]
Get the function with :functionname
>> myblk: [" this " " is " " a " " line " "for" " testing " " only "]
== [" this " " is " " a " " line " "for" " testing " " only "]
>> probe mapfn myblk :reverse
[" siht " " si " " a " " enil " "rof" " gnitset " " ylno "]
== [" siht " " si " " a " " enil " "rof" " gnitset " " ylno "]
>> probe mapfn myblk :trim
["this" "is" "a" "line" "for" "testing" "only"]
== ["this" "is" "a" "line" "for" "testing" "only"]
>>
An alternative and better way as you can not copy all datatypes is e.g.
mapfn: function[blk sfn][
collect [
foreach i blk[
keep sfn i
]
]
]
and call the function this way if no do not want to modify the original
mapfn newblk: copy/deep myblk :reverse
This works fine on the Mac, but when trying to set it up on Windows it didn't work.
I have msysGit Bash already on the windows path and tried setting as per other instructions.
(setq explicit-shell-file-name "bash")
(setq shell-file-name explicit-shell-file-name)
(setenv "SHELL" "bash")
(setq explicit-bash-args '("--noediting" "-i"))
(setq w32-quote-process-args ?\")
(setenv "PATH"
(concat ".:/usr/local/bin:/mingw/bin:/bin:"
(replace-regexp-in-string " " "\\\\ "
(replace-regexp-in-string "\\\\" "/"
(replace-regexp-in-string "\\([A-Za-z]\\):" "/\\1"
(getenv "PATH"))))))
I added
(setq debug-on-error t)
And re-ran the command M-x magit-version to get the output:
Debugger entered--Lisp error: (wrong-type-argument stringp nil)
string-match("cmd\\.exe" nil)
(if (string-match "cmd\\.exe" tramp-encoding-shell) "/c" "-c")
eval((if (string-match "cmd\\.exe" tramp-encoding-shell) "/c" "-c"))
I then set
(setq tramp-encoding-shell explicit-shell-file-name)
And everything starts working properly.
Hey guys I need to see my ruby version and gemset name in emacs mode-line. Do you know how to do it?
Take a look at rvm.el. I provides many features that make the use of RVM from Emacs more enjoyable.
I tried but I can't custom strings to mode-line so I customize all of it.
;; modeline
(defun branch-name ()
(when vc-mode
(concat "\ue0a0 " (substring vc-mode 5))
))
(defun current-ruby ()
(when vc-mode
(replace-regexp-in-string "\n$" "" (shell-command-to-string "~/.rvm/bin/rvm-prompt"))
))
(setq-default mode-line-format
(list
"[" mode-line-modified "]"
" "
"%b"
" | "
'mode-name
" | "
'(:eval (projectile-project-name))
" "
'(:eval (branch-name))
" | "
'(:eval (current-ruby))
" | "
"%p (%l,%c)"
))
In emacs org-mode with iimage-mode enabled, it can show an inline image from a relative file path, like:
[[file:images/foo.jpg]]
What I want is: how can it shows an image locates in an zip archive, like this:
[[file:images.zip/foo.jpg]]
Is there a way to do this?
Rules for this to work:
If you have a zip file zippedimg.zip containing the image myimage.png, you should reference it in the org mode file as [[./zippedimg_zip/myimage.png]]. Note that I have replaced the . in zippedimg.zip to _.
The reason for doing so is that I am creating the same directory as mentioned within the [[ and ]]. But as the zippedimg.zip file exists, emacs does not allow creating a directory with the same name. So for zippedimg.zip file, I am extracting ONLY the image in it while preserving the path inside the zip into a zippedimg_zip directory.
Here is a mininum working org file to test it out:
#+TITLE: Image from archive
#+STARTUP: inlineimages
#+NAME: fig:myimage
#+HEADER: :extractfromarchive t
# The below caption line is optional
#+CAPTION: My image myimage.png inside ./zippedimg.zip
[[./zippedimg_zip/myimage.png]]
Here is the code you need to put somewhere in your emacs init:
;; Execute the `modi/org-include-img-from-archive' function just before saving the file
(add-hook 'before-save-hook #'modi/org-include-img-from-archive)
;; Execute the `modi/org-include-img-from-archive' function before processing the
;; file for export
(add-hook 'org-export-before-processing-hook #'modi/org-include-img-from-archive)
(defun modi/org-include-img-from-archive (&rest ignore)
"Extract image files from the archive files. Only .zip files are supported
as of now.
Only looks at #HEADER: lines that have \":extractfromarchive t\".
This function does nothing if not in org-mode, so you can safely
add it to `before-save-hook'."
(interactive)
(when (derived-mode-p 'org-mode)
(save-excursion
(goto-char (point-min)) ; go to the beginning of the buffer
(while (search-forward-regexp
"^\\s-*#\\+HEADER:.*\\s-:extractfromarchive\\s-+t"
nil :noerror)
(let (;; only .zip supported as of now
(search-expr "\\[\\[\\(.*?\\)_zip/\\(.*?\\)\\([^/]+\\..*\\)\\]\\]")
arc-file
path-in-arc-file
img-file img-file-full-path
dest-dir dest-dir-full-path
cmd)
;; Keep on going on to the next line till it finds a line with
;; `[[./path/to/zip-file/path/inside/zip/to/the/image]]'
(while (progn
(forward-line 1)
(or (not (looking-at search-expr))
(eobp))))
(when (looking-at search-expr)
(setq arc-file (expand-file-name
(concat (match-string-no-properties 1) ".zip")))
(setq path-in-arc-file (match-string-no-properties 2))
(setq img-file (match-string-no-properties 3))
(setq dest-dir (concat "./" (file-name-base arc-file)
"_zip/" path-in-arc-file))
(setq dest-dir-full-path (concat (file-name-sans-extension arc-file)
"_zip/" path-in-arc-file))
(setq img-file-full-path (expand-file-name img-file dest-dir))
;; (message (concat "arc-file: %s\npath-in-arc-file: %s\n"
;; "img-file: %s\nimg-file-full-path: %s\n"
;; "dest-dir: %s\ndest-dir-full-path: %s")
;; arc-file path-in-arc-file
;; img-file img-file-full-path
;; dest-dir dest-dir-full-path)
(when (file-newer-than-file-p arc-file img-file-full-path)
;; This block is executed only if arc-file is newer than
;; img-file-full-path
;; or if img-file does not exist
;; https://www.gnu.org/software/emacs/manual/html_node/elisp/Testing-Accessibility.html
(when (file-exists-p dest-dir-full-path)
(delete-directory dest-dir-full-path t t))
(make-directory dest-dir-full-path t)
(setq cmd (format "unzip -j %s %s%s -d ./%s."
arc-file path-in-arc-file img-file
(concat (file-name-base arc-file) "_zip/"
path-in-arc-file)))
(message "%s" cmd)
(shell-command cmd)
(shell-command (concat "touch " img-file-full-path)))))))))
thanks kaushalmodi,
below is what I did, by following the answer in this question how to display pdf images in org mode. As this is the first time I do lisp programming, the code maybe not well designed.
(add-hook 'org-mode-hook #'modi/org-include-img-from-zip)
(defun modi/org-include-img-from-zip (&rest ignore)
"extract images from zip an archive.
This function does nothing if not in org-mode."
(interactive)
(when (derived-mode-p 'org-mode)
(save-excursion
(goto-char (point-min))
(while (search-forward-regexp
"^\\s-*#\\+STARTUP:.*\\s-convertfromzip"
nil 'noerror)
(let* (filenoext imgext imgfile imgfilename zipfile imgpath cmd)
(setq zipfile "arc.zip")
;; Keep on going on to the next line till it finds a line with
;; `[[FILE]]'
(while (progn
(forward-line 1)
(not (looking-at "\\[\\[\\(.*\\)\\.\\(.*\\)\\]\\]"))))
(when (looking-at "\\[\\[file:\\(.*\\)\\.\\(.*\\)\\]\\]")
(setq filenoext (match-string-no-properties 1))
(setq imgext (match-string-no-properties 2))
(setq imgpath (concat filenoext "." imgext))
(setq imgfile (expand-file-name (concat filenoext "." imgext)))
(setq imgfilename (file-name-nondirectory imgfile))
(when (string= (substring imgpath 0 1) "~")
(setq imgpath (concat "%HOME%" (substring imgpath 1 nil)))
(setq imgpath (file-name-directory imgpath))
(setq cmd (concat "7z e " zipfile " -y -o"imgpath " " imgfilename " " imgfilename ))
(with-temp-buffer (shell-command cmd t))
)
))))))
I'm struggling to adapt my emacs conf file for my mac...
What I'm trying to do is being able to scroll up/down by block by using ctrl + arrows.
The problem is that when I try to do it, mac intercepts the command and does something specific to mac os...
Here is my current .emacs file:
(add-to-list 'load-path "~/.emacs.d/")
(setq column-number-mode t)
(setq shift-select-mode t)
(show-paren-mode 1)
(global-linum-mode 1)
(set-face-attribute 'linum nil :background "#000000")
(setq linum-format
(lambda (line)
(propertize (format
(let ((w (length (number-to-string
(count-lines (point-min) (point-max))))))
(concat "%" (number-to-string w) "d "))
line)
'face 'linum)))
(add-to-list 'auto-mode-alist (cons "\\.php[0-9]*$" 'php-mode))
(global-set-key "" 'do_insert_time)
(global-set-key "" 'std-file-header)
(setq header-made-by "Made by "
header-login "Login "
header-login-beg "<"
header-login-mid "#"
header-login-end ">"
header-started "Started on "
header-last "Last update "
header-for " for "
header-in " in "
domaine-name "")
(if (setq user-nickname (getenv "USER_NICKNAME"))
t
(setq user-nickname (user-full-name))
)
(setq write-file-hooks (cons 'update-std-header write-file-hooks))
(setq std-c-alist '( (cs . "/*") (cc . "** ") (ce . "*/") )
std-css-alist '( (cs . "/*") (cc . "** ") (ce . "*/") )
std-cpp-alist '( (cs . "//") (cc . "// ") (ce . "//") )
std-pov-alist '( (cs . "//") (cc . "// ") (ce . "//") )
std-java-alist '( (cs . "//") (cc . "// ") (ce . "//") )
std-latex-alist '( (cs . "%%") (cc . "%% ") (ce . "%%") )
std-lisp-alist '( (cs . ";;") (cc . ";; ") (ce . ";;") )
std-xdefault-alist '( (cs . "!!") (cc . "!! ") (ce . "!!") )
std-pascal-alist '( (cs . "{ ") (cc . " ") (ce . "}" ) )
std-makefile-alist '( (cs . "##") (cc . "## ") (ce . "##") )
std-text-alist '( (cs . "##") (cc . "## ") (ce . "##") )
std-fundamental-alist '( (cs . " ") (cc . " ") (ce . " ") )
std-html-alist '( (cs . "<!--") (cc . " -- ") (ce . "-->"))
std-php-alist '( (cs . "#!/usr/local/bin/php\n<?php") (cc . "// ")(ce . "//"))
std-nroff-alist '( (cs . "\\\"") (cc . "\\\" ") (ce . "\\\""))
std-sscript-alist '( (cs . "#!/bin/sh") (cc . "## ") (ce . "##"))
std-perl-alist '( (cs . "#!/usr/local/bin/perl -w") (cc . "## ")(ce . "##"))
std-cperl-alist '( (cs . "#!/usr/local/bin/perl -w") (cc . "## ")(ce . "##"))
)
(setq std-modes-alist '(("C" . std-c-alist)
("C/l" . std-c-alist)
("CSS" . std-c-alist)
("PoV" . std-pov-alist)
("C++" . std-cpp-alist)
("C++/l" . std-cpp-alist)
("Lisp" . std-lisp-alist)
("Lisp Interaction" . std-lisp-alist)
("Emacs-Lisp" . std-lisp-alist)
("Fundamental" . std-fundamental-alist)
("Shell-script" . std-sscript-alist)
("Makefile" . std-makefile-alist)
("GNUmakefile" . std-makefile-alist)
("BSDmakefile" . std-makefile-alist)
("Perl" . std-cperl-alist)
("CPerl" . std-cperl-alist)
("xdefault" . std-xdefault-alist)
("java" . std-java-alist)
("latex" . std-latex-alist)
("Pascal" . stdp-ascal-alist)
("Text" . std-text-alist)
("HTML" . std-html-alist)
("PHP" . std-php-alist)
("Nroff" . std-nroff-alist)
("TeX" . std-latex-alist)
("LaTeX" . std-latex-alist))
)
(defun std-get (a)
(interactive)
(cdr (assoc a (eval (cdr (assoc mode-name std-modes-alist)))))
)
(defun update-std-header ()
"Updates std header with last modification time & owner.\n(According to mode)"
(interactive)
(save-excursion
(if (buffer-modified-p)
(progn
(goto-char (point-min))
(if (search-forward header-last nil t)
(progn
; (delete-region (point-at-bol) (point-at-eol))
(delete-region
(progn (beginning-of-line) (point))
(progn (end-of-line) (point)))
(insert-string (concat (std-get 'cc)
header-last
(current-time-string)
" "
user-nickname))
(message "Last modification header field updated."))))))
nil)
(defun std-file-header ()
"Puts a standard header at the beginning of the file.\n(According to mode)"
(interactive)
(goto-char (point-min))
(let ((projname "toto")(location "tiuti"))
(setq projname (read-from-minibuffer
(format "Type project name (RETURN to quit) : ")))
(setq location (getenv "PWD"))
(insert-string (std-get 'cs))
(newline)
(insert-string (concat (std-get 'cc)
(buffer-name)
header-for
projname
header-in
location))
(newline)
(insert-string (std-get 'cc))
(newline)
(insert-string (concat (std-get 'cc) header-made-by user-nickname))
(newline)
(insert-string (concat (std-get 'cc)
header-login
header-login-beg
(getenv "USER")
header-login-mid
domaine-name
header-login-end))
(newline)
(insert-string (std-get 'cc))
(newline)
(insert-string (concat (std-get 'cc)
header-started
(current-time-string)
" "
user-nickname))
(newline)
(insert-string (concat (std-get 'cc)
header-last
(current-time-string)
" "
user-nickname))
(newline)
(insert-string (std-get 'ce))
(newline)))
(defun insert-std-vertical-comments ()
"Inserts vertical comments (according to mode)."
(interactive)
(beginning-of-line)
(insert-string (std-get 'cs))
(newline)
(let ((ok t)(comment ""))
(while ok
(setq comment (read-from-minibuffer
(format "Type comment (RETURN to quit) : ")))
(if (= 0 (length comment))
(setq ok nil)
(progn
(insert-string (concat (std-get 'cc) comment))
(newline)))))
(insert-string (std-get 'ce))
(newline))
(defun std-toggle-comment ()
"Toggles line comment on or off (according to mode)."
(interactive)
(save-excursion
(let (beg end)
(beginning-of-line)
(setq beg (point))
(end-of-line)
(setq end (point))
(save-restriction
(if (not (equal beg end))
(progn
(narrow-to-region beg end)
(goto-char beg)
(if (search-forward (std-get 'cs) end t)
(progn
(beginning-of-line)
(replace-string (std-get 'cs) "")
(replace-string (std-get 'ce) ""))
(progn
(beginning-of-line)
(insert-string (std-get 'cs))
(end-of-line)
(insert-string (std-get 'ce)))))))))
;; (indent-according-to-mode)
(indent-for-tab-command)
(next-line 1))
;;; Added by Eole Wednesday May 29 2002, 1:33:55
;;; Extended bindings for this package and for commenting code
(global-set-key "h" 'update-std-header)
(global-set-key "" 'std-file-header)
;;;; Generating local keymaps for exotics modes.
;;; In CPerl mode, C-c C-h is used to do some help.
;;; so it is C-c C-h h
;;; For working, it requires info pages about perl
(add-hook 'cperl-mode-hook
'(lambda ()
(define-key cperl-mode-map ""
'comment-region)
(define-key cperl-mode-map "h"
'std-file-header)))
;; for perl-mode
(add-hook 'perl-mode-hook
'(lambda ()
(define-key perl-mode-map ""
'comment-region)))
;; for all kind of lisp code
(add-hook 'emacs-lisp-mode-hook
'(lambda ()
(define-key emacs-lisp-mode-map ""
'comment-region)))
(add-hook 'lisp-mode-hook
'(lambda ()
(define-key lisp-mode-map ""
'comment-region)))
;; for La(TeX)-mode
(add-hook 'tex-mode-hook
'(lambda ()
(define-key tex-mode-map ""
'comment-region)))
(custom-set-variables
;; custom-set-variables was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
'(inhibit-startup-screen t))
(custom-set-faces
;; custom-set-faces was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
)
Thanks very much!
Reassign or disable the "Mission control" and "Application windows" keyboard shortcuts in System Preferences.
The normal way to bind Ctrl-up and -down is via the following lines in your .emacs file:
(global-set-key "C-<up>" 'scroll-down)
(global-set-key "C-<down>" 'scroll-up)
This works when Emacs receives all key strokes directly from the OS. In the case emacs is run inside a terminal it depends additionally on the terminal settings. You test the keys your Emacs instance receives with the Emacs command describe-key (keyboard shortcut Ctrl-h k) plus the key combo you want to use, say, Ctrl-up. If, as I suspect, the help window only mentions <up> instead of <C-up>, the terminal settings drop the Ctrl modifier.
Your best change, I suggest, is to install an emacs build which runs as a Carbon application like emacsformacosx.
Then the above listed bindings should work.