小编Ron*_*Ron的帖子

Emacs:在终端中禁用主题背景颜色

当我在终端中打开一个框架时,我想让emacs没有背景颜色.我正在使用具有半透明背景的终端,而具有背景颜色的字符不是"透视".TERM设置为"xterm-256color".

当框架不是图形时,如何让emacs使用默认背景颜色(根本没有颜色)?

编辑: 我有它,有点:

(add-to-list 'custom-theme-load-path "~/.emacs.d/themes")
(load-theme 'my-awesome-theme t)

(defun on-frame-open (frame)
  (if (not (display-graphic-p frame))
    (set-face-background 'default "unspecified-bg" frame)))
(on-frame-open (selected-frame))
(add-hook 'after-make-frame-functions 'on-frame-open)
Run Code Online (Sandbox Code Playgroud)

我把上面的代码放在我的init文件中,但只是在终端中打开emacsclient时禁止后台,而不是emacs本身(即仅在调用时调用,emacsclient -t而不是在调用时调用emacs).添加额外(unless window-system (set-face-background 'default "unspecified-bg" (selected-frame)))功能不起作用,只会混淆图形帧.

有关为何会发生这种情况的任何想法

emacs elisp colors

17
推荐指数
3
解决办法
7293
查看次数

在perl中将值从一个哈希值复制到另一个哈希值

我有两个哈希,一个大,一个小.所有较小的哈希键都显示在较大的哈希值中,但值不同.我想将值从较大的哈希值复制到较小的哈希值.

例如:

# I have two hashes like so
%big_hash = (A => '1', B => '2', C => '3', D => '4', E => '5');
%small_hash = (A => '0', B => '0', C => '0');
# I want small_hash to get the values of big_hash like this
%small_hash = (A => '1', B => '2', C => '3');
Run Code Online (Sandbox Code Playgroud)

一个明显的答案是循环遍历小哈希的键,并复制像这样的值

foreach $key (keys %small_hash) { $small_hash{$key} = $big_hash{$key}; }
Run Code Online (Sandbox Code Playgroud)

有没有更短的方法来做到这一点?

perl hash

13
推荐指数
2
解决办法
2万
查看次数

std :: optional作为union vs char []/aligned_storage实现

在阅读GCC的实现时,std::optional我发现了一些有趣的东西.我知道boost::optional实现如下:

template <typename T>
class optional {
    // ...
private:
    bool has_value_;
    aligned_storage<T, /* ... */> storage_;
}
Run Code Online (Sandbox Code Playgroud)

但是libstdc ++libc ++(以及Abseil)都实现了optional这样的类型:

template <typename T>
class optional {
    // ...
private:
    struct empty_byte {};
    union {
        empty_byte empty_;
        T value_;
    };
    bool has_value_;
}
Run Code Online (Sandbox Code Playgroud)

他们看起来因为它们在功能上是相同的,但使用一个优于另一个有什么优势吗?(除了明显缺乏后者的新位置,这真的很好.)

c++ placement-new unions c++17 stdoptional

9
推荐指数
2
解决办法
432
查看次数

将文件内容传递给外部变量

我正在使用 FileReader 和 HTML 文件对话框来读取脚本中的文件。如何将此文件的内容传递到 FileReader.onload 函数之外?

function readFileData(evt) {
  var file = evt.target.files[0];
  var reader = new FileReader();

  reader.onload = function(e) {
    var contents = e.target.result;
  }
  reader.readAsText(file);
}
document.getElementById('file').addEventListener
    ('change', readFileData, false);

/* I want to access the contents here */
Run Code Online (Sandbox Code Playgroud)

我尝试在 readFileData 和 onload 函数中粘贴返回值,但我不确定它们返回什么。

javascript filereader

7
推荐指数
1
解决办法
5897
查看次数

Emacs Lisp:获得ascii的角色价值

我想将Emacs中的一个字符翻译成其数字ascii代码,类似于char a = 'a'; int i = (int)ac中的强制转换.我已经尝试string-to-number了一些其他功能,但似乎没有一个能让Emacs最终将char作为数字读取.

最简单的方法是什么?

ascii elisp char

7
推荐指数
3
解决办法
3173
查看次数

无法理解 libstdc++ 的红黑树迭代器

我正在尝试为三元搜索树实现迭代器,并且因为 TST 与 BST 非常相似,所以我想我应该看看libstdc++所述迭代器的实现。这个想法是迭代所有树节点,而不修改树或在迭代器中保存除当前树节点之外的任何内容。树节点有一个父指针来允许这种迭代。

以下是最新 GCC 的代码,取自GitHub 镜像

static _Rb_tree_node_base*
local_Rb_tree_increment(_Rb_tree_node_base* __x) throw ()
{
  if (__x->_M_right != 0)
    {
      __x = __x->_M_right;
      while (__x->_M_left != 0)
        __x = __x->_M_left;
    }
  else
    {
      _Rb_tree_node_base* __y = __x->_M_parent;
      while (__x == __y->_M_right)
        {
          __x = __y;
          __y = __y->_M_parent;
        }
      if (__x->_M_right != __y)
        __x = __y;
    }
  return __x;
}
Run Code Online (Sandbox Code Playgroud)

它看起来相当简单,增量操作要么带你到你的右孩子,然后一直到左孩子,要么到你的第一个非右父母。我不明白的是第二if条:

if (__x->_M_right != __y)
  __x = __y;
Run Code Online (Sandbox Code Playgroud)

这种情况怎么会评估为false__x …

c++ iteration stl red-black-tree

6
推荐指数
1
解决办法
721
查看次数

C++:查找满足谓词的元组的第一个元素

我有以下详细代码:

struct thing1 { int key, std::string value; };
struct thing2 { int key, std::string value; };
// ...
struct thingN { int key, std::string value; };

struct thing_map {
  thing1 t1;
  thing2 t2;
  // ...
  thingN tN;

  std::string get(int key) {
    if(t1.key == key) return t1.value;
    if(t2.key == key) return t2.value;
    // ...
    if(tN.key == key) return tN.value;
    throw std::runtime_error("bad key");
  }
};
Run Code Online (Sandbox Code Playgroud)

我可以将things 重构为 an std::tuple<thing1, thing2, /* ... */ thingN>,这允许我使用 typed 访问它们std::get,因此不会丢失任何功能(即 …

c++ templates stdtuple c++17

5
推荐指数
1
解决办法
869
查看次数

Elisp:引用let中的先前变量

我想在let中定义两个变量,其中一个变量取决于另一个变量的值,如下所示:

(let ((a (func))
      (b (if (eq a 1) 2 3)))
  ...)    
Run Code Online (Sandbox Code Playgroud)

显然,这不是正确的方法,emacs说这a是无效的.这样做的正确方法是什么?

elisp local-variables let

4
推荐指数
1
解决办法
520
查看次数

Emacs Lisp:"if:no catch for tag: - cl-block-nil--,t"返回nil时

今天我尝试编写一个elisp函数,除了在函数返回nil时不断抛出的错误外,一切正常.这是功能:(请原谅格式化,我不确定如何缩进它.)

(defun byte-is-readable (byte)
  "Determine whether BYTE is readable or not.
Returns t or nil."
  (if (and (<= byte 126) (>= byte 32)) t nil))

;; Read the 100 bytes of a file
;; If 10 in a row are not 'readable', it is safe to assume the file is a binary
(defun is-file-binary (file)
  "Determine whether FILE is a binary file.
Returns t or nil."
  (let* ((i 1)
         (c 0)
         (size (nth 7 (file-attributes file)))
         (lim (if (< size …
Run Code Online (Sandbox Code Playgroud)

emacs elisp function

4
推荐指数
1
解决办法
984
查看次数

jsdom:运行页面的 javascript 函数

我一直在搞 jsdom ,我不知道如何从 html 页面运行函数。例如,我有一个像这样的简单页面:

<html>
  <body>
    Hello
  </body>
  <script>
    function test() {
      document.write("Bye bye")
    }
  </script>
</html>
Run Code Online (Sandbox Code Playgroud)

我想test从 jsdom执行该函数。我该怎么做?我试过简单地调用该函数,但节点抱怨它不存在。

jsdom.env({
    url : "http://localhost:8000/test.html",
    features : {
        FetchExternalResources : ['script'],
        ProcessExternalResources : ['script']
    },
    done : function (error, window) {
        test(); // I'd like to do something like this.
        console.log(window.document.innerHTML);
    }
});
Run Code Online (Sandbox Code Playgroud)

javascript node.js jsdom

4
推荐指数
1
解决办法
5444
查看次数

检测类型是否为"映射"

我想使用其::iterator成员类型将c ++容器解析为另一个对象.迭代器成员类型指向单个类型的对象(向量,队列等)的容器将变成类似列表的对象,并且迭代器成员类型指向的容器std::pair将变成类似于地图的对象.

我正在尝试编写一个成员函数来检测后一种容器,但它不起作用.这是我到目前为止所拥有的:

#include <tuple>
#include <iterator>
#include <type_traits>

template <typename T>
struct is_pair : std::false_type { };

template <typename T, typename U>
struct is_pair<std::pair<T, U>> : std::true_type { };

template <typename T>
constexpr bool is_pair_v = is_pair<T>::value;

template <typename...>
struct is_mapping : std::false_type { };

template <typename Container>
struct is_mapping<Container, std::enable_if_t<
    is_pair_v<std::iterator_traits<typename Container::iterator>::value_type>
>> : std::true_type { };

template <typename T>
constexpr bool is_mapping_v = is_mapping<T>::value;

#include <map>
#include <vector>
#include <iostream>

int main() { …
Run Code Online (Sandbox Code Playgroud)

c++ template-meta-programming

4
推荐指数
1
解决办法
444
查看次数

const限定符的类型推导失败

在编写自定义迭代器类型时,我决定我希望能够从const迭代器转换为非const迭代器.我写了以下remove_const函数.由于某种原因,编译器无法推断出const P与...相同的推断const int*.我从GCC 8.2得到的错误是types 'const P' and 'const int*' have incompatible cv-qualifiers.

我的代码中是否存在阻止编译器正确推导的内容?此外,如果有更好的方式做我想做的事我很想知道.

template <int, typename P>
struct my_iterator { P ptr; };

using iterator = my_iterator<3, int*>;
using const_iterator = my_iterator<3, const int*>;

template <int I, typename P>
my_iterator<I, P> remove_const(my_iterator<I, const P> it) {
    return my_iterator<I, P>{ const_cast<P>(it.ptr) };
}

int main() {
    const_iterator it{ nullptr };
    remove_const( it );
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这是与代码的Godbolt链接

c++ templates const template-argument-deduction

1
推荐指数
1
解决办法
61
查看次数